Skip to content

Commit

Permalink
Update simulationData after re-simulation (#985)
Browse files Browse the repository at this point in the history
The new test added here shows that you can edit a transaction with
`tx.raw = cloneFrom(tx.build)` and that the `tx.simulationData` will be
updated correctly.
  • Loading branch information
chadoh committed Jun 13, 2024
1 parent 840f57d commit 7dc0a8c
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 5 deletions.
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

0 comments on commit 7dc0a8c

Please sign in to comment.