Day1 情况搭建&摆设样例合同

访客2023-12-02 13:54:4235

先reference呵呵薄罗藓的blog

Truffle 4.0、Geth 1.7.2、TestRPC在专有链上修建智能合约​mshk.top/2017/11/truffle-4-0-geth-1-7-2-testrpc/

*总实的有了阿谁reference没必要看我的啦

1. 天然情况如是说Ubuntu 16.04 LTSTruffle v4.1.11Solidity v0.4.24TestRPC v6.0.3

VScode做为我拉沙泰格赖厄县合做开发的GUI,先加拆应用法式solidity

1.1 加拆(预览)npm

阿谁吗贼关键!就算版不如新前面非要拆不上。那儿参照了[OliverYoung](人类文明加密 - SegmentFault)的blog。但在此以后他们先看一看node.js的版

$ node -v v10.4.1

我加拆的是v10.4.1,无论如何他们先晋级换代呵呵

$ npm install -g n $ n stable

n stable那句可能会要很久,去喝个水打个盹:)

查阅npm的版能那么干

$ npm view npm version #查阅新一代版 6.1.0 $ sudo npm -g install npm@latest

晋级换代完node.js吗如坐针毡,以后的error全被tells。提过给npm加进设置装备摆设文档

$ code ~/.zshrc #关上zshrc,若接纳其它的shell提过取代掉.zshrc

在.zshrc最末把npm的产物目次从头参加$PATH

export PATH="$PATH:/home/usr/.npm-global/bin" #取代掉usr 1.2 加拆Truffle和TestRPC $ sudo npm update truffle -g && sudo npm install -g truffle $ sudo npm update ethereumjs-testrpc -g && sudo npm install -g ethereumjs-testrpc

理论上是不克不及error了...我以后testrpc狂热error原因在于没预览node.js

接着就能试一试看版,校正加拆获得胜利

$ truffle version Truffle v4.1.11 (core: 4.1.11) Solidity v0.4.24 (solc-js) $ testrpc version EthereumJS TestRPC v6.0.3 (ganache-core: 2.0.2) #有那句就算,前面有奇异的输入没必要管 2. Truffle智能合约合做开发 2.1 挪用Truffle工程项目

增建两个空设置装备摆设文档,先万万别放合约文档进来。他们增建两个工程项目

$ mkdir Test #提议设置装备摆设文档名第两个字母小写 $ cd Test $ truffle init Downloading... Unpacking... Setting up... Unbox successful. Sweet! Commands: Compile: truffle compile Migrate: truffle migrate Test contracts: truffle test

想晓得每一设置装备摆设文档是干嘛的...再看呵呵

Truffle 4.0、Geth 1.7.2、TestRPC在专有链上修建智能合约​mshk.top/2017/11/truffle-4-0-geth-1-7-2-testrpc/

关于版问题带来的差别,我列呵呵我的truffle init的成果

$ tree Test #没tree能apt-get呵呵 Test ├── contracts │ └── Migrations.sol ├── migrations │ └── 1_initial_migration.js ├── test ├── truffle-config.js └── truffle.js 3 directories, 4 files

现实上若是你加拆了新一代的Truffle,他们还需要在migrations里增建2_deploy_contracts.js文档。Solidity和Truffle都开展的太快了...

truffle-config.js和truffle.js的区别我还没发现,哪个是默认的说什么的都有,我就两个文档内容一模一样复造一边啦

2.2 创建智能合约

在contracts产物目次里增建合约Conference.sol。留意小写第两个字母!我也不晓得为啥,归正小写给我报错了emmmmmm

阿谁例程是开会买票的,变量名都起得很明晰。当然因为版预览换代问题有做一些修改。你能想象我去查关键词查到的都是几小时前的答复吗!还能查到提议solidity官方修改关键词的P)

*新常识:

address就理解成你的身份象征mapping (address => uint) public registrantsPaidmapping 就是字面意思映射, registrantsPaid[address]就是括号里进来两个address, 出来两个uint。event是在链上写两个事务,写哪些信息是你本身在参数里定的。构造函数第一次挪用的msg就是卖票方!就是大麦永乐什么的那种...以后挪用阿谁合约的就有可能是广阔的抢票粉丝了,看详细函数的功用。payable是涉及到msg.value会用到的润色符,意思是阿谁你筹办好要付钱了!selfdestruct是说合约到期了,完毕了,所有暂存在合约里的钱都要转给万恶的卖票方了,否则里面的以太坊就困在合约里拿不出来了。pragma solidity ^0.4.23; contract Conference { address public organizer; mapping (address => uint) public registrantsPaid; uint public numRegistrants; uint public quota; event Deposit(address _from, uint _amount); event Refund(address _to, uint _amount); constructor() public { organizer = msg.sender; quota = 500; numRegistrants = 0; } function buyTicket() payable public returns (bool success) { if (numRegistrants >= quota) return false; registrantsPaid[msg.sender] = msg.value; ++numRegistrants; address myAddress = this; myAddress.transfer(msg.value); emit Deposit(msg.sender, msg.value); return true; } function changeQuota(uint newquota) public { if (msg.sender != organizer) return; quota = newquota; } function refundTicket(address recipient, uint amount) public { if (msg.sender != organizer) return; if (registrantsPaid[recipient] == amount) { address myAddress = this; if (myAddress.balance >= amount) { recipient.transfer(amount); registrantsPaid[recipient] = 0; --numRegistrants; emit Refund(recipient, amount); } } } function destroy() public { if (msg.sender == organizer) selfdestruct(organizer); } }

良多语法都预览得很快,好比payable,view和selfdestruct。我仍是很思念selfdestruct本来的名字suicide的,究竟结果阿谁名字完美诠释了删库圈钱跑路一套操做。你看官方都给你摆设好了嘿嘿嘿。

2.3 设置装备摆设文档

刚起头玩JS,我如今大要就理解成makefile那种...

migrations/1_initial_migration.js文档

var Migrations = artifacts.require("./Migrations.sol"); module.exports = function(deployer) { deployer.deploy(Migrations); };

migrations/2_deploy_contracts.js文档

var Conference = artifacts.require("./Conference.sol"); module.exports = function(deployer) { deployer.deploy(Conference); };

truffle.js和truffle-config.js我都写了

module.exports = { networks: { development: { //合做开发者形式 host: "localhost", //当地跑,不花吗以太币 port: 8545, //阿谁端口可能会被占用,前面有讲 network_id: "*" //肆意婚配 } } }; 2.4 编译合约

上zsh代码。详细问题仍是看reference啊哈哈哈哈哈哈

$ truffle compile --compile-all $ truffle migrate --network development #也能布署到吗收集,看reference

开两个新的Terminal

$ testrpc --gasLimit 0x800000000 EthereumJS TestRPC v6.0.3 (ganache-core: 2.0.2) Available Accounts ================== (0) 0xbb8fda55366e680d53077fdd73cd2460ad98ff3b (1) 0xc4544ca875a017378b42e2bec114170eff99d432 (2) 0x8faf985b2f66a0974c7e8fbfd8b78cba7d071385 (3) 0xf73cec96ccb5d09984332a4d78f9969e761757c7 (4) 0x3ae0ce12da96677d0073d912ebf743bf00f82a4f (5) 0x727ef8d97299cfb2c31edb6d0a2e3570b3c8b2b7 (6) 0x326aaf6d499b0a680f89983097f88df482a1bffe (7) 0x3adebc3b47530ead70db05403df0ba79b6a2481d (8) 0x42d15725ce171d1900d8adfa9e1beda5d21fd8e9 (9) 0xb0545ae94c52118fb1cdc164ca5adaa208137d45 Private Keys ================== (0) 6ce6cfd89287c0c70b5e3b3596d48726dbe9c4f057b8323a7017198b58819147 (1) ea56b8a84b5bc131d38ca998a199af63c4dbf8e8044bf042106fd40a38f6894e (2) ce43139a9714528bfbadd02464ba607a86f1e25a1780f81de8858545b9a40cd9 (3) 946138b63168909e9f05482b91bfd1afc4bbfaa739bdf57a4dc7d9266d56654a (4) 0aa3ec196d29f862686b16724fe269943b228b5a0a2879a9baf980ea988bf894 (5) 45e21f4a1e2c70aba52168246f806485db2b0de2c9ace86ee2ae3a4787502b83 (6) 27e280162f5914eca6d1c99a2000d9a2391f0b471e3a9647e91dfcf2336190f3 (7) 73dc5a4d685cd535940dcd47e5f8a08430b0ba66421411d930d653d11f3b6aba (8) c3f97f0bd569e4ecb30eafdd091a67426591bdde25a8927f084953b756dcc6b2 (9) 7152b29695fa17a34b1ac29471b14ea84e52f7d653611a28d3e555252b7bf2eb HD Wallet ================== Mnemonic: kidney oak gospel midnight strong weasel tip smoke picnic render urge pluck Base HD Path: m/44/60/0/0/{account_index} Gas Limit ================== 34359738368 Listening on localhost:8545

能看到10个账号列表和10个账号密码。若是呈现

Error: listen EADDRINUSE :::8545

申明有软件在占用8545,试着关掉一些应用。或者Log out再Log in呵呵。

接着再在本来的Terminal里布署一次合约看一看

$ truffle migrate --reset #有时候问题就会出在已经编译过的处所,仍是reset呵呵吧

Testrpc应该会有变革

net_version eth_accounts

恬逸!

接着关上truffle的控造台

$ truffle console truffle(development)> var contract; undefined truffle(development)> Conference.deployed().then(function(instance){contract= instance;}); undefined

他们挪用一次changeQuota(uint newquota)

truffle(development)> contract.changeQuota(100);

输入一堆看不懂,但是看TestRPC的末端,已经烧掉了好多gas

Transaction: 0xe607698009eff2ddaf27c231d2f9126c029346c0189148df9200aec0af218fd2 Contract created: 0x13a1ea20c634146abaed2395579b2e8b23a9cca1 Gas usage: 277462 Block Number: 1 Block Time: Wed Jun 20 2018 19:52:28 GMT+0800 (China Standard Time)

QAQ固然万万笔钱但觉得本身仍是很败家...

那今天就到那儿啦!

控制面板

您好,欢迎到访网站!
  查看权限

最新留言