Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to access Ethernet Square Intelligent contract Nethereum in .NET Application

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article shows you how to access the ethernet smart contract Nethereum in. Net applications, the content is concise and easy to understand, it will definitely make your eyes shine. I hope you can get something through the detailed introduction of this article.

Nethereum is basically the only web3.js migration package currently available on the .NET platform. In this tutorial, we will first write and deploy a simple smart contract, then create a simple .NET application and use Nethereum to access the smart contract on Ethernet Square. Nethereum accesses intelligent contracts through the standard RPC interface of Ethernet Square nodes, so you can use Nethereum to dock all Ethernet Square nodes, such as geth or parity.

Intelligent contract development and deployment

First of all, install and develop the Taifang node software Ganache:

~ $npm install-g ganache-cli

Then install the ethernet development framework Truffle:

~ $npm install-g truffle

Now create a project directory, enter it, and execute truffle init to initialize it:

~ $mkdir demo & & cd hubwiz~/hubwiz$ truffle init

Truffle creates some new folders: contract, test, migration, and so on. In the contract folder, create a new contract file, Vote.sol:

~ / hubwiz/contracts$ touch Vote.sol

Edit Vote.sol as follows, this contract simply tracks the number of votes obtained by two candidates, it uses the transaction initiation account as the voter, and each account can only cast one vote:

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;}}

Next, create a new js file, 2_vote.js, in the migration folder, as follows:

Var vote = artifacts.require ("Vote"); module.exports = function (deployer) {/ / deployment steps deployer.deploy (vote);}

Then open truffle.js under the project folder and replace it with the following:

Module.exports = {networks: {host: "127.0.0.1", port: 7545, network_id: "*" / / Match any network id}

Now open a terminal and start ganache:

~ $ganache-cli

Then open another terminal and deploy the contract with truffle:

~ / hubwiz$ truffle deploy-reset-network ganache

You will see the terminal output similar to the following contract address, copy it, and use it later:

Vote: 0xe4e47451aad6c89a6d9e4ad104a7b77ffe1d3b36.Net Application Development and Intelligent contract access

Create a new console project and add dependencies on the following development packages:

Nethereum.Web3

Nethereum.Contracts

Then modify the program.cs as follows:

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: address of contract deployment 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 Task candidate1Function = 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);}

Don't forget to modify the contract address in the above code with the contract address you deployed. Now run the app and you can vote!

With Nethereum, you can easily add the ability to access Ethernet Square smart contracts for .net applications, and because Nethereum is based on the .NET platform, it can be used in .NET Core applications, .NET Standard applications, Xamarin, and various windows applications.

The above is how to access the ethernet smart contract Nethereum in the. Net application. Have you learned the knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report