Useful web3 constant
Project description
web3-constant
Install
pip install web3-constant
Connect to Web3
from web3 import Web3
from web3constant.Fantom.Url import FTM_RPC
w3 = Web3(Web3.HTTPProvider(FTM_RPC))
if w3.isConnected():
print("Web3 is connected.")
Listen to a topic
from web3 import Web3
from web3constant.Fantom.Url import FTM_RPC
from web3constant.topics import PAIR_SYNC
w3 = Web3(Web3.HTTPProvider(FTM_RPC))
prev_block_num = w3.eth.get_block_number()
while(True):
current_block = w3.eth.get_block_number()
if (prev_block_num == current_block):
continue
topic_d = {
'fromBlock': prev_block_num,
'topics': [PAIR_SYNC]
}
logs = w3.eth.get_logs(topic_d)
for l in logs:
print(l)
Create contract
from web3 import Web3
from web3constant.Fantom.Url import FTM_RPC
from web3constant.Fantom.Dex import SPOOKY_SWAP_FACTORY_ADDRESS
from web3constant.abi.UniswapV2 import SPOOKY_SWAP_FACTORY_ABI, UNISWAP_V2_PAIR_ABI
w3 = Web3(Web3.HTTPProvider(FTM_RPC))
# create factory contract
spooky_swap_factory_contract = w3.eth.contract(
address=SPOOKY_SWAP_FACTORY_ADDRESS, abi=SPOOKY_SWAP_FACTORY_ABI
)
# get pair address for factory contract
pair_address = spooky_swap_factory_contract.functions.allPairs(0).call()
print("pair contract address:", pair_address)
# create pair contract
pair_contract = w3.eth.contract(address=pair_address, abi=UNISWAP_V2_PAIR_ABI)
# get token0, token1
token0 = pair_contract.functions.token0().call()
token1 = pair_contract.functions.token1().call()
# get reserves
r0, r1, timestamp = pair_contract.functions.getReserves().call()
# print
print("token0:", token0)
print("token0 reserves:", r0)
print("token1:", token1)
print("token1 reserves:", r1)
print("sync timestamp:", timestamp)
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
web3-constant-0.0.4.tar.gz
(9.7 kB
view hashes)
Built Distribution
Close
Hashes for web3_constant-0.0.4-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7fbb3d733e61ff015ff26e60860272f7205f4010fc3ff53924a02cbdbcd6b344 |
|
MD5 | 3db15139b1dc89371f92b3c39be296c9 |
|
BLAKE2b-256 | 2e67d17b2c4379032a8e916e25ead3a05a33f93dcc4d5122ffe63146d85e0134 |