如何从 web3 中给定的对地址解码令牌地址
How to decode Token Address from a given Pair Address in web3
我想要完成的是在给定 (Token/BNB) 对中的 LP 对地址 0x0D0b63b32595957ae58D4dD60aa5409E79A5Aa96
的情况下获得合同地址 0x1ada8bb610c59aae25d8dd8f354282f5693cefb1
。我当前的代码让我无处可去。想法会很有帮助。
当前代码:
from web3 import Web3, HTTPProvider
import json
bsc = 'https://bsc-dataseed.binance.org/'
web3 = Web3(Web3.HTTPProvider(bsc))
print("Connected to BSC: ", web3.isConnected())
address = "0x0d0b63b32595957ae58d4dd60aa5409e79a5aa96" #-- LP Pair Address to check
#-- 0x1ada8bb610c59aae25d8dd8f354282f5693cefb1 Token Address
#-- 0x0D0b63b32595957ae58D4dD60aa5409E79A5Aa96 Token LP Pair Address
abi = json.loads('[{"inputs":[{"internalType":"address","name":"_feeToSetter","type":"address"}],"payable":false,"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"token0","type":"address"},{"indexed":true,"internalType":"address","name":"token1","type":"address"},{"indexed":false,"internalType":"address","name":"pair","type":"address"},{"indexed":false,"internalType":"uint256","name":"","type":"uint256"}],"name":"PairCreated","type":"event"},{"constant":true,"inputs":[],"name":"INIT_CODE_PAIR_HASH","outputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"internalType":"uint256","name":"","type":"uint256"}],"name":"allPairs","outputs":[{"internalType":"address","name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"allPairsLength","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"internalType":"address","name":"tokenA","type":"address"},{"internalType":"address","name":"tokenB","type":"address"}],"name":"createPair","outputs":[{"internalType":"address","name":"pair","type":"address"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"feeTo","outputs":[{"internalType":"address","name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"feeToSetter","outputs":[{"internalType":"address","name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"internalType":"address","name":"","type":"address"},{"internalType":"address","name":"","type":"address"}],"name":"getPair","outputs":[{"internalType":"address","name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"internalType":"address","name":"_feeTo","type":"address"}],"name":"setFeeTo","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"internalType":"address","name":"_feeToSetter","type":"address"}],"name":"setFeeToSetter","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"}]')
PCS_FACTORY_ADDRESS="0xca143ce32fe78f1f7019d7d551a6402fc5350c73"
PCS_FACTORY_ADDRESS = Web3.toChecksumAddress(PCS_FACTORY_ADDRESS.lower() )
PCS_FACTORY_CONTRACT = web3.eth.contract(address=PCS_FACTORY_ADDRESS, abi=abi)
pair_contract = PCS_FACTORY_CONTRACT.functions.allPairs.call()
contract = web3.eth.contract(address=address , abi=abi)
token0 = contract.functions.token0().call()
token1 = contract.functions.token1().call()
print (token0) #-- could be token Address or LP Pair Address
print (token1) #-- could token Address or LP Pair Address
需要输出:
Pair Address : 0x0d0b63b32595957ae58d4dd60aa5409e79a5aa96 #-- LP Pair Address / Given input
Token Address : 0x1ada8bb610c59aae25d8dd8f354282f5693cefb1 #-- token Address (output)
哈希函数被设计为不可逆的。因此,除非您不知道散列中使用的所有数据,否则您无法找出散列的内容。
首先,正如@Kendiro 提到的,您无法解码 哈希。
其次,尽管您可能对 token0(在本例中为代币)而不是 token1(wBNB)感兴趣,但您无法通过合约的性质来区分 token0 和 token1。
我不确定你在哪里找到你的 ABI,但你可以使用 BSCScan API 来做这个。
以下代码包含从给定地址 return Web3 合约的函数,并且
请注意,总共调用了 3 次 API(一次用于获取 LP 合约,两次分别用于获取 token0 和 token1 的代币合约)。
import requests
from web3 import Web3
apikey = 'YourApiKeyHere'
bsc = 'https://bsc-dataseed.binance.org/'
web3 = Web3(Web3.HTTPProvider(bsc))
print("Connected to BSC: ", web3.isConnected())
def get_contract(address):
checksum_address = Web3.toChecksumAddress(address)
params = {
'module': 'contract',
'action': 'getabi',
'address': checksum_address,
'apikey': apikey,
}
res = requests.get('https://api.bscscan.com/api', params=params)
abi = res.json()['result']
contract = web3.eth.contract(address=checksum_address, abi=abi)
return contract
lp_address = "0x0d0b63b32595957ae58d4dd60aa5409e79a5aa96" #-- LP Pair Address to check
lp_contract = get_contract(lp_address)
token0_address = lp_contract.functions.token0().call()
token1_address = lp_contract.functions.token1().call()
token0_contract = get_contract(token0_address)
token1_contract = get_contract(token1_address)
token0_symbol = token0_contract.functions.symbol().call()
token1_symbol = token1_contract.functions.symbol().call()
print(f'Pair Address: {lp_address}')
print(f'Token0 is {token0_symbol}: {token0_address}')
print(f'Token1 is {token1_symbol}: {token1_address}')
输出:
Pair Address: 0x0d0b63b32595957ae58d4dd60aa5409e79a5aa96
Token0 is $YSHIBAINU: 0x1AdA8bB610C59Aae25d8DD8f354282F5693CefB1
Token1 is WBNB: 0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c
这是我能得到的最接近的。如果你想区分这两个令牌,你可能需要进一步处理。
我想要完成的是在给定 (Token/BNB) 对中的 LP 对地址 0x0D0b63b32595957ae58D4dD60aa5409E79A5Aa96
的情况下获得合同地址 0x1ada8bb610c59aae25d8dd8f354282f5693cefb1
。我当前的代码让我无处可去。想法会很有帮助。
当前代码:
from web3 import Web3, HTTPProvider
import json
bsc = 'https://bsc-dataseed.binance.org/'
web3 = Web3(Web3.HTTPProvider(bsc))
print("Connected to BSC: ", web3.isConnected())
address = "0x0d0b63b32595957ae58d4dd60aa5409e79a5aa96" #-- LP Pair Address to check
#-- 0x1ada8bb610c59aae25d8dd8f354282f5693cefb1 Token Address
#-- 0x0D0b63b32595957ae58D4dD60aa5409E79A5Aa96 Token LP Pair Address
abi = json.loads('[{"inputs":[{"internalType":"address","name":"_feeToSetter","type":"address"}],"payable":false,"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"token0","type":"address"},{"indexed":true,"internalType":"address","name":"token1","type":"address"},{"indexed":false,"internalType":"address","name":"pair","type":"address"},{"indexed":false,"internalType":"uint256","name":"","type":"uint256"}],"name":"PairCreated","type":"event"},{"constant":true,"inputs":[],"name":"INIT_CODE_PAIR_HASH","outputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"internalType":"uint256","name":"","type":"uint256"}],"name":"allPairs","outputs":[{"internalType":"address","name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"allPairsLength","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"internalType":"address","name":"tokenA","type":"address"},{"internalType":"address","name":"tokenB","type":"address"}],"name":"createPair","outputs":[{"internalType":"address","name":"pair","type":"address"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"feeTo","outputs":[{"internalType":"address","name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"feeToSetter","outputs":[{"internalType":"address","name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"internalType":"address","name":"","type":"address"},{"internalType":"address","name":"","type":"address"}],"name":"getPair","outputs":[{"internalType":"address","name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"internalType":"address","name":"_feeTo","type":"address"}],"name":"setFeeTo","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"internalType":"address","name":"_feeToSetter","type":"address"}],"name":"setFeeToSetter","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"}]')
PCS_FACTORY_ADDRESS="0xca143ce32fe78f1f7019d7d551a6402fc5350c73"
PCS_FACTORY_ADDRESS = Web3.toChecksumAddress(PCS_FACTORY_ADDRESS.lower() )
PCS_FACTORY_CONTRACT = web3.eth.contract(address=PCS_FACTORY_ADDRESS, abi=abi)
pair_contract = PCS_FACTORY_CONTRACT.functions.allPairs.call()
contract = web3.eth.contract(address=address , abi=abi)
token0 = contract.functions.token0().call()
token1 = contract.functions.token1().call()
print (token0) #-- could be token Address or LP Pair Address
print (token1) #-- could token Address or LP Pair Address
需要输出:
Pair Address : 0x0d0b63b32595957ae58d4dd60aa5409e79a5aa96 #-- LP Pair Address / Given input
Token Address : 0x1ada8bb610c59aae25d8dd8f354282f5693cefb1 #-- token Address (output)
哈希函数被设计为不可逆的。因此,除非您不知道散列中使用的所有数据,否则您无法找出散列的内容。
首先,正如@Kendiro 提到的,您无法解码 哈希。 其次,尽管您可能对 token0(在本例中为代币)而不是 token1(wBNB)感兴趣,但您无法通过合约的性质来区分 token0 和 token1。
我不确定你在哪里找到你的 ABI,但你可以使用 BSCScan API 来做这个。 以下代码包含从给定地址 return Web3 合约的函数,并且
请注意,总共调用了 3 次 API(一次用于获取 LP 合约,两次分别用于获取 token0 和 token1 的代币合约)。
import requests
from web3 import Web3
apikey = 'YourApiKeyHere'
bsc = 'https://bsc-dataseed.binance.org/'
web3 = Web3(Web3.HTTPProvider(bsc))
print("Connected to BSC: ", web3.isConnected())
def get_contract(address):
checksum_address = Web3.toChecksumAddress(address)
params = {
'module': 'contract',
'action': 'getabi',
'address': checksum_address,
'apikey': apikey,
}
res = requests.get('https://api.bscscan.com/api', params=params)
abi = res.json()['result']
contract = web3.eth.contract(address=checksum_address, abi=abi)
return contract
lp_address = "0x0d0b63b32595957ae58d4dd60aa5409e79a5aa96" #-- LP Pair Address to check
lp_contract = get_contract(lp_address)
token0_address = lp_contract.functions.token0().call()
token1_address = lp_contract.functions.token1().call()
token0_contract = get_contract(token0_address)
token1_contract = get_contract(token1_address)
token0_symbol = token0_contract.functions.symbol().call()
token1_symbol = token1_contract.functions.symbol().call()
print(f'Pair Address: {lp_address}')
print(f'Token0 is {token0_symbol}: {token0_address}')
print(f'Token1 is {token1_symbol}: {token1_address}')
输出:
Pair Address: 0x0d0b63b32595957ae58d4dd60aa5409e79a5aa96
Token0 is $YSHIBAINU: 0x1AdA8bB610C59Aae25d8DD8f354282F5693CefB1
Token1 is WBNB: 0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c
这是我能得到的最接近的。如果你想区分这两个令牌,你可能需要进一步处理。