13.06.2017 Views

Whitepaper - Ethereum Classic With Cover

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>Ethereum</strong> <strong>Classic</strong> Documentation, Release 0.1<br />

Interacting with a contract<br />

Interaction with a contract is typically done using an abstraction layer such as the eth.contract() function which<br />

returns a javascript object with all of the contract functions available as callable functions in javascript.<br />

The standard way to describe the available functions of a contract is the ABI definition. This object is an array<br />

which describles the call signature and return values for each available contract function.<br />

var Multiply7 = eth.contract(contract.info.abiDefinition);<br />

var myMultiply7 = Multiply7.at(address);<br />

Now all the function calls specified in the ABI are made available on the contract instance. You can just call those<br />

methods on the contract instance in one of two ways.<br />

> myMultiply7.multiply.sendTransaction(3, {from: address})<br />

"0x12345"<br />

> myMultiply7.multiply.call(3)<br />

21<br />

When called using sendTransaction the function call is executed via sending a transaction. This will cost<br />

ether to send and the call will be recorded forever on the blockchain. The return value of calls made in this manner<br />

is the hash of the stransaction.<br />

When called using call the function is executed locally in the EVM and the return value of the function is<br />

returned with the function. Calls made in this manner are not recorded on the blockchain and thus, cannot modify<br />

the internal state of the contract. This manner of call is referred to as a constant function call. Calls made in this<br />

manner do not cost any ether.<br />

You should use call if you are interested only in the return value and use sendTransaction if you only care<br />

about side effects on the state of the contract.<br />

In the example above, there are no side effects, therefore sendTransaction only burns gas and increases the<br />

entropy of the universe.<br />

Contract metadata<br />

In the previous sections we explained how you create a contract on the blockchain. Now we will deal with the rest<br />

of the compiler output, the contract metadata or contract info.<br />

When interacting with a contract you did not create you might want documentation or to look at the source code.<br />

Contract authors are encouraged to make such information available by registering it on the blockchain or through<br />

a third party service, such as EtherChain. The admin API provides convenience methods to fetch this bundle for<br />

any contract that chose to register.<br />

// get the contract info for contract address to do manual verification<br />

var info = admin.getContractInfo(address) // lookup, fetch, decode<br />

var source = info.source;<br />

var abiDef = info.abiDefinition<br />

The underlying mechanism that makes this work is is that:<br />

• contract info is uploaded somewhere identifiable by a URI which is publicly accessible<br />

• anyone can find out what the URI is only knowing the contracts address<br />

These requirements are achieved using a 2 step blockchain registry. The first step registers the contract code (hash)<br />

with a content hash in a contract called HashReg. The second step registers a url with the content hash in the<br />

UrlHint contract. These registry contracts were part of the Frontier release and have carried on into Homestead.<br />

By using this scheme, it is sufficient to know a contract’s address to look up the url and fetch the actual contract<br />

metadata info bundle.<br />

So if you are a conscientious contract creator, the steps are the following:<br />

1. Deploy the contract itself to the blockchain<br />

1.7. Contracts and Transactions 83

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!