From 601198e7210360a807cd011bef9b02899915a3ac Mon Sep 17 00:00:00 2001 From: George Date: Thu, 11 Jul 2024 09:44:34 -0700 Subject: [PATCH 1/4] Add support for getFeeStats endpoint Signed-off-by: George --- src/rpc/api.ts | 26 ++++++++ src/rpc/server.ts | 11 ++++ .../unit/server/soroban/get_fee_stats_test.js | 59 +++++++++++++++++++ 3 files changed, 96 insertions(+) create mode 100644 test/unit/server/soroban/get_fee_stats_test.js diff --git a/src/rpc/api.ts b/src/rpc/api.ts index 42c97348b..d99f70ad2 100644 --- a/src/rpc/api.ts +++ b/src/rpc/api.ts @@ -362,4 +362,30 @@ export namespace Api { /** State Difference information */ stateChanges?: RawLedgerEntryChange[]; } + + export interface GetFeeStatsResponse { + sorobanInclusionFee: FeeDistribution; + inclusionFee: FeeDistribution; + latestLedger: number; // uint32 + } + + interface FeeDistribution { + max: string; // uint64 + min: string; // uint64 + mode: string; // uint64 + p10: string; // uint64 + p20: string; // uint64 + p30: string; // uint64 + p40: string; // uint64 + p50: string; // uint64 + p60: string; // uint64 + p70: string; // uint64 + p80: string; // uint64 + p90: string; // uint64 + p95: string; // uint64 + p99: string; // uint64 + + transactionCount: string; // uint32 + ledgerCount: number; // uint32 + } } diff --git a/src/rpc/server.ts b/src/rpc/server.ts index 1ec1a9804..ed1f71f75 100644 --- a/src/rpc/server.ts +++ b/src/rpc/server.ts @@ -854,4 +854,15 @@ export class Server { throw error; } } + + /** + * Provides an analysis of the recent fee stats for regular and smart + * contract operations. + * + * @returns {Promise} the fee stats + * @see https://soroban.stellar.org/api/methods/getFeeStats + */ + public async getFeeStats(): Promise { + return jsonrpc.postObject(this.serverURL.toString(), 'getFeeStats'); + } } diff --git a/test/unit/server/soroban/get_fee_stats_test.js b/test/unit/server/soroban/get_fee_stats_test.js new file mode 100644 index 000000000..96d65efb5 --- /dev/null +++ b/test/unit/server/soroban/get_fee_stats_test.js @@ -0,0 +1,59 @@ +const { Server, AxiosClient } = StellarSdk.rpc; + +describe("Server#getFeeStats", function () { + beforeEach(function () { + this.server = new Server(serverUrl); + this.axiosMock = sinon.mock(AxiosClient); + }); + + afterEach(function () { + this.axiosMock.verify(); + this.axiosMock.restore(); + }); + + it("requests the correct endpoint", function (done) { + const innerFeeStat = { + max: "100000000000000000000", // just > uint32 + min: "100", + mode: "100", + p10: "100", + p20: "100", + p30: "100", + p40: "100", + p50: "100", + p60: "100", + p70: "100", + p80: "100", + p90: "100", + p95: "100", + p99: "100", + transactionCount: "200", + ledgerCount: "300", + } + const feeStatResult = { + sorobanInclusionFee: innerFeeStat, + inclusionFee: innerFeeStat, + latestLedger: "12345678", + } + + this.axiosMock + .expects("post") + .withArgs(serverUrl, { + jsonrpc: "2.0", + id: 1, + method: "getFeeStats", + params: null, + }) + .returns(Promise.resolve({ data: { feeStatResult } })); + + this.server + .getFeeStats() + .then(function (response) { + expect(response).to.be.deep.equal(feeStatResult); + done(); + }) + .catch(function (err) { + done(err); + }); + }); +}); From ac11b4e445474cc4c77725125010757f2ccdec87 Mon Sep 17 00:00:00 2001 From: George Date: Thu, 11 Jul 2024 09:50:30 -0700 Subject: [PATCH 2/4] Add changelog entry Signed-off-by: George --- CHANGELOG.md | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1880928d6..aaabec928 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,36 @@ A breaking change will get clearly marked in this log. ## Unreleased +### Added +- `rpc.Server` now has a `getFeeStats` method which retrieves fee statistics for a previous chunk of ledgers to provide users with a way to provide informed decisions about getting their transactions included in the following ledgers ([#998](https://github.com/stellar/js-stellar-sdk/issues/998)): + +```typescript +export interface GetFeeStatsResponse { + sorobanInclusionFee: FeeDistribution; + inclusionFee: FeeDistribution; + latestLedger: number; // uint32 +} + +interface FeeDistribution { + max: string; // uint64 + min: string; // uint64 + mode: string; // uint64 + p10: string; // uint64 + p20: string; // uint64 + p30: string; // uint64 + p40: string; // uint64 + p50: string; // uint64 + p60: string; // uint64 + p70: string; // uint64 + p80: string; // uint64 + p90: string; // uint64 + p95: string; // uint64 + p99: string; // uint64 + transactionCount: string; // uint32 + ledgerCount: number; // uint32 +} +``` + ## [v12.1.0](https://github.com/stellar/js-stellar-sdk/compare/v12.0.1...v12.1.0) From b3135eabd361763b5b68cf2ae627bad112e4af46 Mon Sep 17 00:00:00 2001 From: George Date: Thu, 11 Jul 2024 10:35:23 -0700 Subject: [PATCH 3/4] Fixup the property name on the test Signed-off-by: George --- test/unit/server/soroban/get_fee_stats_test.js | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/test/unit/server/soroban/get_fee_stats_test.js b/test/unit/server/soroban/get_fee_stats_test.js index 96d65efb5..02ba2a61a 100644 --- a/test/unit/server/soroban/get_fee_stats_test.js +++ b/test/unit/server/soroban/get_fee_stats_test.js @@ -30,11 +30,11 @@ describe("Server#getFeeStats", function () { transactionCount: "200", ledgerCount: "300", } - const feeStatResult = { - sorobanInclusionFee: innerFeeStat, - inclusionFee: innerFeeStat, - latestLedger: "12345678", - } + const result = { + sorobanInclusionFee: innerFeeStat, + inclusionFee: innerFeeStat, + latestLedger: "12345678", + }; this.axiosMock .expects("post") @@ -44,16 +44,14 @@ describe("Server#getFeeStats", function () { method: "getFeeStats", params: null, }) - .returns(Promise.resolve({ data: { feeStatResult } })); + .returns(Promise.resolve({ data: { result } })); this.server .getFeeStats() .then(function (response) { - expect(response).to.be.deep.equal(feeStatResult); + expect(response).to.be.deep.equal(result); done(); }) - .catch(function (err) { - done(err); - }); + .catch((err) => done(err)); }); }); From 9936a7855f6f6402a0c6292e3bc4329a3c7d57ef Mon Sep 17 00:00:00 2001 From: George Date: Thu, 11 Jul 2024 10:47:36 -0700 Subject: [PATCH 4/4] Run formatter --- .../unit/server/soroban/get_fee_stats_test.js | 34 +++++++++---------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/test/unit/server/soroban/get_fee_stats_test.js b/test/unit/server/soroban/get_fee_stats_test.js index 02ba2a61a..047521de6 100644 --- a/test/unit/server/soroban/get_fee_stats_test.js +++ b/test/unit/server/soroban/get_fee_stats_test.js @@ -13,23 +13,23 @@ describe("Server#getFeeStats", function () { it("requests the correct endpoint", function (done) { const innerFeeStat = { - max: "100000000000000000000", // just > uint32 - min: "100", - mode: "100", - p10: "100", - p20: "100", - p30: "100", - p40: "100", - p50: "100", - p60: "100", - p70: "100", - p80: "100", - p90: "100", - p95: "100", - p99: "100", - transactionCount: "200", - ledgerCount: "300", - } + max: "100000000000000000000", // just > uint32 + min: "100", + mode: "100", + p10: "100", + p20: "100", + p30: "100", + p40: "100", + p50: "100", + p60: "100", + p70: "100", + p80: "100", + p90: "100", + p95: "100", + p99: "100", + transactionCount: "200", + ledgerCount: "300", + }; const result = { sorobanInclusionFee: innerFeeStat, inclusionFee: innerFeeStat,