2
votes

I was wondering, what is the proper way to transfer an ERC20 token between two wallets using web3.py? I am testing everything on the Rinkeby test network.

Here is what I've tried...

from ethtoken.abi import EIP20_ABI
from web3 import Web3

token_from = "from_address"
token_to = "to_address"
token_to_private_key = "your_private_key"

#w3 = Web3(Web3.HTTPProvider(infura_url))


contractAddress = "contract_address"
infura_url = "https://rinkeby.infura.io/v3/your_infura_key"
# Fill in your infura API key here
w3 = Web3(Web3.HTTPProvider(infura_url))

contract = w3.eth.contract(address=contractAddress, abi=EIP20_ABI)

nonce = w3.eth.getTransactionCount(token_from)  



# Build a transaction that invokes this contract's function, called transfer
token_txn = contract.functions.transfer(
     token_to,
     1,
 ).buildTransaction({
     'chainId': 1,
     'gas': 70000,
     'gasPrice': w3.toWei('1', 'gwei'),
     'nonce': nonce,
 })


signed_txn = w3.eth.account.signTransaction(token_txn, private_key=token_to_private_key)

w3.eth.sendRawTransaction(signed_txn.rawTransaction)  

When I try to run this with my addresses and private key , I get the error : ValueError: {'code': -32000, 'message': 'invalid sender'}

How can I fix this? Or... is there a better way to transfer a erc20 token with web3.py?

Any help is appreciated.

Thanks

1

1 Answers