Skip to content

Commit

Permalink
Merge pull request #1844 from argentlabs/BLO-769
Browse files Browse the repository at this point in the history
[BLO-769] Make wait time dynamic to speed up QA and testing work
  • Loading branch information
diegodelrieu authored Mar 7, 2023
2 parents 2307e98 + 6c01619 commit a09f3d6
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 17 deletions.
2 changes: 1 addition & 1 deletion packages/extension/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ ARGENT_X_STATUS_URL=
#FEATURE_ARGENT_SHIELD=
#ARGENT_SHIELD_NETWORK_ID=
#FEATURE_MULTISIG=
#FEATURE_VERIFIED_DAPPS=
#FEATURE_VERIFIED_DAPPS=
36 changes: 25 additions & 11 deletions packages/extension/src/background/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ import { handleTransactionMessage } from "./transactions/transactionMessaging"
import { handleUdcMessaging } from "./udcMessaging"
import { Wallet, sessionStore } from "./wallet"

const DEFAULT_POLLING_INTERVAL = 15
const LOCAL_POLLING_INTERVAL = 5

browser.alarms.create("core:transactionTracker:history", {
periodInMinutes: 5, // fetch history transactions every 5 minutes from voyager
})
Expand All @@ -59,22 +62,33 @@ browser.alarms.onAlarm.addListener(async (alarm) => {
}
if (alarm.name === "core:transactionTracker:update") {
console.info("~> fetching transaction updates")
let hasInFlightTransactions = await transactionTracker.update()

let inFlightTransactions = await transactionTracker.update()
// the config below will run transaction updates 4x per minute, if there are in-flight transactions
// it will update on second 0, 15, 30 and 45
const maxRetries = 3 // max 3 retries
const waitTimeInS = 15 // wait 15 seconds between retries
// By default it will update on second 0, 15, 30 and 45 but by updating WAIT_TIME we can change the number of executions
const maxExecutionTimeInMs = 60000 // 1 minute max execution time
let transactionPollingIntervalInS = DEFAULT_POLLING_INTERVAL
const startTime = Date.now()

let runs = 0
while (hasInFlightTransactions && runs < maxRetries) {
console.info(`~> waiting ${waitTimeInS}s for transaction updates`)
await delay(waitTimeInS * 1000)
while (
inFlightTransactions.length > 0 &&
Date.now() - startTime < maxExecutionTimeInMs
) {
const localTransaction = inFlightTransactions.find(
(tx) => tx.account.networkId === "localhost",
)
if (localTransaction) {
transactionPollingIntervalInS = LOCAL_POLLING_INTERVAL
} else {
transactionPollingIntervalInS = DEFAULT_POLLING_INTERVAL
}
console.info(
`~> waiting ${transactionPollingIntervalInS}s for transaction updates`,
)
await delay(transactionPollingIntervalInS * 1000)
console.info(
"~> fetching transaction updates as pending transactions were detected",
)
runs++
hasInFlightTransactions = await transactionTracker.update()
inFlightTransactions = await transactionTracker.update()
}
}
})
Expand Down
8 changes: 3 additions & 5 deletions packages/extension/src/background/transactions/tracking.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { uniqWith } from "lodash-es"

import { getInFlightTransactions } from "../../shared/transactions"
import { Transaction, getInFlightTransactions } from "../../shared/transactions"
import { WalletAccount } from "../../shared/wallet.model"
import { accountsEqual } from "../../shared/wallet.service"
import { getTransactionsUpdate } from "./sources/onchain"
Expand All @@ -9,7 +9,7 @@ import { transactionsStore } from "./store"

export interface TransactionTracker {
loadHistory: (accountsToPopulate: WalletAccount[]) => Promise<void>
update: () => Promise<boolean>
update: () => Promise<Transaction[]>
}

export const transactionTracker: TransactionTracker = {
Expand All @@ -29,8 +29,6 @@ export const transactionTracker: TransactionTracker = {
allTransactions,
)
await transactionsStore.push(updatedTransactions)
const hasPendingTransactions =
getInFlightTransactions(allTransactions).length > 0
return hasPendingTransactions
return getInFlightTransactions(allTransactions)
},
}

0 comments on commit a09f3d6

Please sign in to comment.