From c773ec5db5d9658ad9649bbdc36dcb6e1d185f95 Mon Sep 17 00:00:00 2001 From: Octavian Axente Date: Sun, 30 Apr 2023 19:53:37 +0300 Subject: [PATCH 1/5] Blocks endpoints added --- src/Mx.NET.SDK/Domain/Data/Block/Block.cs | 150 ++++++++++++++++++ src/Mx.NET.SDK/Domain/Data/Block/Blocks.cs | 133 ++++++++++++++++ .../Domain/Data/Common/ProposerIdentity.cs | 56 +++++++ .../Provider/Dtos/API/Block/BlockDto.cs | 28 ++++ .../Provider/Dtos/API/Block/BlocksDto.cs | 24 +++ .../Dtos/API/Block/ProposerIdentityDto.cs | 23 +++ .../Provider/IMultiversxProvider.cs | 44 +++++ src/Mx.NET.SDK/Provider/MultiversxProvider.cs | 54 +++++++ 8 files changed, 512 insertions(+) create mode 100644 src/Mx.NET.SDK/Domain/Data/Block/Block.cs create mode 100644 src/Mx.NET.SDK/Domain/Data/Block/Blocks.cs create mode 100644 src/Mx.NET.SDK/Domain/Data/Common/ProposerIdentity.cs create mode 100644 src/Mx.NET.SDK/Provider/Dtos/API/Block/BlockDto.cs create mode 100644 src/Mx.NET.SDK/Provider/Dtos/API/Block/BlocksDto.cs create mode 100644 src/Mx.NET.SDK/Provider/Dtos/API/Block/ProposerIdentityDto.cs diff --git a/src/Mx.NET.SDK/Domain/Data/Block/Block.cs b/src/Mx.NET.SDK/Domain/Data/Block/Block.cs new file mode 100644 index 0000000..1e467e4 --- /dev/null +++ b/src/Mx.NET.SDK/Domain/Data/Block/Block.cs @@ -0,0 +1,150 @@ +using Mx.NET.SDK.Domain.Data.Common; +using Mx.NET.SDK.Domain.Helper; +using Mx.NET.SDK.Provider.Dtos.API.Block; +using System; + +namespace Mx.NET.SDK.Domain.Data.Block +{ + public class Block + { + /// + /// Block hash + /// + public string Hash { get; private set; } + + /// + /// Block epoch + /// + public int Epoch { get; private set; } + + /// + /// Block nonce + /// + public ulong Nonce { get; private set; } + + /// + /// Block previous hash + /// + public string PrevHash { get; private set; } + + /// + /// Block proposer + /// + public string Proposer { get; private set; } + + /// + /// Block proposer identity + /// + public ProposerIdentity ProposerIdentity { get; private set; } + + /// + /// Block public key bitmap + /// + public string PubKeyBitmap { get; private set; } + + /// + /// Block round + /// + public ulong Round { get; private set; } + + /// + /// Block shard + /// + public int Shard { get; private set; } + + /// + /// Block size + /// + public int Size { get; private set; } + + /// + /// Block size txs + /// + public int SizeTxs { get; private set; } + + /// + /// Block state root hash + /// + public string StateRootHash { get; private set; } + + /// + /// Block date + /// + public DateTime BlockDate { get; private set; } + + /// + /// Block transactions count + /// + public long TxCount { get; private set; } + + /// + /// Block gas consumed + /// + public ulong GasConsumed { get; private set; } + + /// + /// Block gas refunded + /// + public ulong GasRefunded { get; private set; } + + /// + /// Block gas penalized + /// + public ulong GasPenalized { get; private set; } + + /// + /// Block max gas limit + /// + public ulong MaxGasLimit { get; private set; } + + /// + /// Mini blocks hashes + /// + public string[] MiniBlocksHashes { get; private set; } + + /// + /// Notarized blocks hashes + /// + public string[] NotarizedBlocksHashes { get; private set; } + + /// + /// Block validators + /// + public string[] Validators { get; private set; } + + private Block() { } + + /// + /// Creates a new Block from data + /// + /// Block Data Object from API + /// + public static Block From(BlockDto block) + { + return new Block() + { + Hash = block.Hash, + Epoch = block.Epoch ?? 0, + Nonce = block.Nonce ?? 0, + PrevHash = block.PrevHash, + Proposer = block.Proposer, + ProposerIdentity = ProposerIdentity.From(block.ProposerIdentity), + PubKeyBitmap = block.PubKeyBitmap, + Round = block.Round ?? 0, + Shard = block.Shard ?? 0, + Size = block.Size ?? 0, + SizeTxs = block.SizeTxs ?? 0, + StateRootHash = block.StateRootHash, + BlockDate = (block.Timestamp ?? 0).ToDateTime(), + TxCount = block.TxCount ?? 0, + GasConsumed = block.GasConsumed ?? 0, + GasRefunded = block.GasRefunded ?? 0, + GasPenalized = block.GasPenalized ?? 0, + MaxGasLimit = block.MaxGasLimit ?? 0, + MiniBlocksHashes = block.MiniBlocksHashes, + NotarizedBlocksHashes = block.NotarizedBlocksHashes, + Validators = block.Validators + }; + } + } +} diff --git a/src/Mx.NET.SDK/Domain/Data/Block/Blocks.cs b/src/Mx.NET.SDK/Domain/Data/Block/Blocks.cs new file mode 100644 index 0000000..09d91a2 --- /dev/null +++ b/src/Mx.NET.SDK/Domain/Data/Block/Blocks.cs @@ -0,0 +1,133 @@ +using Mx.NET.SDK.Domain.Data.Common; +using Mx.NET.SDK.Domain.Helper; +using Mx.NET.SDK.Provider.Dtos.API.Block; +using System; +using System.Linq; + +namespace Mx.NET.SDK.Domain.Data.Block +{ + public class Blocks + { + /// + /// Block hash + /// + public string Hash { get; private set; } + + /// + /// Block epoch + /// + public int Epoch { get; private set; } + + /// + /// Block nonce + /// + public ulong Nonce { get; private set; } + + /// + /// Block previous hash + /// + public string PrevHash { get; private set; } + + /// + /// Block proposer + /// + public string Proposer { get; private set; } + + /// + /// Block proposer identity + /// + public ProposerIdentity ProposerIdentity { get; private set; } + + /// + /// Block public key bitmap + /// + public string PubKeyBitmap { get; private set; } + + /// + /// Block round + /// + public ulong Round { get; private set; } + + /// + /// Block shard + /// + public int Shard { get; private set; } + + /// + /// Block size + /// + public int Size { get; private set; } + + /// + /// Block size txs + /// + public int SizeTxs { get; private set; } + + /// + /// Block state root hash + /// + public string StateRootHash { get; private set; } + + /// + /// Block date + /// + public DateTime BlockDate { get; private set; } + + /// + /// Block transactions count + /// + public long TxCount { get; private set; } + + /// + /// Block gas consumed + /// + public ulong GasConsumed { get; private set; } + + /// + /// Block gas refunded + /// + public ulong GasRefunded { get; private set; } + + /// + /// Block gas penalized + /// + public ulong GasPenalized { get; private set; } + + /// + /// Block max gas limit + /// + public ulong MaxGasLimit { get; private set; } + + private Blocks() { } + + /// + /// Creates a new array of Blocks from data + /// + /// Array of Blocks Data Object from API + /// Array of Blocks objects + public static Blocks[] From(BlocksDto[] blocks) + { + return blocks.Select(block => new Blocks() + { + Hash = block.Hash, + Epoch = block.Epoch ?? 0, + Nonce = block.Nonce ?? 0, + PrevHash = block.PrevHash, + Proposer = block.Proposer, + ProposerIdentity = ProposerIdentity.From(block.ProposerIdentity), + PubKeyBitmap = block.PubKeyBitmap, + Round = block.Round ?? 0, + Shard = block.Shard ?? 0, + Size = block.Size ?? 0, + SizeTxs = block.SizeTxs ?? 0, + StateRootHash = block.StateRootHash, + BlockDate = (block.Timestamp ?? 0).ToDateTime(), + TxCount = block.TxCount ?? 0, + GasConsumed = block.GasConsumed ?? 0, + GasRefunded = block.GasRefunded ?? 0, + GasPenalized = block.GasPenalized ?? 0, + MaxGasLimit = block.MaxGasLimit ?? 0, + }).ToArray(); + } + } +} diff --git a/src/Mx.NET.SDK/Domain/Data/Common/ProposerIdentity.cs b/src/Mx.NET.SDK/Domain/Data/Common/ProposerIdentity.cs new file mode 100644 index 0000000..7f0a5ae --- /dev/null +++ b/src/Mx.NET.SDK/Domain/Data/Common/ProposerIdentity.cs @@ -0,0 +1,56 @@ +using Mx.NET.SDK.Provider.Dtos.API.Block; + +namespace Mx.NET.SDK.Domain.Data.Common +{ + /// + /// Proposer Identity for Block + /// + public class ProposerIdentity + { + public string Identity { get; private set; } + public string Name { get; private set; } + public string Description { get; private set; } + public string Avatar { get; private set; } + public string Website { get; private set; } + public string Twitter { get; private set; } + public string Location { get; private set; } + public double Score { get; private set; } + public int Validators { get; private set; } + public string Stake { get; private set; } + public string TopUp { get; private set; } + public string Locked { get; private set; } + public dynamic Distribution { get; private set; } + public string[] Providers { get; private set; } + public double StakePercent { get; private set; } + public double Apr { get; private set; } + public int Rank { get; private set; } + + private ProposerIdentity() { } + + public static ProposerIdentity From(ProposerIdentityDto proposerIdentity) + { + if (proposerIdentity == null) return null; + + return new ProposerIdentity() + { + Identity = proposerIdentity.Identity, + Name = proposerIdentity.Name, + Description = proposerIdentity.Description, + Avatar = proposerIdentity.Avatar, + Website = proposerIdentity.Website, + Twitter = proposerIdentity.Twitter, + Location = proposerIdentity.Location, + Score = proposerIdentity.Score ?? 0, + Validators = proposerIdentity.Validators ?? 0, + Stake = proposerIdentity.Stake, + TopUp = proposerIdentity.TopUp, + Locked = proposerIdentity.Locked, + Distribution = proposerIdentity.Distribution, + Providers = proposerIdentity.Providers, + StakePercent = proposerIdentity.StakePercent ?? 0, + Apr = proposerIdentity.Apr ?? 0, + Rank = proposerIdentity.Rank ?? 0 + }; + } + } +} diff --git a/src/Mx.NET.SDK/Provider/Dtos/API/Block/BlockDto.cs b/src/Mx.NET.SDK/Provider/Dtos/API/Block/BlockDto.cs new file mode 100644 index 0000000..13bec36 --- /dev/null +++ b/src/Mx.NET.SDK/Provider/Dtos/API/Block/BlockDto.cs @@ -0,0 +1,28 @@ +namespace Mx.NET.SDK.Provider.Dtos.API.Block +{ + public class BlockDto + { + public string Hash { get; set; } + public int? Epoch { get; set; } + public ulong? Nonce { get; set; } + public string PrevHash { get; set; } + public string Proposer { get; set; } + public ProposerIdentityDto ProposerIdentity { get; set; } + public string PubKeyBitmap { get; set; } + public ulong? Round { get; set; } + public int? Shard { get; set; } + public int? Size { get; set; } + public int? SizeTxs { get; set; } + public string StateRootHash { get; set; } + public long? Timestamp { get; set; } + public long? TxCount { get; set; } + public ulong? GasConsumed { get; set; } + public ulong? GasRefunded { get; set; } + public ulong? GasPenalized { get; set; } + public ulong? MaxGasLimit { get; set; } + public string[] MiniBlocksHashes { get; set; } + public string[] NotarizedBlocksHashes { get; set; } + public string[] Validators { get; set; } + + } +} diff --git a/src/Mx.NET.SDK/Provider/Dtos/API/Block/BlocksDto.cs b/src/Mx.NET.SDK/Provider/Dtos/API/Block/BlocksDto.cs new file mode 100644 index 0000000..f183889 --- /dev/null +++ b/src/Mx.NET.SDK/Provider/Dtos/API/Block/BlocksDto.cs @@ -0,0 +1,24 @@ +namespace Mx.NET.SDK.Provider.Dtos.API.Block +{ + public class BlocksDto + { + public string Hash { get; set; } + public int? Epoch { get; set; } + public ulong? Nonce { get; set; } + public string PrevHash { get; set; } + public string Proposer { get; set; } + public ProposerIdentityDto ProposerIdentity { get; set; } + public string PubKeyBitmap { get; set; } + public ulong? Round { get; set; } + public int? Shard { get; set; } + public int? Size { get; set; } + public int? SizeTxs { get; set; } + public string StateRootHash { get; set; } + public long? Timestamp { get; set; } + public long? TxCount { get; set; } + public ulong? GasConsumed { get; set; } + public ulong? GasRefunded { get; set; } + public ulong? GasPenalized { get; set; } + public ulong? MaxGasLimit { get; set; } + } +} diff --git a/src/Mx.NET.SDK/Provider/Dtos/API/Block/ProposerIdentityDto.cs b/src/Mx.NET.SDK/Provider/Dtos/API/Block/ProposerIdentityDto.cs new file mode 100644 index 0000000..a523f6b --- /dev/null +++ b/src/Mx.NET.SDK/Provider/Dtos/API/Block/ProposerIdentityDto.cs @@ -0,0 +1,23 @@ +namespace Mx.NET.SDK.Provider.Dtos.API.Block +{ + public class ProposerIdentityDto + { + public string Identity { get; set; } + public string Name { get; set; } + public string Description { get; set; } + public string Avatar { get; set; } + public string Website { get; set; } + public string Twitter { get; set; } + public string Location { get; set; } + public double? Score { get; set; } + public int? Validators { get; set; } + public string Stake { get; set; } + public string TopUp { get; set; } + public string Locked { get; set; } + public dynamic Distribution { get; set; } + public string[] Providers { get; set; } + public double? StakePercent { get; set; } + public double? Apr { get; set; } + public int? Rank { get; set; } + } +} diff --git a/src/Mx.NET.SDK/Provider/IMultiversxProvider.cs b/src/Mx.NET.SDK/Provider/IMultiversxProvider.cs index 632d32c..fe4c501 100644 --- a/src/Mx.NET.SDK/Provider/IMultiversxProvider.cs +++ b/src/Mx.NET.SDK/Provider/IMultiversxProvider.cs @@ -1,4 +1,5 @@ using Mx.NET.SDK.Provider.Dtos.API.Account; +using Mx.NET.SDK.Provider.Dtos.API.Block; using Mx.NET.SDK.Provider.Dtos.API.Collection; using Mx.NET.SDK.Provider.Dtos.API.Common; using Mx.NET.SDK.Provider.Dtos.API.Exchange; @@ -292,6 +293,49 @@ public interface IMultiversxProvider #endregion + #region blocks + + /// + /// Returns an array of Blocks + /// + /// Number of items to retrieve (max 10000) + /// Number of items to skip for the result set + /// Parameters for query + /// + Task GetBlocks(int size = 25, int from = 0, Dictionary parameters = null); + + /// + /// Returns an array of Blocks + /// + /// Number of items to retrieve (max 10000) + /// Number of items to skip for the result set + /// Parameters for query + /// + Task GetBlocksCustom(int size = 25, int from = 0, Dictionary parameters = null); + + /// + /// Returns the counter of Blocks + /// + /// Parameters for query + /// + Task GetBlocksCount(Dictionary parameters = null); + + /// + /// Returns a Block + /// + /// Block Hash + /// + Task GetBlock(string blockHash); + + /// + /// Returns a Block + /// + /// Block Hash + /// + Task GetBlockCustom(string blockHash); + + #endregion + #region collections /// diff --git a/src/Mx.NET.SDK/Provider/MultiversxProvider.cs b/src/Mx.NET.SDK/Provider/MultiversxProvider.cs index ed3f1db..baa7e7f 100644 --- a/src/Mx.NET.SDK/Provider/MultiversxProvider.cs +++ b/src/Mx.NET.SDK/Provider/MultiversxProvider.cs @@ -19,6 +19,7 @@ using Mx.NET.SDK.Core.Domain.Constants; using Mx.NET.SDK.Provider.Dtos.Gateway.Query; using System.Net; +using Mx.NET.SDK.Provider.Dtos.API.Block; namespace Mx.NET.SDK.Provider { @@ -396,6 +397,59 @@ public async Task GetAccountHistoryToken(string addres #endregion + #region blocks + + public async Task GetBlocks(int size = 25, int from = 0, Dictionary parameters = null) + { + return await GetBlocksCustom(size, from, parameters); + } + + public async Task GetBlocksCustom(int size = 25, int from = 0, Dictionary parameters = null) + { + size = size > 10000 ? 10000 : size; + string args = ""; + if (parameters != null) + args = $"&{string.Join("&", parameters.Select(e => $"{e.Key}={e.Value}"))}"; + var response = await _httpAPIClient.GetAsync($"blocks?from={from}&size={size}{args}"); + var content = await response.Content.ReadAsStringAsync(); + if (!response.IsSuccessStatusCode) + throw new APIException(JsonWrapper.Deserialize(content)); + + var result = JsonWrapper.Deserialize(content); + return result; + } + + public async Task GetBlocksCount(Dictionary parameters = null) + { + string args = ""; + if (parameters != null) + args = $"?{string.Join("&", parameters.Select(e => $"{e.Key}={e.Value}"))}"; + var response = await _httpAPIClient.GetAsync($"blocks/count{args}"); + var content = await response.Content.ReadAsStringAsync(); + if (!response.IsSuccessStatusCode) + throw new APIException(JsonWrapper.Deserialize(content)); + + return content; + } + + public async Task GetBlock(string blockHash) + { + return await GetBlockCustom(blockHash); + } + + public async Task GetBlockCustom(string blockHash) + { + var response = await _httpAPIClient.GetAsync($"blocks/{blockHash}"); + var content = await response.Content.ReadAsStringAsync(); + if (!response.IsSuccessStatusCode) + throw new APIException(JsonWrapper.Deserialize(content)); + + var result = JsonWrapper.Deserialize(content); + return result; + } + + #endregion + #region collections public async Task GetCollections(int size = 100, int from = 0, Dictionary parameters = null) From 3f4c7245dc89ecf6772578865cf6dad3cd24fc58 Mon Sep 17 00:00:00 2001 From: Octavian Axente Date: Sun, 30 Apr 2023 19:54:01 +0300 Subject: [PATCH 2/5] ToTimestamp method added --- .../Domain/Helper/TimestampConverter.cs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/Mx.NET.SDK/Domain/Helper/TimestampConverter.cs b/src/Mx.NET.SDK/Domain/Helper/TimestampConverter.cs index d82e73b..df00010 100644 --- a/src/Mx.NET.SDK/Domain/Helper/TimestampConverter.cs +++ b/src/Mx.NET.SDK/Domain/Helper/TimestampConverter.cs @@ -4,10 +4,26 @@ namespace Mx.NET.SDK.Domain.Helper { public static class TimestampConverter { + /// + /// Converts the timestamp to datetime + /// + /// + /// UTC datetime public static DateTime ToDateTime(this long timestamp) { DateTime dateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc); return dateTime.AddSeconds(timestamp).ToUniversalTime(); } + + /// + /// Converts the datetime to timestamp + /// + /// UTC DateTime + /// timestamp + public static long ToTimestamp(this DateTime dateTime) + { + DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc); + return (long)dateTime.Subtract(origin).TotalSeconds; + } } } From f2b0001c2fac17f648404383d6cb982463c173ef Mon Sep 17 00:00:00 2001 From: Octavian Axente Date: Sun, 30 Apr 2023 19:54:20 +0300 Subject: [PATCH 3/5] summary correction --- src/Mx.NET.SDK/Domain/Data/NFT/NFT.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Mx.NET.SDK/Domain/Data/NFT/NFT.cs b/src/Mx.NET.SDK/Domain/Data/NFT/NFT.cs index 946b48b..a619b2c 100644 --- a/src/Mx.NET.SDK/Domain/Data/NFT/NFT.cs +++ b/src/Mx.NET.SDK/Domain/Data/NFT/NFT.cs @@ -155,7 +155,7 @@ public static NFT From(NFTDto nft) /// /// Creates a new array of NFTs from data - /// a + /// /// Array of NFT Data Object from API /// Array of NFT objects public static NFT[] From(NFTDto[] nfts) From 97952bfe1e82f521434198c755e82b7f352fe3de Mon Sep 17 00:00:00 2001 From: Octavian Axente Date: Sun, 30 Apr 2023 19:54:42 +0300 Subject: [PATCH 4/5] nuget package++ --- src/Mx.NET.SDK/Mx.NET.SDK.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Mx.NET.SDK/Mx.NET.SDK.csproj b/src/Mx.NET.SDK/Mx.NET.SDK.csproj index 3a6c15c..c3cab1e 100644 --- a/src/Mx.NET.SDK/Mx.NET.SDK.csproj +++ b/src/Mx.NET.SDK/Mx.NET.SDK.csproj @@ -11,7 +11,7 @@ https://github.com/RemarkableTools/Mx.NET.SDK GitHub Remarkable Tools - 1.0.16 + 1.0.17 true RemarkableTools.Mx README.md From 6cf5b3a5f641bc63d37fd380730d9b5678a83b14 Mon Sep 17 00:00:00 2001 From: Octavian Axente Date: Sun, 30 Apr 2023 20:03:09 +0300 Subject: [PATCH 5/5] blockdate renamed to creationdate --- src/Mx.NET.SDK/Domain/Data/Block/Block.cs | 6 +++--- src/Mx.NET.SDK/Domain/Data/Block/Blocks.cs | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Mx.NET.SDK/Domain/Data/Block/Block.cs b/src/Mx.NET.SDK/Domain/Data/Block/Block.cs index 1e467e4..a39a008 100644 --- a/src/Mx.NET.SDK/Domain/Data/Block/Block.cs +++ b/src/Mx.NET.SDK/Domain/Data/Block/Block.cs @@ -68,9 +68,9 @@ public class Block public string StateRootHash { get; private set; } /// - /// Block date + /// Block creation date /// - public DateTime BlockDate { get; private set; } + public DateTime CreationDate { get; private set; } /// /// Block transactions count @@ -135,7 +135,7 @@ public static Block From(BlockDto block) Size = block.Size ?? 0, SizeTxs = block.SizeTxs ?? 0, StateRootHash = block.StateRootHash, - BlockDate = (block.Timestamp ?? 0).ToDateTime(), + CreationDate = (block.Timestamp ?? 0).ToDateTime(), TxCount = block.TxCount ?? 0, GasConsumed = block.GasConsumed ?? 0, GasRefunded = block.GasRefunded ?? 0, diff --git a/src/Mx.NET.SDK/Domain/Data/Block/Blocks.cs b/src/Mx.NET.SDK/Domain/Data/Block/Blocks.cs index 09d91a2..0bb0592 100644 --- a/src/Mx.NET.SDK/Domain/Data/Block/Blocks.cs +++ b/src/Mx.NET.SDK/Domain/Data/Block/Blocks.cs @@ -69,9 +69,9 @@ public class Blocks public string StateRootHash { get; private set; } /// - /// Block date + /// Block creation date /// - public DateTime BlockDate { get; private set; } + public DateTime CreationDate { get; private set; } /// /// Block transactions count @@ -121,7 +121,7 @@ public static Blocks[] From(BlocksDto[] blocks) Size = block.Size ?? 0, SizeTxs = block.SizeTxs ?? 0, StateRootHash = block.StateRootHash, - BlockDate = (block.Timestamp ?? 0).ToDateTime(), + CreationDate = (block.Timestamp ?? 0).ToDateTime(), TxCount = block.TxCount ?? 0, GasConsumed = block.GasConsumed ?? 0, GasRefunded = block.GasRefunded ?? 0,