Skip to content

Commit

Permalink
Add support for getFeeStats endpoint (#1006)
Browse files Browse the repository at this point in the history
Signed-off-by: George <george@stellar.org>
  • Loading branch information
Shaptic authored Jul 17, 2024
1 parent 444fb7b commit 5f144e1
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 0 deletions.
30 changes: 30 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
26 changes: 26 additions & 0 deletions src/rpc/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
11 changes: 11 additions & 0 deletions src/rpc/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Api.GetFeeStatsResponse>} the fee stats
* @see https://soroban.stellar.org/api/methods/getFeeStats
*/
public async getFeeStats(): Promise<Api.GetFeeStatsResponse> {
return jsonrpc.postObject(this.serverURL.toString(), 'getFeeStats');
}
}
57 changes: 57 additions & 0 deletions test/unit/server/soroban/get_fee_stats_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
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 result = {
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: { result } }));

this.server
.getFeeStats()
.then(function (response) {
expect(response).to.be.deep.equal(result);
done();
})
.catch((err) => done(err));
});
});

0 comments on commit 5f144e1

Please sign in to comment.