【问题标题】:Compute the LP Address of a token pair using web3.py使用 web3.py 计算令牌对的 LP 地址
【发布时间】:2021-12-24 15:40:58
【问题描述】:

经过几个小时的搜索,我设法让这段代码运行,但不幸的是,这并没有产生我想要的输出,即在 (TOKEN/BNB LP) 中获取 LP 池地址。

给定令牌地址:0xe56842ed550ff2794f010738554db45e60730371

我想获取 BIN/BNB 池地址:0xe432afB7283A08Be24E9038C30CA6336A7cC8218

任何想法可能是什么问题?

from web3 import Web3
from eth_abi.packed import encode_abi_packed
from eth_abi import encode_abi
import eth_abi

"""
Contract: 0xe56842ed550ff2794f010738554db45e60730371
BIN/BNB Address: 0xe432afB7283A08Be24E9038C30CA6336A7cC8218
BIN/BNB LP URL: https://bscscan.com/token/0xe432afB7283A08Be24E9038C30CA6336A7cC8218#balances
"""

CONTRACTS = {"CONTRACT": "0xe56842ed550ff2794f010738554db45e60730371",}

PANCAKE_SWAP_FACTORY = "0xcA143Ce32Fe78f1f7019d7d551a6402fC5350c73"
PANCAKE_SWAP_ROUTER  = "0x10ED43C718714eb63d5aA57B78B54704E256024E"
WBNB_ADDRESS = "0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c"

hexadem_= '0x96e8ac4277198ff8b6f785478aa9a39f403cb768dd02cbee326c3e7da348845f'
factory = PANCAKE_SWAP_FACTORY
abiEncoded_1 = encode_abi_packed(['address', 'address'], (CONTRACTS['CONTRACT'], WBNB_ADDRESS))
salt_ = Web3.solidityKeccak(['bytes'], ['0x' +abiEncoded_1.hex()])
abiEncoded_2 = encode_abi_packed([ 'address', 'bytes32'], ( factory, salt_))
resPair = Web3.solidityKeccak(['bytes','bytes'], ['0xff' + abiEncoded_2.hex(), hexadem_])[12:]

# resPair is the address for the pancakeswap CONTRACT /WBNB pair
print("Token Contract: ", CONTRACTS)
print("BNB-LP Address: ", resPair.hex())    #-- expecting to get  0xe432afB7283A08Be24E9038C30CA6336A7cC8218

电流输出:

BNB-LP Address:  0xde173b8a63b9641a531de0fbb1c5c9eee3b4bc0c

预期输出:

Token Contract:  0xe56842ed550ff2794f010738554db45e60730371
BNB-LP Address:  0xe432afB7283A08Be24E9038C30CA6336A7cC8218   #-- correct LP Address

【问题讨论】:

  • 我正在尝试,但我没有得到正确的 BNB-LP 地址:0xde173b8a63b9641a531de0fbb1c5c9eee3b4bc0c
  • 我真的不知道是什么问题。对不起

标签: python solidity web3 binance-smart-chain web3py


【解决方案1】:

您需要按字母顺序输入两个硬币。

pair_traded = [token_a, token_b] #token_a, token_b are the address's
pair_traded.sort()

hexadem_1 = 0xff
abiEncoded_1 = encode_abi_packed(['address', 'address'], (token_list[0], token_list[1] ))
salt_ = w3.solidityKeccak(['bytes'], ['0x' +abiEncoded_1.hex()])
abiEncoded_2 = encode_abi_packed([ 'address', 'bytes32'], ( factory, salt_))
pair_address = w3.solidityKeccak(['bytes','bytes'], ['0xff' + abiEncoded_2.hex(), pair_code_hash])[12:]

【讨论】:

  • 即使参数顺序正确,但推理不同。 pair contract 是通过按 WBNB、BIN 顺序传递参数创建的。您可以查看创建配对合约的transaction 事件日志——特别是您正在寻找PairCreated 事件。因此,如果您想获得相同的合约地址对,则需要传递相同的输入参数(以正确的顺序)。
  • 我收到错误:pair_address = Web3.solidityKeccak(['bytes','bytes'], ['0xff' + abiEncoded_2.hex(), pair_code_hash])[12:] NameError: name 'pair_code_hash' 未定义
  • 尝试替换:pair_code_hash 到 hexadem_1 并且可以,但 lp 地址仍然错误
猜你喜欢
  • 2022-06-25
  • 2018-11-18
  • 2018-06-17
  • 2021-12-04
  • 2018-08-02
  • 2021-10-07
  • 2021-12-22
  • 2022-10-06
  • 2018-06-19
相关资源
最近更新 更多