Skip to content

Commit

Permalink
Fixed UnhandledPromise error, retry check status on 503 (#52)
Browse files Browse the repository at this point in the history
* Fixed UnhandledPromise error, retry check status on 503
  • Loading branch information
dribeiro-ShardLabs authored Feb 3, 2022
1 parent 66881f6 commit 69f1f3d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
2 changes: 2 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ export const ALPHA_URL = "https://alpha4.starknet.io";
export const ALPHA_MAINNET_URL = "https://alpha-mainnet.starknet.io";

export const CHECK_STATUS_TIMEOUT = 5000; // ms
export const CHECK_STATUS_RECOVER_TIMEOUT = 10000; // ms

export const LEN_SUFFIX = "_len";
export const VOYAGER_GOERLI_CONTRACT_API_URL = "https://goerli.voyager.online/api/contract/";
export const VOYAGER_MAINNET_CONTRACT_API_URL = "https://voyager.online/api/contract/";
Expand Down
18 changes: 13 additions & 5 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as fs from "fs";
import * as starknet from "./starknet-types";
import { HardhatPluginError } from "hardhat/plugins";
import { PLUGIN_NAME, CHECK_STATUS_TIMEOUT, PENDING_BLOCK_NUMBER } from "./constants";
import { PLUGIN_NAME, CHECK_STATUS_TIMEOUT, PENDING_BLOCK_NUMBER, CHECK_STATUS_RECOVER_TIMEOUT } from "./constants";
import { adaptLog } from "./utils";
import { adaptInput, adaptOutput } from "./adapt";
import { StarknetWrapper } from "./starknet-wrappers";
Expand Down Expand Up @@ -122,18 +122,26 @@ export async function iterativelyCheckStatus(
resolve: (status: string) => void,
reject: (reason?: any) => void
) {
const statusObject = await checkStatus(txHash, starknetWrapper, gatewayUrl, feederGatewayUrl);
if (isTxAccepted(statusObject)) {
const statusObject = await checkStatus(txHash, starknetWrapper, gatewayUrl, feederGatewayUrl)
.catch(reason => {
console.warn(reason);
return undefined;
});

if (!statusObject) {
console.warn("Retrying transaction status check...");
// eslint-disable-next-line prefer-rest-params
setTimeout(iterativelyCheckStatus, CHECK_STATUS_RECOVER_TIMEOUT, ...arguments);
} else if (isTxAccepted(statusObject)) {
resolve(statusObject.tx_status);
} else if (isTxRejected(statusObject)) {
reject(new Error("Transaction rejected. Error message:\n\n" + statusObject.tx_failure_reason.error_message));
} else {
// Make a recursive call, but with a delay.
// Local var `arguments` holds what was passed in the current call
const timeout = CHECK_STATUS_TIMEOUT; // ms

// eslint-disable-next-line prefer-rest-params
setTimeout(iterativelyCheckStatus, timeout, ...arguments);
setTimeout(iterativelyCheckStatus, CHECK_STATUS_TIMEOUT, ...arguments);
}
}

Expand Down

0 comments on commit 69f1f3d

Please sign in to comment.