How to write smart contracts in C# on macOS
This document will guide you through the process of setting up the C# development environment in macOS, creating contract projects with sample code, and compiling the smart contract.
Downloading Visual Studio for Mac
Download Visual Studio for Mac and follow the on-screen instructions to install the software.
Just check the basic functions when installing, without selecting the platform specific components like Android / iOS / macOS.
Creating a new project
-
Open Visual Studio for Mac, create a new project, and select .NET Standard Library.
-
Select .NET Standard 2.0 for the .NET Core version and follow the instructions to create the project with the name “test”.
-
Right click on
Dependencies
and chooseManage NuGet Package
. -
Search for
neo.smart
and chooseNeo.SmartContract.Framework
, then clickAdd Packge
. -
Write the following sample code in your Class1.cs file.
-
In the menu choose
Build
->Build All
to compile the corresponding code.A test.dll file is created in the folder
bin/Debug/netstandard2.0/
. Next we will use neon to compile it into a .avm smart contract file that can be deployed on the blockchain.
Downloading contract compiler
-
Download neo-devpack-dotnet from GitHub.
The master branch has been upgraded to Neo3 compiler, so you must switch the Git branch to
master-2.x
before downloading. -
Run Visual Studio for Mac and open the solution neo-devpack-dotnet.sln. Right-click Neo.Compiler.MSIL and then select
Publish
. -
Publish neon to the default path。After published, you can find neon.dll is generated under the path.
Compiling your smart contract to .avm
-
Download and install .NET Core .
-
Copy the new project file test.dll generated in previous steps into the directory where neon.dll locates in.
-
Run Terminal, and use
cd
to go to the directory where neon.dll locates in. Enterdotnet neon.dll test.dll
to compile the .dll file into .avm.The output should look like this
You should now find the .avm
file in the test output folder.