Friday 23 December 2016

Ethereum: To create a smart contract

1) Start the geth server

2) Create an account with command:
 personal.newAccount("password")
Remember the password for the account, you will need it to unlock the account if its locked. Command to unlock the account:
 personal.unlockAccount("addressofyouraccount", "password")
3) Set the created account as the default account:
primaryAddress = eth.accounts[0]
4) Make sure you account has ethers by tying the command
eth.getBalance(eth.coinbase).toNumber();
If your account don’t have any coins, start the miner with the command miner.start(). It will mine empty blocks and add ethers to your account.

5) Write the following simple contract in online solidity compiler
     contract SimpleStorage {
    uint storedData;
    function set(uint x) {
        storedData = x;
    }
    function get() constant returns (uint retVal) {
        return storedData;
    }
}
Online solidity compiler url:  https://ethereum.github.io/browser-solidity/#version=soljson-v0.4.1+commit.4fc6fc2c.js

Click on compile and copy whatever is in Web3 deploy textbox to a js file in ur server.  (create a file with extension js eg:test.js, then copy the code to it)

Example of compiled code 
var simplestorageContract = web3.eth.contract([{"constant":false,"inputs":[{"name":"x","type":"uint256"}],"name":"set","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"get","outputs":[{"name":"retVal","type":"uint256"}],"payable":false,"type":"function"}]);
var simplestorage = simplestorageContract.new(
   {
     from: web3.eth.accounts[0],
     data: '606060405260a18060106000396000f360606040526000357c01000000000000000000000000000000000000000000000000000000009004806360fe47b11460435780636d4ce63c14605d57603f565b6002565b34600257605b60048080359060200190919050506082565b005b34600257606c60048050506090565b6040518082815260200191505060405180910390f35b806000600050819055505b50565b60006000600050549050609e565b9056',
     gas: 4700000
   }, function (e, contract){
    console.log(e, contract);
    if (typeof contract.address !== 'undefined') {
         console.log('Contract mined! address: ' + contract.address + ' transactionHash: ' + contract.transactionHash);
    }
 }) 

6) Type the command on your geth console:
 loadScript("/home/test.js")
Now your contract is published in to the network as a transcation, you can see this by typing

eth.getBlock("pending", true).transactions

7) Then have someone mine — either you or a companion on the testnet. Once mining starts, you’ll see transaction get processed and it should spit back a transaction ID. This, of course, means you mined a new block that included that transaction.

You can check whether your transcation is mined or not by trying the command:
eth.getBlock("pending", true).transactions
If its mined you will see a empty list.

8) Now you can access your contract by typing simplestorage.get(). This will return 0 as the intial value on the code is set to 0

9) Try to update the contract with the command:
 simplestorage.set(5,{from: eth.accounts[0], gas: 3000000})
This will create a pending transcation, start the miner once your transcation is mined, access your contract with simplestorage.get() . You will see the updated value 5.


No comments:

Post a Comment