Skip to content

Commit

Permalink
Properly define the signed extension
Browse files Browse the repository at this point in the history
  • Loading branch information
clangenb committed Aug 31, 2024
1 parent d187623 commit d299a88
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Substrate.NetApi.Test/Extrinsic/PayloadTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public void EncodeExtraTest()
var startEra = new Hash();
startEra.Create(blockHash);

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

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

Expand Down
2 changes: 1 addition & 1 deletion Substrate.NetApi.Test/Extrinsic/SignedExtensionsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public void EncodeExtraTest()
var startEra = new Hash();
startEra.Create(blockHash);

var assetTxPayment = new ChargeAssetTxPayment(0, new BaseOpt<I32>());
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.TestNode/ExtrinsicsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ public async Task Extrinsic_SubmitAndWatchExtrinsicAsync()
{
var iType = new BaseVec<U8>(new U8[] { (U8)0x04, (U8)0xFF });

var assetCharge = ChargeAssetTxPayment.NewWithAsset(0, new I32(0));
var method = new Method(0, "System", 0, "remark", new IType[] { iType });

var taskCompletionSource = new TaskCompletionSource<(bool, Hash)>();
Expand All @@ -92,7 +93,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 Down
54 changes: 46 additions & 8 deletions Substrate.NetApi/Model/Extrinsics/ChargeAssetTxPayment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,49 @@ namespace Substrate.NetApi.Model.Extrinsics
/// 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, I32>
{
}


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

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

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

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

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

Expand All @@ -57,7 +81,21 @@ public override void Decode(byte[] byteArray, ref int p)
/// <returns></returns>
public static ChargeAssetTxPayment Default()
{
return new ChargeAssetTxPayment(0, new BaseOpt<I32>());
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, I32 assetId)
{
var asset = new EnumNativeOrWithId();
asset.Create( NativeOrWithId.WithId, assetId);

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

Expand Down

0 comments on commit d299a88

Please sign in to comment.