Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 7 additions & 17 deletions Substrate.NetApi.Test/TypeConverters/BaseTypesTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using NUnit.Framework;
using System.Collections.Generic;
using Newtonsoft.Json.Linq;
using Microsoft.VisualStudio.TestPlatform.ObjectModel;

namespace Substrate.NetApi.Test
{
Expand Down Expand Up @@ -191,8 +192,8 @@ public void BaseOptTest()
public void BaseBitSeqTest()
{
var testCase1 = "0xa00b80050000";
var bitSeqTest1 = FromBitString("0b11010000_00000001_10100000_00000000_00000000");
var baseBitSeq1 = new BaseBitSeq<U8, U8>();
var bitSeqTest1 = Utils.FromBitString("0b11010000_00000001_10100000_00000000_00000000");
var baseBitSeq1 = new BaseBitSeq<U8>();
baseBitSeq1.Create(testCase1);
for (int i = 0; i < bitSeqTest1.Length; i++)
{
Expand All @@ -201,14 +202,14 @@ public void BaseBitSeqTest()
Assert.AreEqual(testCase1, Utils.Bytes2HexString(baseBitSeq1.Encode()).ToLower());

// Let's create the same object but with the other Create() method
var baseBitSeq1_1 = new BaseBitSeq<U8, U8>();
baseBitSeq1_1.Create(baseBitSeq1.Value);
var baseBitSeq1_1 = new BaseBitSeq<U8>();
baseBitSeq1_1.Create(baseBitSeq1.Value, BitOrder.Lsb0);
Assert.AreEqual(baseBitSeq1.Bytes, baseBitSeq1_1.Bytes);
Assert.AreEqual(baseBitSeq1.Value, baseBitSeq1_1.Value);
Assert.AreEqual(baseBitSeq1.TypeSize, baseBitSeq1_1.TypeSize);

var bitSeqTest2 = FromBitString("0b10000010_10000010_00101000_00000000_00000000");
var baseBitSeq2 = new BaseBitSeq<U8, U8>();
var bitSeqTest2 = Utils.FromBitString("0b10000010_10000010_00101000_00000000_00000000");
var baseBitSeq2 = new BaseBitSeq<U8>();
baseBitSeq2.Create("0xa04141140000");
for (int i = 0; i < bitSeqTest1.Length; i++)
{
Expand All @@ -217,17 +218,6 @@ public void BaseBitSeqTest()
Assert.AreEqual("0xa04141140000", Utils.Bytes2HexString(baseBitSeq2.Encode()).ToLower());
}

private byte[] FromBitString(string str)
{
var s = str.Replace("0b", "").Split('_');
var result = new byte[s.Length];
for (int i = 0; i < s.Length; i++)
{
result[i] = Convert.ToByte(s[i], 2);
}
return result;
}

[Test]
public void BaseComTest()
{
Expand Down
10 changes: 10 additions & 0 deletions Substrate.NetApi.Test/TypeConverters/PrimitiveTypesTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
using Substrate.NetApi.Model.Types.Primitive;
using NUnit.Framework;
using Newtonsoft.Json.Linq;
using System.Linq;
using System.Net;

namespace Substrate.NetApi.Test
{
Expand Down Expand Up @@ -232,6 +234,14 @@ public void PrimU256Test()

BigInteger primImplicit = new U256(value);
Assert.AreEqual(value, primImplicit);

var testArray = Utils.GetPublicKeyFrom("unixuHLc4UoAjwLpkQHUWy2NpT5LV4tUqJFnFVNyLaeqBfq22").Reverse().ToArray();

var test = new U256();
test.Create(testArray);

Assert.AreEqual("88948098633472856107773808278376342488924045139856962685537580476255925753211", test.Value.ToString());
Assert.IsTrue(test.Bytes.SequenceEqual(testArray));
}

[Test]
Expand Down
128 changes: 88 additions & 40 deletions Substrate.NetApi.Test/TypeConverters/TypeEncodingTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Substrate.NetApi.Model.Types.Primitive;
using Substrate.NetApi.TypeConverters;
using NUnit.Framework;
using System;

namespace Substrate.NetApi.Test
{
Expand Down Expand Up @@ -112,11 +113,22 @@ public enum PhaseState
[Test]
public void ExtEnumEncodingTest()
{
var extEnumType = new BaseEnumExt<PhaseState, U8, BaseVoid, BaseVoid>();
// Set up type decoder map for PhaseState
var typeDecoderMap = new Dictionary<PhaseState, Type>
{
{ PhaseState.None, typeof(U8) },
{ PhaseState.Finalization, typeof(BaseVoid) },
{ PhaseState.Initialization, typeof(BaseVoid) }
};

// Initialize BaseEnumRust with the decoder map
var extEnumType = new BaseEnumRust<PhaseState>(typeDecoderMap);

// Decode the input data
int p = 0;
extEnumType.Decode(new byte[] { 0x00, 0x01 }, ref p);

// Assertions to verify values
Assert.AreEqual(PhaseState.None, extEnumType.Value);
Assert.AreEqual("U8", extEnumType.Value2.GetType().Name);
Assert.AreEqual(1, (extEnumType.Value2 as U8).Value);
Expand All @@ -125,45 +137,75 @@ public void ExtEnumEncodingTest()
[Test]
public void ExtEnumDencodingTest()
{
var extEnumType = new BaseEnumExt<PhaseState, U8, BaseVoid, BaseVoid>();
// Set up type decoder map for PhaseState
var typeDecoderMap = new Dictionary<PhaseState, Type>
{
{ PhaseState.None, typeof(U8) },
{ PhaseState.Finalization, typeof(BaseVoid) },
{ PhaseState.Initialization, typeof(BaseVoid) }
};

// Initialize BaseEnumRust with the decoder map
var extEnumType = new BaseEnumRust<PhaseState>(typeDecoderMap);

// Decode the input data
int p = 0;
extEnumType.Decode(new byte[] { 0x00, 0x01 }, ref p);

// Assertions to verify values
Assert.AreEqual(PhaseState.None, extEnumType.Value);
Assert.AreEqual("U8", extEnumType.Value2.GetType().Name);
Assert.AreEqual(1, (extEnumType.Value2 as U8).Value);

// Verify the bytes are preserved correctly
Assert.AreEqual(new byte[] { 0x00, 0x01 }, extEnumType.Bytes);
}

[Test]
public void ExtEnumCreateTest()
{
var u8 = new U8(1);

var vecExtEnumTypeFromCreateValue = new BaseEnumExt<PhaseState, U8>();
vecExtEnumTypeFromCreateValue.Create(PhaseState.None, u8);

var vecExtEnumTypeFromCreateByteArray = new BaseEnumExt<PhaseState, U8>();
vecExtEnumTypeFromCreateByteArray.Create(new byte[] { 0, 1 });

var vecExtEnumTypeFromCreateHex = new BaseEnumExt<PhaseState, U8>();
vecExtEnumTypeFromCreateHex.Create("0x0001");

Assert.That(vecExtEnumTypeFromCreateValue.Bytes, Is.EqualTo(vecExtEnumTypeFromCreateByteArray.Bytes));
Assert.That(vecExtEnumTypeFromCreateValue.Value, Is.EqualTo(vecExtEnumTypeFromCreateByteArray.Value));

var typeDecoderMap = new Dictionary<PhaseState, Type>
{
{ PhaseState.None, typeof(U8) },
{ PhaseState.Finalization, typeof(BaseVoid) },
{ PhaseState.Initialization, typeof(BaseVoid) }
};

Assert.That(vecExtEnumTypeFromCreateValue.Bytes, Is.EqualTo(vecExtEnumTypeFromCreateHex.Bytes));
Assert.That(vecExtEnumTypeFromCreateValue.Value, Is.EqualTo(vecExtEnumTypeFromCreateHex.Value));
// Create instance using enum value and U8 value
var u8 = new U8(1);
var byValue = new BaseEnumRust<PhaseState>(typeDecoderMap);
byValue.Create(PhaseState.None, u8);

// Create instance using byte array
var byArray = new BaseEnumRust<PhaseState>(typeDecoderMap);
byArray.Create(new byte[] { 0x00, 0x01 });

// Create instance using hex string
var byHex = new BaseEnumRust<PhaseState>(typeDecoderMap);
byHex.Create("0x0001");

// Assert equality between different creation methods
Assert.That(byValue.Bytes, Is.EqualTo(byArray.Bytes));
Assert.That(byValue.Value, Is.EqualTo(byArray.Value));
Assert.That(byValue.Bytes, Is.EqualTo(byHex.Bytes));
Assert.That(byValue.Value, Is.EqualTo(byHex.Value));
}

[Test]
public void ExtEnumXXX()
public void ExtEnumXXX_NewTest()
{
var vecExtEnumType = new BaseVec<BaseEnumExt<PhaseState, BaseTuple<Arr4U8, BaseVec<U8>>, BaseVoid, BaseVoid, BaseVoid, BaseVoid, BaseVoid, BaseVoid, BaseVoid, BaseVoid>>();
// Create the type decoder map for PhaseState
var typeDecoderMap = new Dictionary<PhaseState, Type>
{
{ PhaseState.None, typeof(BaseTuple<Arr4U8, BaseVec<U8>>) },
{ PhaseState.Finalization, typeof(BaseVoid) },
{ PhaseState.Initialization, typeof(BaseVoid) }
};

// Initialize the enum wrapper
var baseEnum = new BaseEnumRust<PhaseState>(typeDecoderMap);

// Prepare values
var u8 = new U8();
u8.Create(byte.MaxValue);

Expand All @@ -173,17 +215,16 @@ public void ExtEnumXXX()
var vec82 = new BaseVec<U8>();
vec82.Create(new U8[] { u8 });

var tu = new BaseTuple<Arr4U8, BaseVec<U8>>();
tu.Create(vec8, vec82);

var it = new BaseEnumExt<PhaseState, BaseTuple<Arr4U8, BaseVec<U8>>, BaseVoid, BaseVoid, BaseVoid, BaseVoid, BaseVoid, BaseVoid, BaseVoid, BaseVoid>();
it.Create(PhaseState.None, tu);
var tuple = new BaseTuple<Arr4U8, BaseVec<U8>>();
tuple.Create(vec8, vec82);

vecExtEnumType.Create(new[] { it });
// Create with PhaseState.None and tuple value
baseEnum.Create(PhaseState.None, tuple);

var encoded = vecExtEnumType.Encode();
// Encode and decode
var encoded = baseEnum.Encode();
int p = 0;
vecExtEnumType.Decode(encoded, ref p);
baseEnum.Decode(encoded, ref p);

Assert.Pass();
}
Expand All @@ -194,27 +235,34 @@ internal enum TestEnum26
}

[Test]
public void ExtEnum26()
public void ExtEnum26_NewTest()
{
var ext1 = new BaseEnumExt<TestEnum26, U8, U16, U8, U16, U8, U16, U8, U16, U8, U16, U8, U16, U8, U16, U8, U16, U8, U16, U8, U16, U8, U16, U8, U16, U8, U16>();
// Create the type decoder map for TestEnum26
var typeDecoderMap = new Dictionary<TestEnum26, Type>
{
{ TestEnum26.T1, typeof(U8) },
{ TestEnum26.T2, typeof(U16) },
// Add other mappings as needed
};

var u8 = new U8();
u8.Create(byte.MaxValue);
// Initialize BaseEnumRust
var baseEnum = new BaseEnumRust<TestEnum26>(typeDecoderMap);

// Create U16 value
var u16 = new U16();
u16.Create(ushort.MaxValue);

ext1.Create(TestEnum26.T2, u16);

var encoded = ext1.Encode();

var ext2 = new BaseEnumExt<TestEnum26, U8, U16, U8, U16, U8, U16, U8, U16, U8, U16, U8, U16, U8, U16, U8, U16, U8, U16, U8, U16, U8, U16, U8, U16, U8, U16>();
// Create an instance with TestEnum26.T2
baseEnum.Create(TestEnum26.T2, u16);

// Encode and decode
var encoded = baseEnum.Encode();
int p = 0;
ext2.Decode(encoded, ref p);
baseEnum.Decode(encoded, ref p);

Assert.AreEqual(TestEnum26.T2, ext2.Value);
Assert.AreEqual(ushort.MaxValue, ((U16)ext2.Value2).Value);
// Assertions
Assert.AreEqual(TestEnum26.T2, baseEnum.Value);
Assert.AreEqual(ushort.MaxValue, ((U16)baseEnum.Value2).Value);
}

[Test]
Expand Down
63 changes: 63 additions & 0 deletions Substrate.NetApi.TestNode/ModuleStateNextTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
using NUnit.Framework;
using StreamJsonRpc;
using Substrate.NetApi.Model.Rpc;
using Substrate.NetApi.Model.Types.Base;
using Substrate.NetApi.Model.Types.Primitive;

namespace Substrate.NetApi.TestNode
{
public class ModuleStateNextTest : NodeTest
{
public ModuleStateNextTest()
: base("wss://hydradx-rpc.dwellir.com") { }

[Test]
public async Task GetBlockAsyncTestAsync()
{
var result = await _substrateClient.Chain.GetBlockAsync(new Hash("0x467fb6268675b96e707df72d382c14da0045ebc553edd9850414560053870b09"), CancellationToken.None);

Assert.IsNotNull(result);
}

[Test]
public async Task GetKeysPagedAtTestAsync()
{
var parameters = RequestGenerator.GetStorage("System", "Number",
Model.Meta.Storage.Type.Plain);

var currentBlocknumber = await _substrateClient.GetStorageAsync<U32>(parameters, CancellationToken.None);

var blockNumber = new BlockNumber();
blockNumber.Create(currentBlocknumber.Value);

var blockHash = await _substrateClient.Chain.GetBlockHashAsync(blockNumber);

var result = await _substrateClient.State.GetKeysPagedAsync(RequestGenerator.GetStorageKeyBytesHash("System", "BlockHash"), 10, null, blockHash.Bytes, CancellationToken.None);

Assert.IsNotNull(result);
Assert.AreEqual(10, result.Count);
}

[Test]
[TestCase("0x467fb6268675b96e707df72d382c14da0045ebc553edd9850414560053870b09")]
public async Task GetStorageAt_ShouldWorkAsync(string storageKeyHex)
{
var blockHash = await GivenBlockAsync();
var storageKeys = Utils.HexToByteArray(storageKeyHex);

var call_1 = await _substrateClient.State.GetStorageAsync(storageKeys, blockHash, CancellationToken.None);
var call_2 = await _substrateClient.State.GetStorageAsync(storageKeys, Utils.Bytes2HexString(blockHash), CancellationToken.None);

Assert.That(call_1, Is.Not.Null);
Assert.That(call_2, Is.Not.Null);
Assert.That(call_1, Is.EqualTo(call_2));
}

}
}
8 changes: 7 additions & 1 deletion Substrate.NetApi.TestNode/NodeTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ public abstract class NodeTest

protected SubstrateClient _substrateClient;

public string Url { get; }

protected NodeTest(string url = null)
{
Url = url ?? WebSocketUrl;
}

[SetUp]
public async Task ConnectAsync()
Expand All @@ -33,7 +39,7 @@ public async Task CloseAsync()
[OneTimeSetUp]
public void CreateClient()
{
_substrateClient = new SubstrateClient(new Uri(WebSocketUrl), ChargeTransactionPayment.Default());
_substrateClient = new SubstrateClient(new Uri(Url), ChargeTransactionPayment.Default());
}

[OneTimeTearDown]
Expand Down
14 changes: 11 additions & 3 deletions Substrate.NetApi/Model/Extrinsics/ChargeAssetTxPayment.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using Substrate.NetApi.Model.Types.Base;
using Substrate.NetApi.Model.Types.Primitive;

Expand Down Expand Up @@ -29,11 +30,18 @@ public enum NativeOrWithId
/// <summary>
/// Asset Identifier for AssetHubs and the Ajuna Chain.
/// </summary>
public sealed class EnumNativeOrWithId : BaseEnumExt<NativeOrWithId, BaseVoid, U32>
public sealed class EnumNativeOrWithId : BaseEnumRust<NativeOrWithId>
{
/// <summary>
/// EnumNativeOrWithId Constructor
/// </summary>
public EnumNativeOrWithId() : base(new Dictionary<NativeOrWithId, Type>
{
{ NativeOrWithId.Native, typeof(BaseVoid) },
{ NativeOrWithId.WithId, typeof(U32) }
}) { }
}


/// <summary>
/// Charge Asset Tx Payment
/// </summary>
Expand Down
Loading
Loading