This article is a mirror article of machine translation, please click here to jump to the original article.

View: 16201|Reply: 5

Developing Ethereum smart contracts through Nethereum and .NET

[Copy link]
Posted on 8/6/2018 1:55:51 PM | | |
. .NET is an ancient framework loved by both indie and enterprise developers. The ability to choose from a variety of languages and deploy to a wide variety of platforms, including a wide range of capabilities from mobile to server, makes . .NET becomes a great choice for a variety of applications. Although. .NET does a lot, but it can also be said that it does nothing. For example, you can't use . .NET (unless using Silverlight...... ), nor does it exist. .NET supported languages, which enables developers to write smart contracts.

The development language of smart contracts is solidity. Solidity is a purpose-specific language that assumes a lot of things about the environment in which it operates, and it is very closely integrated with blockchain technology. It is these hypothetical features that exclude other languages.

The smart contract itself is only half the story. For applications, smart contracts need to call them via RPC. This is where Web3 clients come in. A Web3 client is simply a client application that enables the client application to interface with smart contracts running on Ethereum, so as long as a given language exists, then it can interface with Ethereum.

For . The most important thing for .NET is a project called Nethereum. (Nethereum is a portal for .NET and Ethereum at the address:https://nethereum.com/)。 This library attempts to replicate the same functionality provided by Web3.js, similar to JavaScript applications running in browsers and NodeJS.

In this tutorial, we'll deploy a smart contract to Ganache and then use . NET core creates a simple . .NET applications to interface with smart contracts.

Create and publish a smart contract

1. Download and install Ganache, a very useful private blockchain for Ethereum development.

The hyperlink login is visible.

2. In a terminal, command prompt, or Powershell session, install Truffle. Truffle is a framework and set of utility tools that help facilitate solidity development of smart contracts. Use NPM to complete the command below.



3. Create a folder and run truffle init under this folder



4. Truffle will create some new folders contract, test, and migration. Create a new file Vote.sol in the contracts folder.

5. Paste the following code into the newly created Vote.sol and save the file. This smart contract only records the number of votes cast by 2 candidates. The contract uses the message sender (i.e., the account address) as the voter. It only allows 1 vote per account.



6. Create a new file called 2_vote.js in the migrations folder.

7. Copy the following code into the 2_vote.js and save it.


8. Open truffle.js in the root directory of the folder you created and paste the code below, then save the file. This configuration of Truffle uses Ganache.



9. Launch Ganache in the Start menu.

10. Deploy smart contracts with Truffle. The ganache network is defined in truffle.js file.


11. Pay attention to the output. You'll see an output similar to the one below. This is the address of the voting contract. Copy and paste the hex string for later use.


Create a . .NET applications call smart contracts

1. In a new directory, create a new console application using the dotnet command.


2. Install the Nethereum package.


3. Edit Program.cs file. Replace the default code with the code below. This will allow the console to prompt account addresses and user votes. Save the file.


4. On program.cs, paste the address we just recorded to the address of the deployment contact and save the file.


5. Compile the application.


6. Run the app.


7. You'll see the app prompt you to type. Enter the address and vote. You can copy the account address from the Ganache GUI under the Accounts tab.


8. Run the app again and you will see that the vote has increased.


For Nethereum, it's easy to integrate smart contracts with any . .NET applications. Because Nethereum is based on .NET, it can be used for .NET core applications, . .NET standard, Xamarin, and various Windows applications. Using Nethereum, the mighty Ethereum and . NET is now at your disposal!


Posted on 10/13/2020 5:17:23 PM |
Boss, you wrote very well, I have some questions. 1. How is your ABI file generated? After I copied the contract code, the abi files generated by compiling with VSCode's F5 are all double-quoted, and then embedding them in the code will be problematic, and yours is single-quoted. 2. My code copy Web3 web3 = new Web3 (url); This line of code gives an error, saying "Web3 is a namespace, but it is used as a type", and the modification suggestion is to change it to IWeb3. Very urgent! I hope God can help answer. I am eager to contact QQ:1206814310 (only consulting and usually do not disturb) is really urgent! Thank you very much!
Posted on 1/6/2022 12:45:19 PM |
Learn to develop Ethereum smart contracts with Nethereum and .NET
Posted on 1/17/2022 2:01:56 PM |
Good thing, like one, learn to learn
Posted on 3/19/2022 4:37:53 PM |
Good thing, like one, learn to learn
Posted on 7/2/2022 11:40:47 AM |
Like one, learn to learn
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com