1

I'm trying to encode a function call on a ERC721 smart contract.

I'm using web3-python package to use some utils.

Here is my code:

from web3 import Web3

r = Web3.soliditySha3(['bytes32'], [b'tokenURI(uint256)'])
0xc87b56dda752230262935940d907f047a9f86bb5ee6aa33511fc86db33fea6cc

r[0:10]
0xc87b56dd

This code works partially: I get the signature of the function (0xc87b56dd). However, I want to pass an argument and encode it with the function name together.

So if I want to pass 275 as a parameter, I should get this result instead (at the end of the hash, 113 is 275 hex value):

0xc87b56dd0000000000000000000000000000000000000000000000000000000000000113

I don't know how to achieve that. I read a lot of forums but can't find the right way to do it.

Basically I want to achieve what this website is doing.

3

1 Answer 1

1

In solidity, I have the following :

contract TestContract {
    uint256 public i;
    function callMe(uint256 j, uint256 a) public payable {
        console.log("TestContract.callMe(%d,%d) called", j, a);
        i += j;
    }
    function getData(uint256 j, uint256 a) public view returns (bytes memory) {
        console.log("call signature encoded:");
        console.logBytes(
            abi.encodeWithSignature("callMe(uint256,uint256)", j, a)
        );
        return abi.encodeWithSignature("callMe(uint256,uint256)", j, a);
    }
}

In Python (brownie), I will call the function as following:

    v = 123
    a = 10000
    encoded_signature = t.getData(v, a)
    print(encoded_signature)
    sig = Web3.keccak(text="callMe(uint256,uint256)")[:4].hex()
    v_hex = convert.to_bytes(v).hex()
    a_hex = convert.to_bytes(a).hex()
    data = sig+v_hex+a_hex
    print(data)
    assert encoded_signature == data

Well, the idea is to obtain the function signature and to concatenate with parameter values converted to bytes.

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.