diff --git a/CHANGELOG.md b/CHANGELOG.md index 6912e6125..92ecab48d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,12 @@ A breaking change will get clearly marked in this log. +## Unreleased + +### Added +* `SorobanRpc.Server.simulateTransaction` now supports an optional `addlResources` parameter to allow users to specify additional resources that they want to include in a simulation ([#896](https://github.com/stellar/js-stellar-sdk/pull/896)). + + ## [v11.0.1](https://github.com/stellar/js-stellar-sdk/compare/v10.2.1...v11.0.0) ### Fixed diff --git a/src/soroban/server.ts b/src/soroban/server.ts index afb24136c..19a886627 100644 --- a/src/soroban/server.ts +++ b/src/soroban/server.ts @@ -40,6 +40,11 @@ export namespace Server { limit?: number; } + /** Describes additional resource leeways for transaction simulation. */ + export interface ResourceLeeway { + cpuInstructions: number; + } + export interface Options { allowHttp?: boolean; timeout?: number; @@ -487,18 +492,28 @@ export class Server { * }); */ public async simulateTransaction( - transaction: Transaction | FeeBumpTransaction + tx: Transaction | FeeBumpTransaction, + addlResources?: Server.ResourceLeeway ): Promise { - return this._simulateTransaction(transaction).then(parseRawSimulation); + return this._simulateTransaction(tx, addlResources) + .then(parseRawSimulation); } public async _simulateTransaction( - transaction: Transaction | FeeBumpTransaction + transaction: Transaction | FeeBumpTransaction, + addlResources?: Server.ResourceLeeway ): Promise { - return jsonrpc.post( + return jsonrpc.postObject( this.serverURL.toString(), 'simulateTransaction', - transaction.toXDR() + { + transaction: transaction.toXDR(), + ...(addlResources !== undefined && { + resourceConfig: { + instructionLeeway: addlResources.cpuInstructions + } + }) + } ); } diff --git a/test/unit/server/soroban/simulate_transaction_test.js b/test/unit/server/soroban/simulate_transaction_test.js index 1f6f61270..8fbb4a3c8 100644 --- a/test/unit/server/soroban/simulate_transaction_test.js +++ b/test/unit/server/soroban/simulate_transaction_test.js @@ -81,7 +81,9 @@ describe("Server#simulateTransaction", async function (done) { jsonrpc: "2.0", id: 1, method: "simulateTransaction", - params: [this.blob], + params: { + transaction: this.blob + } }) .returns( Promise.resolve({ data: { id: 1, result: simulationResponse } }), @@ -98,6 +100,33 @@ describe("Server#simulateTransaction", async function (done) { }); }); + it("simulates a transaction with add'l resource usage", function (done) { + this.axiosMock + .expects("post") + .withArgs(serverUrl, { + jsonrpc: "2.0", + id: 1, + method: "simulateTransaction", + params: { + transaction: this.blob, + resourceConfig: { instructionLeeway: 100 } + } + }) + .returns( + Promise.resolve({ data: { id: 1, result: simulationResponse } }), + ); + + this.server + .simulateTransaction(this.transaction, { cpuInstructions: 100 }) + .then(function (response) { + expect(response).to.be.deep.equal(parsedSimulationResponse); + done(); + }) + .catch(function (err) { + done(err); + }); + }); + it("works when there are no results", function () { const simResponse = baseSimulationResponse(); const parsedCopy = cloneSimulation(parsedSimulationResponse);