智能合约开发

上传人:ba****u6 文档编号:166196876 上传时间:2022-10-31 格式:DOCX 页数:16 大小:29.01KB
收藏 版权申诉 举报 下载
智能合约开发_第1页
第1页 / 共16页
智能合约开发_第2页
第2页 / 共16页
智能合约开发_第3页
第3页 / 共16页
资源描述:

《智能合约开发》由会员分享,可在线阅读,更多相关《智能合约开发(16页珍藏版)》请在装配图网上搜索。

1、智能合约开发语言:solidityIDE:mix-ide or whatever实例编写,Conference.sol :contract Conference address public organizer;mapping (address = uint) public registrantsPaid;uint public numRegistrants;uint public quota;event Deposit(address _from, uint _amount); / so you can log these eventsevent Refund(address _to, ui

2、nt _amount);function Conference() / Constructororganizer = msg.sender;quota = 500;numRegistrants = 0;function buyTicket() public returns (bool success) if (numRegistrants = quota) return false; registrantsPaidmsg.sender = msg.value;numRegistrants+;Deposit(msg.sender, msg.value);return true;function

3、changeQuota(uint newquota) public if (msg.sender != organizer) return; quota = newquota;function refundTicket(address recipient, uint amount) public if (msg.sender != organizer) return; if (registrantsPaidrecipient = amount) address myAddress = this;if (myAddress.balance = amount) recipient.send(amo

4、unt);registrantsPaidrecipient = 0;numRegistrants-;Refund(recipient, amount);function destroy() / so funds not locked in contract foreverif (msg.sender = organizer) suicide(organizer); / send funds to organizer二智能合约部署使用truffle部署智能合约的步骤:1. truffle init (在新目录中)= 创建truffle项目目录结构2. 编写合约代码,保存到 con tracts/

5、YourC on tractName.sol 文件。3. 把合约名字加到config/app.json的contracts部分。4. 启动以太坊节点(例如在另一个终端里面运行testrpc )。5- truffle deploy (在truffle项目目录中)添加一个智能合约。在truffle init执行后或是一个现有的项目目录中,复制粘帖上面的会议合约到 con tracts/C onferen ce.sol 文件中。然后打开truffle.js文件,把Conference加入deploy数组中。19 deploy:20 / Names of contracts that should b

6、e deployed to th亡 network.21 Conference22 ,启动 testrpco在另一个终端中启动testrpc。编译或部署。执行truffle compile看一下合约是否能成功编译,或者直接 truffle deploy 一步完成编译和部 署。这条命令会把部署好的合约的地址和ABI(应用接口)加入到配置文件中,这样之后的 truffle test和truffle build步骤可以使用这些信息。出错了?编译是否成功了?记住,错误信息即可能出现在testrpc终端也可能出现在truffle终 端。重启节点后记得重新部署!如果你停止了 testrpc节点,下一次使用

7、任何合约之前切记使用 truffle deploy 重新部署。 testrpc在每一次重启之后都会回到完全空白的状态。部署truffle deploy启动服务truffle serve启动服务后,可以在浏览器访问项目:http:/loca山ost:8080/三 智能合约测试用例编写/ 测试用例编写/把项目目录test/中的example.js文件重命名为conference.js,内容修改为如下,然后启动testrpc后,运行/ Truffle testcontract(Conference, function(accounts) / 1.初始化一个新的Conference,然后检查变量是否都

8、正确赋值it(Initial conference settings should match, function(done) var conference = Conference.at(Conference.deployed_address);/ same as previous example up to hereConference.new( from: accounts。 ).then(function(conference) conference.quota.call().then(function(quota) assert.equal(quota, 500, Quota doe

9、snt match!);).then( function() return conference.numRegistrants.call();).then( function(num) assert.equal(num, 0, Registrants should be zero!);return conference.organizer.call();).then( function(organizer) assert.equal(organizer, accounts。, Owner doesnt match!);done(); / to stop these tests earlier,

10、 move this up).catch(done);).catch(done););/ 2.测试合约函数调用 测试改变Quote变量的函数能否工作it(Should update quota, function(done) var c = Conference.at(Conference.deployed_address);Conference.new(from: accounts。 ).then(function(conference) conference.quota.call().then(function(quota) assert.equal(quota, 500, Quota d

11、oesnt match!);).then( function() return conference.changeQuota(300);).then( function(result) / result here is a transaction hashconsole.log(result); / if you were to print this out it d be long hex - the transaction hash return conference.quota.call().then( function(quota) assert.equal(quota, 300, N

12、ew quota is not correct!);done();).catch(done);).catch(done););/ 3.测试交易调用一个需要发起人发送资金的函数。a ticketvar c = Conference.at(Conference.deployed_address);Conference.new( from: accounts。 ).then(function(conference) var ticketPrice = web3.toWei(.O5, ether);var initialBalance = web3.eth.getBalance(conference.

13、address).toNumber();conference.buyTicket( from: accounts1, value: ticketPrice ).then(function() var newBalance = web3.eth.getBalance(conference.address).toNumber();var difference = newBalance - initialBalance;assert.equal(difference, ticketPrice, Difference should be what was sent); return conferenc

14、e.numRegistrants.call();).then(function(num) assert.equal(num, 1, there should be 1 registrant);return conference.registrantsPaid.call(accountsl);).then(function(amount) assert.equal(amount.toNumber(), ticketPrice, Senders paid but is not listed); done();).catch(done);).catch(done););/ 4.测试包含转账的合约最后

15、,为了完整性,确认一下refundTicket方法能正常工作,而且只有会议组 织者能调用it(Should issue a refund by owner only, function(done) var c = Conference.at(Conference.deployed_address);Conference.new( from: accounts0 ).then(function(conference) var ticketPrice = web3.toWei(.O5, ether);var initialBalance = web3.eth.getBalance(conferen

16、ce.address).toNumber();conference.buyTicket( from: accounts1, value: ticketPrice ).then(function() var newBalance = web3.eth.getBalance(conference.address).toNumber();var difference = newBalance - initialBalance;assert.equal(difference, ticketPrice, Difference should be what was sent); / same as bef

17、ore up to here/ Now try to issue refund as second user - should failreturn conference.refundTicket(accountsl, ticketPrice, from: accountsl);).then(function() var balance = web3.eth.getBalance(conference.address).toNumber();assert.equal(web3.toBigNumber(balance), ticketPrice, Balance should be unchan

18、ged);/ Now try to issue refund as organizer/owner - should workreturn conference.refundTicket(accountsl, ticketPrice, from: accounts。);).then(function() var postRefundBalance = web3.eth.getBalance(conference.address).toNumber(); assert.equal(postRefundBalance, initialBalance, Balance should be initi

19、al balance); done();).catch(done);).catch(done);););/ Solidity编译器提供了一个参数让你可以从命令行获取合约的Gas开销概要/ solc -gas Conference.sol/ 输出:= Conference =Gas estimation:construction:45438 + 271200 =316638external:registrantsPaid(address): 323organizer():282refundTicket(address,uint256):destroy(): 384changeQuota(uint

20、256):20370quota():333numRegistrants():355buyTicket():42023internal:四 为合约创建 DApp 界面1. 修改 app/javascripts/app.js window.onload = function。var accounts = web3.eth.accounts;var conference = Conference.at(Conference.deployed_address);$(#confAddress).html(Conference.deployed_address);var myConferencelnsta

21、nce;Conference.new(from: accounts。, gas: 3141592).then(function(conf) myConferencelnstance = conf; checkValues(););/ Check Valuesfunction checkValues() myConferenceInstance.quota.call().then(function(quota) $(input#confQuota).val(quota);return myConferenceInstance.organizer.call();).then(function(or

22、ganizer) $(input#confOrganizer).val(organizer);return myConferenceInstance.numRegistrants.call();).then(function(num) $(#numRegistrants).html(num.toNumber();return myConferenceInstance.organizer.call(););/ Change Quotafunction changeQuota(val) myConferenceInstance.changeQuota(val, from: accounts0).t

23、hen( function。return myConferenceInstance.quota.call();).then(function(quota) if (quota = val) var msgResult;msgResult = Change successful; else msgResult = Change failed;$(#changeQuotaResult).html(msgResult););/ buyTicketfunction buyTicket(buyerAddress, ticketPrice) myConferenceInstance.buyTicket(

24、from: buyerAddress, value: ticketPrice ).then( function() return myConferenceInstance.numRegistrants.call();).then(function(num) $(#numRegistrants).html(num.toNumber();return myConferencelnstance.registrantsPaid.call(buyerAddress);).then(function(valuePaid) var msgResult;if (valuePaid.toNumber() = t

25、icketPrice) msgResult = Purchase successful; else msgResult = Purchase failed;$(#buyTicketResult).html(msgResult);function refundTicket(buyerAddress, ticketPrice) var msgResult;myConferenceInstance.registrantsPaid.call(buyerAddress).then(function(result) if (result.toNumber() = 0) $(#refundTicketRes

26、ult).html(Buyer is not registered - no refund!); else myConferenceInstance.refundTicket(buyerAddress,ticketPrice, from: accountsO).then(function。return myConferenceInstance.numRegistrants.call();).then(function(num) $(#numRegistrants).html(num.toNumber(); return myConferencelnstance.registrantsPaid.

27、call(buyerAddress);).then(function(valuePaid) if (valuePaid.toNumber() = 0) msgResult = Refund successful; else msgResult = Refund failed;$(#refundTicketResult).html(msgResult););/ createWalletfunction createWallet(password) var msgResult;var secretSeed = lightwallet.keystore.generateRandomSeed();$(

28、#seed).html(secretSeed);lightwallet.keystore.deriveKeyFromPassword(password, function (err, pwDerivedKey) console.log(createWallet);var keystore = new lightwallet.keystore(secretSeed, pwDerivedKey);/ generate one new address/private key pairs/ the corresponding private keys are also encryptedkeystor

29、e.generateNewAddress(pwDerivedKey);var address = keystore.getAddresses()0;var privateKey = keystore.exportPrivateKey(address, pwDerivedKey);console.log(address);$(#wallet).html(Ox+address);$(#privateKey).html(privateKey);$(#balance).html(getBalance(address);/ Now set ks as transaction_signer in the

30、hooked web3 provider/ and you can start using web3 using the keys/addresses in ks!switchToHooked3(keystore););function getBalance(address) return web3.fromWei(web3.eth.getBalance(address).toNumber(), ether);/ switch to hooked3webprovider which allows for external Tx signing/ (rather than signing fro

31、m a wallet in the Ethereum client)function switchToHooked3(_keystore) console.log(switchToHooked3);var web3Provider = new HookedWeb3Provider(host: http:/localhost:8545, / check what using in truffle.jstransaction_signer: _keystore);web3.setProvider(web3Provider);function fundEth(newAddress, amt) con

32、sole.log(fundEth);var fromAddr = accounts。; / default owner address of clientvar toAddr = newAddress;var valueEth = amt;var value = parseFloat(valueEth)*1.0el8;var gasPrice = 1000000000000;var gas = 50000;web3.eth.sendTransaction(from: fromAddr, to: toAddr, value: value, function (err, txhash) if (e

33、rr) console.log(ERROR: + err)console.log(txhash: + txhash + ( + amt + in ETH sent);$(#balance).html(getBalance(toAddr););/ Wire up the UI elements$(#changeQuota).click(function() var val = $(#confQuota).val(); changeQuota(val););$(#buyTicket).click(function() var val = $(#ticketPrice).val();var buye

34、rAddress = $(#buyerAddress).val();buyTicket(buyerAddress, web3.toWei(val););$(#refundTicket).click(function() var val = $(#ticketPrice).val();var buyerAddress = $(#refBuyerAddress).val();refundTicket(buyerAddress, web3.toWei(val););$(#createWallet).click(function() var val = $(#password).val();if (!

35、val) $(”#password”).val(”PASSWORD NEEDED”).css(”color”, red);$(#password).click(function() $(”#password”).val(”).css(”color”, black);); else createWallet(val););var address = $(#wallet).html();fundEth(address, 1););$(#checkBalance).click(function() var address = $(#wallet).html();$(#balance).html(ge

36、tBalance(address););/ Set value of wallet to accounts1$(#buyerAddress).val(accounts1); $(#refBuyerAddress).val(accounts1);2.修改 index.htmlConference DAppbody font-family: Arial, sans-serif;.section margin: 20px;button padding: 5px 16px;border-radius: 4px;button#changeQuota background-color: yellow; b

37、utton#buyTicket background-color: #98fb9 8; button#refundTicket background-color: pink; button#createWallet background-color: #add8e6; #seed color: green;vscript type=text/javascript src=vscript src=./app.jsConference DAppContract deployed at: Organizer: vinput type=text id=confOrganizer /Quota: vin

38、put type=text id=confQuota /vbutton id=changeQuotaChangevspan id=changeQuotaResultvdiv class=sectionRegistrants: vspan id=numRegistrants0vdiv class=sectionBuy a Ticketv/h2Ticket Price: vinput type=text id=ticketPrice value=0.05 /Buyer Address: vinput type=text id=buyerAddress /vbutton id=buyTicketBu

39、y Ticketv/buttonvspan id=buyTicketResultv/spanv/divvhr/Refund a TicketTicket Price: Buyer Address: vinput type=text id=refBuyerAddress / vbutton id=refundTicketRefund Ticket vspan id=refundTicketResultCreate a WalletPassword: vinput type=text id=password /vbutton id=createWalletCreate Walletvbutton

40、id=fundWalletFund Wallet with 1 ETHv/buttonvbr/Your Wallet Secret Seed: vspan id=seedv/span vbr/vbr/Your New Wallet Address: vspan id=walletv/span vbr/vbr/Your New Wallet Private Key: vspan id=privateKeyv/span vbr/vbr/Wallet Balance: vspan id=balancev/span vbr/vbr/vbutton id=checkBalanceCheck Balanc

41、ev/button v/divv/bodyv/html3. 最终效果:应用 个人 购物 新闻 任务学习日常禅读 屬百度一下校招Conference DAppContract deployed at:0x34dc41 blbf4cfcb 9968e172MB23+c0ra 1 bdfbcaOrganizer: &xT1 c414917ba127eaebQuota: 100ChangeRegistrants: 0Buy a TicketTicket Price: a.05Buyer Address: 0Kra5s3tb929aa62eC1E 3uy TicketRefund a TicketTicket Price: 0.05B uyer Add re ss: 0xfB5s3tb 92 9aa62 eC1E Reftj n d TicketCreate a WalletPassword:Your Wallet Secret Seed:Ytour N ew Wai let Addre&s:Ybur N 已w Wai let Private Key:Wallet Balance:Ch&ck BalanceCreate Wallet Fun-d Wallet with 1 ETH

展开阅读全文
温馨提示:
1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
2: 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
3.本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
关于我们 - 网站声明 - 网站地图 - 资源地图 - 友情链接 - 网站客服 - 联系我们

copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!