![](/img/trans.png)
[英]How to check if the token on opensea is ERC721 or ERC1155 using node.js
[英]How to transfer ERC1155 token using web3 Python?
我相信这个主题会帮助很多其他人,我搜索了很多但没有找到任何明确的东西。
我已经研究了好几天了,但找不到解决方案。
我需要使用 python 转移一个 ERC1155 代币。
非常简单,使用 python 将令牌从 account1 发送到 account2。
代币: ERC 1155网络: Polygon语言: Python
有人可以举例说明如何做吗?
谢谢
尝试一下:
你需要:
转移 ERC1155 代币的步骤:
pip install 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)
if txn_receipt.status: print("转账成功") else: print("转账失败")
再检查一次:
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.