Skip to content

Commit

Permalink
fix: don't decode send error as real result
Browse files Browse the repository at this point in the history
throw useful error types/messages instead
  • Loading branch information
chadoh committed Nov 1, 2023
1 parent ad3d6b6 commit 360ed32
Showing 1 changed file with 18 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ export class NotImplementedError extends Error { }
export class ExpiredStateError extends Error { }
export class NeedsMoreSignaturesError extends Error { }
export class WalletDisconnectedError extends Error { }
export class SendResultOnlyError extends Error { }
export class SendFailedError extends Error { }

type SendTx = SorobanRpc.SendTransactionResponse;
type GetTx = SorobanRpc.GetTransactionResponse;
Expand Down Expand Up @@ -51,8 +53,7 @@ export class AssembledTransaction<T> {

static async fromSimulation<T>(options: AssembledTransactionClassOptions<T>): Promise<AssembledTransaction<T>> {
const tx = new AssembledTransaction(options)
await tx.simulate()
return tx
return await tx.simulate()
}

constructor(public options: AssembledTransactionClassOptions<T>) {
Expand All @@ -61,7 +62,7 @@ export class AssembledTransaction<T> {
});
}

simulate = async () => {
simulate = async (): Promise<this> => {
const contract = new Contract(this.options.contractId);

this.txUnsigned = new TransactionBuilder(await this.getAccount(), {
Expand All @@ -79,6 +80,8 @@ export class AssembledTransaction<T> {
this.options.networkPassphrase,
this.simulation
).build()

return this
}

getWallet = async (): Promise<Wallet> => {
Expand Down Expand Up @@ -256,8 +259,18 @@ export class AssembledTransaction<T> {

// 2. otherwise, maybe it was merely sent with `sendTransaction`
if (this.sendTransactionResponse) {
// if it didn't await, it returned the result of `sendTransaction`
return this.options.parseResultXdr(this.sendTransactionResponse.errorResultXdr!)
const errorResultXdr = this.sendTransactionResponse.errorResultXdr &&
xdr.TransactionResult.fromXDR(
this.sendTransactionResponse.errorResultXdr, "base64"
).result().results().toString()
if (errorResultXdr) {
throw new SendFailedError(
`Transaction simulation looked correct, but attempting to send the transaction failed. Check \`simulation\` and \`sendTransactionResponseAll\` to troubleshoot. Decoded \`sendTransactionResponse.errorResultXdr\`: ${errorResultXdr}`
)
}
throw new SendResultOnlyError(
`Transaction was sent to the network, but not yet awaited. No result to show. Await transaction completion with \`getTransaction(sendTransactionResponse.hash)\``
)
}

// 3. finally, if neither of those are present, then parse the simulation result
Expand Down

0 comments on commit 360ed32

Please sign in to comment.