diff --git a/CHANGELOG.md b/CHANGELOG.md index aef0a95..b17de77 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,12 @@ All notable changes to this project will be documented in this file. The format [comment]: <> (Fixed: any bug fixes) [comment]: <> (Security: in case of vulnerabilities) +## [1.1.2] + +### Added +* New RPC method `GetEraSummary()`. +* New global state key `era-summary-0000...0000`. + ## [1.1.1] ### Fixed @@ -33,6 +39,7 @@ All notable changes to this project will be documented in this file. The format ### Added * Initial release of Casper .NET SDK. +[1.1.2]: https://github.com/make-software/casper-net-sdk/releases/tag/v1.1.2 [1.1.1]: https://github.com/make-software/casper-net-sdk/releases/tag/v1.1.1 [1.1.0]: https://github.com/make-software/casper-net-sdk/releases/tag/v1.1.0 [1.0.0]: https://github.com/make-software/casper-net-sdk/releases/tag/v1.0.0 diff --git a/Casper.Network.SDK.Test/NctlContractTest.cs b/Casper.Network.SDK.Test/NctlContractTest.cs index 08610c6..f5e4349 100644 --- a/Casper.Network.SDK.Test/NctlContractTest.cs +++ b/Casper.Network.SDK.Test/NctlContractTest.cs @@ -88,7 +88,7 @@ public async Task CallContractByNameTest() "counter_inc", null, _faucetKey.PublicKey, - 15_000_000, + 150_000_000, _chainName, 1, //gasPrice=1 24*3_600_000); //ttl='1day' @@ -115,7 +115,7 @@ public async Task CallContractByHashTest() "counter_inc", null, _faucetKey.PublicKey, - 15_000_000, + 150_000_000, _chainName); deploy.Sign(_faucetKey); @@ -205,4 +205,4 @@ public async Task GetItemTest() Assert.IsTrue((int)count > 0); } } -} \ No newline at end of file +} diff --git a/Casper.Network.SDK.Test/NctlGetXTest.cs b/Casper.Network.SDK.Test/NctlGetXTest.cs index 25e8ecc..73fbeb1 100644 --- a/Casper.Network.SDK.Test/NctlGetXTest.cs +++ b/Casper.Network.SDK.Test/NctlGetXTest.cs @@ -173,6 +173,39 @@ public async Task GetAuctionInfoTest() Assert.Fail(e.RpcError.Message); } } + + [Test] + public async Task GetEraSummaryTest() + { + try + { + var response = await _client.GetEraSummary(); + var result = response.Parse(); + Assert.IsTrue(result.EraSummary.EraId > 0); + Assert.IsNotNull(result.EraSummary.StoredValue.EraInfo); + Assert.IsTrue(result.EraSummary.StoredValue.EraInfo.SeigniorageAllocations.Count > 0); + + var response2 = await _client.GetEraSummary(result.EraSummary.BlockHash); + var result2 = response2.Parse(); + Assert.IsTrue(result2.EraSummary.EraId > 0); + Assert.IsNotNull(result2.EraSummary.StoredValue.EraInfo); + Assert.IsTrue(result2.EraSummary.StoredValue.EraInfo.SeigniorageAllocations.Count > 0); + + var response3 = await _client.GetBlock(result.EraSummary.BlockHash); + var result3 = response3.Parse(); + Assert.IsNotNull(result3.Block.Hash); + + var response4 = await _client.GetEraSummary((int)result3.Block.Header.Height); + var result4 = response4.Parse(); + Assert.IsTrue(result4.EraSummary.EraId > 0); + Assert.IsNotNull(result4.EraSummary.StoredValue.EraInfo); + Assert.IsTrue(result4.EraSummary.StoredValue.EraInfo.SeigniorageAllocations.Count > 0); + } + catch (RpcClientException e) + { + Assert.Fail(e.RpcError.Message); + } + } [Test] public async Task GetValidatorChangesTest() @@ -206,4 +239,4 @@ public async Task GetRpcSchemaTest() } } } -} \ No newline at end of file +} diff --git a/Casper.Network.SDK.Test/NctlMyDictContractTest.cs b/Casper.Network.SDK.Test/NctlMyDictContractTest.cs index 8c4ce75..f3513ad 100644 --- a/Casper.Network.SDK.Test/NctlMyDictContractTest.cs +++ b/Casper.Network.SDK.Test/NctlMyDictContractTest.cs @@ -70,7 +70,7 @@ public async Task CallVersionedContractByNameTest() "store_mydict", namedArgs, _faucetKey.PublicKey, - 100_000_000, + 1_000_000_000, _chainName); deploy.Sign(_faucetKey); @@ -176,4 +176,4 @@ public async Task GetDictItemByKey() Assert.AreEqual("second-entry", (string) result.StoredValue.CLValue); } } -} \ No newline at end of file +} diff --git a/Casper.Network.SDK.Test/NctlQueryGlobalStateTest.cs b/Casper.Network.SDK.Test/NctlQueryGlobalStateTest.cs index 7553c6c..799d144 100644 --- a/Casper.Network.SDK.Test/NctlQueryGlobalStateTest.cs +++ b/Casper.Network.SDK.Test/NctlQueryGlobalStateTest.cs @@ -33,6 +33,16 @@ public async Task QueryURef() Assert.IsNotNull(clValue); } + [Test] + public async Task QueryEraSummary() + { + var rpcResponse = await _client.QueryGlobalState("era-summary-0000000000000000000000000000000000000000000000000000000000000000"); + var eraInfo = rpcResponse.Parse().StoredValue.EraInfo; + Assert.IsNotNull(eraInfo); + Assert.IsNotNull(eraInfo.SeigniorageAllocations); + Assert.IsTrue(eraInfo.SeigniorageAllocations.Count > 0); + } + [Test] public async Task QueryWrongKey() { @@ -51,4 +61,4 @@ public async Task QueryWrongKey() } } } -} \ No newline at end of file +} diff --git a/Casper.Network.SDK.Test/NctlTransferTest.cs b/Casper.Network.SDK.Test/NctlTransferTest.cs index f5a2e91..9b231c9 100644 --- a/Casper.Network.SDK.Test/NctlTransferTest.cs +++ b/Casper.Network.SDK.Test/NctlTransferTest.cs @@ -147,7 +147,7 @@ public async Task DelegateTokens() _myAccount.PublicKey, validatorPk, 600_000_000_000, - 5_000_000_000, + 10_000_000_000, _chainName); deploy.Sign(_myAccount); @@ -175,7 +175,7 @@ public async Task UndelegateTokens() _myAccount.PublicKey, validatorPk, 10_000_000_000, - 5_000_000_000, + 10_000_000_000, _chainName); deploy.Sign(_myAccount); @@ -190,4 +190,4 @@ public async Task UndelegateTokens() Assert.IsTrue(execResult.IsSuccess); } } -} \ No newline at end of file +} diff --git a/Casper.Network.SDK.Test/TestData/delegate.wasm b/Casper.Network.SDK.Test/TestData/delegate.wasm index 1d46db5..5cae44c 100644 Binary files a/Casper.Network.SDK.Test/TestData/delegate.wasm and b/Casper.Network.SDK.Test/TestData/delegate.wasm differ diff --git a/Casper.Network.SDK.Test/TestData/undelegate.wasm b/Casper.Network.SDK.Test/TestData/undelegate.wasm index 322bfa5..82ae947 100644 Binary files a/Casper.Network.SDK.Test/TestData/undelegate.wasm and b/Casper.Network.SDK.Test/TestData/undelegate.wasm differ diff --git a/Casper.Network.SDK/Casper.Network.SDK.csproj b/Casper.Network.SDK/Casper.Network.SDK.csproj index 699e5d0..c578cc0 100644 --- a/Casper.Network.SDK/Casper.Network.SDK.csproj +++ b/Casper.Network.SDK/Casper.Network.SDK.csproj @@ -2,9 +2,9 @@ net5.0 - 1.1.1.0 - 1.1.1 - 1.1.1 + 1.1.2.0 + 1.1.2 + 1.1.2 Casper.Network.SDK make-software https://github.com/make-software/casper-net-sdk diff --git a/Casper.Network.SDK/JsonRpc/CasperMethods.cs b/Casper.Network.SDK/JsonRpc/CasperMethods.cs index 0834a1d..228b177 100644 --- a/Casper.Network.SDK/JsonRpc/CasperMethods.cs +++ b/Casper.Network.SDK/JsonRpc/CasperMethods.cs @@ -241,6 +241,25 @@ public GetEraInfoBySwitchBlock(int height) : base("chain_get_era_info_by_switch_ } } + public class GetEraSummary : RpcMethod + { + /// + /// Retrieves current era info from the network given a block hash + /// + /// Block hash. + public GetEraSummary(string blockHash) : base("chain_get_era_summary", blockHash) + { + } + + /// + /// Retrieves current era info from the network given a block height + /// + /// Block height. + public GetEraSummary(int height) : base("chain_get_era_summary", height) + { + } + } + public class GetDictionaryItem : RpcMethod { /// diff --git a/Casper.Network.SDK/JsonRpc/ResultTypes/GetEraSummaryResult.cs b/Casper.Network.SDK/JsonRpc/ResultTypes/GetEraSummaryResult.cs new file mode 100644 index 0000000..ba6821a --- /dev/null +++ b/Casper.Network.SDK/JsonRpc/ResultTypes/GetEraSummaryResult.cs @@ -0,0 +1,17 @@ +using System.Text.Json.Serialization; +using Casper.Network.SDK.Types; + +namespace Casper.Network.SDK.JsonRpc.ResultTypes +{ + /// + /// Result for "chain_get_era_summary" RPC response. + /// + public class GetEraSummaryResult : RpcResult + { + /// + /// The Era summary. + /// + [JsonPropertyName("era_summary")] + public EraSummary EraSummary { get; init; } + } +} diff --git a/Casper.Network.SDK/NetCasperClient.cs b/Casper.Network.SDK/NetCasperClient.cs index 1be6386..15dffb3 100644 --- a/Casper.Network.SDK/NetCasperClient.cs +++ b/Casper.Network.SDK/NetCasperClient.cs @@ -391,6 +391,26 @@ public async Task> GetEraInfoBySwitch var method = new GetEraInfoBySwitchBlock(blockHeight); return await SendRpcRequestAsync(method); } + + /// + /// Request current Era Info from the network given a block hash + /// + /// Block hash. Null for the latest block. + public async Task> GetEraSummary(string blockHash = null) + { + var method = new GetEraSummary(blockHash); + return await SendRpcRequestAsync(method); + } + + /// + /// Request current Era Info from the network given a block hash + /// + /// Block height. + public async Task> GetEraSummary(int blockHeight) + { + var method = new GetEraSummary(blockHeight); + return await SendRpcRequestAsync(method); + } /// /// Lookup a dictionary item from its dictionary item key. @@ -533,4 +553,4 @@ protected virtual void Dispose(bool disposing) } } } -} \ No newline at end of file +} diff --git a/Casper.Network.SDK/Types/GlobalStateKey.cs b/Casper.Network.SDK/Types/GlobalStateKey.cs index 1309bb6..50757e8 100644 --- a/Casper.Network.SDK/Types/GlobalStateKey.cs +++ b/Casper.Network.SDK/Types/GlobalStateKey.cs @@ -53,7 +53,11 @@ public enum KeyIdentifier /// /// Dictionary keys store dictionary items. /// - Dictionary = 0x09 + Dictionary = 0x09, + /// + /// Era Summary keys store current era info. + /// + EraSummary = 0x11, } /// @@ -127,7 +131,8 @@ public static GlobalStateKey FromString(string value) return new WithdrawKey(value); if (value.StartsWith("dictionary")) return new DictionaryKey(value); - + if (value.StartsWith("era-summary-")) + return new EraSummaryKey(value); throw new ArgumentException($"Key not valid. Unknown key prefix."); } @@ -401,4 +406,16 @@ public DictionaryKey(byte[] key) : this("dictionary-" + CEP57Checksum.Encode(key { } } -} \ No newline at end of file + + /// + /// Key used to query current era info. + /// Format: 32-byte length with prefix 'era-summary-'. + /// + public class EraSummaryKey : GlobalStateKey + { + public EraSummaryKey(string key) : base(key, "era-summary-") + { + KeyIdentifier = KeyIdentifier.EraSummary; + } + } +} diff --git a/Casper.Network.SDK/Types/SeigniorageAllocation.cs b/Casper.Network.SDK/Types/SeigniorageAllocation.cs index d86518b..95c487e 100644 --- a/Casper.Network.SDK/Types/SeigniorageAllocation.cs +++ b/Casper.Network.SDK/Types/SeigniorageAllocation.cs @@ -86,8 +86,33 @@ public override void Write( SeigniorageAllocation value, JsonSerializerOptions options) { - throw new NotImplementedException("Write method for SeigniorageAllocation not yet implemented"); + if (value.IsDelegator) + { + writer.WriteStartObject(); + writer.WritePropertyName("Delegator"); + writer.WriteStartObject(); + writer.WritePropertyName("delegator_public_key"); + writer.WriteStringValue(value.DelegatorPublicKey.ToString()); + writer.WritePropertyName("validator_public_key"); + writer.WriteStringValue(value.ValidatorPublicKey.ToString()); + writer.WritePropertyName("amount"); + writer.WriteStringValue(value.Amount.ToString()); + writer.WriteEndObject(); + writer.WriteEndObject(); + } + else //validator + { + writer.WriteStartObject(); + writer.WritePropertyName("Validator"); + writer.WriteStartObject(); + writer.WritePropertyName("validator_public_key"); + writer.WriteStringValue(value.ValidatorPublicKey.ToString()); + writer.WritePropertyName("amount"); + writer.WriteStringValue(value.Amount.ToString()); + writer.WriteEndObject(); + writer.WriteEndObject(); + } } } } -} \ No newline at end of file +}