3

I'm using python to sign and send ETH transactions, and would like to do the same for ERC-20 token transactions as well. I'm having trouble correctly encoding the data payload to invoke the transfer method within ERC-20 token contracts.

Here's the method I want to use: transfer(address _to, uint256 _value)

Here's basically what I'm currently doing for the data field:

METHOD = "0xa9059cbb"
ADDRESS = "000000000000000000000000cD60535a5C13ED2D07199aDCe494D33b01930208" #this is the to_address padded to 32 bytes (changed for privacy)
VALUE = "0000000000000000000000000000000000000000000000000de0b6b3a7640000" #this is the hex of the value (1*10^18) padded to 32 bytes

DATA = METHOD+ADDRESS+VALUE

And I'm signing that along with the rest of the tx info. However, the resulting transaction I get has something completely different than my data payload in the "script" field. Any idea what I'm doing wrong?

Here's my code for signing the transaction. Unfortunately I didn't write this function so I don't understand a lot of what's going on in the rlp and ethereum libraries:

def signTransaction(to, value, privkey, nonce, data="", gasPrice=36000000000, gas=100000):
    from ethereum import transactions
    import rlp
    try:
        return {'error':False, 'sign':rlp.encode(transactions.Transaction(nonce, gasPrice, gas, to, value, data).sign(privkey)).encode('hex')}
    except Exception as msg:
        return {'error':True, 'message':msg}


signTransaction(CONTRACT_ADDRESS, 0, PRIVATE_KEY, NONCE, DATA)

Thanks!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.