十年網(wǎng)站開發(fā)經(jīng)驗 + 多家企業(yè)客戶 + 靠譜的建站團隊
量身定制 + 運營維護+專業(yè)推廣+無憂售后,網(wǎng)站問題一站解決
本篇文章為大家展示了怎么在.NET應(yīng)用中訪問以太坊智能合約Nethereum,內(nèi)容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。
創(chuàng)新互聯(lián)專注于秀洲網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗。 熱誠為您提供秀洲營銷型網(wǎng)站建設(shè),秀洲網(wǎng)站制作、秀洲網(wǎng)頁設(shè)計、秀洲網(wǎng)站官網(wǎng)定制、小程序開發(fā)服務(wù),打造秀洲網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供秀洲網(wǎng)站排名全網(wǎng)營銷落地服務(wù)。
Nethereum基本上是目前唯一可用的.NET平臺下的web3.js移植包。在這個教程中,我們將首先編寫并部署一個簡單的智能合約,然后創(chuàng)建一個簡單的.NET應(yīng)用,并使用Nethereum來訪問以太坊上的智能合約。Nethereum是通過以太坊節(jié)點旳標(biāo)準(zhǔn)RPC接口訪問智能合約,因此使用Nethereum可以對接所有的以太坊節(jié)點實現(xiàn),例如geth或parity。
首先安裝開發(fā)用以太坊節(jié)點軟件Ganache:
~$ npm install -g ganache-cli
然后安裝以太坊開發(fā)框架Truffle:
~$ npm install -g truffle
現(xiàn)在創(chuàng)建一個項目目錄,進入該目錄,并執(zhí)行truffle init
進行初始化:
~$ mkdir demo && cd hubwiz ~/hubwiz$ truffle init
truffle會創(chuàng)建一些新的文件夾:contract、test、migration等。在contract文件夾中,創(chuàng)建一個新的合約文件Vote.sol:
~/hubwiz/contracts$ touch Vote.sol
按如下內(nèi)容編輯Vote.sol,這個合約只是簡單地跟蹤兩個候選人的得票數(shù),它使用交易發(fā)起賬戶作為投票人,并且每個賬戶只能投一票:
pragma solidity ^0.4.16; contract Vote { uint public candidate1; uint public candidate2; mapping (address => bool) public voted; function castVote(uint candidate) public { require(!voted[msg.sender] && (candidate == 1 || candidate == 2)); if(candidate == 1){ candidate1++; }else{ candidate2++; } voted[msg.sender] = true; } }
接下來在migration文件夾創(chuàng)建一個新的js文件2_vote.js,內(nèi)容如下:
var vote = artifacts.require("Vote"); module.exports = function(deployer) { // deployment steps deployer.deploy(vote); };
然后打開項目文件夾下的truffle.js,用以下內(nèi)容替換:
module.exports = { networks: { ganache: { host: "127.0.0.1", port: 7545, network_id: "*" // Match any network id } } };
現(xiàn)在打開一個終端,啟動ganache:
~$ ganache-cli
然后打開另一個終端,用truffle部署合約:
~/hubwiz$ truffle deploy --reset --network ganache
你會看到終端輸出類似下面的合約地址,拷貝下來,后面還要用到:
Vote: 0xe4e47451aad6c89a6d9e4ad104a7b77ffe1d3b36
創(chuàng)建一個新的控制臺項目,添加對如下開發(fā)包的依賴:
Nethereum.Web3
Nethereum.Contracts
然后按如下內(nèi)容修改program.cs:
using System; using System.Numerics; using System.Threading.Tasks; using Nethereum.Contracts; using Nethereum.Hex.HexTypes; using Nethereum.Web3; namespace console { class Program { static void Main(string[] args) { //The URL endpoint for the blockchain network. string url = "HTTP://localhost:7545"; //The contract address:合約部署的地址 string address = "0x345cA3e014Aaf5dcA488057592ee47305D9B3e10"; //The ABI for the contract. string ABI = @"[{'constant':true,'inputs':[],'name':'candidate1','outputs':[{'name':'','type':'uint256'}],'payable':false,'stateMutability':'view','type':'function'},{'constant':false,'inputs':[{'name':'candidate','type':'uint256'}],'name':'castVote','outputs':[],'payable':false,'stateMutability':'nonpayable','type':'function'},{'constant':true,'inputs':[],'name':'candidate2','outputs':[{'name':'','type':'uint256'}],'payable':false,'stateMutability':'view','type':'function'},{'constant':true,'inputs':[{'name':'','type':'address'}],'name':'voted','outputs':[{'name':'','type':'bool'}],'payable':false,'stateMutability':'view','type':'function'}]"; //Creates the connecto to the network and gets an instance of the contract. Web3 web3 = new Web3(url); Contract voteContract = web3.Eth.GetContract(ABI, address); //Reads the vote count for Candidate 1 and Candidate 2 Taskcandidate1Function = voteContract.GetFunction("candidate1").CallAsync (); candidate1Function.Wait(); int candidate1 = (int)candidate1Function.Result; Task candidate2Function = voteContract.GetFunction("candidate2").CallAsync (); candidate2Function.Wait(); int candidate2 = (int)candidate2Function.Result; Console.WriteLine("Candidate 1 votes: {0}", candidate1); Console.WriteLine("Candidate 2 votes: {0}", candidate2); //Prompts for the account address. Console.Write("Enter the address of your account: "); string accountAddress = Console.ReadLine(); //Prompts for the users vote. int vote = 0; Console.Write("Press 1 to vote for candidate 1, Press 2 to vote for candidate 2: "); Int32.TryParse(Convert.ToChar(Console.Read()).ToString(), out vote); Console.WriteLine("You pressed {0}", vote); //Executes the vote on the contract. try{ HexBigInteger gas = new HexBigInteger(new BigInteger(400000)); HexBigInteger value = new HexBigInteger(new BigInteger(0)); Task castVoteFunction = voteContract.GetFunction("castVote").SendTransactionAsync(accountAddress, gas, value, vote); castVoteFunction.Wait(); Console.WriteLine("Vote Cast!"); }catch(Exception e){ Console.WriteLine("Error: {0}", e.Message); } } } }
別忘了用你自己部署的合約地址修改上面代碼中的合約地址?,F(xiàn)在運行應(yīng)用,就可以投票了!
用Nethereum很容易就可以為.Net應(yīng)用添加訪問以太坊智能合約的能力,由于Nethereum基于.NET平臺,因此它可以用于.NET Core應(yīng)用、.NET Standard應(yīng)用、Xamarin以及各種windows應(yīng)用中。
上述內(nèi)容就是怎么在.NET應(yīng)用中訪問以太坊智能合約Nethereum,你們學(xué)到知識或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識儲備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。