1

I've deployed a contract into my testrpc network.

$ truffle deploy
Using network 'development'.

Running migration: 1_initial_migration.js
  Deploying Migrations...
  Migrations: 0x9ec9e7fedb27068a70aedf453012bbced070d1e0
Saving successful migration to network...
Saving artifacts...
Running migration: 2_deploy_contracts.js
  Deploying CrowdFunding...
  CrowdFunding: 0x61a235a21078920353dbb719e7c1e6bd8c8e182b
Saving successful migration to network...
Saving artifacts...

How do I get the address for the function contribute (from the example code for https://dappsforbeginners.wordpress.com/tutorials/contracts-that-send-transactions/) that looks like this:

function contribute(uint campaignID) {
    Campaign c = campaigns[campaignID];
    Funder f = c.funders[c.numFunders++];
    f.addr = msg.sender;
    f.amount = msg.value;
    c.amount += f.amount;
}

I assume I need to get the function address, and then provide that as a parameters inside the MetaMask send dialog under transaction data. I can send the eth as the amount, correct? Then, msg.value and msg.sender are provided as context to the call.

1 Answer 1

1

I assume I need to get the function address, and then provide that as a parameters inside the MetaMask send dialog under transaction data. I can send the eth as the amount, correct?

Yes.

To get the Method ID (see What is an ABI and why is it needed to interact with contracts?), you need to use canonical types and then hash it with Keccak-256 and take the first 4 bytes.

Example

For contribute(uint), you need to use contribute(uint256).

Enter contribute(uint256) into a tool like https://emn178.github.io/online-tools/keccak_256.html

You'll get c1cbbca71c96db867642d5aeca3697d2e3bd24b386562e2b7004d1b0f4fbaaed

The first 4 bytes (8 hex characters) are c1cbbca7.

You can double-check 0xc1cbbca7 by https://www.4byte.directory

ID      text signature      bytes signature
3752    contribute(uint256) 0xc1cbbca7

0xc1cbbca7 is what you put in MetaMask's transaction data BUT you still need to encode the uint256 parameter to contribute.

See https://github.com/ethereum/wiki/wiki/Ethereum-Contract-ABI#examples for examples.

Manually encoding for the ABI can be tricky and prone to mistakes. You may end up calling the wrong function on the contract, or send your ETH to a function that will not give you what you are looking for. Be careful and always double-check if you need to do this. In this example, you could make a mistake encoding the campaignID and end up sending (and possibly losing) ETH to the wrong campaign.

Your Answer

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

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