简体   繁体   中英

Ethereum raw contract interaction using web3 python

I'm looking for an example of a raw json/body of a contract interaction before it is signed and passed to web3py / web3js / ethers

We are using AWS KMS for our wallet setup. There are some automated actions that this wallet is supposed to take. The problem I am facing is that in order to create a signature, the action has to be passed to KMS in a raw format.

I have found tutorials and samples describing the process to transfer a native token (ethereum itself) from the KMS wallet to a recipient, the process described is to create a raw dict containing all the required info, sign it with a given KMS method and then post it as a raw transaction.

For example a simple transaction raw dict should look like this (post EIP-1559)

{
    'nonce': nonce,
    'to': 0x0131c121,
    'value': 1000000000000,
    'data': '0x00',
    'gas': 160000,
    'maxFeePerGas': max_fee_per_gas,
    'maxPriorityFeePerGas': max_priority_fee_per_gas,
    'type': type,
    'chainId': chainid,
}

What we are trying to do, however, is to interact with various contracts that require some changes based on user actions. The way that I believe should work is to figure out what the dictionary body components look like for a given contract interaction. It would be great if someone could point me into the right direction or explain what the raw json/body of a contract interaction looks like

Actually a lot simpler than I thought.

{
    'nonce': nonce,
    'to': 0x0131c121,
    'value': 1000000000000,
    'data': '0x00',
    'gas': 160000,
    'maxFeePerGas': max_fee_per_gas,
    'maxPriorityFeePerGas': max_priority_fee_per_gas,
    'type': type,
    'chainId': chainid,
    'data': 0x38ed1739000000000000000000000000000000000000000000000000000000009502f900000000000000000000000000000000000000000000a07e38bf71936cbe39594100000000000000000000000000000000000000000000000000000000000000a00000000000000000000000003c02cebb49f6e8f1fc96158099ffa064bbfee38b00000000000000000000000000000000000000000000000000000000616e11230000000000000000000000000000000000000000000000000000000000000003000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2000000000000000000000000528b3e98c63ce21c6f680b713918e0f89dfae555
}

The data field is what describes the interaction with a given recipient address (which is the contract) and works as described in the following specs

https://docs.soliditylang.org/en/latest/abi-spec.html#formal-specification-of-the-encoding

Long story short,

I just used the buildTransaction function on web3py to build a raw transaction with the given abi as such

tx = usdc.functions.transfer(recipient_address, amount).buildTransaction({'from': signing_address})

where usdc is an instance of the contract.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM