diff --git a/bots/lending-liquidator/test/integration/liquidate.test.ts b/bots/lending-liquidator/test/integration/liquidate.test.ts index 5202906..dd93354 100644 --- a/bots/lending-liquidator/test/integration/liquidate.test.ts +++ b/bots/lending-liquidator/test/integration/liquidate.test.ts @@ -5,7 +5,7 @@ import { AccountId } from "@polkadot/types/interfaces"; import { expect } from "chai"; import { APPROX_BLOCK_TIME_MS, DEFAULT_PARACHAIN_ENDPOINT, DEFAULT_SUDO_URI, DEFAULT_USER_1_URI, DEFAULT_USER_2_URI } from "../config"; -import { setExchangeRate } from "../utils"; +import { setExchangeRate, waitForNthBlock, waitRegisteredLendingMarkets } from "../utils"; describe("liquidate", () => { const approx10Blocks = 10 * APPROX_BLOCK_TIME_MS; @@ -32,13 +32,8 @@ describe("liquidate", () => { let underlyingCurrency3: CurrencyExt; before(async function () { - // Sleep for 30s while the parachain is registering - // TODO: Check the chain state for when the parachain has already produces a few blocks - const sleepTimeMs = 30_000; - await sleep(sleepTimeMs); - this.timeout(approx10Blocks + sleepTimeMs); - api = await createSubstrateAPI(DEFAULT_PARACHAIN_ENDPOINT); + await waitForNthBlock(api); keyring = new Keyring({ type: "sr25519" }); userAccount = keyring.addFromUri(DEFAULT_USER_1_URI); user2Account = keyring.addFromUri(DEFAULT_USER_2_URI); @@ -108,13 +103,14 @@ describe("liquidate", () => { ]); const [eventFound] = await Promise.all([ - DefaultTransactionAPI.waitForEvent(sudoInterBtcAPI.api, sudoInterBtcAPI.api.events.sudo.Sudid, approx10Blocks), + DefaultTransactionAPI.waitForEvent(sudoInterBtcAPI.api, sudoInterBtcAPI.api.events.loans.ActivatedMarket, approx10Blocks), api.tx.sudo.sudo(addMarkets).signAndSend(sudoAccount), ]); expect( eventFound, `Sudo event to create new market not found - timed out after ${approx10Blocks} ms` ).to.be.true; + await waitRegisteredLendingMarkets(api); }); after(async () => { diff --git a/bots/lending-liquidator/test/utils.ts b/bots/lending-liquidator/test/utils.ts index 15f2277..c070a90 100644 --- a/bots/lending-liquidator/test/utils.ts +++ b/bots/lending-liquidator/test/utils.ts @@ -1,5 +1,7 @@ -import { CurrencyExt, InterBtcApi, storageKeyToNthInner, getStorageMapItemKey, createExchangeRateOracleKey, setStorageAtKey } from "@interlay/interbtc-api"; +import { CurrencyExt, InterBtcApi, storageKeyToNthInner, getStorageMapItemKey, createExchangeRateOracleKey, setStorageAtKey, sleep, SLEEP_TIME_MS } from "@interlay/interbtc-api"; + +import { ApiPromise } from "@polkadot/api"; export async function setExchangeRate( sudoInterBtcAPI: InterBtcApi, @@ -24,3 +26,25 @@ export async function setExchangeRate( const exchangeRateStorageKey = getStorageMapItemKey("Oracle", "Aggregate", exchangeRateOracleKey.toHex()); await setStorageAtKey(sudoInterBtcAPI.api, exchangeRateStorageKey, newExchangeRateHex, sudoAccount); } + +export async function waitForNthBlock(api: ApiPromise, n: number = 2): Promise { + while (true) { + const currentBlockNo = await api.query.system.number(); + if (currentBlockNo.toNumber() >= n) { + return; + } + console.log(`Waiting for ${n} blocks to be produced... Current block is ${currentBlockNo}`); + await sleep(SLEEP_TIME_MS); + } +} + +export async function waitRegisteredLendingMarkets(api: ApiPromise): Promise { + while (true) { + const currentBlockNo = await api.query.loans.markets.entries(); + if (currentBlockNo.length > 0) { + return; + } + console.log(`Waiting for lending markets to be registered`); + await sleep(SLEEP_TIME_MS); + } +} \ No newline at end of file