Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Properly implement ChargeAssetTXPayment and test it. #90

Merged
merged 10 commits into from
Sep 2, 2024
3 changes: 2 additions & 1 deletion Substrate.NetApi.Test/Extrinsic/PayloadTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Substrate.NetApi.Model.Extrinsics;
using Substrate.NetApi.Model.Types.Base;
using NUnit.Framework;
using Substrate.NetApi.Model.Types.Primitive;

namespace Substrate.NetApi.Test.Extrinsic
{
Expand Down Expand Up @@ -52,7 +53,7 @@ public void EncodeExtraTest()
var startEra = new Hash();
startEra.Create(blockHash);

var assetTxPayment = new ChargeAssetTxPayment(0, 0);
var assetTxPayment = new ChargeAssetTxPayment(0, new BaseOpt<EnumNativeOrWithId>());

var signedExtensions = new SignedExtensions(259, 1, genesis, startEra, era, 0, assetTxPayment);

Expand Down
3 changes: 2 additions & 1 deletion Substrate.NetApi.Test/Extrinsic/SignedExtensionsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Substrate.NetApi.Model.Extrinsics;
using Substrate.NetApi.Model.Types.Base;
using NUnit.Framework;
using Substrate.NetApi.Model.Types.Primitive;

namespace Substrate.NetApi.Test.Extrinsic
{
Expand Down Expand Up @@ -36,7 +37,7 @@ public void EncodeExtraTest()
var startEra = new Hash();
startEra.Create(blockHash);

var assetTxPayment = new ChargeAssetTxPayment(0, 0);
var assetTxPayment = new ChargeAssetTxPayment(0, new BaseOpt<EnumNativeOrWithId>());

var signedExtensions = new SignedExtensions(259, 1, genesis, startEra, era, 0, assetTxPayment);

Expand Down
16 changes: 11 additions & 5 deletions Substrate.NetApi.TestNode/ExtrinsicsTest.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
using NUnit.Framework;
using NUnit.Framework.Internal;
using Substrate.NET.Schnorrkel.Keys;
using Substrate.NetApi.Model.Extrinsics;
using Substrate.NetApi.Model.Rpc;
using Substrate.NetApi.Model.Types;
using Substrate.NetApi.Model.Types.Base;
using Substrate.NetApi.Model.Types.Primitive;
using System;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;

Expand Down Expand Up @@ -40,7 +40,7 @@ public async Task CloseAsync()
[OneTimeSetUp]
public async Task SetupAsync()
{
_chargeType = ChargeTransactionPayment.Default();
_chargeType = ChargeAssetTxPayment.Default();
_substrateClient = new SubstrateClient(new Uri(WebSocketUrl), _chargeType);

try
Expand All @@ -50,7 +50,7 @@ public async Task SetupAsync()
catch (Exception ex)
{
Console.WriteLine($"Failed to connect to Substrate node: {ex.Message}");
Assert.Ignore("Skipped test because no active Substrate node was found on 127.0.0.1:9944");
Assert.Ignore("Skipped test because no active Substrate node was found on 127.0.0.1:9999");
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The assert message mentioned the wrong port.

}
}

Expand All @@ -65,19 +65,23 @@ public async Task TearDownAsync()
}

/// <summary>
/// Extrinsic Submit And Watch
/// Extrinsic Submit And Watch with non-native payment.
/// </summary>
/// <returns></returns>
[Test]
public async Task Extrinsic_SubmitAndWatchExtrinsicAsync()
{
var iType = new BaseVec<U8>(new U8[] { (U8)0x04, (U8)0xFF });

// Pay with a non-native asset with `assetId=0`, e.g. DOT on Ajuna Polkadot or PAS on Ajuna Paseo.
var assetCharge = ChargeAssetTxPayment.NewWithAsset(0, new U32(0));
var method = new Method(0, "System", 0, "remark", new IType[] { iType });

var taskCompletionSource = new TaskCompletionSource<(bool, Hash)>();
await _substrateClient.Author.SubmitAndWatchExtrinsicAsync((string subscriptionId, ExtrinsicStatus extrinsicUpdate) =>
{
Debug.WriteLine($"ExtrinsicUpdate: {extrinsicUpdate.ExtrinsicState}");

switch (extrinsicUpdate.ExtrinsicState)
{
case ExtrinsicState.Finalized:
Expand All @@ -92,7 +96,7 @@ await _substrateClient.Author.SubmitAndWatchExtrinsicAsync((string subscriptionI
Assert.Fail("Extrinsic was invalid!");
break;
}
}, method, Alice, _chargeType, 64, CancellationToken.None);
}, method, Alice, assetCharge, 64, CancellationToken.None);

var finished = await Task.WhenAny(taskCompletionSource.Task, Task.Delay(TimeSpan.FromMinutes(1)));
Assert.AreEqual(taskCompletionSource.Task, finished, "Test timed out waiting for final callback");
Expand All @@ -111,6 +115,8 @@ public async Task Extrinsic_TransactionV1SubmitAndWatchAsync()
_ = await _substrateClient.TransactionWatchCalls.TransactionWatchV1SubmitAndWatchAsync(
(subscriptionId, extrinsicUpdate) =>
{
Debug.WriteLine($"ExtrinsicUpdate: {extrinsicUpdate.TransactionEvent.ToString()}");

switch (extrinsicUpdate.TransactionEvent)
{
case TransactionEvent.Finalized:
Expand Down
56 changes: 48 additions & 8 deletions Substrate.NetApi/Model/Extrinsics/ChargeAssetTxPayment.cs
Original file line number Diff line number Diff line change
@@ -1,31 +1,56 @@
using System.Collections.Generic;
using Substrate.NetApi.Model.Types.Base;
using Substrate.NetApi.Model.Types.Primitive;

namespace Substrate.NetApi.Model.Extrinsics
{
/// <summary>
/// Charge Type
/// </summary>
public abstract class ChargeType : BaseType
{ }
{
}

/// <summary>
/// Asset Identifier for AssetHubs and the Ajuna Chain.
/// </summary>
public enum NativeOrWithId
{
/// <summary>
/// Native token. However, in the `ChargeAssetTxPayment` this is unused, as the none implies the Native asset.
/// </summary>
Native = 0,
/// <summary>
/// Some asset with a corresponding asset should be used for payment.
/// </summary>
WithId = 1,
}

/// <summary>
/// Asset Identifier for AssetHubs and the Ajuna Chain.
/// </summary>
public sealed class EnumNativeOrWithId : BaseEnumExt<NativeOrWithId, BaseVoid, U32>
{
}


/// <summary>
/// Charge Asset Tx Payment
/// </summary>
public class ChargeAssetTxPayment : ChargeType
{
private CompactInteger _tip;
private CompactInteger _assetId;
private BaseOpt<EnumNativeOrWithId> _assetId;

/// <summary>
/// Charge Asset Tx Payment Constructor
/// </summary>
/// <param name="tip"></param>
/// <param name="asset"></param>
public ChargeAssetTxPayment(CompactInteger tip, CompactInteger asset)
/// <param name="assetId"></param>
public ChargeAssetTxPayment(CompactInteger tip, BaseOpt<EnumNativeOrWithId> assetId)
{
_tip = tip;
_assetId = asset;
_assetId = assetId;
}

/// <inheritdoc/>
Expand All @@ -36,7 +61,7 @@ public override byte[] Encode()
// Tip
bytes.AddRange(_tip.Encode());

// Asset Id
// AssetId
bytes.AddRange(_assetId.Encode());

return bytes.ToArray();
Expand All @@ -46,7 +71,8 @@ public override byte[] Encode()
public override void Decode(byte[] byteArray, ref int p)
{
_tip = CompactInteger.Decode(byteArray, ref p);
_assetId = CompactInteger.Decode(byteArray, ref p);
_assetId = new BaseOpt<EnumNativeOrWithId>();
_assetId.Decode(byteArray, ref p);
}

/// <summary>
Expand All @@ -55,7 +81,21 @@ public override void Decode(byte[] byteArray, ref int p)
/// <returns></returns>
public static ChargeAssetTxPayment Default()
{
return new ChargeAssetTxPayment(0, 0);
return new ChargeAssetTxPayment(0, new BaseOpt<EnumNativeOrWithId>());
}

/// <summary>
/// Defines extrinsic payment with the asset id
/// </summary>
/// <param name="tip"></param>
/// <param name="assetId"></param>
/// <returns></returns>
public static ChargeAssetTxPayment NewWithAsset(CompactInteger tip, U32 assetId)
{
var asset = new EnumNativeOrWithId();
asset.Create(NativeOrWithId.WithId, assetId);

return new ChargeAssetTxPayment(tip, new BaseOpt<EnumNativeOrWithId>(asset));
}
}

Expand Down
2 changes: 1 addition & 1 deletion Substrate.NetApi/Substrate.NetApi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<PackageId>Substrate.NET.API</PackageId>
<TargetFrameworks>netstandard2.0;netstandard2.1;net6.0</TargetFrameworks>
<Version>0.9.24-rc1</Version>
<Version>0.9.24-rc2</Version>
<Company>Substrate Gaming</Company>
<Authors>Substrate Gaming</Authors>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
Expand Down
Loading