Ethereum Local development environment and smart contracts

Source: Internet
Author: User

First install the GO environment https://golang.org/dl/directly with the installation package.
When you are done, look down.
go env
Then focus on the next Gopath path,
GOPATH="/Users/MacPro/box/
We want to put the go version of the client here, go compiler execution is to set the directory execution. The project will be put here when I write go later.

Download go version ethereum client
git clone https://github.com/ethereum/go-ethereum
cd go-ethereum
make geth
The Ethereum local test starts after the compilation is successful
First use the command to see if it succeeds,
build/bin/geth -h

If you need to do the environment variable directly with Geth
export PATH=$PATH:/全路径/build/bin/geth

Start building a private test chain
1 Find a directory to store mining data/home/vagrant/
2 Creating a Genesis block profile
vim genesis.json

{"config": {        "chainId": 1024,        "homesteadBlock": 0,        "eip155Block": 0,        "eip158Block": 0},"nonce": "0x0000000000000042","difficulty": "0x020000","mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000","coinbase": "0x0000000000000000000000000000000000000000","timestamp": "0x00","parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000","extraData": "0x11bbe8db4e347b4e8c937c1c8370e4b5ed33adb3db69cbdb7a38e1e50b1b82fa","gasLimit": "0xffffffff","alloc": {}}

3 Initializing the configuration
geth --datadir data init genesis.json

4 Start Node
geth --datadir data --networkid 123456 --rpc --rpccorsdomain "*" --nodiscover console
Each parameter represents the following meanings:
Networkid sets the network ID of the current blockchain to distinguish between different networks, 1 means the public chain
RPC represents initiating RPC traffic and can be deployed and debugged for smart contracts
Console indicates start command line mode, can execute command in Geth
JavaScript console environment that will enter the blockchain after successful execution

5 geth JavaScript Console Environment usage instructions
Create a new account
personal.newAccount() 或 personal.newAccount("123456")

View node Information
admin.nodeInfo

Digging mine
Start digging mine.miner.start(1)
Stop Digging mineminer.stop()

View current miner account number
eth.coinbaseDefault to First Account

Change Miner Account
miner.setEtherbase(eth.accounts[1])

View account Information
eth.accounts[0]

View account Balances
eth.getBalance(eth.accounts[0]) 或者 web3.fromWei(eth.getBalance(eth.accounts[0]), "ether")

Unlock Account
personal.unlockAccount(eth.accounts[0])
You need to unlock your account before using your funds

Transfereth.sendTransaction({from:eth.accounts[0],to:eth.accounts[1],value:web3.toWei(3,"ether")})
Use txpool.status to see trade status

View Chunk Data
eth.blockNumber
eth.getTransaction("0x0c59f431068937cbe9e230483bc79f59bd7146edc8ff5ec37fea6710adcab825")
eth.getBlock(1)View chunks with block numbers

can open a window to mine, and then open a window to do the trade.
build/bin/geth attach ipc:/Users/MacPro/go_project/src/github.com/ethereum/go-ethereum/data/geth.ipc

6 Smart Contracts
Create a Token.sol file written by solidity, with the following content:

contract Token {    address issuer;    mapping (address => uint) balances;    event Issue(address account, uint amount);    event Transfer(address from, address to, uint amount);    function Token() {        issuer = msg.sender;    }    function issue(address account, uint amount) {        if (msg.sender != issuer) throw;        balances[account] += amount;    }    function transfer(address to, uint amount) {        if (balances[msg.sender] < amount) throw;        balances[msg.sender] -= amount;        balances[to] += amount;        Transfer(msg.sender, to, amount);    }    function getBalance(address account) constant returns (uint) {        return balances[account];    }}

This code implements a simple token contract function.
The issue function can store tokens directly to the account
The transfer function can send tokens to other accounts
The GetBalance function can get the token balance of an account
From this contract, it is understood that the ERC20 agreement is to save a number on the Ethereum, and then add the so-called transaction, the other can not actually do more.
7 Deployment Contract
Build a test contract on http://remix.ethereum.org/to generate a deployment code in WEB3 format


Unlock First personal.unlockAccount(eth.accounts[0])
Click on the details to the left Web3deploy code to copy to the Geth window
Then post code to enter

Execute token after success see ABI data
Start execution of contract methods
At this point, the mining process has to work.

>token.issue(eth.accounts[0],30,{from: eth.accounts[0]})"0xae0fafd672cfcbaf3bf717a2dee49db3017d4da87cb3e087f2b3e2f28f933cf7">token.getBalance(eth.accounts[0])100> token.transfer(eth.accounts[1], 30, {from: eth.accounts[0]})"0x0df5fb59e8c96cb0884db40163f50fba9d65ad6e03080c21ba7d9fc2ca9663f0"> token.getBalance(eth.accounts[1])30

Reference articles
An introductory tutorial on Ethereum private chain and smart contract deployment
Ethereum's deployment of smart contracts

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.