以太坊智能合約開發(fā)DApp的方法是什么

本篇內(nèi)容主要講解“以太坊智能合約開發(fā)DApp的方法是什么”,感興趣的朋友不妨來(lái)看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來(lái)帶大家學(xué)習(xí)“以太坊智能合約開發(fā)DApp的方法是什么”吧!

龍泉網(wǎng)站建設(shè)公司成都創(chuàng)新互聯(lián),龍泉網(wǎng)站設(shè)計(jì)制作,有大型網(wǎng)站制作公司豐富經(jīng)驗(yàn)。已為龍泉千余家提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\成都外貿(mào)網(wǎng)站建設(shè)公司要多少錢,請(qǐng)找那個(gè)售后服務(wù)好的龍泉做網(wǎng)站的公司定做!

1. 設(shè)置開發(fā)環(huán)境

我們使用一個(gè)模擬的內(nèi)存區(qū)塊鏈(ganache)代替真實(shí)的區(qū)塊鏈在進(jìn)行開發(fā)。在本教程的2章,我們將與真實(shí)的區(qū)塊鏈交互。下面是安裝ganache、web3js的步驟,然后在linux上啟動(dòng)一個(gè)測(cè)試鏈。在macOS上安裝過(guò)程也是一樣的。

以太坊智能合約開發(fā)DApp的方法是什么

你可以看到ganache-cli自動(dòng)創(chuàng)建了10個(gè)測(cè)試賬號(hào),每個(gè)賬號(hào)預(yù)分配了100(虛構(gòu)的)ethers

如果需要更詳細(xì)的開發(fā)環(huán)境安裝教程,可以參考如下文章:

  • windows以太坊開發(fā)環(huán)境搭建

  • linux/ubuntu以太坊開發(fā)環(huán)境搭建

2.簡(jiǎn)單的投票合約

我們將使用solidity編程語(yǔ)言來(lái)編寫我們的合約。如果您熟悉面向?qū)ο缶幊?,學(xué)習(xí)編寫solidity合約應(yīng)該是輕而易舉的事。我們將編寫一個(gè)合約對(duì)象,含有一個(gè)構(gòu)造函數(shù)初始化候選人數(shù)組。合約對(duì)象有2個(gè)方法:

  1. 返回候選人獲得的總票數(shù)

  2. 增加候選人的投票數(shù)。

注意:構(gòu)造函數(shù)只被調(diào)用一次,當(dāng)您部署合約到區(qū)塊鏈。不像在網(wǎng)絡(luò)世界里的每一個(gè)部署你的代碼覆蓋舊的代碼,部署后的代碼在區(qū)塊鏈上是不變的。例如,如果你更新你的合約并且再次部署,舊合約仍然會(huì)在區(qū)塊鏈上, 它所存儲(chǔ)的數(shù)據(jù)不受影響,新的部署將創(chuàng)建一個(gè)新實(shí)例的合約。

下面是投票合約的代碼:

pragma solidity ^0.4.18;  // We have to specify what version of compiler this code will compile with   
    contract Voting {  
      /* mapping field below is equivalent to an associative array or hash. The key of the mapping is candidate name stored as type bytes32 and value is an unsigned integer to store the vote count */  
        
      mapping (bytes32 => uint8) public votesReceived;  
        
      /* Solidity doesn't let you pass in an array of strings in the constructor (yet). We will use an array of bytes32 instead to store the list of candidates */  
        
      bytes32[] public candidateList;  
      
      /* This is the constructor which will be called once when you deploy the contract to the blockchain. When we deploy the contract, we will pass an array of candidates who will be contesting in the election */  
      function Voting(bytes32[] candidateNames) public {  
        candidateList = candidateNames;  
      }  
      
      // This function returns the total votes a candidate has received so far   function totalVotesFor(bytes32 candidate) view public returns (uint8) {  require(validCandidate(candidate));  return votesReceived[candidate];  
      }  
      
      // This function increments the vote count for the specified candidate. This   // is equivalent to casting a vote   function voteForCandidate(bytes32 candidate) public {  require(validCandidate(candidate));  
        votesReceived[candidate] += 1;  
      }  
      
      function validCandidate(bytes32 candidate) view public returns (bool) {  for(uint i = 0; i < candidateList.length; i++) {  
          if (candidateList[i] == candidate) {  return true;  
          }  
        }  return false;  
      }  
    }

復(fù)制上面的代碼,在hello_world_voting目錄下創(chuàng)建一個(gè)Voting.sol文件?,F(xiàn)在讓我們來(lái)編譯代碼并將其部署到ganache的區(qū)塊鏈上.

為了編譯solidity代碼,我們需要安裝名字為solc的npm模塊

~/hello_world_voting$ npm install solc

我們將在node控制臺(tái)中使用這個(gè)庫(kù)來(lái)編譯我們的合約。在上一篇文章中我們提到,web3js是一個(gè)讓我們可以通過(guò)rpc訪問區(qū)塊鏈的庫(kù)。我們將使用該庫(kù)來(lái)部署我們的應(yīng)用程序并與之交互。

首先,在命令行中斷運(yùn)行node命令進(jìn)入node控制臺(tái),初始化solc和文本對(duì)象。下面的所有代碼片段都需要在node控制臺(tái)中鍵入

~/hello_world_voting$ node  
> Web3 = require('web3')  
> web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));

為了確保web3對(duì)象已經(jīng)初始化、區(qū)塊鏈能夠訪問,讓我們?cè)囈幌虏樵儏^(qū)塊鏈上的所有賬戶。您應(yīng)該看到如下的結(jié)果:

> web3.eth.accounts  
    ['0x9c02f5c68e02390a3ab81f63341edc1ba5dbb39e',  '0x7d920be073e92a590dc47e4ccea2f28db3f218cc',  '0xf8a9c7c65c4d1c0c21b06c06ee5da80bd8f074a9',  '0x9d8ee8c3d4f8b1e08803da274bdaff80c2204fc6',  '0x26bb5d139aa7bdb1380af0e1e8f98147ef4c406a',  '0x622e557aad13c36459fac83240f25ae91882127c',  '0xbf8b1630d5640e272f33653e83092ce33d302fd2',  '0xe37a3157cb3081ea7a96ba9f9e942c72cf7ad87b',  '0x175dae81345f36775db285d368f0b1d49f61b2f8',  '0xc26bda5f3370bdd46e7c84bdb909aead4d8f35f3']

從voting.sol加載代碼,保存在一個(gè)字符串變量中,然后開始編譯

> code = fs.readFileSync('Voting.sol').toString()  
> solc = require('solc')  
> compiledCode = solc.compile(code)

當(dāng)你的代碼編譯成功并打印了合約對(duì)象的內(nèi)容(在node控制臺(tái)中輸出的內(nèi)容),有2個(gè)字段很重要,需要理解它們:

  • compiledCode.contracts[‘:Voting’].bytecode: Voting.sol源代碼編譯后得到的字節(jié)碼。這是將被部署到blockchain的代碼。

  • compiledCode.contracts[‘:Voting’].interface: 合約接口或模板(稱為ABI)告訴用戶合約含有哪些方法。您需要這些ABI的定義,因?yàn)閷?lái)你總是需要與合約交互的。

到此,相信大家對(duì)“以太坊智能合約開發(fā)DApp的方法是什么”有了更深的了解,不妨來(lái)實(shí)際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

文章題目:以太坊智能合約開發(fā)DApp的方法是什么
文章源于:http://bm7419.com/article48/pcgcep.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供域名注冊(cè)、網(wǎng)站策劃品牌網(wǎng)站制作、服務(wù)器托管、網(wǎng)站改版、外貿(mào)建站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都網(wǎng)頁(yè)設(shè)計(jì)公司