Skip to main content

Blockchain Solidity-Remix

info

Before the coding, prepare MetaMask wallet (chrome extension) and faucet Etherum from the listed website on ChainLink, where I used https://faucets.chain.link/rinkeby

First solidity contract#

info

This is a contract that stores a list of peoples' names and their numbers, functioned with adding, mapping and public views.

  • wallet: MetaMask
  • online editor: Remix
  • language: solidity
  • environment: Javascript VM (virtual machine simulate actually deploying to a test/real network)
  • specify solidity version: pragma solidity ^0.6.0;
  • define a contract:
contract SimpleStorage{
}
  • types: uint, address, bool, string, bytes
uint256 number=5;bool thebool=true;string thestring="hello world";int256 theint=-5;address theaddress=0x1234567890123456789012345678901234567890;bytes32 thebytes='cat';
  • function: function

  • visibility: the key words public,external,internal,private defines the visibility of the variable and function. The default visibility is internal.

  • Any state-change Function calls are transactions which costs gas.

  • Fake Deploy on Javascript VM: costs some gas to deploy the contract.

  • view and pure are non-state-changing functions, no transaction are made to call them. public parameter binds the view function.

function retrieve() public view returns(uint256){        return somenumber;    }
  • struct: defines new types in solidity.

  • Arrays: a way of storing a list of object or type. Dynamic or fixed arrays.

  • memory: data will only be stored during the execution of the function. Usually bind with string type. storage: data will be stored in the state of the blockchain.

  • mapping: a way of storing a list of object or type.

  • SPDX-License-Identifier

Go live#

Deploy to some test/real network, that all people can access this contract. chain-link faucet test-net. Change environment to Injected Web3, which take the MetaMask we use into the source code of the browser or to say MetaMask is the Web3 provider. Web3 Provider if we want to use own blochchain node. we can followed the link in the output to view the details on etherscan. What we just did is we are making a transaction on the blockchain to create a contract.

  • Interact with the deployed contract. Store a new number

full source code#

SimpleStorage.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.6.0;
contract SimpleStorage {
    uint256 somenumber;
    struct People {        // store some number associate with some people        uint256 somenumber;        string name;
    }    People[] public people;    mapping(string => uint256) public nametosomenumber;

    function addPerson(string memory _name, uint256 _somenumber) public {        //add to the dynamic array        people.push(People({somenumber:_somenumber,name:_name}));        nametosomenumber[_name] = _somenumber;    }
    function store(uint256 _somenumber) public {        // store value into variable        somenumber = _somenumber;    }    function retrieve() public view returns(uint256){        return somenumber;    }    }