Ethereum - How Depoly contract on the private chain

  • By,




    This tutorial shows you how to write contracts, compile contracts, and deploy contracts to your own private chain and call contract function.

    Environment Setup:

    Write a simple contract:

    Create a new text file, type the following code in this file:

    pragma solidity ^0.4.0;
    contract SimpleStorage {
        uint storedData;
        function set(uint x) {
            storedData = x;
        function get() constant returns (uint) {
            return storedData;
    The first line of code is used to declare to the compiler that the current contract is written in 0.4 Solidity syntax.The purpose of this statement is to ensure that the contract is backward compatible with the new version of the compiler.The following code is the contract body.Declaring a “State Variable” called the storedData type is uint, which is recorded in the blockchain database.The “State Variables” in the contract are similar to static member variables in c+ +.The two functions, set and get, are then declared to set and get the “state variable” values that are declared above。

    Save this text file and rename it SimpleStorage.sol. 这里写图片描述

    Compile the contract:

    Open the Windows command line and enter the following commands:

    echo var contractContent = > SimpleStorage.js
    solc --combined-json abi,bin,interface SimpleStorage.sol >> SimpleStorage.js1
    The main purpose of this step is to generate a javascript script that initializes a contractContent variable, which records the compiled contract script in string form.The second command is to use solidity compiler to compile our contract, SimpleStorage, and generate ABI, contract bytecode, and interface description.Wrap this information in json format.

    After running the above command.We got a javascript script, SimpleStorage.js 这里写图片描述

    Deploy the contract:

    Start the geth console.Enter the following command in the Windows command line:

    geth --datadir "E:\EthDBSpace\PrivChain" --jspath "D:\workspace\solidity\" console      1
    –datadir option is used to specify the eth data path, which specifies the path of the private chain data we built before.For how to build a private chain, see the previous lesson on “how to create a private chain”(。 –jspath option is used to specify the javascript storage directory.Here we specify the directory where we just compiled SimpleStorage.js. console command is used to open the geth command line

    After a few seconds of initialization, enter the geth command line.The results are as follows:


    Call the contract function:

    Next, we call the set function to assign values to the state variable, storedData.Because our set function is to write the data on the chain, we need to use sendTransaction to call it.To use the sendTransaction to call the contract on the chain, we first need to get the Function Selector for the signature of the Function.You need to pass the function signature to the sha3 function to generate a hash256, the first four bytes of the hash that is the value of the function selector.

    In the geth command line, enter the following command to get the set function hash:

    In the parameter list of the function signature, you can only fill in the parameter type, the parameter type is separated by a comma, and the parameter list should not contain Spaces. The parameter type must contain the word length.For example, the current contract’s storedData variable is uint, and the uint type in solidity is the same as uint256, so fill uint256 here.

    The result of the set function hash is as follows: 


    Query call result:

    We can also query the state variable storedData by calling the get function in the contract.Because the get function is constant, you can call locally.In the geth command line, enter the following command:

    The results are as follows:


Looks like your connection to Cryptocentral was lost, please wait while we try to reconnect.