Skip to content

Commit

Permalink
chore(net): add some extension methods to Substrate.Gear.Client (#609)
Browse files Browse the repository at this point in the history
  • Loading branch information
DennisInSky authored Oct 24, 2024
1 parent 418c351 commit 0e0c513
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 20 deletions.
11 changes: 10 additions & 1 deletion net/src/Substrate.Gear.Client/GasInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,27 @@ namespace Substrate.Gear.Client;

public sealed record GasInfo
{
/// <summary>
/// Represents minimum gas limit required for execution.
/// </summary>
public required GasUnit MinLimit { get; init; }
/// <summary>
/// Gas amount that we reserve for some other on-chain interactions.
/// </summary>
public required GasUnit Reserved { get; init; }
/// <summary>
/// Contains number of gas burned during message processing.
/// </summary>
public required GasUnit Burned { get; init; }
/// <summary>
/// The value may be returned if a program happens to be executed
/// the second or next time in a block.
/// </summary>
public required GasUnit MayBeReturned { get; init; }
/// <summary>
/// Was the message placed into waitlist at the end of calculating.
///
/// This flag shows, that `min_limit` makes sense and have some guarantees
/// only before insertion into waitlist.
/// </summary>
public bool IsInWaitList { get; init; }
}
21 changes: 21 additions & 0 deletions net/src/Substrate.Gear.Client/Model/Types/AccountExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using EnsureThat;
using Substrate.NET.Schnorrkel;
using Substrate.NetApi.Model.Types;

namespace Substrate.Gear.Client.Model.Types;

public static class AccountExtensions
{
/// <summary>
/// Returns a public key of the account.
/// </summary>
/// <param name="account"></param>
/// <returns></returns>
public static PublicKey GetPublicKey(this Account account)
{
EnsureArg.IsNotNull(account, nameof(account));
EnsureArg.HasItems(account.Bytes, nameof(account.Bytes));

return new PublicKey(account.Bytes);
}
}
105 changes: 86 additions & 19 deletions net/src/Substrate.Gear.Client/SubstrateClientExtExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using Newtonsoft.Json;
using Substrate.Gear.Api.Generated;
using Substrate.Gear.Api.Generated.Model.gprimitives;
using Substrate.NET.Schnorrkel.Keys;
using Substrate.NET.Schnorrkel;
using Substrate.NetApi;
using GasUnit = Substrate.NetApi.Model.Types.Primitive.U64;
using ValueUnit = Substrate.NetApi.Model.Types.Primitive.U128;
Expand All @@ -14,20 +14,77 @@ namespace Substrate.Gear.Client;

public static class SubstrateClientExtExtensions
{
public static async Task<GasInfo> CalculateGasForUploadAsync(
/// <summary>
/// Calculates amount of gas required for creating a new program from previously uploaded code.
/// </summary>
/// <param name="nodeClient"></param>
/// <param name="signingAccountKey"></param>
/// <param name="codeId"></param>
/// <param name="encodedInitPayload"></param>
/// <param name="value"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public static async Task<GasInfo> CalculateGasForCreateProgramAsync(
this SubstrateClientExt nodeClient,
MiniSecret accountSecret,
PublicKey signingAccountKey,
CodeId codeId,
IReadOnlyCollection<byte> encodedInitPayload,
ValueUnit value,
CancellationToken cancellationToken)
{
EnsureArg.IsNotNull(nodeClient, nameof(nodeClient));
EnsureArg.IsNotNull(signingAccountKey, nameof(signingAccountKey));
EnsureArg.IsNotNull(codeId, nameof(codeId));
EnsureArg.IsNotNull(encodedInitPayload, nameof(encodedInitPayload));

var accountPublicKeyStr = Utils.Bytes2HexString(signingAccountKey.Key);
var encodedInitPayloadStr = Utils.Bytes2HexString(
encodedInitPayload is byte[] encodedInitPayloadBytes
? encodedInitPayloadBytes
: [.. encodedInitPayload]);
var valueBigInt = value.Value;
var parameters = new object[]
{
accountPublicKeyStr,
codeId,
encodedInitPayloadStr,
valueBigInt,
true
};

var gasInfoJson = await nodeClient.InvokeAsync<GasInfoJson>(
"gear_calculateInitCreateGas",
parameters,
cancellationToken)
.ConfigureAwait(false);

return gasInfoJson.ToGasInfo();
}

/// <summary>
/// Calculates amount of gas required for uploading code and creating a new program from it.
/// </summary>
/// <param name="nodeClient"></param>
/// <param name="signingAccountKey"></param>
/// <param name="wasm"></param>
/// <param name="encodedInitPayload"></param>
/// <param name="value"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public static async Task<GasInfo> CalculateGasForUploadProgramAsync(
this SubstrateClientExt nodeClient,
PublicKey signingAccountKey,
IReadOnlyCollection<byte> wasm,
IReadOnlyCollection<byte> encodedInitPayload,
ValueUnit value,
CancellationToken cancellationToken)
{
EnsureArg.IsNotNull(nodeClient, nameof(nodeClient));
EnsureArg.IsNotNull(accountSecret, nameof(accountSecret));
EnsureArg.IsNotNull(signingAccountKey, nameof(signingAccountKey));
EnsureArg.HasItems(wasm, nameof(wasm));
EnsureArg.IsNotNull(encodedInitPayload, nameof(encodedInitPayload));

var accountPublicKeyStr = Utils.Bytes2HexString(accountSecret.GetPair().Public.Key);
var accountPublicKeyStr = Utils.Bytes2HexString(signingAccountKey.Key);
var wasmBytesStr = Utils.Bytes2HexString(
wasm is byte[] wasmBytes
? wasmBytes
Expand Down Expand Up @@ -55,25 +112,36 @@ encodedInitPayload is byte[] encodedInitPayloadBytes
return gasInfoJson.ToGasInfo();
}

public static async Task<GasInfo> CalculateGasGorHandleAsync(
/// <summary>
/// Calculates amount of gas required for executing a message by specified program.
/// </summary>
/// <param name="nodeClient"></param>
/// <param name="signingAccountKey"></param>
/// <param name="programId"></param>
/// <param name="encodedPayload"></param>
/// <param name="value"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public static async Task<GasInfo> CalculateGasForHandleAsync(
this SubstrateClientExt nodeClient,
MiniSecret accountSecret,
PublicKey signingAccountKey,
ActorId programId,
IReadOnlyCollection<byte> encodedPayload,
ValueUnit value,
CancellationToken cancellationToken)
{
EnsureArg.IsNotNull(nodeClient, nameof(nodeClient));
EnsureArg.IsNotNull(accountSecret, nameof(accountSecret));
EnsureArg.IsNotNull(signingAccountKey, nameof(signingAccountKey));
EnsureArg.IsNotNull(programId, nameof(programId));
EnsureArg.IsNotNull(encodedPayload, nameof(encodedPayload));

var accountPublicKeyStr = Utils.Bytes2HexString(accountSecret.GetPair().Public.Key);
var accountPublicKeyStr = Utils.Bytes2HexString(signingAccountKey.Key);
var encodedPayloadStr = Utils.Bytes2HexString(
encodedPayload is byte[] encodedPayloadBytes
? encodedPayloadBytes
: [.. encodedPayload]);
var parameters = new object[] {
var parameters = new object[]
{
accountPublicKeyStr,
programId,
encodedPayloadStr,
Expand All @@ -92,21 +160,20 @@ encodedPayload is byte[] encodedPayloadBytes

private sealed record GasInfoJson
{
/// Represents minimum gas limit required for execution.
// Represents minimum gas limit required for execution.
[JsonProperty("min_limit")]
public ulong MinLimit { get; init; }
/// Gas amount that we reserve for some other on-chain interactions.
// Gas amount that we reserve for some other on-chain interactions.
public ulong Reserved { get; init; }
/// Contains number of gas burned during message processing.
// Contains number of gas burned during message processing.
public ulong Burned { get; init; }
/// The value may be returned if a program happens to be executed
/// the second or next time in a block.
// The value may be returned if a program happens to be executed
// the second or next time in a block.
[JsonProperty("may_be_returned")]
public ulong MayBeReturned { get; init; }
/// Was the message placed into waitlist at the end of calculating.
///
/// This flag shows, that `min_limit` makes sense and have some guarantees
/// only before insertion into waitlist.
// Was the message placed into waitlist at the end of calculating.
// This flag shows, that `min_limit` makes sense and have some guarantees
// only before insertion into waitlist.
[JsonProperty("waited")]
public bool IsInWaitList { get; init; }

Expand Down

0 comments on commit 0e0c513

Please sign in to comment.