2022-12-30

How to transfer ERC1155 token using web3 Python?

I believe this subject will help many other people, i searched a lot and did not find anything clear.

I've been researching this for days, but i can't find a solution.

I need to transfer an ERC1155 token using python.

Something very simple, send token from account1 to account2 using python.

Token: ERC 1155 Network: Polygon Language: Python

Could someone please leave an example how to do it.

Thanks

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")

Error message: web3.exceptions.ABIFunctionNotFound: ("The function 'safeTransactFrom' was not found in this contract's abi. ", 'Are you sure you provided the correct contract abi?')



No comments:

Post a Comment