【区块链】以太坊ethereum智能合约部署

以太坊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
  • 进入contracts目录,创建下面的文件
// SPDX-License-Identifier: GPL-3.0 
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
$ 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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

夏 克

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值