Skip to content

BlockForks/SubstrateNetApi

 
 

Repository files navigation

SubstrateNetApi (NETStandard2.0)

Just another Substrate .NET API
Build status nuget GitHub issues license contributors

SubstrateNetApi is written in NETStandard2.0 to provide maximum compatibility for Unity3D. Feedback, constructive critisme and disscussions are welcome and will help us to improve the API!

Table of Content

  • Requirements
  • Installation
  • Usage
    • connecting
    • pallet storage data
    • pallet call
    • extrinsic (pallet author)
  • Wallet

Requirements

Installation

Substrate in .NET

Add NuGet package https://www.nuget.org/packages/SubstrateNetApi/

Substrate in Unity3D

Method A:

Added the dependency needed, there is an example project (https://github.com/darkfriend77/Unity3DExample), which already imported the necessary dependencies.

NuGet Dependencies

Method B:

Currently there is an issue where NuGetForUnity, will pull to many dependencies and break the project.

Usage

Create a connection

var WEBSOCKETURL = "wss://xyz.node.com"; // or local node ws://127.0.0.1:9944

using var client = new SubstrateClient(new Uri(WEBSOCKETURL));
client.RegisterTypeConverter(new MogwaiStructTypeConverter());
await client.ConnectAsync(cancellationToken);

Access a pallet storage data

Example 1: Sudo Key (no parameter)

// [Plain] Value: T::AccountId (from metaData)
var reqResult = await client.GetStorageAsync("Sudo", "Key", cancellationToken);
Console.WriteLine($"RESPONSE: '{reqResult}' [{reqResult.GetType().Name}]");

RESPONSE: '{"Address":"5GYZnHJ4dCtTDoQj4H5H9E727Ykv8NLWKtPAupEc3uJ89BGr","PublicKey":"xjCev8DKRhmK9W9PWJt82svJRhLQnZ5xsp5Z0cHy3mg="}' [AccountId]

Example 2: System Account (Key: AccountId (public key))

// [Map] Key: T::AccountId, Hasher: Blake2_128Concat, Value: AccountInfo<T::Index, T::AccountData> (from metaData)
var reqResult = await client.GetStorageAsync("System", "Account", "0xD43593C715FDD31C61141ABD04A99FD6822C8558854CCDE39A5684E7A56DA27D", cancellationToken);
Console.WriteLine($"RESPONSE: '{reqResult}' [{reqResult.GetType().Name}]");

RESPONSE: '{"Nonce":4,"RefCount":0,"AccountData":{"Free":{"Value":{"Value":17665108313441014531489792}},"Reserved":{"Value":{"Value":0}},"MiscFrozen":{"Value":{"Value":0}},"FeeFrozen":{"Value":{"Value":0}}}}' [AccountInfo]

Access a pallet call

var systemName = await client.System.NameAsync(cancellationToken);

Submit extrinsic (from pallet author)

var balanceTransfer = ExtrinsicCall.BalanceTransfer("5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY", 1000);
var reqResult = await client.SubmitExtrinsicAsync(balanceTransfer, accountDMOG_GALxeh, 0, 64, cancellationToken);

Subscribe and unsubscribe with registering a call back

// create a call back action with the expected type
Action<ExtrinsicStatus> actionExtrinsicUpdate = (extrinsicUpdate) => Console.WriteLine($"CallBack: {extrinsicUpdate}");
// create the extrinsic parameters
var balanceTransfer = ExtrinsicCall.BalanceTransfer("5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY", 1000);
// submit and subscribe to the extrinsic
var subscriptionId = await client.Author.SubmitAndWatchExtrinsicAsync(actionExtrinsicUpdate, balanceTransfer, accountDMOG_GALxeh, 0, 64, cancellationToken);
// wait for extrinsic pass into a finalized block
Thread.Sleep(60000);
// unsubscribe
var reqResult = await client.Author.UnwatchExtrinsicAsync(subscriptionId, cancellationToken);

Wallet

SubstrateNetWallet is a Wallet buit on top of the SubstrateNetApi, it offers common functionalities. This is an implementation in progress, feedback is welcome!

  • Key derivation, currently only for ED25519
  • Private keystore with AES encryption (please verify code before using in a productive environment)
  • Sign message & verify message
  • Transfer balance
  • realtime (subscription) updated newHeads and finalizedHeads
  • realtime (subscription) updated extrinsicUpdate

Special Thanks

About

Just another Substrate .NET API

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C# 100.0%