Skip to content

Commit

Permalink
Drop the unnecessary networkPassphrase parameter (#870)
Browse files Browse the repository at this point in the history
  • Loading branch information
Shaptic committed Oct 24, 2023
1 parent 978eda3 commit 2891ee1
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 39 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ A breaking change will get clearly marked in this log.

### Breaking Changes
* The `soroban-client` library ([stellar/js-soroban-client](https://github.com/stellar/js-soroban-client)) has been merged into this package, causing significant breaking changes in the module structure ([#860](https://github.com/stellar/js-stellar-sdk/pull/860)):

- The namespaces have changed to move each server-dependent component into its own module. Shared components (e.g. `TransactionBuilder`) are still in the top level, Horizon-specific interactions are in the `Horizon` namespace (i.e. `Server` is now `Horizon.Server`), and new Soroban RPC interactions are in the `SorobanRpc` namespace.
- There is a [detailed migration guide](https://gist.github.com/Shaptic/5ce4f16d9cce7118f391fbde398c2f30) available to outline both the literal (i.e. necessary code changes) and philosophical (i.e. how to find certain functionality) changes needed to adapt to this merge.
* The `SorobanRpc.Server.prepareTransaction` and `SorobanRpc.assembleTransaction` methods no longer need an optional `networkPassphrase` parameter, because it is implicitly part of the transaction already ([#870](https://github.com/stellar/js-stellar-sdk/pull/870)).


## [v11.0.0-beta.4](https://github.com/stellar/js-stellar-sdk/compare/v11.0.0-beta.3...v11.0.0-beta.4)
Expand Down
4 changes: 1 addition & 3 deletions src/horizon/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -322,9 +322,7 @@ export class Server {
return response.data;
}

// TODO: fix stellar-base types.
const responseXDR: xdr.TransactionResult = (xdr.TransactionResult
.fromXDR as any)(response.data.result_xdr, "base64");
const responseXDR = xdr.TransactionResult.fromXDR(response.data.result_xdr, "base64");

// TODO: fix stellar-base types.
const results = (responseXDR as any).result().value();
Expand Down
23 changes: 5 additions & 18 deletions src/soroban/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -532,9 +532,6 @@ export class Server {
* from the simulation. In other words, if you include auth entries, you
* don't care about the auth returned from the simulation. Other fields
* (footprint, etc.) will be filled as normal.
* @param {string} [networkPassphrase] explicitly provide a network
* passphrase (default: requested from the server via
* {@link Server.getNetwork}).
*
* @returns {Promise<Transaction | FeeBumpTransaction>} a copy of the
* transaction with the expected authorizations (in the case of
Expand All @@ -544,7 +541,8 @@ export class Server {
*
* @see assembleTransaction
* @see https://soroban.stellar.org/api/methods/simulateTransaction
* @throws {jsonrpc.Error<any> | Error} if simulation fails
* @throws {jsonrpc.Error<any>|Error|Api.SimulateTransactionErrorResponse}
* if simulation fails
* @example
* const contractId = 'CA3D5KRYM6CB7OWQ6TWYRR3Z4T7GNZLKERYNZGGA5SOAOPIFY6YQGAXE';
* const contract = new StellarSdk.Contract(contractId);
Expand Down Expand Up @@ -576,24 +574,13 @@ export class Server {
* console.log("errorResultXdr:", result.errorResultXdr);
* });
*/
public async prepareTransaction(
transaction: Transaction | FeeBumpTransaction,
networkPassphrase?: string
): Promise<Transaction | FeeBumpTransaction> {
const [{ passphrase }, simResponse] = await Promise.all([
networkPassphrase
? Promise.resolve({ passphrase: networkPassphrase })
: this.getNetwork(),
this.simulateTransaction(transaction)
]);
public async prepareTransaction(tx: Transaction | FeeBumpTransaction) {
const simResponse = await this.simulateTransaction(tx);
if (Api.isSimulationError(simResponse)) {
throw simResponse.error;
}
if (!simResponse.result) {
throw new Error('transaction simulation failed');
}

return assembleTransaction(transaction, passphrase, simResponse).build();
return assembleTransaction(tx, simResponse).build();
}

/**
Expand Down
8 changes: 2 additions & 6 deletions src/soroban/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ import { parseRawSimulation } from './parsers';
/**
* 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 (see
* {@link Networks} for options)
* @param raw the initial transaction, w/o simulation applied
* @param simulation the Soroban RPC simulation result (see
* {@link Server.simulateTransaction})
*
Expand All @@ -29,7 +27,6 @@ import { parseRawSimulation } from './parsers';
*/
export function assembleTransaction(
raw: Transaction | FeeBumpTransaction,
networkPassphrase: string,
simulation:
| Api.SimulateTransactionResponse
| Api.RawSimulateTransactionResponse
Expand All @@ -38,7 +35,6 @@ export function assembleTransaction(
// TODO: Handle feebump transactions
return assembleTransaction(
raw.innerTransaction,
networkPassphrase,
simulation
);
}
Expand Down Expand Up @@ -70,7 +66,7 @@ export function assembleTransaction(
fee: (classicFeeNum + minResourceFeeNum).toString(),
// apply the pre-built Soroban Tx Data from simulation onto the Tx
sorobanData: success.transactionData.build(),
networkPassphrase
networkPassphrase: raw.networkPassphrase
});

switch (raw.operations[0].type) {
Expand Down
14 changes: 3 additions & 11 deletions test/unit/transaction_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ describe("assembleTransaction", () => {

function singleContractFnTransaction(auth) {
return new StellarSdk.TransactionBuilder(source, { fee: 100 })
.setNetworkPassphrase("Test")
.setNetworkPassphrase(networkPassphrase)
.setTimeout(StellarSdk.TimeoutInfinite)
.addOperation(
StellarSdk.Operation.invokeHostFunction({
Expand All @@ -82,7 +82,6 @@ describe("assembleTransaction", () => {
const txn = singleContractFnTransaction();
const result = SorobanRpc.assembleTransaction(
txn,
networkPassphrase,
simulationResponse,
).build();

Expand All @@ -100,7 +99,6 @@ describe("assembleTransaction", () => {
const txn = singleContractFnTransaction();
const result = SorobanRpc.assembleTransaction(
txn,
networkPassphrase,
simulationResponse,
).build();

Expand Down Expand Up @@ -143,11 +141,7 @@ describe("assembleTransaction", () => {
const txn = singleContractFnTransaction();
let simulateResp = JSON.parse(JSON.stringify(simulationResponse));
simulateResp.results[0].auth = null;
const result = SorobanRpc.assembleTransaction(
txn,
networkPassphrase,
simulateResp,
).build();
const result = SorobanRpc.assembleTransaction(txn, simulateResp).build();

expect(
result
Expand Down Expand Up @@ -176,7 +170,7 @@ describe("assembleTransaction", () => {
.build();

expect(() => {
SorobanRpc.assembleTransaction(txn, networkPassphrase, {
SorobanRpc.assembleTransaction(txn, {
transactionData: {},
events: [],
minResourceFee: "0",
Expand Down Expand Up @@ -208,7 +202,6 @@ describe("assembleTransaction", () => {

const tx = SorobanRpc.assembleTransaction(
txn,
networkPassphrase,
simulationResponse,
).build();
expect(tx.operations[0].type).to.equal(op.body().switch().name);
Expand All @@ -219,7 +212,6 @@ describe("assembleTransaction", () => {
const txn = singleContractFnTransaction([fnAuth, fnAuth, fnAuth]);
const tx = SorobanRpc.assembleTransaction(
txn,
networkPassphrase,
simulationResponse,
).build();

Expand Down

0 comments on commit 2891ee1

Please sign in to comment.