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

Simplify transaction assembly after new base features #120

Merged
merged 4 commits into from
Aug 7, 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
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -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 <hello@stellar.org>",
"homepage": "https://github.com/stellar/js-soroban-client",
Expand Down Expand Up @@ -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"
}
Expand Down
54 changes: 25 additions & 29 deletions src/transaction.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {
Account,
FeeBumpTransaction,
Operation,
Transaction,
Expand All @@ -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,
Expand All @@ -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, {
Shaptic marked this conversation as resolved.
Show resolved Hide resolved
// 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.
Expand All @@ -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(
Expand All @@ -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();
}

Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down