https://github.com/powei-lin/web3-constant
Useful constants for web3py
https://github.com/powei-lin/web3-constant
cryptocurrency web3 web3py
Last synced: about 1 month ago
JSON representation
Useful constants for web3py
- Host: GitHub
- URL: https://github.com/powei-lin/web3-constant
- Owner: powei-lin
- License: mit
- Created: 2022-04-13T21:13:40.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2022-05-02T20:51:12.000Z (about 4 years ago)
- Last Synced: 2024-08-08T18:04:08.583Z (almost 2 years ago)
- Topics: cryptocurrency, web3, web3py
- Language: Python
- Homepage:
- Size: 17.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# web3-constant
### Install
```
pip install web3-abi web3-constant
```
### Connect to Web3
``` py3
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
``` py3
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
``` py3
from web3 import Web3
from web3constant.Fantom.Url import FTM_RPC
from web3constant.Fantom.Dex import SPOOKY_SWAP_FACTORY_ADDRESS
from web3abi.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)
```