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)

2 Answers
2

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.)

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.