Interacting with smart contracts using truffle

Ubuntu 17.10, my heart hurts so much, I bought another one

It is strongly recommended to use xshell for preliminary testing work, it is easy to manage several terminals, as shown in the figure below, connect 5 terminals, 4 for experiments, and 1 for viewing related file parameters, which is much more convenient than the original virtual machine.

 

It is more appropriate to understand the relevant instructions first, and then watch: http://truffle.tryblockchain.org/

 

Install:

First complete the installation of the previous blog, and then perform the following operations: http://www.cnblogs.com/tianlongtc/p/8877579.html

#first terminal

sudo apt install npm #install npm

sudo npm install npm @ latest -g # 升级 npm

sudo npm install -gn #Install nodejs module n

sudo n stable #download nodejs stable version 

sudo npm install -g truffle #Install the truffle framework

Copy and paste one by one, okay, your environment is set up

 

Deploy the project:

Here I connect directly to the geth client, instead of choosing to connect to testrpc, although testrpc is much more convenient to use than geth

There are many blogs connected to the testrpc side, and you can Baidu by yourself if you need them.

#first terminal

cd /usr/local

mkdir mytest && cd mytest

truffle unbox metacoin #Generate default demo

 

open geth

Open two new terminals: (make sure geth has completed the relevant deployment): http://www.cnblogs.com/tianlongtc/p/8877579.html

#second terminal

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 <http://truffleframework.com/docs/advanced/configuration>
// 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/

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324858516&siteId=291194637