英文:
How do I find the correct ABI, containing the function to transfer tokens to Polygon zkEVM bridge contract?
问题
从以太坊钱包向**Polygon zkEVM本地桥接**的智能合约地址**0x2a3DD3EB832aF982ec71669E178424b10Dca2EDe**进行代币转移的Python代码如下:
```python
# 代码部分
如果你点击链接,你也可以找到合约ABI(以及其他数据):
[{ "inputs":[{ "internalType":"address","name":"_logic","type":"address"},{ "internalType":"address","name":"admin_","type":"address"},{ "internalType":"bytes","name":"_data","type":"bytes"}],"stateMutability":"payable","type":"constructor"},{ "anonymous":false,"inputs":[{ "indexed":false,"internalType":"address","name":"previousAdmin","type":"address"},{ "indexed":false,"internalType":"address","name":"newAdmin","type":"address"}],"name":"AdminChanged","type":"event"},{ "anonymous":false,"inputs":[{ "indexed":true,"internalType":"address","name":"beacon","type":"address"}],"name":"BeaconUpgraded","type":"event"},{ "anonymous":false,"inputs":[{ "indexed":true,"internalType":"address","name":"implementation","type":"address"}],"name":"Upgraded","type":"event"},{ "stateMutability":"payable","type":"fallback"},{ "inputs":[],"name":"admin","outputs":[{ "internalType":"address","name":"admin_","type":"address"}],"stateMutability":"nonpayable","type":"function"},{ "inputs":[{ "internalType":"address","name":"newAdmin","type":"address"}],"name":"changeAdmin","outputs":[],"stateMutability":"nonpayable","type":"function"},{ "inputs":[],"name":"implementation","outputs":[{ "internalType":"address","name":"implementation_","type":"address"}],"stateMutability":"nonpayable","type":"function"},{ "inputs":[{ "internalType":"address","name":"newImplementation","type":"address"}],"name":"upgradeTo","outputs":[],"stateMutability":"nonpayable","type":"function"},{ "inputs":[{ "internalType":"address","name":"newImplementation","type":"address"},{ "internalType":"bytes","name":"data","type":"bytes"}],"name":"upgradeToAndCall","outputs":[],"stateMutability":"payable","type":"function"},{ "stateMutability":"payable","type":"receive"}]
以下是您的代码:
# 代码部分
如果我运行它,会出现以下错误:
# 错误信息部分
这是正确的,如果你仔细查看合约ABI,就会发现没有这样的函数。
因此,我决定手动进行交易(通过桥将代币转移到Polygon zkEVM网络),并找出真实的合约ID、ABI和函数名称。
以下是在etherscan上查看的交易输入数据(在https://etherscan.io/tx/0x1c0c723c1408354a1123a138693af2f70ff20f3aeb136e75bc7b6fb0e46f213d查看交易):
Function: bridgeAsset(uint32 destinationNetwork,address destinationAddress,uint256 amount,address token,bool forceUpdateGlobalExitRoot,bytes permitData) ***
因此,在我的代码中,函数名称是正确的。
我的问题是:我从哪里找到包含我的函数名称的正确ABI,或者为了使一切正常工作,我应该将什么样的函数名称放入我的代码中?
<details>
<summary>英文:</summary>
I am writing python code to transfer tokens from ETH wallet to smart contract of **Polygon zkEVM Native Bridge** (*https://wallet.polygon.technology*) with address **0x2a3DD3EB832aF982ec71669E178424b10Dca2EDe** (*https://etherscan.io/address/0x2a3dd3eb832af982ec71669e178424b10dca2ede*).
If you follow the link you can also find contract ABI (*and other data*):
[{"inputs":[{"internalType":"address","name":"logic","type":"address"},{"internalType":"address","name":"admin","type":"address"},{"internalType":"bytes","name":"data","type":"bytes"}],"stateMutability":"payable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"previousAdmin","type":"address"},{"indexed":false,"internalType":"address","name":"newAdmin","type":"address"}],"name":"AdminChanged","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"beacon","type":"address"}],"name":"BeaconUpgraded","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"implementation","type":"address"}],"name":"Upgraded","type":"event"},{"stateMutability":"payable","type":"fallback"},{"inputs":[],"name":"admin","outputs":[{"internalType":"address","name":"admin","type":"address"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"newAdmin","type":"address"}],"name":"changeAdmin","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"implementation","outputs":[{"internalType":"address","name":"implementation_","type":"address"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"newImplementation","type":"address"}],"name":"upgradeTo","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"newImplementation","type":"address"},{"internalType":"bytes","name":"data","type":"bytes"}],"name":"upgradeToAndCall","outputs":[],"stateMutability":"payable","type":"function"},{"stateMutability":"payable","type":"receive"}]
**Now here is my code:**
from web3 import Web3
import logging
polygon_zkEVM_RPC = "https://zkevm-rpc.com"
polygon_zkEVM_contract = "0x2a3DD3EB832aF982ec71669E178424b10Dca2EDe"
polygon_zkEVM_bridge_abi = [{"inputs":[{"internalType":"address","name":"logic","type":"address"},{"internalType":"address","name":"admin","type":"address"},{"internalType":"bytes","name":"data","type":"bytes"}],"stateMutability":"payable","type":"constructor"},{"anonymous":False,"inputs":[{"indexed":False,"internalType":"address","name":"previousAdmin","type":"address"},{"indexed":False,"internalType":"address","name":"newAdmin","type":"address"}],"name":"AdminChanged","type":"event"},{"anonymous":False,"inputs":[{"indexed":True,"internalType":"address","name":"beacon","type":"address"}],"name":"BeaconUpgraded","type":"event"},{"anonymous":False,"inputs":[{"indexed":True,"internalType":"address","name":"implementation","type":"address"}],"name":"Upgraded","type":"event"},{"stateMutability":"payable","type":"fallback"},{"inputs":[],"name":"admin","outputs":[{"internalType":"address","name":"admin","type":"address"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"newAdmin","type":"address"}],"name":"changeAdmin","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"implementation","outputs":[{"internalType":"address","name":"implementation_","type":"address"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"newImplementation","type":"address"}],"name":"upgradeTo","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"newImplementation","type":"address"},{"internalType":"bytes","name":"data","type":"bytes"}],"name":"upgradeToAndCall","outputs":[],"stateMutability":"payable","type":"function"},{"stateMutability":"payable","type":"receive"}]
mnemonic = config.mnemonic
def bridge_polygon_zkEVM(name, owner_id, mnemonic):
logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(message)s", datefmt="%Y-%m-%d %H:%M:%S",
filename=f"wallet_{name}_{owner_id}.log")
web3 = Web3(Web3.HTTPProvider(polygon_zkEVM_RPC))
web3.eth.account.enable_unaudited_hdwallet_features()
account = web3.eth.account.from_mnemonic(mnemonic)
private_key = account._private_key
address = account.address
logging.info(f"connected to wallet {name} | address {address} | network {polygon_zkEVM_RPC} | owner_id "
f"{owner_id}")
contract = web3.eth.contract(address=web3.to_checksum_address(polygon_zkEVM_contract), abi=polygon_zkEVM_bridge_abi)
gas_price_gwei = 10
transaction = {
'to': polygon_zkEVM_contract,
'gas': web3.eth.estimate_gas(
{'to': polygon_zkEVM_contract, 'data': contract.functions.bridgeAsset().buildTransaction()}),
'gasPrice': web3.to_wei(gas_price_gwei, 'gwei'),
'nonce': web3.eth.get_transaction_count(address),
'from': address
}
signed_txn = web3.eth.account.sign_transaction(transaction, private_key)
transaction_hash = web3.eth.send_raw_transaction(signed_txn.rawTransaction)
transaction_receipt = web3.eth.wait_for_transaction_receipt(transaction_hash)
bridge_polygon_zkEVM('main', 1234, mnemonic)
**If I launch it, the following error arises:**
Traceback (most recent call last):
File "C:\Users\childoflogos\Desktop\airdrop_hunter\api.py", line 58, in <module>
bridge_polygon_zkEVM('main', 1234, mnemonic)
File "C:\Users\childoflogos\Desktop\airdrop_hunter\api.py", line 25, in bridge_polygon_zkEVM
{'to': polygon_zkEVM_contract, 'data': contract.functions.bridgeAsset().buildTransaction()}),
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\childoflogos\Desktop\airdrop_hunter\venv\Lib\site-packages\web3\contract\contract.py", line 397, in getattr
raise ABIFunctionNotFound(
web3.exceptions.ABIFunctionNotFound: ("The function 'bridgeAsset' was not found in this contract's abi.", ' Are you sure you provided the correct contract abi?')
And it is right, there is no such function in contract ABI if you look through it.
So I decided to make a transaction (*transfer of tokens to Polygon zkEVM network through the bridge*) manually and find out real contract id, ABI and function name.
Here is input data of transaction on etherscan (*view transaction at https://etherscan.io/tx/0x1c0c723c1408354a1123a138693af2f70ff20f3aeb136e75bc7b6fb0e46f213d*):
Function: bridgeAsset(uint32 destinationNetwork,address destinationAddress,uint256 amount,address token,bool forceUpdateGlobalExitRoot,bytes permitData) ***
So, the function name in my code is right.
My question is: where do I find the correct ABI of this contract which contains my function name or what is the right name of the function to put into my code for everything to work correctly?
</details>
# 答案1
**得分**: 1
你目前拥有的ABI是透明代理合约的ABI。要获取实际函数的ABI,您可以获取实施合约的ABI。您可以在这里找到它:[实施合约][1]
[1]: https://etherscan.io/address/0x5ac4182a1dd41aeef465e40b82fd326bf66ab82c#code
<details>
<summary>英文:</summary>
The ABI you currently have is the ABI for the transparent proxy contract. To get the ABI for the actual functions you can get the ABI of the implementation contract. You can find it here: [Implementation Contract][1]
[1]: https://etherscan.io/address/0x5ac4182a1dd41aeef465e40b82fd326bf66ab82c#code
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论