Skip to content
This repository has been archived by the owner on Jan 7, 2025. It is now read-only.
darkfriend77 edited this page Jul 31, 2021 · 2 revisions

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));
await client.ConnectAsync(cancellationToken);

Access MetaData as JSON (chain specific)

// MetaData 
Console.WriteLine(client.MetaData.Serialize());

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: '"5DotMog6fcsVhMPqniyopz5sEJ5SMhHpz7ymgubr56gDxXwH"' [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", new [] {Utils.Bytes2HexString(Utils.GetPublicKeyFrom(address))}, cancellationToken);
Console.WriteLine($"RESPONSE: '{reqResult}' [{reqResult.GetType().Name}]");

RESPONSE: '{"Nonce":{"Value":15},"Consumers":{"Value":0},"Providers":{"Value":1},"AccountData":{"Free":{"Value":1998766600579252800594},"Reserved":{"Value":0},"MiscFrozen":{"Value":0},"FeeFrozen":{"Value":0}}}' [AccountInfo]

Access a pallet call

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

RESPONSE: 'DOTMog Node' [String]

Submit extrinsic (from pallet author)

// Dest: <T::Lookup as StaticLookup>::Source, Value: Compact<T::Balance>
var balanceTransfer = ExtrinsicCall.BalanceTransfer("5DotMog6fcsVhMPqniyopz5sEJ5SMhHpz7ymgubr56gDxXwH", BigInteger.Parse("100000000000"));
var reqResult = await client.Author.SubmitExtrinsicAsync(balanceTransfer, accountZurich, 0, 64, cancellationToken);

RESPONSE: '"0xB1435DA6A0F2C9C00E1AC0FAD7EBD2515B8AACDA12F27384A2148C556FEE627A"' [Hash]

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);