Skip to content

Commit

Permalink
remove assembled argument from init
Browse files Browse the repository at this point in the history
  • Loading branch information
BlaineHeffron committed Jun 11, 2024
1 parent c583515 commit 2e1e965
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 14 deletions.
3 changes: 1 addition & 2 deletions src/contract/assembled_transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -582,8 +582,7 @@ export class AssembledTransaction<T> {
if(!this.signed){
throw new Error("The transaction has not yet been signed. Run `sign` first, or use `signAndSend` instead.");
}
const typeChecked: AssembledTransaction<T> = this;
const sent = await SentTransaction.init(typeChecked, this.signed);
const sent = await SentTransaction.init(this.options, this.signed);
return sent;
}

Expand Down
22 changes: 10 additions & 12 deletions src/contract/sent_transaction.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
/* disable max-classes rule, because extending error shouldn't count! */
/* eslint max-classes-per-file: 0 */
import type { MethodOptions, Tx } from "./types";
import type { MethodOptions, SentTransactionOptions, Tx } from "./types";
import { Server } from "../rpc/server"
import { Api } from "../rpc/api"
import { DEFAULT_TIMEOUT, withExponentialBackoff } from "./utils";
import type { AssembledTransaction } from "./assembled_transaction";

/**
* A transaction that has been sent to the Soroban network. This happens in two steps:
Expand Down Expand Up @@ -50,12 +49,11 @@ export class SentTransaction<T> {
};

constructor(
public assembled: AssembledTransaction<T>,

public options: SentTransactionOptions<T>,
public signed: Tx,
) {
this.server = new Server(this.assembled.options.rpcUrl, {
allowHttp: this.assembled.options.allowHttp ?? false,
this.server = new Server(this.options.rpcUrl, {
allowHttp: this.options.allowHttp ?? false,
});
}

Expand All @@ -65,12 +63,12 @@ export class SentTransaction<T> {
* network.
*/
static init = async <U>(
/** {@link AssembledTransaction} from which this SentTransaction was initialized */
assembled: AssembledTransaction<U>,
/** {@link SentTransactionOptions} from which this SentTransaction was initialized */
options: SentTransactionOptions<U>,
/** The signed transaction to send to the network */
signed: Tx,
): Promise<SentTransaction<U>> => {
const tx = new SentTransaction(assembled, signed);
const tx = new SentTransaction(options, signed);
const sent = await tx.send();
return sent;
};
Expand All @@ -93,7 +91,7 @@ export class SentTransaction<T> {
const { hash } = this.sendTransactionResponse;

const timeoutInSeconds =
this.assembled.options.timeoutInSeconds ?? DEFAULT_TIMEOUT;
this.options.timeoutInSeconds ?? DEFAULT_TIMEOUT;
this.getTransactionResponseAll = await withExponentialBackoff(
() => this.server.getTransaction(hash),
(resp) => resp.status === Api.GetTransactionStatus.NOT_FOUND,
Expand Down Expand Up @@ -130,7 +128,7 @@ export class SentTransaction<T> {
if ("getTransactionResponse" in this && this.getTransactionResponse) {
// getTransactionResponse has a `returnValue` field unless it failed
if ("returnValue" in this.getTransactionResponse) {
return this.assembled.options.parseResultXdr(
return this.options.parseResultXdr(
this.getTransactionResponse.returnValue!,
);
}
Expand All @@ -155,7 +153,7 @@ export class SentTransaction<T> {

// 3. finally, if neither of those are present, throw an error
throw new Error(
`Sending transaction failed: ${JSON.stringify(this.assembled)}`,
`Sending transaction failed: ${JSON.stringify(this.signed)}`,
);
}
}
7 changes: 7 additions & 0 deletions src/contract/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,10 @@ export type AssembledTransactionOptions<T = string> = MethodOptions &
args?: any[];
parseResultXdr: (xdr: xdr.ScVal) => T;
};

export type SentTransactionOptions<T> = {
timeoutInSeconds?: number,
rpcUrl: string,
allowHttp?: boolean,
parseResultXdr: (xdr: xdr.ScVal) => T,
};

0 comments on commit 2e1e965

Please sign in to comment.