Skip to content

Commit

Permalink
feat: add tx polling timeout and comments
Browse files Browse the repository at this point in the history
  • Loading branch information
icfor committed Mar 13, 2024
1 parent 4e8ee9e commit 6761746
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/screens/staking/lib/staking_sdk/utils/coins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ const denomMap: Record<DenomToNormalise, [CoinDenom, number]> = {
ADYM: [CoinDenom.DYM, aExp],
AEVMOS: [CoinDenom.EVMOS, aExp],
AISLM: [CoinDenom.ISLM, aExp],
inj: [CoinDenom.INJ, exp0], // This is handled when normalising the coin
// Because inj != INJ, this needs to keep the lower case, it is handled when
// normalising the coin
inj: [CoinDenom.INJ, exp0],
PPICA: [CoinDenom.PICA, pExp],
UAKT: [CoinDenom.AKT, uExp],
UATOM: [CoinDenom.ATOM, uExp],
Expand Down
20 changes: 18 additions & 2 deletions src/screens/staking/lib/staking_sdk/wallet_operations/cosmos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,14 +131,17 @@ const getCosmosError = (err: Error): CosmosError => {

type PollTxOpts = {
rpc: string;
timeout?: number;
txHash: string;
};

const pollTx = async ({ rpc, txHash }: PollTxOpts) => {
const pollTx = async ({ rpc, timeout, txHash }: PollTxOpts) => {
const tmClient = await connectComet(rpc);

const queryClient = QueryClient.withExtensions(tmClient, setupTxExtension);

let elapsed = 0;

while (true) {
try {
const tx = await queryClient.tx.getTx(txHash);
Expand All @@ -149,7 +152,15 @@ const pollTx = async ({ rpc, txHash }: PollTxOpts) => {
console.log("debug: cosmos.ts: err", err);
}

await new Promise((resolve) => setTimeout(resolve, 5000));
const toWait = 5000;

elapsed += toWait;

if (timeout && elapsed >= timeout) {
throw new Error("Timeout");
}

await new Promise((resolve) => setTimeout(resolve, toWait));
}
};

Expand All @@ -175,6 +186,8 @@ const signAndBroadcastEthermint = async (

const { account_number: accountNumber, sequence } = latestAccountInfo;

// The `sequence` can be `0` here if the account is new. But check that both
// values are set in case the types are not valid.
if (typeof sequence !== "number" || !accountNumber) {
throw new Error("Account number or sequence is missing");
}
Expand Down Expand Up @@ -249,6 +262,9 @@ const signAndBroadcastEthermint = async (

const txResult = await pollTx({
rpc,
// Three minutes, since some transactions (e.g. for Injective) can take
// a long time
timeout: 1000 * 60 * 3,
txHash,
});

Expand Down

0 comments on commit 6761746

Please sign in to comment.