Skip to content

Commit

Permalink
Support resource leeway parameter when simulating Soroban transaction…
Browse files Browse the repository at this point in the history
…s. (#896)

* Add resourceUsage parameter to simulateTransaction
  • Loading branch information
Shaptic committed Dec 15, 2023
1 parent dc07e03 commit 828e355
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 6 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 20 additions & 5 deletions src/soroban/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -487,18 +492,28 @@ export class Server {
* });
*/
public async simulateTransaction(
transaction: Transaction | FeeBumpTransaction
tx: Transaction | FeeBumpTransaction,
addlResources?: Server.ResourceLeeway
): Promise<Api.SimulateTransactionResponse> {
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<Api.RawSimulateTransactionResponse> {
return jsonrpc.post(
return jsonrpc.postObject(
this.serverURL.toString(),
'simulateTransaction',
transaction.toXDR()
{
transaction: transaction.toXDR(),
...(addlResources !== undefined && {
resourceConfig: {
instructionLeeway: addlResources.cpuInstructions
}
})
}
);
}

Expand Down
31 changes: 30 additions & 1 deletion test/unit/server/soroban/simulate_transaction_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 } }),
Expand All @@ -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);
Expand Down

0 comments on commit 828e355

Please sign in to comment.