Skip to content

Commit

Permalink
Merge pull request #107 from neo-project/dev
Browse files Browse the repository at this point in the history
Merge dev updates to master
  • Loading branch information
apisit authored May 20, 2024
2 parents 84cb9ed + 432ec86 commit bd0c195
Show file tree
Hide file tree
Showing 67 changed files with 1,068 additions and 199 deletions.
6 changes: 3 additions & 3 deletions docs/n3/Advances/Oracles.md
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
2 changes: 1 addition & 1 deletion docs/n3/Advances/_category_.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"label": "Advances",
"position": 5
"position": 4
}
2 changes: 1 addition & 1 deletion docs/n3/develop/_category_.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"label": "Development Guide",
"position": 6
"position": 5
}
31 changes: 30 additions & 1 deletion docs/n3/develop/deploy/invoke.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Use the RPC API [getcontractstate method](../../reference/rpc/latest-version/api

The detailed contract information is displayed in Neo-GUI. You can also view the manifest and nef files.

## Invoking a contract
## Invoking a contract with Neo Node Clients

### Invoking a contract using Neo-CLI

Expand Down Expand Up @@ -128,3 +128,32 @@ Assuming that the contract A calls the contract B, the following table details t
| The contract defined in the Permissions of contract A is wildcard * and the method is m<br/>{"contract":"\*", "method": "m"} | Prompts that contract A will call the method m of any contract and asks whether to authorize the signature to contract B. | Default or Global according to the user's decision | Determined by the user |
| The contract defined in the Permissions of contract A is wildcard * and the method is wildcard \*<br/>{"contract":"\*", "method": "\*"} | Prompts that contract A will call any method of any contract and asks whether to set the signature to Global. | Default or Global according to the user's decision | Determined by the user |

## Invoking a contract with wallets/dAPIs

You can use browser wallets to invoke a smart contract, which is helpful for creating a front-end interface on Neo.

### Available Wallets

Below is a list of several wallets that allow users to invoke smart contracts on front-end interfaces across different platforms. While additional wallets may also be available, the ones listed here operate with slight variations in invocation.

- [Neoline](https://neoline.io/en/)
- [Neoline Dapi Doc](https://neoline.io/dapi/N3.html)

#### Desktop

- [Neon](https://neon.coz.io/)
- [WalletConnect SDK](https://github.com/CityOfZion/wallet-connect-sdk)
- [WalletConnect SDK Guide](https://github.com/CityOfZion/wallet-connect-sdk/blob/main/README.md)
- [O3 Wallet](https://o3.network/#/wallet)

#### Mobile

- [OneGate](https://onegate.space/)
- [Neoline Mobile](https://neoline.io/en/)

OneGate & Neoline Mobile support the [Neo Dapi](https://github.com/neo-ngd/neo-dapi-monorepo). Here you can learn more about [Neo dapi demo](https://github.com/neo-ngd/neo-dapi-demo).

### Usage Example

Here is an [example](https://neo-dapp-demo.vercel.app/) that is applicable to all of the wallets listed above. In this example, you can learn how to connect to a wallet and call methods supported by these wallets. Corresponding source code can be found at [dAPP Demo](https://github.com/ShaySong99/neo-dapp-demo).

20 changes: 16 additions & 4 deletions docs/n3/develop/write/1_dotnet.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,20 @@ dotnet new install Neo.SmartContract.Template
dotnet new list
```

There are three default templates available after installing Neo.SmartContract:

- neocontractowner - Standard contract template with Owner, including GetOwner and SetOwner methods)

- neocontractoracle - A contract template using OracleRequest)

- neocontractnep17 - NEP-17 Contract Template, including Mint and Burn methods)

:::note

The `neocontract` template used before has been renamed to `neocontractowner`.

:::

### Uninstall the template

```
Expand All @@ -51,10 +65,10 @@ dotnet new update Neo.SmartContract.Template
### Create a project using templates with Terminal

```
dotnet new neocontract
dotnet new neocontractowner
```

The project name defaults to the name of the current directory, you can also specify the project name with `-n, --name <name>`, e.g. `dotnet new neocontract -n MyFirstContract`.
The project name defaults to the name of the current directory, you can also specify the project name with `-n, --name <name>`, e.g. `dotnet new neocontractowner -n MyFirstContract`.

### Create a project using templates with Visual Studio

Expand All @@ -64,8 +78,6 @@ In the Visual Studio interface, create a new project, Neo.SmartContract.Template

![](../assets/neo-devpack-dotnet-2.png)



## Neo.Compiler.CSharp

Neo.Compiler.CSharp (nccs) is the Neo smart contract compiler that compiles the C# language into NeoVM executable OpCodes.
Expand Down
126 changes: 66 additions & 60 deletions docs/n3/develop/write/basics.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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;

Expand All @@ -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;
Expand All @@ -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)
Expand All @@ -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; }
}
}
```
Expand Down Expand Up @@ -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;
Expand All @@ -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);
}
}
}
```

9 changes: 7 additions & 2 deletions docs/n3/develop/write/nep11.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class MyTokenState : Nep11TokenState
After inheriting `Nep11Token<Nep11TokenState>` you need to rewrite the Symbol method, as follows:

```cs
public override string Symbol() => "MNFT";
public override string Symbol { [Safe] get => "MNFT"; }
```

## Distribution Method
Expand Down Expand Up @@ -80,7 +80,7 @@ namespace Contract1

private static bool IsOwner() => Runtime.CheckWitness(Owner);

public override string Symbol() => "MNFT";
public override string Symbol { [Safe] get => "MNFT"; }

public static bool Airdrop(UInt160 to, string name)
{
Expand Down Expand Up @@ -150,6 +150,11 @@ The base class `Nep11Token` also provides the following methods and events:
| -------- | ------------------------------------------------------------ | --------------- | ------------------------------------------------------------ |
| transfer | Hash160(from) Hash160(to) Integer(amount) ByteArray(tokenId) | Transfer event | When the `from` address is set to `null` tokens are created; When the `to` address set to `null`tokens are burned. |

#### Compatibility check

Compatibility checks will be activated for any contract that includes the `[SupportedStandards("NEP-17")]` or `[SupportedStandards("NEP-11")]` attribute.
The Compatibility Check reviews method names, parameters, return values, events, and similar elements to ensure they comply with the standard, and alerts about any failures in the check.

## See also

[NEP-11 Proposal](https://github.com/neo-project/proposals/blob/master/nep-11.mediawiki)
Expand Down
Loading

0 comments on commit bd0c195

Please sign in to comment.