Ethereum Smart Contract Development fourth: Implementing Hello World Smart Contract

Source: Internet
Author: User
Tags access properties constant json php class require

Originally Posted in: Ethereum Smart Contract Development fourth: Implementing Hello World Smart Contract

When most developers learn a language, they start with the output of a Hello world. We also start from the realization of a Hello world contract as a starting point to enter the global smart contract. Environment Preparation

Install node and npm. The installation process for node and NPM is not described in detail here. This article depends on the environment version:

node:v8.9.0
npm:5.5.1

In your code directory, create a folder named Smartcontract , and create the following two files Package.json , Hello.sol .

Smartcontract
├──hello.sol
└──package.json

In the package.json file, add the following dependency package configuration:

{
  "name": "Smartcontract",
  "version": "0.0.1",
  "dependencies": {
    "FS": "0.0.1-security",
    " Solc ":" ^0.4.21 ",
    " web3 ":" ^0.20.0 "
  }
}

FS module related operations for files
The Solc module is the compiler
The WEB3 module is a toolkit provided by Ethereum, primarily for communication with contracts

Next, execute the npm install download dependent package. Writing Contract Code

Once the environment is ready, you can start writing the contract code. Open the hello.sol file and write the following code:

pragma keyword: version declaration.
//used to instruct the compiler to compile the code into a specific version, so as not to cause a compatibility problem
//This does not support 0.4.0 before the compiler, and does not support 0.5.0 after the compiler (condition ^)
pragma solidity ^0.4.0;

Contract Keywords: contract statement
//And Java, PHP class similar
//here is the declaration of a contract named Hello
contract Hello {

    //public: function Access Properties ( Subsequent articles are elaborated in detail)
    //returns (String): Defines a return value type of string
    function say (string name) public returns (string) {
        return name;
    }
}

The code is simple. The operation that the contract returns as if it implements what string the user enters.

Next, we need to write a contract deployment script. writing a contract deployment script

Under the smartcontract directory, create a new file named deploy.js . The code is as follows:

Set WEB3 connection
var Web3 = require (' web3 ');
http://localhost:7545 the node link provided for ganache
var web3 = new Web3 (' Web3.providers.HttpProvider : 7545 '));
Read Contract
var fs = require (' FS ');
var contractcode = Fs.readfilesync (' Hello.sol '). toString ();
Compile contract code
var Solc = require (' Solc ');
var compilecode = Solc.compile (contractcode);

Console.log (Compilecode);

Get contract ABI and byte code
var abi = json.parse (compilecode.contracts[': Hello '].interface);
var bytecode = compilecode.contracts[': Hello '].bytecode;
Create contract Object
var votingcontract = web3.eth.contract (ABI);
Deploy the contract and return the deployment Object
var deployedcontract = votingcontract.new ({
    data:bytecode,
    from:web3.eth.accounts[0 ],  //The external Account address of the deployment contract
    gas:750000        //The miner fee for the deployment contract
});
Console.log (deployedcontract);

I added a simple comment in the code. This explains the concept of Abi .

The ABI full name is Application binary Interface, which is the application binary interface. Simply put, it is the interface description of the contract

It should be noted that the miner's gas cost is 750000. The execution of each trade in Ethereum (packed by miners) will be charged a certain amount of gas. Gas is designed to limit the amount of work required to execute an exchange and to pay for its execution. When EVM executes a trade, gas is gradually consumed in accordance with a specific rule, regardless of where it is executed, and when gas is exhausted, an out-of-gas exception is triggered. All state modifications made by the current call frame will be rolled back. If the execution ends and there is gas remaining, the gas will be returned to the sending account. Therefore, we can raise the gas value appropriately if we throw out the exception of gas at the time of deployment. Deployment

Under current directory, execute the node deploy.js command. We print out the compilecode variable in the deployment script, just a cursory look at the line:

{contracts: {': Hello ': {assembly: [Object], bytecode: ' 6060604052341561000f57600080fd5b61016c8061001e6 000396000f300606060405260043610610041576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16 8063d5c6130114610046575b600080fd5b341561005157600080fd5b6100a1600480803590602001908201803590602001908080601f0160208091040 260200160405190810160405280939291908181526020018383808284378201915050505050509190505061011c565b60405180806020018281038252 83818151815260200191508051906020019080838360005b838110156100e15780820151818401526020810190506100c6565b5050505090509081019 0601f16801561010e5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b61012461012c565b819050 919050565b6020604051908101604052806000815250905600a165627a7a72305820ff14cafd1df21e1edf19eff7598bc82a98940cc0fe045d6107d04 bb224014f990029 ', functionhashes: [Object], Gasestimates: [Object], interface: ' [{' constant ': false , "inputs": [{"Name": "Name", "TypE ":" string "}]," name ":" Say "," outputs ": [{" Name ":" "," Type ":" string "}]," payable ": false," statemutability ":" Nonpayable "," type ":" Function "}] ', metadata: ' {" compiler ": {" version ":" 0.4.21+commit.dfe3193c "}," language ":" Solidity "," output ": {" ABI ": [{" Constant ": false," inputs ": [{" Name ":" Name "," Type ":" string "}]," name ":" Say "," outputs " : [{"Name": "", "Type": "string"}], "payable": false, "statemutability": "Nonpayable", "type": "Function"}], "Devdoc": {" Methods ": {}}," Userdoc ": {" methods ": {}}," Settings ": {" Compilationtarget ": {" ":" Hello "}," Evmversion ":" Byzantium "," Libraries ": {}," optimizer ": {" Enabled ": False," runs ": $}," remappings ": []}," sources ": {" ": {" keccak256 ":" 0x2e3dd18fbfbd17bb4f866b1bfbb38082172a0bb58d9396b63bab04e67d9d8e08 "," URLs ": [" bzzr:// D1aae746dfab03e712d8a3cb76b7d4b5bf60f48fafbffa04dfa8a2d53ad5d0ca "]}}," Version ": 1} ', opcodes: ' PUSH1 0x60 PUSH1 0x4 0 mstore callvalue iszero PUSH2 0xF jumpi PUSH1 0x0 DUP1 REVERT jumpdest PUSH2 0x16c DUP1 PUSH2 0x1E PUSH1 0x0 codecopy PU SH1 0x0 RETURN STOP PUSH1 0x60 PUSH1 0x40 mstore PUSH1 0x4 calldatasize LT PUSH2 0x41 jumpi PUSH1 0x0 calldataload PUSH29 0x1000000000000000000000000 00000000000000000000000000000000 SWAP1 DIV PUSH4 0xFFFFFFFF and DUP1 PUSH4 0xd5c61301 EQ PUSH2 0x46 jumpi jumpdest PUSH1 0 X0 DUP1 REVERT jumpdest callvalue iszero PUSH2 0x51 jumpi PUSH1 0x0 DUP1 REVERT jumpdest PUSH2 0xa1 PUSH1 0x4 DUP1 DUP1 CA  Lldataload SWAP1 PUSH1 0x20 add SWAP1 DUP3 add DUP1 calldataload SWAP1 PUSH1 0x20 add SWAP1 DUP1 DUP1 PUSH1 0x1F Add PUSH1 0x20 DUP1 SWAP2 DIV MUL PUSH1 0x20 add PUSH1 0x40 mload SWAP1 DUP2 add PUSH1 0x40 mstore DUP1 SWAP4 SWAP3 SWAP2 SWAP1 DUP 2 DUP2 mstore PUSH1 0x20 ADD DUP4 DUP4 DUP1 DUP3 DUP5 calldatacopy DUP3 ADD SWAP2 pop pop pop pop pop SWAP2 SWAP1 pop POP PUSH2 0x11c Jump jumpdest PUSH1 0x40 mload DUP1 DUP1 PUSH1 0x20 ADD DUP3 DUP2 SUB DUP3 mstore DUP4 DUP2 DUP2 mload DUP 2 Mstore PUSH1 0x20 ADD SWAP2 POP DUP1 mload SWAP1 PUSH1 0x20 ADD SWAP1 DUP1 DUP4 DUP4 PUSH1 0x0 jumpdest DUP4 DUP2 LT ISZ ERO PUSH2 0xe1 Jumpi DUP1 DUP3 Add mload DUP2 DUP5 add mstore PUSH1 0x20 DUP2 add SWAP1 pop PUSH2 0xC6 jump jumpdest pop pop pop pop SWAP1 pop SWA P1 DUP2 ADD SWAP1 PUSH1 0x1F and DUP1 iszero PUSH2 0x10e jumpi DUP1 DUP3 SUB DUP1 mload PUSH1 0x1 DUP4 PUSH1 0x20 SUB PUSH 2 0x100 EXP SUB not and DUP2 mstore PUSH1 0x20 ADD SWAP2 pop jumpdest pop SWAP3 pop pop pop PUSH1 0x40 mload DUP1 SWAP2 SU B SWAP1 RETURN jumpdest PUSH2 0x124 PUSH2 0x12c jump Jumpdest DUP2 SWAP1 pops SWAP2 SWAP1 pop jump jumpdest PUSH1 0x20 PUSH 1 0x40 mload SWAP1 DUP2 ADD PUSH1 0x40 mstore DUP1 PUSH1 0x0 DUP2 mstore POP SWAP1 jump STOP LOG1 PUSH6 0x627a7a723058 KEC CAK256 selfdestruct EQ 0xca REVERT SAR callcode 0x1e 0x1e 0xdf not 0xef 0xf7 msize DUP12 0xc8 0x2a SWAP9 SWAP5 0xc 0xc0 in VALID DIV 0x5d PUSH2 0x7d0 0x4b 0xb2 0x24 ADD 0x4f SWAP10 STOP 0x29 ', Runtimebytecode: ' 6060604052600436106100415 76000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff168063d5c6130114610046575b600080fd5b3415 61005157600080fd5b6100a1600480803590602001908201803590602001908080601f0160208091040260200160405190810160405280939291908181526020018383808284378201 915050505050509190505061011c565b6040518080602001828103825283818151815260200191508051

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.