ethereum and smart contracts

RPC In previous sections we have seen how contracts can be written, deployed and interacted with.Now it’s time to dive in the details of communicating with the Ethereum network and smart contracts.An Ethereum node offers a RPC interface.This interface gives Ðapp’s access to the Ethereum blockchain and functionality that the node provides, such as compiling smart contract code.It uses a subset of the JSON-RPC 2.0 specification (no support for notifications or named parameters) as serialisation protocol and is available over HTTP and IPC (unix domain sockets on linux/OSX and named pipe’s on Windows).If you are not interested in the details but are looking for an easy to use javascript library you can skip the following sections and continue with .Conventions The RPC interface uses a couple of conventions that are not part of the JSON-RPC 2.0 specification: Numbers are hex encoded.This decision was made because some languages have no or limited support for working with extremly large numbers.

To prevent these type of errors numbers are hex encoded and it is up to the deverloper to parse these numbers and handle them appropriately.See the hex encoding section on the wiki for examples.Default block number, several RPC methods accept a block number.
legit bitcoin walletIn some cases it’s not possible to give a block number or not very convenient.
ethereum in singaporeFor these cases the default block number can be one of these strings [“earliest”, “latest”, “pending”].
bitcoin volume paypalSee the wiki page for a list of RPC methods that use the default block parameters.
gia bitcoin tang m?nhDeploy contract We will go through the different steps to deploy the following contract using only the RPC interface.
first bitcoin atm canada

The first thing to do is make sure the HTTP RPC interface is enabled.This means for geth we supply the flag on startup and for eth the flag.In this example we use the geth node on a private development chain.Using this approach we don’t need ether on the real network.
bitcoin assignmentThis will start the HTTP RPC interface on .
ethereum chart usdNote geth supports CORS, see the flag for more information.
illuminati behind bitcoinWe can verify that the interface is running by retrieving the coinbase address and balance using curl.
bitcoin blockchain hash algorithmPlease note that data in these examples will differ on your local node.If you want to try these command replace the request params accordingly.

Remember when we said that numbers are hex encoded?In this case the balance is returned in wei as a hex string.If we want to have the balance in ether as a number we can use web3 from the geth console.Now that we have some ether on our private development chain we can deploy the contract.The first step is to verify that the solidity compiler is available.We can retrieve available compilers using the RPC method.We can see that the solidity compiler is available.If it’s not available follow these instructions.The next step is to compile the Multiply7 contract to byte code that can be send to the EVM.Now that we have the compiled code we need to determine how much gas it costs to deploy it.The RPC interface has an method that will give us an estimate.And finally deploy the contract.The transaction is accepted by the node and a transaction hash is returned.We can use this hash to track the transaction.The next step is to determine the address where our contract is deployed.

Each executed transaction will create a receipt.This receipt contains various information about the transaction such as in which block the transaction was included and how much gas was used by the EVM.If a transaction creates a contract it will also contain the contract address.We can retrieve the receipt with the RPC method.We can see that our contract was created on .If you got null instead of a receipt the transaction has not been included in a block yet.Wait for a moment and check if your miner is running and retry it.Interacting with smart contracts Now that our contract is deployed we can interact with it.There are 2 methods for this, sending a transaction or .In this example we will be sending a transaction to the multiply method of the contract.If we look at the documentation for the eth_sendTransaction we can see that we need to supply several arguments.In our case we need to specify the , and arguments.is the public address of our account and the contract address.

The argument is a bit harder.It contains a payload that defines which method must be called and with which arguments.This is were the ABI comes into play.The ABI defines how to define and encode data for the EVM.You can read all the details about the ABI here.The bytes of the payload is the function selector and defines which method is called.This is done by taking the first 4 bytes from the Keccak hash over the function name and its argument types and hex encode it.The function accepts an which is an alias for .This leaves us with: See for details this page.The next step is to encode the arguments.We only have one uint256, lets assume we supply the value 6.The ABI has a section which specifies how to encode uint256 types.This encodes to .Combining the function selector and the encoded argument our will be .Lets try it: Since we sent a transaction we got the transaction hash returned.If we retrieve the receipt we can see something new: {: ,: ,: ,: ,: , : : ,: ,: ,: ,: 0,: [: ,: 0: ,: 0 } The receipt contains a log.

This log was generated by the EVM on transaction execution and included in the receipt.If we look at the multipy function we can see that the Print event was raised with the input times 7.Since the argument for the Print event was a uint256 we can decode it according to the ABI rules which will leave us with the expected decimal 42.Apart from the data it is worth noting that topics can be used to determine which event created the log: You can read more about events, topics and indexing in the Solidity tutorial.This was just a brief introduction into some of the most common tasks.See for a full list of available RPC methods the RPC wiki page.Web3.js As we have seen in the previous example using the JSON-RPC interface can be quite tedious and error-prone, especially when we have to deal with the ABI.Web3.js is a javascript library that works on top of the Ethereum RPC interface.Its goal is to provide a more user friendly interface and reducing the chance for errors.