diff --git a/CHANGELOG.md b/CHANGELOG.md index eb1195d9..df61413f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,13 @@ A breaking change should be clearly marked in this log. ## Unreleased +## v0.10.1 + +### Fixed +* The `stellar-base` dependency has been upgraded to fix a TypeScript bug ([#665](https://github.com/stellar/js-stellar-base/pull/665)). +* Decreased bundle size by refactoring `assembleTransaction` to use new abstractions from `stellar-base` ([#120](https://github.com/stellar/js-soroban-client/pull/120)). + + ## v0.10.0 ### Breaking Changes diff --git a/package.json b/package.json index 0ba66d81..206517f7 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "soroban-client", - "version": "0.10.0", + "version": "0.10.1", "description": "A library for working with Stellar's Soroban RPC servers.", "author": "Stellar Development Foundation ", "homepage": "https://github.com/stellar/js-soroban-client", @@ -145,7 +145,7 @@ "eventsource": "^2.0.2", "lodash": "^4.17.21", "randombytes": "^2.1.0", - "stellar-base": "10.0.0-soroban.5", + "stellar-base": "10.0.0-soroban.6", "toml": "^3.0.0", "urijs": "^1.19.1" } diff --git a/src/transaction.ts b/src/transaction.ts index 48b83ff3..ddce4992 100644 --- a/src/transaction.ts +++ b/src/transaction.ts @@ -1,5 +1,4 @@ import { - Account, FeeBumpTransaction, Operation, Transaction, @@ -9,7 +8,23 @@ import { import { SorobanRpc } from "./soroban_rpc"; -// TODO: Transaction is immutable, so we need to re-build it here. :( +/** + * Combines the given raw transaction alongside the simulation results. + * + * @param raw the initial transaction, w/o simulation applied + * @param networkPassphrase the network this simulation applies to + * @param simulation the Soroban RPC simulation result + * + * @returns a new, cloned transaction with the proper auth and resource (fee, + * footprint) simulation data applied + * + * @note if the given transaction already has authorization entries in a host + * function invocation (see {@link Operation.invokeHostFunction}), **the + * simulation entries are ignored**. + * + * @see {Server.simulateTransaction} + * @see {Server.prepareTransaction} + */ export function assembleTransaction( raw: Transaction | FeeBumpTransaction, networkPassphrase: string, @@ -36,10 +51,10 @@ export function assembleTransaction( throw new Error(`simulation results invalid: ${simulation.results}`); } - const source = new Account(raw.source, `${parseInt(raw.sequence, 10) - 1}`); const classicFeeNum = parseInt(raw.fee, 10) || 0; const minResourceFeeNum = parseInt(simulation.minResourceFee, 10) || 0; - const txnBuilder = new TransactionBuilder(source, { + + const txnBuilder = TransactionBuilder.cloneFrom(raw, { // automatically update the tx fee that will be set on the resulting tx to // the sum of 'classic' fee provided from incoming tx.fee and minResourceFee // provided by simulation. @@ -49,18 +64,16 @@ export function assembleTransaction( // in the tx, so can make simplification of total classic fees for the // soroban transaction will be equal to incoming tx.fee + minResourceFee. fee: (classicFeeNum + minResourceFeeNum).toString(), - memo: raw.memo, - networkPassphrase, - timebounds: raw.timeBounds, - ledgerbounds: raw.ledgerBounds, - minAccountSequence: raw.minAccountSequence, - minAccountSequenceAge: raw.minAccountSequenceAge, - minAccountSequenceLedgerGap: raw.minAccountSequenceLedgerGap, - extraSigners: raw.extraSigners, + // apply the pre-built Soroban Tx Data from simulation onto the Tx + sorobanData: simulation.transactionData, }); switch (raw.operations[0].type) { case "invokeHostFunction": + // In this case, we don't want to clone the operation, so we drop it. + // @ts-ignore hack because `TransactionBuilder.operations` is private + txnBuilder.operations = []; + const invokeOp: Operation.InvokeHostFunction = raw.operations[0]; const existingAuth = invokeOp.auth ?? []; txnBuilder.addOperation( @@ -81,25 +94,8 @@ export function assembleTransaction( }) ); break; - - case "bumpFootprintExpiration": - txnBuilder.addOperation( - Operation.bumpFootprintExpiration(raw.operations[0]) - ); - break; - - case "restoreFootprint": - txnBuilder.addOperation(Operation.restoreFootprint(raw.operations[0])); - break; } - // apply the pre-built Soroban Tx Data from simulation onto the Tx - const sorobanTxData = xdr.SorobanTransactionData.fromXDR( - simulation.transactionData, - "base64" - ); - txnBuilder.setSorobanData(sorobanTxData); - return txnBuilder.build(); } diff --git a/yarn.lock b/yarn.lock index 661de2d5..54d46f01 100644 --- a/yarn.lock +++ b/yarn.lock @@ -6539,10 +6539,10 @@ statuses@~1.5.0: resolved "https://registry.yarnpkg.com/statuses/-/statuses-1.5.0.tgz#161c7dac177659fd9811f43771fa99381478628c" integrity sha512-OpZ3zP+jT1PI7I8nemJX4AKmAX070ZkYPVWV/AaKTJl+tXCTGyVdC1a4SL8RUQYEwk/f34ZX8UTykN68FwrqAA== -stellar-base@10.0.0-soroban.5: - version "10.0.0-soroban.5" - resolved "https://registry.yarnpkg.com/stellar-base/-/stellar-base-10.0.0-soroban.5.tgz#bf12f3a947ac15fa22a61121a705e80ee7eb4834" - integrity sha512-4gASMPVftdtuG4JqrRVgjjUyy/gIzv3FTJxR9Y8jfYi7KaduXn5YwaNjRC3UABlScqh2G1ax5+7NFkHhZXg5oA== +stellar-base@10.0.0-soroban.6: + version "10.0.0-soroban.6" + resolved "https://registry.yarnpkg.com/stellar-base/-/stellar-base-10.0.0-soroban.6.tgz#b63e3d8e49866a0d1386b2f418c25cb0700f3457" + integrity sha512-672OQgps7BjyCvrQywOn7vc/NaaexsH2WrBl+EjlKMDsRp/na0fZcM7+053r9FYzK8nCcXgbOd5IXM6Hmb1pog== dependencies: base32.js "^0.1.0" bignumber.js "^9.1.1"