21

Why do I need a compiled version of the contract just to get the ABI definition? Seems like the contract address should be enough.

Im very confused because I am simply trying to run a contract assuming it was made on another computer. It looks like first I need the entire code used to create the contract, which I don't think I would have hypothetically. (I'm literally just following along with the 'greeter' example)

13

Seems like the contract address should be enough.

You're right, it would be better if the ABI could be obtained by just looking at the contract.

So why do contract writers need to also provide the ABI, in addition to the contract address?

One reason is that the function Method IDs in a contract are computed using a hash function. So it is infeasible for a tool to look at the contract bytecode and extract the names of the functions/methods inside that contract. (The tool would have to guess what the function names are, hash them, and see if the first 4 bytes match the Method ID -- there are too many possible function names.)

  • 3
    Does this imply that after creating a contract, the author could potentially lose the method names forever? I mean if they did not save them locally on their own machine... – ZMitton Feb 17 '16 at 5:22
  • 6
    Yes, just like if you write a normal program, compile it, and lose the source code. – eth Feb 17 '16 at 6:00
11

You don't need the source code, just the ABI. The ABI is produced when the code is compiled, but can be, and usually is, distributed separately.

The ABI is essentially just a list of the function names in a contract and what types of arguments they take. You don't even need the whole ABI, just the parts that you want to interact with. For example, to generate the ABI for the greeter contract's greet function, you can just recompile this contract:

contract Greeter {
  function greet() constant returns(string);
}

using solc or the online compiler. Look for the Interface field in the online compiler. That's the ABI definition.

  • 1
    Where does Interface field exists on the new solidity browser (ethereum.github.io/browser-solidity/…) @Tjaden Hess♦ – alper Feb 25 '18 at 0:01
  • @Alper did you have any luck finding how to get the ABI of a contract from the current version of remix? – John Murphy Mar 5 '18 at 9:00
  • Yes sir, under Compile tab and click to details for the contract you want to. Then you should see ABI section, click copy icon next to it. @JohnMurphy – alper Mar 5 '18 at 9:09

Your Answer

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

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