ethereum / web3.py

A python interface for interacting with the Ethereum blockchain and ecosystem.
http://web3py.readthedocs.io
MIT License
5.02k stars 1.71k forks source link

I would like to know how to add Input Data when sending ERC20 tokens. #2128

Closed bitcoin1000k closed 3 years ago

bitcoin1000k commented 3 years ago

Adding data when sending erc20 tokens causes an error.

How can I solve this?

This is example. https://etherscan.io/tx/0xab6c66b44b58b57358336409458258ee249cba2d989b75f28463c7e6c8ae5ecf

from web3 import Web3, HTTPProvider

infura_url = "https://mainnet.infura.io/v3/XXXXXXXX"
web3 = Web3(Web3.HTTPProvider(infura_url))

From = '0xAAA'
From_private = "AAAAAA"
To = '0xBBB'

abi = "USDT abi ~~~"
erc20 = web3.eth.contract(address='0xdAC17F958D2ee523a2206206994597C13D831ec7', abi=abi)

tx = erc20.functions.transfer(To, 1*10**6)
tx_1 = tx.buildTransaction({
    'chainId': "0x1",
    'gas': 200000,
    'nonce': web3.eth.getTransactionCount(From),
    'data': '0x94b918de000000000000000000000000000000000000000000000000000000012a05f200',
})

signed_txn = web3.eth.account.signTransaction(tx_1, From_private)
txn_hash = web3.eth.sendRawTransaction(signed_txn.rawTransaction)

print(web3.toHex(web3.sha3(signed_txn.rawTransaction)))

What was wrong?

Traceback (most recent call last): File "C:/Users/prog/PycharmProjects/untitled/web3/web3_test.py", line 23, in 'nonce': web3.eth.getTransactionCount(From)}) File "C:\Users\prog\AppData\Local\Programs\Python\Python37\lib\site-packages\web3\contract.py", line 1061, in buildTransaction raise ValueError("Cannot set data in build transaction") ValueError: Cannot set data in build transaction

How can it be fixed?

Fill this section in if you know how this could or should be fixed.

kclowes commented 3 years ago

The data should be added automatically. Try without the data key/value. I'm going to close, but if that doesn't solve the problem, feel free to reopen!