Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support resource leeway parameter when simulating Soroban transactions. #896

Merged
merged 4 commits into from
Dec 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading