0

I'm trying to send USDT token (1.234567 USDT) from my wallet with this code:

>>> from web3 import Web3
>>> 
>>> INFURA_URL = "https://mainnet.infura.io/v3/<infura id>"
>>> PRIVATE_KEY = b'<some private key>'
>>> web3 = Web3(Web3.HTTPProvider(INFURA_URL))
>>> 
>>> my_account = web3.eth.account.privateKeyToAccount(PRIVATE_KEY)
>>> receiver_address = "0x3Aa124E916C920ff30f7E236092C7a41e95bA1eF"
>>> usdt_abi = [{"constant":True,"inputs":[],"name":"name","outputs":[{"name":"","type":"string"}],"payable":False,"stateMutability":"view","type":"function"},{"constant":False,"inputs":[{"name":"_upgradedAddress","type":"address"}],"name":"deprecate","outputs":[],"payable":False,"stateMutability":"nonpayable","type":"function"},{"constant":False,"inputs":[{"name":"_spender","type":"address"},{"name":"_value","type":"uint256"}],"name":"approve","outputs":[],"payable":False,"stateMutability":"nonpayable","type":"function"},{"constant":True,"inputs":[],"name":"deprecated","outputs":[{"name":"","type":"bool"}],"payable":False,"stateMutability":"view","type":"function"},{"constant":False,"inputs":[{"name":"_evilUser","type":"address"}],"name":"addBlackList","outputs":[],"payable":False,"stateMutability":"nonpayable","type":"function"},{"constant":True,"inputs":[],"name":"totalSupply","outputs":[{"name":"","type":"uint256"}],"payable":False,"stateMutability":"view","type":"function"},{"constant":False,"inputs":[{"name":"_from","type":"address"},{"name":"_to","type":"address"},{"name":"_value","type":"uint256"}],"name":"transferFrom","outputs":[],"payable":False,"stateMutability":"nonpayable","type":"function"},{"constant":True,"inputs":[],"name":"upgradedAddress","outputs":[{"name":"","type":"address"}],"payable":False,"stateMutability":"view","type":"function"},{"constant":True,"inputs":[{"name":"","type":"address"}],"name":"balances","outputs":[{"name":"","type":"uint256"}],"payable":False,"stateMutability":"view","type":"function"},{"constant":True,"inputs":[],"name":"decimals","outputs":[{"name":"","type":"uint256"}],"payable":False,"stateMutability":"view","type":"function"},{"constant":True,"inputs":[],"name":"maximumFee","outputs":[{"name":"","type":"uint256"}],"payable":False,"stateMutability":"view","type":"function"},{"constant":True,"inputs":[],"name":"_totalSupply","outputs":[{"name":"","type":"uint256"}],"payable":False,"stateMutability":"view","type":"function"},{"constant":False,"inputs":[],"name":"unpause","outputs":[],"payable":False,"stateMutability":"nonpayable","type":"function"},{"constant":True,"inputs":[{"name":"_maker","type":"address"}],"name":"getBlackListStatus","outputs":[{"name":"","type":"bool"}],"payable":False,"stateMutability":"view","type":"function"},{"constant":True,"inputs":[{"name":"","type":"address"},{"name":"","type":"address"}],"name":"allowed","outputs":[{"name":"","type":"uint256"}],"payable":False,"stateMutability":"view","type":"function"},{"constant":True,"inputs":[],"name":"paused","outputs":[{"name":"","type":"bool"}],"payable":False,"stateMutability":"view","type":"function"},{"constant":True,"inputs":[{"name":"who","type":"address"}],"name":"balanceOf","outputs":[{"name":"","type":"uint256"}],"payable":False,"stateMutability":"view","type":"function"},{"constant":False,"inputs":[],"name":"pause","outputs":[],"payable":False,"stateMutability":"nonpayable","type":"function"},{"constant":True,"inputs":[],"name":"getOwner","outputs":[{"name":"","type":"address"}],"payable":False,"stateMutability":"view","type":"function"},{"constant":True,"inputs":[],"name":"owner","outputs":[{"name":"","type":"address"}],"payable":False,"stateMutability":"view","type":"function"},{"constant":True,"inputs":[],"name":"symbol","outputs":[{"name":"","type":"string"}],"payable":False,"stateMutability":"view","type":"function"},{"constant":False,"inputs":[{"name":"_to","type":"address"},{"name":"_value","type":"uint256"}],"name":"transfer","outputs":[],"payable":False,"stateMutability":"nonpayable","type":"function"},{"constant":False,"inputs":[{"name":"newBasisPoints","type":"uint256"},{"name":"newMaxFee","type":"uint256"}],"name":"setParams","outputs":[],"payable":False,"stateMutability":"nonpayable","type":"function"},{"constant":False,"inputs":[{"name":"amount","type":"uint256"}],"name":"issue","outputs":[],"payable":False,"stateMutability":"nonpayable","type":"function"},{"constant":False,"inputs":[{"name":"amount","type":"uint256"}],"name":"redeem","outputs":[],"payable":False,"stateMutability":"nonpayable","type":"function"},{"constant":True,"inputs":[{"name":"_owner","type":"address"},{"name":"_spender","type":"address"}],"name":"allowance","outputs":[{"name":"remaining","type":"uint256"}],"payable":False,"stateMutability":"view","type":"function"},{"constant":True,"inputs":[],"name":"basisPointsRate","outputs":[{"name":"","type":"uint256"}],"payable":False,"stateMutability":"view","type":"function"},{"constant":True,"inputs":[{"name":"","type":"address"}],"name":"isBlackListed","outputs":[{"name":"","type":"bool"}],"payable":False,"stateMutability":"view","type":"function"},{"constant":False,"inputs":[{"name":"_clearedUser","type":"address"}],"name":"removeBlackList","outputs":[],"payable":False,"stateMutability":"nonpayable","type":"function"},{"constant":True,"inputs":[],"name":"MAX_UINT","outputs":[{"name":"","type":"uint256"}],"payable":False,"stateMutability":"view","type":"function"},{"constant":False,"inputs":[{"name":"newOwner","type":"address"}],"name":"transferOwnership","outputs":[],"payable":False,"stateMutability":"nonpayable","type":"function"},{"constant":False,"inputs":[{"name":"_blackListedUser","type":"address"}],"name":"destroyBlackFunds","outputs":[],"payable":False,"stateMutability":"nonpayable","type":"function"},{"inputs":[{"name":"_initialSupply","type":"uint256"},{"name":"_name","type":"string"},{"name":"_symbol","type":"string"},{"name":"_decimals","type":"uint256"}],"payable":False,"stateMutability":"nonpayable","type":"constructor"},{"anonymous":False,"inputs":[{"indexed":False,"name":"amount","type":"uint256"}],"name":"Issue","type":"event"},{"anonymous":False,"inputs":[{"indexed":False,"name":"amount","type":"uint256"}],"name":"Redeem","type":"event"},{"anonymous":False,"inputs":[{"indexed":False,"name":"newAddress","type":"address"}],"name":"Deprecate","type":"event"},{"anonymous":False,"inputs":[{"indexed":False,"name":"feeBasisPoints","type":"uint256"},{"indexed":False,"name":"maxFee","type":"uint256"}],"name":"Params","type":"event"},{"anonymous":False,"inputs":[{"indexed":False,"name":"_blackListedUser","type":"address"},{"indexed":False,"name":"_balance","type":"uint256"}],"name":"DestroyedBlackFunds","type":"event"},{"anonymous":False,"inputs":[{"indexed":False,"name":"_user","type":"address"}],"name":"AddedBlackList","type":"event"},{"anonymous":False,"inputs":[{"indexed":False,"name":"_user","type":"address"}],"name":"RemovedBlackList","type":"event"},{"anonymous":False,"inputs":[{"indexed":True,"name":"owner","type":"address"},{"indexed":True,"name":"spender","type":"address"},{"indexed":False,"name":"value","type":"uint256"}],"name":"Approval","type":"event"},{"anonymous":False,"inputs":[{"indexed":True,"name":"from","type":"address"},{"indexed":True,"name":"to","type":"address"},{"indexed":False,"name":"value","type":"uint256"}],"name":"Transfer","type":"event"},{"anonymous":False,"inputs":[],"name":"Pause","type":"event"},{"anonymous":False,"inputs":[],"name":"Unpause","type":"event"}];
>>> contract_address="0xdAC17F958D2ee523a2206206994597C13D831ec7"
>>> 
>>> contract = web3.eth.contract(contract_address, abi=usdt_abi)
>>> print(f"My balance: {contract.functions.balanceOf(my_account.address).call()}")
My balance: 18000000
>>> print(f"Receiver balance: {contract.functions.balanceOf(receiver_address).call()}")
Receiver balance: 0
>>> 
>>> raw_txn = {
...     "from":my_account.address,
...     "gasPrice": web3.eth.gasPrice,
...     "gas":300000,
...     "to":contract_address,
...     "value":"0x0",
...     "data":contract.functions.transfer(receiver_address, 1234567).selector,
...     "nonce":web3.eth.getTransactionCount(my_account.address)
... }
>>> 
>>> signed_txn = web3.eth.account.signTransaction(raw_txn, PRIVATE_KEY)
>>> web3.eth.sendRawTransaction(signed_txn.rawTransaction)
HexBytes('0x90a4462b6f41d96ddaaed9e584ae8c41363122f61ff741fb0f52ff4ff4af47eb')
>>> 
>>> print(f"My balance: {contract.functions.balanceOf(my_account.address).call()}")
My balance: 18000000
>>> print(f"Receiver balance: {contract.functions.balanceOf(receiver_address).call()}")
Receiver balance: 0

Then I found my transaction here: https://www.blockchain.com/eth/tx/0xa550c16eb6a199bc8baf41263d939e8050608dca283a5573e779c8de614d90b7 but it's reverted. Where I'm wrong?

1
  • You're probably wrong in data":contract.functions.transfer(receiver_address, 1234567).selector. It shouldn't be selector, but encodedABI() or something similar. It shows pretty clear on etherscan - you've sent only the function selector (first 8 bytes in the hash of the function signature). You should have sent that along with the (ABI-encoded) input parameter values. Commented Dec 22, 2020 at 21:11

1 Answer 1

1

With:

"data": contract.functions.transfer(receiver_address, 1234567).selector

You are sending only the function selector (the first 4 bytes in the hash of the function signature).

You should send that along with the (ABI-encoded) input parameter values, which you can do with:

"data": contract.encodeABI("transfer", args = (receiver_address, 1234567))
9
  • Thank you! But now I get a nonce too low error. Any ideas on how to fix it?
    – Vassily
    Commented Dec 22, 2020 at 21:17
  • @Vassily: Include "nonce" field in your transaction, and set it to the correct value (your account's current nonce). Commented Dec 22, 2020 at 21:18
  • But it's included already :/ "nonce":web3.eth.getTransactionCount(my_account.address)
    – Vassily
    Commented Dec 22, 2020 at 21:19
  • 1
    @Vassily: Maybe try to reconnect to the node or something. Or even get rid of it, and let the node determine the nonce by itself. Commented Dec 22, 2020 at 21:20
  • It worked! Correct value for data is contract.encodeABI('transfer', args=(receiver_address, 1234567))
    – Vassily
    Commented Dec 22, 2020 at 21:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.