如何使用web3py传输ERC1155令牌?



我需要使用Python传输ERC1155令牌…非常简单:使用python从account1发送令牌到account2。

Token:ERC 1155网络:多边形Python语言:

有人可以留下一个例子如何做到这一点?

from web3 import Web3
import json
rpc_polygon = "https://polygon-rpc.com"
web3 = Web3(Web3.HTTPProvider(rpc_polygon))
# print(web3.isConnected())
account_1 = "FROM_ADDRESS"
account_2 = "TO_ADDRESS"
private_key = "PRIVATE_KEY_HERE"
balance = web3.eth.get_balance(account_1)
humanReadable = web3.fromWei(balance, 'ether')
print(f'balance: {humanReadable}')
nonce = web3.eth.get_transaction_count(account_1)
# print(f'nonce: {nonce}')
ABI = json.loads('[{"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"}]')
interactedContract = 'CONTRACT_ADDRESS'
TOKEN_ID = '7'
amount_humanReadable = 1
amount = web3.toWei(amount_humanReadable, 'ether')
# print(amount)
web3.eth.account.privateKeyToAccount(private_key)
checksumAddress = Web3.toChecksumAddress(interactedContract)
# print(checksumAddress)
contract = web3.eth.contract(address=checksumAddress, abi=ABI)
txn_hash = contract.functions.transfer(account_2, TOKEN_ID, amount).transact()
txn_receipt = web3.eth.waitForTransactionReceipt(txn_hash)
print(txn_receipt)
if txn_receipt.status:
print("Transfer successful")
else:
print("Transfer failed")

错误信息:

web3.exceptions。在此合约的abi中没有找到safeTransactFrom函数。", '你确定你提供了正确的合同abi?')

您需要:

  • 一个包含你想要转移的ERC1155代币的Eth钱包
  • 此钱包签名交易的私钥
  • 您想要的任何提供商与以太坊节点的连接
  • 或者您可以使用Geth或Parity等软件自己运行
  • ERC1155合约的ABI(应用二进制接口)。这控件中可用的函数和变量合同。
  • ERC1155合约的地址。在Etherscan(主网或其他选项)上检查

传输ERC1155令牌的步骤:

  1. 安装web3和其他所需库:

    pip install web3

  2. 连接以太坊节点:

    from web3 import Web3
    web3 = Web3(Web3.HTTPProvider("YOUR_NODE_URL"))
    Unlock the wallet that holds the ERC1155 tokens:
    web3.eth.account.privateKeyToAccount("YOUR_PRIVATE_KEY")
    

    使用ABI和合约地址加载ERC1155合约:

    contract = web3.eth.contract(abi=YOUR_ABI, address=YOUR_CONTRACT_ADDRESS)
    

    调用合约的转移函数来转移ERC1155代币:

    txn_hash = contract.functions.transfer(TO_ADDRESS, ID, AMOUNT).transact()
    

    等待事务被挖掘:

    txn_receipt = web3.eth.waitForTransactionReceipt(txn_hash)
    
  3. 检查交易收据,查看转账是否成功:

    if txn_receipt.status:
    print("Transfer successful")
    else:
    print("Transfer failed")
    

再检查一次:

<<ul>
  • Ethereum地址/gh>
  • 令牌计数
  • 不要多次运行代码,添加阻塞器,if语句
  • 相关内容

    • 没有找到相关文章

    最新更新