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 d32b5a1
Showing 1 changed file with 23 additions and 7 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 @@ -126,8 +129,8 @@ export class AssembledTransaction<T> {

/**
* Sign a transaction with the injected `wallet` (default to Freighter) and
* add the fully-reconstructed to this object as `txSigned`. It can then be
* sent with {@link send}.
* add the fully-reconstructed transaction to this object as `txSigned`. It
* can then be sent with {@link send}.
*/
sign = async (): Promise<this> => {
if (!this.txUnsigned) {
Expand All @@ -143,6 +146,9 @@ export class AssembledTransaction<T> {
throw new WalletDisconnectedError('Wallet is not connected')
}

// throw errors if transaction needs restore first or if it's otherwise not an expected simulation type
this.ensureSuccessfulHostFunctionSimulation()

const wallet = await this.getWallet()
const signed = await wallet.signTransaction(this.txUnsigned.toXDR(), {
networkPassphrase: this.options.networkPassphrase,
Expand Down Expand Up @@ -256,8 +262,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 d32b5a1

Please sign in to comment.