繁体   English   中英

如何使用 web3 Python 转移 ERC1155 令牌?

[英]How to transfer ERC1155 token using web3 Python?

我相信这个主题会帮助很多其他人,我搜索了很多但没有找到任何明确的东西。

我已经研究了好几天了,但找不到解决方案。

我需要使用 python 转移一个 ERC1155 代币。

非常简单,使用 python 将令牌从 account1 发送到 account2。

代币: ERC 1155网络: Polygon语言: Python

有人可以举例说明如何做吗?

谢谢

尝试一下:

你需要:

  • 包含您要转移的 ERC1155 代币的 Eth 钱包
  • 此钱包用于签署交易的私钥
  • 由您想要的任何提供商连接到以太坊节点
  • 或者您可以使用 Geth 或 Parity 等软件运行您自己的
  • ERC1155 合约的 ABI(应用程序二进制接口)。 这指定了合约中可用的函数和变量。
  • ERC1155合约的地址。 在 Etherscan 上检查(mai.net 或其他选项)

转移 ERC1155 代币的步骤:

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

pip install web3

  1. 连接到以太坊节点: from web3 import Web3

web3 = Web3(Web3.HTTPProvider("YOUR_NODE_URL")) 解锁持有 ERC1155 代币的钱包:

web3.eth.account.privateKeyToAccount("YOUR_PRIVATE_KEY") 使用ABI和合约地址加载ERC1155合约:contract = web3.eth.contract(abi=YOUR_ABI, address=YOUR_CONTRACT_ADDRESS) 调用合约的transfer function转入ERC1155 tokens: txn_hash = contract.functions.transfer(TO_ADDRESS, ID, AMOUNT).transact() 等待交易被挖掘:txn_receipt = web3.eth.waitForTransactionReceipt(txn_hash)

  1. 检查交易收据以查看转账是否成功:

if txn_receipt.status: print("转账成功") else: print("转账失败")

再检查一次:

  • 以太坊地址
  • 令牌数
  • 不要多次运行代码,添加拦截器,if 语句
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 = '0xba6666b118f8303f990f3519df07e160227cce87'
TOKEN_ID = '7'

# token = 'https://polygonscan.com/token/0xba6666b118f8303f990f3519df07e160227cce87?a=7#writeProxyContract#F7'

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.ABIFunctionNotFound: ("The function 'safeTransactFrom' was not found in this contract's abi. ", 'Are you sure you provided the correct contract abi?')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM