python - 通过python计算uniswap对地址

标签 python hex solidity web3py uniswap

我正在尝试计算(离线,即没有 http 请求)Uniswap 对的地址, 带有 python、web3 和 eth-abi 库,基于 this坚固性 示例。

address factory = 0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f;
address token0 = 0xCAFE000000000000000000000000000000000000; // change me!
address token1 = 0xF00D000000000000000000000000000000000000; // change me!

address pair = address(uint(keccak256(abi.encodePacked(
  hex'ff',
  factory,
  keccak256(abi.encodePacked(token0, token1)),
  hex'96e8ac4277198ff8b6f785478aa9a39f403cb768dd02cbee326c3e7da348845f'
))));

有一些想法:

hexadem_ ='0x96e8ac4277198ff8b6f785478aa9a39f403cb768dd02cbee326c3e7da348845f'
hexadem_1 = 0xff
abiEncoded_1 = encode_abi_packed(['address', 'address'], (  token_0, token_1 ))
salt_ = web3.Web3.solidityKeccak(['bytes'], ['0x' +abiEncoded_1.hex()])
abiEncoded_2 = encode_abi_packed(['bytes', 'address', 'bytes32'], (bytes(hexadem_1), factory, salt_))
resPair = web3.Web3.solidityKeccak(['bytes','bytes'], ['0x' +abiEncoded_2.hex(), hexadem_])

有人可以建议我,哪里出了问题,应该考虑哪种方式?

最佳答案

首先,resPair 太长,无法作为合约地址。 这些规则可以通过阅读以下内容来解释:https://eips.ethereum.org/EIPS/eip-1014

另请确保按字母顺序输入两个地址。使用 .sort() 函数来执行此操作。

正确的代码如下:

hexadem_ ='0x96e8ac4277198ff8b6f785478aa9a39f403cb768dd02cbee326c3e7da348845f'
hexadem_1 = 0xff
factory = '0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f' 
abiEncoded_1 = encode_abi_packed(['address', 'address'], ('0x7825e833d495f3d1c28872415a4aee339d26ac88', '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2' )) #these two addresses to be changed as fit. Currently they are TLOS and WETH
salt_ = w3.solidityKeccak(['bytes'], ['0x' +abiEncoded_1.hex()])
abiEncoded_2 = encode_abi_packed([ 'address', 'bytes32'], ( factory, salt_))
resPair = w3.solidityKeccak(['bytes','bytes'], ['0xff' + abiEncoded_2.hex(), hexadem_])[12:]
resPair

关于python - 通过python计算uniswap对地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66710238/

相关文章:

c - 为什么我的 fputc/fwrite 一直以十六进制打印以及如何打印到文件的中间?

ethereum - 使用单个 ChainLink 请求从同一 JSON 响应读取多个 uint256 条目

ethereum - Solidity - TypeError : Overriding function is missing "override" specifier

ethereum - Solidity返回函数为什么是常量?

python - Django - importError 在/博客。 (没有名为 urls 的模块)

python - 如何使用 PyQt5 播放视频的特定部分

python - 如何在特定时间间隔内将某些值设置为零

python - 在 Python 中执行多个连续的正则表达式替换。效率低下?

python - 从十六进制到有符号整数的转换不起作用

java - 将十六进制字符串转换为图像