以太坊ethereum智能合约部署
初始化创世区块
{
"config" : {
"chainId" : 666 ,
"homesteadBlock" : 0 ,
"eip150Block" : 0 ,
"eip150Hash" : "0x0000000000000000000000000000000000000000000000000000000000000000" ,
"eip155Block" : 0 ,
"eip158Block" : 0 ,
"byzantiumBlock" : 0 ,
"constantinopleBlock" : 0 ,
"petersburgBlock" : 0 ,
"istanbulBlock" : 0 ,
"ethash" : { }
} ,
"nonce" : "0x0" ,
"timestamp" : "0x5ddf8f3e" ,
"extraData" : "0x0000000000000000000000000000000000000000000000000000000000000000" ,
"gasLimit" : "0x47b760" ,
"difficulty" : "0x00002" ,
"mixHash" : "0x0000000000000000000000000000000000000000000000000000000000000000" ,
"coinbase" : "0x0000000000000000000000000000000000000000" ,
"alloc" : { } ,
"number" : "0x0" ,
"gasUsed" : "0x0" ,
"parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000"
}
然后使用 geth 命令初始化
geth init ./genesis.json --datadir "./chain"
启动私有链
geth --datadir "./chain" --nodiscover --networkid= 88 --rpc --rpcaddr= 0.0.0.0 --rpcport 8545 --rpccorsdomain "*" --allow-insecure-unlock console
创建一个账号
> personal.newAccount( "xk.xmx" )
INFO [ 09-18| 11:51:22.624] Your new key was generated address= 0xDDDd822627F3Ad76487D2Cd0E7DEd2E0d13d8455
WARN [ 09-18| 11:51:22.624] Please backup your key file! path= /home/frank/project/node/eth/chain/keystore/UTC--2020-09-18T03-51-20.307316879Z--dddd822627f3ad76487d2cd0e7ded2e0d13d8455
WARN [ 09-18| 11:51:22.624] Please remember your password!
"0xdddd822627f3ad76487d2cd0e7ded2e0d13d8455"
命令
miner.start( )
eth.getBalance( eth.accounts[ 0] )
miner.stop( )
部署
mkdir dapp
cd dapp
truffle init
pragma solidity >= 0.4 .16 < 0.8 .0 ;
contract SimpleStorage {
uint storedData;
function set ( uint x) public {
storedData = x;
}
function get ( ) public view returns ( uint) {
return storedData;
}
}
truffle compile
$ truffle migrate
Compiling your contracts.. .
== == == == == == == == == == == == == =
> Everything is up to date, there is nothing to compile.
Starting migrations.. .
== == == == == == == == == == ==
> Network name: 'development'
> Network id: 88
> Block gas limit: 6930544 ( 0x69c070)
1_initial_migration.js
== == == == == == == == == == ==
Deploying 'Migrations'
----------------------
> transaction hash: 0xbfcbb276748552711aea52120a552c06e559af7dc764b583e31b84ad79d0304e
> Blocks: 0 Seconds: 0
> contract address: 0x778584EA693D436527aEd861078A57588d90e60B
> block number: 402
> block timestamp: 1600409199
> account: 0x78f6041383bbFc1E87ACeFEdDBCC6fd4fE78389D
> balance: 804
> gas used: 191943 ( 0x2edc7)
> gas price: 20 gwei
> value sent: 0 ETH
> total cost: 0.00383886 ETH
> Saving migration to chain.
> Saving artifacts
-------------------------------------
> Total cost: 0.00383886 ETH
Summary
== == == =
> Total deployments: 1
> Final cost: 0.00383886 ETH
truffle console
let contract
HelloWorld. deployed ( ) . then ( instance => contract = instance)
contract. sayHello. call ( )
“Migrations” – Returned error: authentication needed: password or unlock.
> personal.unlockAccount( eth.accounts[ 0] )
Unlock account 0x32314e87346e13338fb11c0acc8e2d51ac4139eb
Passphrase: // 然后输入你账户的密码
模拟区块连
npm install ganache-cli
ganache-cli
测试工具
npm install -g ethereumjs-testrpc
testrpc