How to transfer ERC1155 token using web3py?

550 views Asked by At

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?

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

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?')

1

There are 1 answers

3
Liar Рonest On

You need:

  • An Eth wallet that contains the ERC1155 tokens you want to transfer
  • Private key for this wallet to sign the transaction
  • A connection to an Ethereum node by any provider you want
  • Or you can run your own using software like Geth or Parity
  • The ABI (Application Binary Interface) for the ERC1155 contract. This specifies the functions and variables that are available in the contract.
  • The address of the ERC1155 contract. Check it on Etherscan (mainnet or else options)

Steps to transfer the ERC1155 tokens:

  1. Install web3 and other required libraries:

    pip install web3

  2. Connect to the Ethereum node:

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

    Load the ERC1155 contract using the ABI and contract address:

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

    Call the transfer function of the contract to transfer the ERC1155 tokens:

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

    Wait for the transaction to be mined:

    txn_receipt = web3.eth.waitForTransactionReceipt(txn_hash)
    
  3. Check the transaction receipt to see if the transfer was successful:

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

Check one more time:

  • Ethereum addresses
  • Count of tokens
  • Do not run code several times, add blocker, if statement