-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #107 from neo-project/dev
Merge dev updates to master
- Loading branch information
Showing
67 changed files
with
1,068 additions
and
199 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -78,9 +78,9 @@ using System.ComponentModel; | |
namespace demo | ||
{ | ||
[DisplayName("Oracle Demo")] | ||
[ManifestExtra("Author", "Neo")] | ||
[ManifestExtra("Email", "[email protected]")] | ||
[ManifestExtra("Description", "This is a Oracle using template")] | ||
[ContractAuthor("core-dev", "[email protected]")] | ||
[ContractEmail("[email protected]")] | ||
[ContractDescription("This is a Oracle contract example")] | ||
public class OracleDemo : SmartContract | ||
{ | ||
static readonly string PreData = "RequstData"; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
{ | ||
"label": "Advances", | ||
"position": 5 | ||
"position": 4 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
{ | ||
"label": "Development Guide", | ||
"position": 6 | ||
"position": 5 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,18 +5,19 @@ In this tutorial, you will learn the basics of developing a smart contract. | |
Let's have a look at our basic hello world contract. | ||
|
||
```cs | ||
using Neo; | ||
using Neo.SmartContract; | ||
using Neo.SmartContract.Framework; | ||
using Neo.SmartContract.Framework.Attributes; | ||
using Neo.SmartContract.Framework.Native; | ||
using Neo.SmartContract.Framework.Services; | ||
using System; | ||
using System.ComponentModel; | ||
|
||
namespace Helloworld | ||
{ | ||
[ManifestExtra("Author", "Neo")] | ||
[ManifestExtra("Email", "[email protected]")] | ||
[ManifestExtra("Description", "This is a contract example")] | ||
[DisplayName("MyContract")] | ||
[ContractAuthor("core-dev", "[email protected]")] | ||
[ContractVersion("0.0.1")] | ||
[ContractDescription("This is a contract example")] | ||
public class Contract1 : SmartContract | ||
{ | ||
//TODO: Replace it with your own address. | ||
|
@@ -80,6 +81,14 @@ In addition, developer can define static method in contract and return a consta | |
public static string Name() => "name of the token"; | ||
``` | ||
|
||
You can also use the get only property to accomplish the same thing. | ||
|
||
```cs | ||
public string Name { [Safe] get => "name of the token"; } | ||
``` | ||
|
||
`[Safe]` represents that this method does not modify the contract data and is safe to access. | ||
|
||
## Storage property | ||
|
||
When you develop the smart contract, you have to store your application data on the blockchain. When a Smart Contract is created or when a transaction awakens it, the Contract’s code can read and write to its storage space. All data stored in the storage of the smart contract are automatically persisted between invocations of the smart contract. Full nodes in the blockchain store the state of every smart contract on the chain. | ||
|
@@ -134,9 +143,8 @@ The basic types of C# are: | |
After analyzing the basic hello world contract, let us move to your first real-world smart contract. Here we provide a very simple DNS system which was written in C#. The main function of the DNS is store the domain for users. It contains all the points above except the events. We can investigate this smart contract to learn how to make a basic smart contract. The source code is here: | ||
|
||
```cs | ||
using Neo.SmartContract; | ||
using Neo.SmartContract.Framework; | ||
using Neo.SmartContract.Framework.Native; | ||
using Neo.SmartContract.Framework.Attributes; | ||
using Neo.SmartContract.Framework.Services; | ||
using System.ComponentModel; | ||
|
||
|
@@ -145,18 +153,19 @@ namespace Domain | |
public class Contract1 : SmartContract | ||
{ | ||
|
||
[Safe] | ||
[DisplayName("query")] | ||
public static byte[] Query(string domain) | ||
{ | ||
return Storage.Get(Storage.CurrentContext, domain); | ||
return (byte[])Storage.Get(Storage.CurrentContext, domain); | ||
} | ||
|
||
[DisplayName("register")] | ||
publilc static bool Register(string domain, byte[] owner) | ||
public static bool Register(string domain, UInt160 owner) | ||
{ | ||
// Check if the contract owner is the same as the one who invokes the contract | ||
if (!Runtime.CheckWitness(owner)) return false; | ||
byte[] value = Storage.Get(Storage.CurrentContext, domain); | ||
byte[] value = (byte[])Storage.Get(Storage.CurrentContext, domain); | ||
if (value != null) return false; | ||
Storage.Put(Storage.CurrentContext, domain, owner); | ||
return true; | ||
|
@@ -178,12 +187,16 @@ Let's slice it and learn it step by step. | |
You can declare more features: | ||
|
||
```cs | ||
[ManifestExtra("Author", "Neo")] | ||
[ManifestExtra("Email", "[email protected]")] | ||
[ManifestExtra("Description", "This is a contract example")] | ||
[ContractAuthor("core-dev", "[email protected]")] | ||
[ContractDescription("A sample NEP-17 token")] | ||
[ContractEmail("[email protected]")] | ||
[ContractSourceCode("https://github.com/neo-project/neo-devpack-dotnet/tree/master/examples/")] | ||
[ContractVersion("0.0.1")] | ||
[DisplayName("SampleNep17Token")] | ||
[SupportedStandards("NEP-17")] | ||
[ContractPermission("*", "onNEP17Payment")] | ||
[ContractTrust("0x0a0b00ff00ff00ff00ff00ff00ff00ff00ff00a4")] | ||
[ManifestExtra("WebSite", "https://neo.org")] | ||
public class Contract1 : SmartContract | ||
{ | ||
public static bool Main(string operation, object[] args) | ||
|
@@ -193,39 +206,57 @@ public class Contract1 : SmartContract | |
} | ||
``` | ||
|
||
`ManifestExtra` represents the extra fields in the Manifest file, where you can add `Author`, `Email`, `Description` and etc. | ||
|
||
`SupportedStandards` represents the NEP standards the contract conform to, such as NEP-17, a token standard on Neo. | ||
- `DisplayName`: The name of the nef and manifest.json files generated by the compiler, and the DisplayName is also written to the name field of manifest.json. | ||
- `SupportedStandards`: The NEP standards the contract conform to, such as NEP-17, a token standard on Neo. | ||
- `ContractPermission` : The permission requested by the contract, and `ContractTrust` indicates which contracts trust the contract to call itself. See [invocation-permission](... /deploy/invoke.html#invocation-permission). | ||
- `ContractAuthor`: The author field, which can be filled with the author's name and email address. It will output to the extra json object in manifest.json. | ||
- `ContractEmail`: The email field. It will be output to the extra json object in manifest.json. | ||
- `ContractSourceCode`: The URL of the contract source code. It will be output to the extra json object in manifest.json. | ||
- `ContractVersion`: The version of the contract. It will be output to the extra json object in manifest.json. | ||
- `ContractDescription`: The description of the contract. It will be output to the extra json object in manifest.json. | ||
- `ManifestExtra`: The extra fields in the Manifest file, where you can add `WebSite`, `Docs` and etc. It will be output to the extra json object in manifest.json. | ||
|
||
`ContractPermission` indicates the permission requested by the contract, and `ContractTrust` indicates which contracts trust the contract to call itself. See [invocation-permission](... /deploy/invoke.html#invocation-permission). | ||
The generated manifest is as follows: | ||
|
||
You can also add other fields, such as: | ||
|
||
```cs | ||
[ManifestExtra("Name", "sample contract")] | ||
[ManifestExtra("Version", "1.0.0")] | ||
```json | ||
{ | ||
"name": "SampleNep17Token", | ||
"supportedstandards": [], | ||
"abi": { | ||
}, | ||
"permissions": [ | ||
{ | ||
"contract": "*", | ||
"methods": "*" | ||
} | ||
], | ||
"trusts": [], | ||
"extra": { | ||
"Author": "core-dev", | ||
"E-mail": "[email protected]", | ||
"Version": "0.0.1", | ||
"Description": "A sample NEP-17 token", | ||
"Sourcecode": "https://github.com/neo-project/neo-devpack-dotnet/tree/master/examples/", | ||
"WebSite": "https://neo.org" | ||
} | ||
} | ||
``` | ||
|
||
### Entry function | ||
|
||
Theoretically, smart contracts can have any entry points. Methods of the public static type in the contract can be used as an entry function to be invoked externally, for example: | ||
Theoretically, smart contracts can have any entry points. Methods of the public static type in the contract can be used as an entry function to be invoked externally, properties in the contract can be used as an entry function to be invoked externally, for example: | ||
|
||
```cs | ||
using Neo.SmartContract; | ||
using Neo.SmartContract.Framework; | ||
|
||
namespace Neo.Compiler.MSIL.UnitTests.TestClasses | ||
namespace MyContract | ||
{ | ||
class Contract_a : SmartContract.Framework.SmartContract | ||
class Contract_a : SmartContract | ||
{ | ||
public static object First(string method, object[] args) | ||
{ | ||
return 'a'; | ||
} | ||
public static object Second(string method, object[] args) | ||
{ | ||
return 'b'; | ||
} | ||
public static string First() => "hello"; | ||
|
||
public int Second { get; set; } | ||
} | ||
} | ||
``` | ||
|
@@ -258,10 +289,10 @@ Usually this method is used to check whether an specified address is the the con | |
Inside our `DNS smart contract`, the `Register` function is firstly check if the owner is the same as the one who invoke the contract. Here we use the `Runtime.CheckWitness` function. Then we try to fetch the domain owner first to see if the domain is already exists in the storage. If not, we can store our domain->owner pair using the `Storage.Put`method. | ||
|
||
```cs | ||
private static bool Register(string domain, byte[] owner) | ||
private static bool Register(string domain, UInt160 owner) | ||
{ | ||
if (!Runtime.CheckWitness(owner)) return false; | ||
byte[] value = Storage.Get(Storage.CurrentContext, domain); | ||
byte[] value = (byte[])Storage.Get(Storage.CurrentContext, domain); | ||
if (value != null) return false; | ||
Storage.Put(Storage.CurrentContext, domain, owner); | ||
return true; | ||
|
@@ -282,28 +313,3 @@ public static event Action<byte[], byte[], BigInteger> OnTransfer; | |
|
||
Transfer is the event name. | ||
|
||
### Json serialization | ||
|
||
In Neo N3 smart contract, the Json serialization/deserialization feature is added: | ||
|
||
```cs | ||
using Neo.SmartContract.Framework.Native; | ||
using Neo.SmartContract.Framework.Services; | ||
|
||
namespace Neo.Compiler.MSIL.TestClasses | ||
{ | ||
public class Contract_Json : SmartContract.Framework.SmartContract | ||
{ | ||
public static string Serialize(object obj) | ||
{ | ||
return Json.Serialize(obj); | ||
} | ||
|
||
public static object Deserialize(string json) | ||
{ | ||
return Json.Deserialize(json); | ||
} | ||
} | ||
} | ||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.