current position:Home>Ethereum series - Smart contract development and commissioning

Ethereum series - Smart contract development and commissioning

2022-02-03 05:29:46 Brick moving leader

This article defaults to the relevant environment required by the smart contract Geth、Truffle、Solidity、Ganache And so on are complete !
This paper mainly introduces :

  1. Remix IDE How the environment connects to the specified chain ;
  2. compile Deploy and debug smart contracts
  3. Use zeppelin-solidity

1. install Remix IDE

    npm install remix-ide -g

There is no special emphasis on the relevant contents of network materials , Therefore, it is specially explained here :
During development and debugging , Using a local environment is relatively , It can be more convenient and intuitive to associate with local code
Install well remix-ide after , You can run command line commands in any path remix-ide
Then open the web page http://localhost:8080
Left side menu Button last “ Connect to local ”, After clicking , Confirm connection , There will be one more on the left localhost working directory
This directory is the command line run remix-ide The path of , It is also the code path of smart contract ( That is to say DAPP Framework environment for development )

2.Geth Start node command and related parameters

   geth --datadir=data --networkid 55661 --port 2000 --rpc --rpcaddr --rpcapi “db,eth,net,web3,personal” --rpcport 8102 --rpccorsdomain "*" --bootnodes "enode://0a2c7ee420570b3f6d59f214731d0d84aa5297f8b704939fd95ad643a752e34d7[email protected]" console

without --rpccorsdomain “*” stay remix-ide Cannot connect to chain in Web environment !

* Description of relevant console command parameters : See

3. A simple storage smart contract

a. stay SimpleStorage Project contract Add under path SimpleStorage.sol file :

    pragma solidity ^0.4.23;
    contract SimpleStorage {
        uint myVariable;

        constructor() public {

        // Store the value in this variable 
        function set(uint x) public {
            myVariable = x;

        // Query stored values 
        function get() constant public returns (uint) {
            return myVariable;

b. stay SimpleStorage Project migrations Add under path S2_deployed_simplestorage.js file :

    var SimpleStorage = artifacts.require("SimpleStorage");

    module.exports = function(deployer) {

c. Compile smart contracts on the command line :

    truffle compile

d. open truffle Console

    truffle develop ( Launched a program based on Ganache In the console , Built in Truffle Locally developed blockchain )
    (0) 0x627306090abab3a6e1400e9345bc60c78a8bef57
    (1) 0xf17f52151ebef6c7334fad080c5704d77216b732
    (2) 0xc5fdf4076b8f3a5357c5e395ab970b5b54098fef
    (3) 0x821aea9a577a9b44299b9c15c88cf3087f3b5544
    (4) 0x0d1d4e623d10f9fba5db95830f7d3839406c6af2
    (5) 0x2932b7a2355d6fecc4b5c0b6bd44cc31df247a2e
    (6) 0x2191ef87e392377ec08e7c08eb105ef5448eced5
    (7) 0x0f4f2ac550a1b4e2280d04c21cea7ebd822934b5
    (8) 0x6330a553fc93768f612722bb8c2ec78ac90b3bbc
    (9) 0x5aeda56215b167893e80b4fe645ba6d5bab767de

    Private Keys:
    (0) c87509a1c067bbde78beb793e6fa76530b6382a4c0241e5e4a9ec0a0f44dc0d3
    (1) ae6ae8e5ccbfb04590405997ee2d52d2b330726137b875053c36d94e974d162f
    (2) 0dbbe8e4ae425a6d2687f1a7e3ba17bc98c673636790f1b8ad91193c05875ef1
    (3) c88b703fb08cbea894b6aeff5a544fb92e78a18e19814cd85da83b71f772aa6c
    (4) 388c684f0ba1ef5017716adb5d21a053ea8e90277d0868337519f97bede61418
    (5) 659cbb0e2411a44db63778987b1e22153c086a95eb6b18bdf89de078917abc63
    (6) 82d052c865f5763aad42add438569276c00d3d88a2d062d36b2bae914d58b8c8
    (7) aa3680d5d48a8283413f7a108367c7299ca73f553735860a87b08f39395618b7
    (8) 0f62d96d6675f32685bbdb8ac13cda7c23436f63efbb9d07700d8669ff12b7c4
    (9) 8d5366123cb560bb606379f90a0bfd4769eecc0557f1b362dcae9012b548b1e5

e. Deploy smart contracts


    Using network 'develop'.

    Running migration: 1_initial_migration.js
    Deploying Migrations...
    ... 0xb70a114fd49735ef6c56ae86e329c38544c70729924e4f8f895e495d38ec3877
    Migrations: 0x8cdaf0cd259887258bc13a92c0a6da92698644c0
    Saving successful migration to network...
    ... 0xd7bc86d31bee32fa3988f1c1eabce403a1b5d570340a3a9cdba53a472ee8c956
    Saving artifacts...
    Running migration: 2_deployed_simplestorage.js
    Deploying SimpleStorage...
    ... 0x1b0294e4b04df57c8ff79001943c913465dc687162553e2a4a2aa0e57dc9bd0c
    SimpleStorage: 0x345ca3e014aaf5dca488057592ee47305d9b3e10
    Saving successful migration to network...
    ... 0xf36163615f41ef7ed8f4a8f192149a0bf633fe1a2398ce001bf44c43dc7bdda0
    Saving artifacts...

     Execute at this time get() function :
    return value.toNumber()});
     The result is  0 

     And then call set() function :
    return instance.set(6);});
         The stored value becomes 6 了 
     Call again get() function :
    return value.toNumber()});
     The result is  6

    * if necessary 

4. truffle What happens when an error occurs on the command line debug ?

     Under normal circumstances ,logs It's empty 
     tx: '0x070e447c68b4337264ccfd83376e40d48f9c580a9a6e54694c7eb420326369ab',
     transactionHash: '0x070e447c68b4337264ccfd83376e40d48f9c580a9a6e54694c7eb420326369ab',
        transactionIndex: 0,
        blockHash: '0xc7a1b8ee4bd6364d482576adae45c84caca8796b3cce59451eacf8038a097d2c',
        blockNumber: 16,
        gasUsed: 41669,
        cumulativeGasUsed: 41669,
        contractAddress: null,
        logs: [],
        status: '0x01',
        logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000' },
    logs: [] 

     Modify... In the contract set function :
    function set(uint x) public {
        myVariable = x;
        if (x%2 == 0) {
        emit Odd();
        } else {
        emit Even();
     Recompile deployment  compile / migrate --reset
     Re execution set() function :
    return instance.set(4);});

     tx: '0x2cc0d39fc0bec51835df91343e64577b34ae335f7d998143349d5ab8b3d63181',
     transactionHash: '0x2cc0d39fc0bec51835df91343e64577b34ae335f7d998143349d5ab8b3d63181',
        transactionIndex: 0,
        blockHash: '0x1ec12a731aff546eb84d87402cfc36f285c7e14faa9f6d14de22ea94f9434bfe',
        blockNumber: 11,
        gasUsed: 42514,
        cumulativeGasUsed: 42514,
        contractAddress: null,
        logs: [ [Object] ],
        status: '0x01',
        logsBloom: '0x00000000000000000000000000000000000000000000000002000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000000000000800000000000000' },
    [ {
     logIndex: 0,
        transactionIndex: 0,
        transactionHash: '0x2cc0d39fc0bec51835df91343e64577b34ae335f7d998143349d5ab8b3d63181',
        blockHash: '0x1ec12a731aff546eb84d87402cfc36f285c7e14faa9f6d14de22ea94f9434bfe',
        blockNumber: 11,
        address: '0xfb88de099e13c3ed21f80a7a1e49f8caecf10df6',
        type: 'mined',
        event: 'Odd',
        args: {
    } } ] }

here logs Information about abnormal conditions is recorded in 【 The specific principle is not yet clear ~】
The most important thing at this time is :transactionHash: ‘0x2cc0d39fc0bec51835df91343e64577b34ae335f7d998143349d5ab8b3d63181’,
stay truffle Under the console , debug 0x2cc0d39fc0bec51835df91343e64577b34ae335f7d998143349d5ab8b3d63181 Continuous return
You can trace the relevant code

    !!! Truffle includes an integrated debugger so that you can debug transactions made against your contracts. This debugger looks and feels like existing command line debuggers available for traditional development environments.
    !!! Debugging a transaction on the blockchain is different than debugging traditional applications (for instance, applications written in C++ or Javascript). When debugging a transaction on the blockchain, you're not running the code in real-time; instead, you're stepping over the historical execution of that transaction, and mapping that execution onto its associated code. This gives us many liberties in debugging, in that we can debug any transaction, any time, so long as we have the code and artifacts for the contracts the transaction interacted with. Think of these code and artifacts as akin to the debugging symbols needed by traditional debuggers.

5. Use zeppelin-solidity Release ERC-20 standard Token

Follow the previous steps to create a smart contract project directory , And initialize , Then install zeppelin-solidity

    sudo npm install zeppelin-solidity
     Call... From the command line Token contract :
    let contract = ZTJCoin.deployed().then(instance => contract = instance);;

6. Use OpenZeppelin Of StandardToken To create your own TutorialToken【ERC20】

     add to TutorialToken.sol file 
    pragma solidity ^0.4.24;
    import "zeppelin-solidity/contracts/token/ERC20/StandardToken.sol";

    contract TutorialToken is StandardToken {
        string public name = 'TutorialToken';
        string public symbol = 'TT';
        uint public decimals = 2;
        uint public INITIAL_SUPPLY = 12000;
        constructor() public {
            totalSupply_ = INITIAL_SUPPLY;
            balances[msg.sender] = INITIAL_SUPPLY;

In the code above ,import 了 StandardToken.sol, And declare TutorialToken Inherited from StandardToken. So you inherit StandardToken All variables and functions in the contract . Inherited contracts can be overridden , Just redefine the corresponding variables and functions in the subclass .

Next , Set up Token Parameters of , Need to define your own name,symbol,decimals and INITIAL_SUPPLY.

add to 2_deploy_contracts.js file

    var TutorialToken = artifacts.require("./TutorialToken.sol");

    module.exports = function(deployer) {

start-up Ganache To configure app.js Port and Ganache Port consistency
truffle console Start and connect Ganache Internal chain
When the project is initialized, it has been installed lite-server
npm run dev function dapp Program , That is, you can perform operations related to account transfer

Past highlights :
Blockchain Knowledge Series
Cryptography series
Zero knowledge proof series
Consensus series
Public chain research series
Bitcoin series
Ethereum series
EOS series
Alliance chain series
Fabric series
Smart contract series
Token series

copyright notice
author[Brick moving leader],Please bring the original link to reprint, thank you.

Random recommended