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

fix(contract-client): allowHttp logic #952

Merged
merged 1 commit into from
May 2, 2024
Merged
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
43 changes: 21 additions & 22 deletions src/contract_client/sent_transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,19 @@ export class SentTransaction<T> {
static Errors = {
SendFailed: class SendFailedError extends Error {},
SendResultOnly: class SendResultOnlyError extends Error {},
}
};

constructor(
public signTransaction: ContractClientOptions["signTransaction"],
public assembled: AssembledTransaction<T>
public assembled: AssembledTransaction<T>,
) {
if (!signTransaction) {
throw new Error(
"You must provide a `signTransaction` function to send a transaction"
"You must provide a `signTransaction` function to send a transaction",
);
}
this.server = new SorobanRpc.Server(this.assembled.options.rpcUrl, {
allowHttp: this.assembled.options.rpcUrl.startsWith("http://"),
allowHttp: this.assembled.options.allowHttp ?? false,
});
}

Expand All @@ -71,31 +71,30 @@ export class SentTransaction<T> {
};

private send = async (): Promise<this> => {
const timeoutInSeconds = this.assembled.options.timeoutInSeconds ?? DEFAULT_TIMEOUT
const timeoutInSeconds =
this.assembled.options.timeoutInSeconds ?? DEFAULT_TIMEOUT;

const signature = await this.signTransaction!(
// `signAndSend` checks for `this.built` before calling `SentTransaction.init`
this.assembled.built!.toXDR(),
{
networkPassphrase: this.assembled.options.networkPassphrase,
}
},
);

this.signed = TransactionBuilder.fromXDR(
signature,
this.assembled.options.networkPassphrase
this.assembled.options.networkPassphrase,
) as Tx;

this.sendTransactionResponse = await this.server.sendTransaction(this.signed);
this.sendTransactionResponse = await this.server.sendTransaction(
this.signed,
);

if (this.sendTransactionResponse.status !== "PENDING") {
throw new SentTransaction.Errors.SendFailed(
'Sending the transaction to the network failed!\n' +
JSON.stringify(
this.sendTransactionResponse,
null,
2
)
"Sending the transaction to the network failed!\n" +
JSON.stringify(this.sendTransactionResponse, null, 2),
);
}

Expand All @@ -104,7 +103,7 @@ export class SentTransaction<T> {
this.getTransactionResponseAll = await withExponentialBackoff(
() => this.server.getTransaction(hash),
(resp) => resp.status === SorobanRpc.Api.GetTransactionStatus.NOT_FOUND,
timeoutInSeconds
timeoutInSeconds,
);

this.getTransactionResponse =
Expand All @@ -119,13 +118,13 @@ export class SentTransaction<T> {
`Sent transaction: ${JSON.stringify(
this.sendTransactionResponse,
null,
2
2,
)}\n` +
`All attempts to get the result: ${JSON.stringify(
this.getTransactionResponseAll,
null,
2
)}`
2,
)}`,
);
}

Expand All @@ -138,7 +137,7 @@ export class SentTransaction<T> {
// getTransactionResponse has a `returnValue` field unless it failed
if ("returnValue" in this.getTransactionResponse) {
return this.assembled.options.parseResultXdr(
this.getTransactionResponse.returnValue!
this.getTransactionResponse.returnValue!,
);
}

Expand All @@ -151,17 +150,17 @@ export class SentTransaction<T> {
const errorResult = this.sendTransactionResponse.errorResult?.result();
if (errorResult) {
throw new SentTransaction.Errors.SendFailed(
`Transaction simulation looked correct, but attempting to send the transaction failed. Check \`simulation\` and \`sendTransactionResponseAll\` to troubleshoot. Decoded \`sendTransactionResponse.errorResultXdr\`: ${errorResult}`
`Transaction simulation looked correct, but attempting to send the transaction failed. Check \`simulation\` and \`sendTransactionResponseAll\` to troubleshoot. Decoded \`sendTransactionResponse.errorResultXdr\`: ${errorResult}`,
);
}
throw new SentTransaction.Errors.SendResultOnly(
`Transaction was sent to the network, but not yet awaited. No result to show. Await transaction completion with \`getTransaction(sendTransactionResponse.hash)\``
`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, throw an error
throw new Error(
`Sending transaction failed: ${JSON.stringify(this.assembled)}`
`Sending transaction failed: ${JSON.stringify(this.assembled)}`,
);
}
}
Loading