系统 ubuntu 17.10 , 心好痛, 又买了一个
强烈建议使用 xshell 进行前期的测试工作, 很容易管理几个终端, 如下图, 连开 5 个终端, 4 个进行实验, 1 个查看相关文件参数, 比原来在虚拟机方便好多
先了解相关指令, 再观看比较合适: http://truffle.tryblockchain.org/
安装:
先完成上一条博客的安装, 再来进行下面的操作: http://www.cnblogs.com/tianlongtc/p/8877579.html
- # 第一个终端
- sudo apt install npm #安装 npm
- sudo npm install npm@latest -g #升级 npm
- sudo npm install -g n #安装 nodejs 的模块 n
- sudo n stable #下载 nodejs 稳定版
- sudo npm install -g truffle #安装 truffle 框架
一条一条复制粘贴, 好, 你的环境搭好了
部署项目:
这里我直接连接到 geth 客户端, 而不是选择连接到 testrpc, 尽管 testrpc 使用比 geth 方便多了
连接到 testrpc 端的博客有许多, 需要的可以自行百度.
- # 第一个终端
- cd /usr/local
- mkdir mytest && cd mytest
truffle unbox metacoin #生成默认 demo
开启 geth
新开两个终端:(确保 geth 已经完成相关部署): http://www.cnblogs.com/tianlongtc/p/8877579.html
# 第二个终端
geth --networkid 15 --datadir data --rpc --rpcaddr 108.61.218.173 --rpcport 8545 --nodiscover --port 80 --unlock 0 --ipcpath "/root/.ethereum/geth.ipc"
# 第三个终端
geth attach #连接到 geth
personal.unlockAccount(eth.accounts[0]) #解锁账户 1
指定网络
- # 第一个终端
- cd /usr/local/mytest
vim truffle.js #下面这段拷进去, 注意修改 live 中的地址
- module.exports = {
- // See
- // to customize your Truffle configuration!
- networks: {
- development: {
- host: "127.0.0.1",
- port: 8588,
- network_id: "*"
- },
- live: {
- host:"108.61.218.173",
- port: 8545,
- network_id: 15,
- gas: 4612388,
- }
- }
- }
新建合约
- # 第一个终端
- cd /usr/local/mytest/contracts
vim Adoption.sol #把下面这段拷进去, 对齐什么的别理啦, 拷过来就这样了, 不管了
- pragma solidity ^0.4.4;
- contract Adoption {
- address[16] public adopters; // 存储地址
- // 采用一个宠物
- function adopt(uint petId) public returns (uint) {
- require(petId>= 0 && petId <= 15);
- adopters[petId] = msg.sender;
- return petId;
- }
- // 返回采用者
- function getAdopters() public returns (address[16]) {
- return adopters;
- }
- }
部署合约
- # 第一个终端
- cd /usr/local/mytest/migrations
vim 2_deploy_contracts.js #下面这段拷进去
- var Adoption = artifacts.require("./Adoption.sol");
- module.exports = function(deployer) {
- deployer.deploy(Adoption);
- };
调用合约
# 第一个终端, 下面很多操作需要到终端三进行挖矿操作, 确认交易, 由于我提前部署, 忘记那些了, 遇到指令卡住不执行, 先试试到终端三执行, miner.start(), 也可以在合约三执行 txpool.status 查看是否有未确认的交易
truffle console --network live #连接到 geth
- compile #编译
- migrate #移植
- deploy #部署
- Adoption.deployed().then(instance => contract = instance) #实例化 Adoption
- contract.adopt(6) #执行合约函数, 领养宠物
- contract.getAdopters() #查看宠物主人
参考博客:
- http://truffleframework.com/docs/getting_started/javascript-tests
- https://blog.csdn.net/diandianxiyu_geek/article/details/78361621
- http://truffle.tryblockchain.org/
来源: http://www.bubuko.com/infodetail-2568169.html