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

fix: update simulationData after re-simulation #985

Merged
merged 2 commits into from
Jun 13, 2024
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ are gathered when using the XDR methods.
- In `contract.AssembledTransaction`, `toJSON` and `fromJSON` should be replaced with `toXDR` and
`fromXDR`. Similarly, in `contract.Client`, `txFromJSON` should be replaced with `txFromXDR`.

### Fixed
- Contract Client: if you edit an `AssembledTransaction` with `tx.raw = cloneFrom(tx.build)`, the `tx.simulationData` will now be updated correctly. `simulationData` is effectively a cache, and we now make sure we clear it when you re-simulate.


## [v12.0.1](https://github.com/stellar/js-stellar-sdk/compare/v11.3.0...v12.0.1)

Expand Down
4 changes: 4 additions & 0 deletions src/contract/assembled_transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,10 @@ export class AssembledTransaction<T> {
}
this.simulation = await this.server.simulateTransaction(this.built);

// need to force re-calculation of simulationData for new simulation
delete this.simulationResult;
delete this.simulationTransactionData;

if (Api.isSimulationSuccess(this.simulation)) {
this.built = assembleTransaction(
this.built,
Expand Down
66 changes: 61 additions & 5 deletions test/e2e/src/test-swap.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
const { expect } = require('chai');
const { describe, it, before } = require('mocha');
const { contract, rpc } = require("../../..");
const { clientFor, generateFundedKeypair } = require("./util");
const { expect } = require("chai");
const { describe, it, before } = require("mocha");
const {
contract,
rpc,
SorobanDataBuilder,
xdr,
TransactionBuilder,
} = require("../../..");
const {
clientFor,
generateFundedKeypair,
networkPassphrase,
} = require("./util");

const amountAToSwap = 2n;
const amountBToSwap = 1n;
Expand Down Expand Up @@ -74,14 +84,60 @@ describe("Swap Contract Tests", function () {
// Further assertions on the error object
expect(error).to.be.instanceOf(contract.AssembledTransaction.Errors.NeedsMoreSignatures,
`error is not of type 'NeedsMoreSignaturesError'; instead it is of type '${error?.constructor.name}'`);

if (error) {
// Using regex to check the error message
expect(error.message).to.match(/needsNonInvokerSigningBy/);
}
});
});

it("modified & re-simulated transactions show updated data", async function () {
const tx = await this.context.swapContractAsRoot.swap({
a: this.context.alice.publicKey(),
b: this.context.bob.publicKey(),
token_a: this.context.tokenAId,
token_b: this.context.tokenBId,
amount_a: amountAToSwap,
min_a_for_b: amountAToSwap,
amount_b: amountBToSwap,
min_b_for_a: amountBToSwap,
});
await tx.signAuthEntries({
publicKey: this.context.alice.publicKey(),
...contract.basicNodeSigner(this.context.alice, networkPassphrase),
});
await tx.signAuthEntries({
publicKey: this.context.bob.publicKey(),
...contract.basicNodeSigner(this.context.bob, networkPassphrase),
});

const originalResourceFee = Number(
tx.simulationData.transactionData.resourceFee()
);
const bumpedResourceFee = originalResourceFee + 10000;

tx.raw = TransactionBuilder.cloneFrom(tx.built, {
fee: tx.built.fee,
sorobanData: new SorobanDataBuilder(
tx.simulationData.transactionData.toXDR()
)
.setResourceFee(
xdr.Int64.fromString(bumpedResourceFee.toString()).toBigInt()
)
.build(),
});

await tx.simulate();

const newSimulatedResourceFee = Number(
tx.simulationData.transactionData.resourceFee()
);

expect(originalResourceFee).to.not.equal(newSimulatedResourceFee);
expect(newSimulatedResourceFee).to.be.greaterThan(bumpedResourceFee);
});

it("alice swaps bob 10 A for 1 B", async function() {
const tx = await this.context.swapContractAsRoot.swap({
a: this.context.alice.publicKey(),
Expand Down
Loading