Skip to main content

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.6.tar.gz (4.5 kB view hashes)

Uploaded Source

Built Distribution

web3_constant-0.0.6-py3-none-any.whl (5.9 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page