diff --git a/net/src/Substrate.Gear.Client/Model/Types/Base/BaseNonZero.cs b/net/src/Substrate.Gear.Client/Model/Types/Base/BaseNonZero.cs new file mode 100644 index 00000000..25761f57 --- /dev/null +++ b/net/src/Substrate.Gear.Client/Model/Types/Base/BaseNonZero.cs @@ -0,0 +1,52 @@ +using System; +using System.Diagnostics.CodeAnalysis; +using Substrate.NetApi.Model.Types.Base; + +namespace Substrate.Gear.Client.Model.Types.Base; + +public class BaseNonZero : BaseType + where T : BaseType, new() +{ + public static explicit operator BaseNonZero(T value) => new(value); + + /// + /// >> value + /// + public required T Value { get; set; } + + public BaseNonZero() + { + } + + [SetsRequiredMembers] + public BaseNonZero(T value) + { + var span = value.Bytes.AsSpan(); + if (span.IsZero()) + { + throw new InvalidOperationException($"Unable to create a {this.TypeName()} instance while value is zero"); + } + this.TypeSize = value.TypeSize; + this.Bytes = span.ToArray(); + this.Value = value; + } + + /// + public override byte[] Encode() => this.Value.Encode(); + + /// + public override void Decode(byte[] byteArray, ref int p) + { + var start = p; + this.Value = new(); + this.Value.Decode(byteArray, ref p); + var bytesLength = p - start; + if (byteArray.AsSpan().Slice(p, bytesLength).IsZero()) + { + throw new InvalidOperationException($"Unable to create a {this.TypeName()} instance while value is zero"); + } + this.TypeSize = bytesLength; + this.Bytes = new byte[bytesLength]; + Array.Copy(byteArray, start, this.Bytes, 0, bytesLength); + } +} diff --git a/net/src/Substrate.Gear.Client/Model/Types/Base/BaseResult.cs b/net/src/Substrate.Gear.Client/Model/Types/Base/BaseResult.cs new file mode 100644 index 00000000..ab94434d --- /dev/null +++ b/net/src/Substrate.Gear.Client/Model/Types/Base/BaseResult.cs @@ -0,0 +1,39 @@ +using Substrate.NetApi.Model.Types; +using Substrate.NetApi.Model.Types.Base; + +namespace Substrate.Gear.Client.Model.Types.Base; + +/// +/// Result +/// +public enum BaseResult +{ + + /// + /// >> Ok + /// + Ok = 0, + + /// + /// >> Err + /// + Err = 1, +} + +/// +/// EnumResult +/// +public sealed class EnumBaseResult : BaseEnumRust + where T1 : IType, new() + where T2 : IType, new() +{ + + /// + /// Initializes a new instance of the class. + /// + public EnumBaseResult() + { + this.AddTypeDecoder(BaseResult.Ok); + this.AddTypeDecoder(BaseResult.Err); + } +} diff --git a/net/src/Substrate.Gear.Client/Model/Types/Base/SpanExtensions.cs b/net/src/Substrate.Gear.Client/Model/Types/Base/SpanExtensions.cs new file mode 100644 index 00000000..2e862135 --- /dev/null +++ b/net/src/Substrate.Gear.Client/Model/Types/Base/SpanExtensions.cs @@ -0,0 +1,22 @@ +using System; + +namespace Substrate.Gear.Client.Model.Types.Base; + +internal static class SpanExtensions +{ + + /// + /// Returns true if all bytes are zero + /// + /// + /// + public static bool IsZero(this Span bytes) + { + byte sum = 0; + foreach (var b in bytes) + { + sum |= b; + } + return sum == 0; + } +} diff --git a/net/src/Substrate.Gear.Client/Model/Types/Primitive/H160.cs b/net/src/Substrate.Gear.Client/Model/Types/Primitive/H160.cs new file mode 100644 index 00000000..fa08e6bf --- /dev/null +++ b/net/src/Substrate.Gear.Client/Model/Types/Primitive/H160.cs @@ -0,0 +1,37 @@ +using System; +using Substrate.Gear.Api.Generated.Types.Base; +using Substrate.NetApi.Attributes; +using Substrate.NetApi.Model.Types.Base; +using Substrate.NetApi.Model.Types.Metadata.Base; + +namespace Substrate.Gear.Client.Model.Types.Primitive; + +/// +/// H160 +/// +[SubstrateNodeType(TypeDefEnum.Composite)] +public sealed class H160 : BaseType +{ + /// + /// >> value + /// + public required Arr20U8 Value { get; set; } + + /// + public override string TypeName() => nameof(H160); + + /// + public override byte[] Encode() => this.Value.Encode(); + + /// + public override void Decode(byte[] byteArray, ref int p) + { + var start = p; + this.Value = new(); + this.Value.Decode(byteArray, ref p); + var bytesLength = p - start; + this.TypeSize = bytesLength; + this.Bytes = new byte[bytesLength]; + Array.Copy(byteArray, start, this.Bytes, 0, bytesLength); + } +} diff --git a/net/src/Substrate.Gear.Client/Model/Types/Primitive/NonZeroU128.cs b/net/src/Substrate.Gear.Client/Model/Types/Primitive/NonZeroU128.cs new file mode 100644 index 00000000..ae22530a --- /dev/null +++ b/net/src/Substrate.Gear.Client/Model/Types/Primitive/NonZeroU128.cs @@ -0,0 +1,16 @@ +using Substrate.Gear.Client.Model.Types.Base; +using Substrate.NetApi.Attributes; +using Substrate.NetApi.Model.Types.Metadata.Base; +using Substrate.NetApi.Model.Types.Primitive; + +namespace Substrate.Gear.Client.Model.Types.Primitive; + +/// +/// NonZeroU128 +/// +[SubstrateNodeType(TypeDefEnum.Composite)] +public sealed class NonZeroU128 : BaseNonZero +{ + /// + public override string TypeName() => nameof(NonZeroU128); +} diff --git a/net/src/Substrate.Gear.Client/Model/Types/Primitive/NonZeroU16.cs b/net/src/Substrate.Gear.Client/Model/Types/Primitive/NonZeroU16.cs new file mode 100644 index 00000000..02008ed7 --- /dev/null +++ b/net/src/Substrate.Gear.Client/Model/Types/Primitive/NonZeroU16.cs @@ -0,0 +1,16 @@ +using Substrate.Gear.Client.Model.Types.Base; +using Substrate.NetApi.Attributes; +using Substrate.NetApi.Model.Types.Metadata.Base; +using Substrate.NetApi.Model.Types.Primitive; + +namespace Substrate.Gear.Client.Model.Types.Primitive; + +/// +/// NonZeroU16 +/// +[SubstrateNodeType(TypeDefEnum.Composite)] +public sealed class NonZeroU16 : BaseNonZero +{ + /// + public override string TypeName() => nameof(NonZeroU16); +} diff --git a/net/src/Substrate.Gear.Client/Model/Types/Primitive/NonZeroU256.cs b/net/src/Substrate.Gear.Client/Model/Types/Primitive/NonZeroU256.cs new file mode 100644 index 00000000..e16b32fa --- /dev/null +++ b/net/src/Substrate.Gear.Client/Model/Types/Primitive/NonZeroU256.cs @@ -0,0 +1,16 @@ +using Substrate.Gear.Client.Model.Types.Base; +using Substrate.NetApi.Attributes; +using Substrate.NetApi.Model.Types.Metadata.Base; +using Substrate.NetApi.Model.Types.Primitive; + +namespace Substrate.Gear.Client.Model.Types.Primitive; + +/// +/// NonZeroU256 +/// +[SubstrateNodeType(TypeDefEnum.Composite)] +public sealed class NonZeroU256 : BaseNonZero +{ + /// + public override string TypeName() => nameof(NonZeroU256); +} diff --git a/net/src/Substrate.Gear.Client/Model/Types/Primitive/NonZeroU64.cs b/net/src/Substrate.Gear.Client/Model/Types/Primitive/NonZeroU64.cs new file mode 100644 index 00000000..de3b5ac1 --- /dev/null +++ b/net/src/Substrate.Gear.Client/Model/Types/Primitive/NonZeroU64.cs @@ -0,0 +1,16 @@ +using Substrate.Gear.Client.Model.Types.Base; +using Substrate.NetApi.Attributes; +using Substrate.NetApi.Model.Types.Metadata.Base; +using Substrate.NetApi.Model.Types.Primitive; + +namespace Substrate.Gear.Client.Model.Types.Primitive; + +/// +/// NonZeroU64 +/// +[SubstrateNodeType(TypeDefEnum.Composite)] +public sealed class NonZeroU64 : BaseNonZero +{ + /// + public override string TypeName() => nameof(NonZeroU64); +} diff --git a/net/src/Substrate.Gear.Client/Model/Types/Primitive/NonZeroU8.cs b/net/src/Substrate.Gear.Client/Model/Types/Primitive/NonZeroU8.cs new file mode 100644 index 00000000..59439d9b --- /dev/null +++ b/net/src/Substrate.Gear.Client/Model/Types/Primitive/NonZeroU8.cs @@ -0,0 +1,16 @@ +using Substrate.Gear.Client.Model.Types.Base; +using Substrate.NetApi.Attributes; +using Substrate.NetApi.Model.Types.Metadata.Base; +using Substrate.NetApi.Model.Types.Primitive; + +namespace Substrate.Gear.Client.Model.Types.Primitive; + +/// +/// NonZeroU8 +/// +[SubstrateNodeType(TypeDefEnum.Composite)] +public sealed class NonZeroU8 : BaseNonZero +{ + /// + public override string TypeName() => nameof(NonZeroU8); +}