Basic knowledge of

Geth

Geth is the official client software provided by the Ethereum Foundation and written in the Go programming language.

Solidity

Solidity is a contract-oriented language that can be used on a variety of different blockchain platforms. Its main developers are Gavin Wood, Christian Reitwiessner, Alex Beregszaszi, Liana Husikyan, Yoichi Hirai and several other early core Ethereum contributors. Solidity enables developers to write smart contracts on blockchains such as Ethereum.

The syntax concept for Solidity was first developed by Gavin Wood in 2014 and later developed by the Ethereum team Solidity led by Christian Reitwiessner. It is one of four languages designed for the Ethereum Virtual Machine (EVM) (Serpent, LLL, Viper (experimental), and Mutan (deprecated)).

Web3.js

A javascript library that can be used to interact with a node. Since it is a JavaScript library, you can use it to build Web-based Dapps.

Remix

Ethereum official recommended smart contract development IDE, suitable for beginners, you can quickly deploy and test smart contracts in the browser.

Setting up the development environment

Geth installation

Install geTH on a Mac

brew tap ethereum/ethereum
brew install ethereum
Copy the code

Geth use

Geth starts an Ethereum network node

geth --datadir testNet --dev console 2>> test.log
Copy the code

The following figure shows the successful startup interface:

Ready to account

View account:

> eth.accounts
Copy the code

We can see that an array of accounts is returned with a default account, such as:

["0xcc4164b8535eb7a9291486a1a5c982fd15c8b75d"]
Copy the code

Check the balance:

> eth.getBalance(eth.accounts[0])
Copy the code

By default, we take the balance of the first account. It is easy to see that this account has a lot of balance allocated by default. To make it easier to see the change of balance, we can create a new account.

Create an account:

personal.newAccount(“password123456”)

Password123456 is the password of the new account, then we output the array of accounts to see the two accounts, output the balance of the new account eth. GetBalance (eth. Accounts [1]), we can see that the value is 0. So we need to transfer an ether to the new account.

Transfer money to new account:

eth.sendTransaction({from: '0xcc4164b8535eb7a9291486a1a5c982fd15c8b75d', to: '0x0a9961a0cde47c7e806a2874dc065cb33f6201c5', value: web3.toWei(1, "ether")})
Copy the code

Then you can see the mining record in your test.log. Check the balance eth. GetBalance (eth. Accounts [1]) again to see that there is an Ether in the account.

Unlock account:

To unlock your account before deploying the contract, enter your account number and the password you set earlier using the following command:

personal.unlockAccount(eth.accounts[1],"password123456");
Copy the code

Writing contract code

Deployment of contract

Here’s a simple smart contract code:

Pragma solidity 0.4.24; contract hello {function mutiply(uint a) returns (uint result) {
        returna*3; }}Copy the code

This code simply passes in a number and returns the number multiplied by three each time mutiply() is called.

Copy this code into remix, which is a browser IDE. The browser IDE can also be installed locally. Here is a tutorial on how to install it: Github address. Click Details to deploy the smart contract code, then find the following code in the pop-up box, copy it directly and change web.eth. Accounts [0] to Web.eth. Accounts [1].

Then type the following code on the command line:

> var helloContract = web3.eth.contract([{"constant":true."inputs": [{"name":"a"."type":"uint256"}]."name":"mutiply"."outputs": [{"name":"result"."type":"uint256"}]."payable":false."stateMutability":"nonpayable"."type":"function"}]);
undefined
> var hello = helloContract.new(
   {
     from: web3.eth.accounts[1], 
     data: '0x608060405234801561001057600080fd5b5060bb8061001f6000396000f300608060405260043610603f576000357c01000000000000000000000 00000000000000000000000000000000000900463ffffffff168063f70d290d146044575b600080fd5b348015604f57600080fd5b50606c600480360 381019080803590602001909291905050506082565b6040518082815260200191505060405180910390f35b60006003820290509190505600a165627 a7a7230582002abd0500936c002b085e7f017e1aca58ca5294e5e4b27f0733e430604076c3f0029', 
     gas: '4700000'
   }, function (e, contract){
    console.log(e, contract);
    if(typeof contract.address ! = ='undefined') {
         console.log('Contract mined! address: ' + contract.address + ' transactionHash: '+ contract.transactionHash); }})Copy the code

After executing the code, the following output indicates that the contract is successfully deployed. Address is the contract address:

> null [object Object]
Contract mined! address: 0xa29b84d8d302820f6ca1ebbf2f159ba12cf82b02 transactionHash: 0x15dd29aa336c8c4bf5868422c202f55153990a04426d5130e454957a87f56b07
Copy the code

Call the contract

After deploying the contract, we are ready to call the contract. The value in quotes is the address of the contract. Here we define a Demo variable and call the multiply() method from Demo.

> Demo=eth.contract(hello.abi).at("0xa29b84d8d302820f6ca1ebbf2f159ba12cf82b02")
{
  abi: [{
      constant: true,
      inputs: [{...}],
      name: "mutiply",
      outputs: [{...}],
      payable: false,
      stateMutability: "nonpayable".type: "function"
  }],
  address: "0xa29b84d8d302820f6ca1ebbf2f159ba12cf82b02",
  transactionHash: null,
  allEvents: function(),
  mutiply: function()
}
> Demo.mutiply(3)
9
Copy the code

reference

Smart contract development environment setup and Hello World contract

Remix Ethereum Solidity IDE build and preliminary use

GETH client basic operations