如需转载请注明出处https://my.oschina.net/feistel/blog/3007913web
1.part6为在Remix上编译测试Solidity编写的智能合约
2.part7为在用geth搭建的私链上部署合约
3.其余部分介绍在ganache-cli模拟链上用truffle编译、部署、调用shell
----------------------------------------------------------------------------------------------------------------api
1跨域
图形界面版 本地测试链ganache-cli
https://truffleframework.com/ganache
用于模拟以太坊区块链生态,包括有旷工在挖矿,保证成功部署到测试链上,能够在该链上测试发布智能合约浏览器
(注意:ganache关闭后,节点服务的数据也会被清空)服务器
----------------------------------------------------------------------------------------------------------------网络
2cors
pragma solidity >=0.4.21 <0.6.0;dom
contract Migrations {
address public owner;
uint public last_completed_migration;ide
constructor() public {
owner = msg.sender;
}
modifier restricted() {
if (msg.sender == owner) _;
}
function setCompleted(uint completed) public restricted {
last_completed_migration = completed;
}
function upgrade(address new_address) public restricted {
Migrations upgraded = Migrations(new_address);
upgraded.setCompleted(last_completed_migration);
}
function getOwner() view public returns (address) {
return owner;
}
}
----------------------------------------------------------------------------------------------------------------
3
//构建新项目
truffle init
建立part2的Solidity合约代码
//编译,自动编译contracts文件夹下sol文件
truffle compile (--all 强制从新编译全部文件) (--network name,指定使用的网络)
//配置truffle-config.js文件
修改:
development: {
host: "127.0.0.1", // Localhost (default: none)
port: 7545, // Standard Ethereum port (default: none)
network_id: "*", // Any network (default: none)
}
//部署到区块链上,保证模拟的区块链客户端ganache在运行
//truffle会链接到上述区块链网络中,进行编译
truffle migrate (--reset 表示强制从新部署)
----------------------------------------------------------------------------------------------------------------
4
PS E:\@2019\truffle\test> truffle migrate
⚠️ Important ⚠️
If you're using an HDWalletProvider, it must be Web3 1.0 enabled or your migration will hang.
Starting migrations...
======================
> Network name: 'development'
> Network id: 5777
> Block gas limit: 6721975
1_initial_migration.js
======================
Deploying 'Migrations'
----------------------
> transaction hash: 0xecf11deb00bf225021fda820ba1f73ba111d372cdcd8cb485fb1033bbc914e54
> Blocks: 0 Seconds: 0
> contract address: 0x77D4F14109d81Dc1d700F9C8A6E9475Fb783D4e9
> account: 0xf0f6e21B63D0c9eF5A5C6f3E3c3272ABdBC5815E
> balance: 99.99430184
> gas used: 284908
> gas price: 20 gwei
> value sent: 0 ETH
> total cost: 0.00569816 ETH
> Saving migration to chain.
> Saving artifacts
-------------------------------------
> Total cost: 0.00569816 ETH
Summary
=======
> Total deployments: 1
> Final cost: 0.00569816 ETH
注:
gas used:284908
gas price:20 gwei
284908 * 20 = 5698160 gwei
5698160 / 1 000 000 000 = 0.005698160 eth
所以,total cost:0.005698160 eth
contract address,合约地址
account,合约帐户
----------------------------------------------------------------------------------------------------------------
5
与智能合约交互
//进入Truffle控制台,须要在项目文件夹下
//可使用命令与geth相似,web3
truffle console
//将合约实例赋给instance
let accounts = await web3.eth.getAccounts()
accounts
[ '0xf0f6e21B63D0c9eF5A5C6f3E3c3272ABdBC5815E',
'0x21123d9E48693c7656B904B82e11e2Fa77D09F41',
'0xd58bcc4Fbb5961FC7E96809B07c9e481981351D2',
'0x225a3aa18c95e291A14fA25fee51dB6Ce87c1eb4',
'0x40c8086a4AB47f547B452fF4C3651ab54F1d41Fb',
'0x0Ce8832b9e9bD099CA0C5aC90cd0712e286B6C39',
'0xBdF920b656ef5819C312F7575B26ebC035C30520',
'0x697798292C73F3DfF1E9B9c9eE2A09dc0A4810b4',
'0x5364d1566C10382E2880123989f006D9AAf4Fea5',
'0xaCC3e08a27B56436bb4684B3d633d16f9e2456d2' ]
//建立合约实例,以{from: accounts[0]}设为调用者
let instance = await Migrations.deployed({from: accounts[0]})
//查看状态变量Owner
instance.getOwner()
'0xf0f6e21B63D0c9eF5A5C6f3E3c3272ABdBC5815E'
instance.setCompleted(1,{from: accounts[0]})
instance.upgrade(accounts[1],{from: accounts[0]})
----------------------------------------------------------------------------------------------------------------
6
使用在线工具,能够直接编译,点击函数进行调用,自动部署到测试链上,无需关心测试链,只须要专心测试智能合约代码
http://remix.ethereum.org/
----------------------------------------------------------------------------------------------------------------
7
在本身建立的以太坊本地区块链私链上(用geth建立的),发布智能合约
//启动以太坊节点
geth --identity "newEth" --rpc --rpccorsdomain "*" --datadir .\data\ --port 30303 --rpcapi "db,eth,net,web3" --networkid 10 console
--identity,自定义节点名
--rpc,启用HTTP-RPC服务器
--rpccorsdomain,容许跨域请求的域名列表(逗号分隔)(浏览器强制)
--rpcapi,基于HTTP-RPC接口提供的API
--networkid,网络标识符(整型, 1=Frontier, 2=Morden (弃用), 3=Ropsten, 4=Rinkeby) (默认: 1)
保持启动该节点的shell运行,下面操做在truffle项目另开shell窗口。
注:建立以太坊私链节点参考 以太坊又是啥? part7
https://my.oschina.net/feistel/blog/3007768
用truffle部署前先配置truffle-config.js文件,如part3所示
development: {
host: "127.0.0.1", // Localhost (default: none)
port: 8545, // Standard Ethereum port (default: none)
network_id: "*", // Any network (default: none)
from: "0x5586fd7bb8b81992526bbf6bf707f0eba9ab3c7a",
gas: 4712387
}
注:1.须要指定部署合约的地址from,该地址是在私链节点上建立的,而且须要解锁
2.须要指定gas
//接着
truffle compile
truffle migrate
便可部署到私链上。
//若是节点没有开始挖矿吗,在节点shell上,能够看到有一个待确认交易在交易池中
txpool.status
{
pending: 1,
queued: 0
}
//让节点开始挖矿
miner.start(1)
要调用合约,回到part5
其中在truffle看到以下:
⚠️ Important ⚠️
If you're using an HDWalletProvider, it must be Web3 1.0 enabled or your migration will hang.
Starting migrations...
======================
> Network name: 'development'
> Network id: 10
> Block gas limit: 4712388
1_initial_migration.js
======================
Deploying 'Migrations'
----------------------
> transaction hash: 0x097f821a5cf84410a0c16d4987f86df061b154413e05d3efb0d5b55a3ea35732
> Blocks: 0 Seconds: 0
> contract address: 0xA920f6F9Aa3993e8cf11f70D3e3c581671B173e6
> account: 0x5586fd7bb8b81992526BbF6bf707F0eba9ab3C7a
> balance: 114.99242616
> gas used: 321992
> gas price: 20 gwei
> value sent: 0 ETH
> total cost: 0.00643984 ETH
> Saving migration to chain.
> Saving artifacts
-------------------------------------
> Total cost: 0.00643984 ETH
Summary
=======
> Total deployments: 1
> Final cost: 0.00643984 ETH