Skip to content

Commit d3f4d85

Browse files
committed
fix: handle case when market has been deleted
1 parent e124653 commit d3f4d85

File tree

1 file changed

+29
-12
lines changed

1 file changed

+29
-12
lines changed

projects/templarfi/index.js

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ async function withRetry(fn, maxAttempts = MAX_RETRY_ATTEMPTS, delayMs = RETRY_D
3939
return await fn()
4040
} catch (error) {
4141
lastError = error
42+
if (error.message && error.message.includes('does not exist')) {
43+
throw error
44+
}
4245
if (attempt === maxAttempts) throw lastError
4346
console.log(`Attempt ${attempt} failed, retrying in ${delayMs}ms: ${error.message}`)
4447
await sleep(delayMs)
@@ -205,10 +208,11 @@ async function fetchDeploymentsFromContract(registryContract) {
205208
}
206209

207210
async function processMarket(marketContract) {
208-
const [snapshotRaw, configurationRaw] = await Promise.all([
209-
safeCall(marketContract, 'get_current_snapshot', {}),
210-
safeCall(marketContract, 'get_configuration', {}),
211-
])
211+
try {
212+
const [snapshotRaw, configurationRaw] = await Promise.all([
213+
safeCall(marketContract, 'get_current_snapshot', {}),
214+
safeCall(marketContract, 'get_configuration', {}),
215+
])
212216

213217
const snapshot = coerceAndValidateSnapshot(snapshotRaw)
214218
const configuration = configurationRaw
@@ -234,14 +238,21 @@ async function processMarket(marketContract) {
234238
.plus(borrow_asset_deposited_incoming)
235239
.minus(borrow_asset_borrowed)
236240

237-
return {
238-
borrowAssetToken,
239-
collateralAssetToken,
240-
availableLiquidity,
241-
totalBorrowed: borrow_asset_borrowed,
242-
totalCollateral: collateral_asset_deposited,
243-
borrowDecimals,
244-
collateralDecimals,
241+
return {
242+
borrowAssetToken,
243+
collateralAssetToken,
244+
availableLiquidity,
245+
totalBorrowed: borrow_asset_borrowed,
246+
totalCollateral: collateral_asset_deposited,
247+
borrowDecimals,
248+
collateralDecimals,
249+
}
250+
} catch (error) {
251+
if (error.message && error.message.includes('does not exist')) {
252+
console.log(`Market ${marketContract} has been deleted, skipping...`)
253+
return null
254+
}
255+
throw error
245256
}
246257
}
247258

@@ -258,6 +269,9 @@ async function tvl() {
258269

259270
results.forEach((result, index) => {
260271
if (result.status === 'fulfilled') {
272+
if (result.value === null) {
273+
return
274+
}
261275
const { borrowAssetToken, collateralAssetToken, availableLiquidity, totalCollateral, borrowDecimals, collateralDecimals } = result.value
262276
sumSingleBalance(balances, borrowAssetToken, scaleTokenAmount(availableLiquidity, borrowAssetToken, borrowDecimals))
263277
sumSingleBalance(balances, collateralAssetToken, scaleTokenAmount(totalCollateral, collateralAssetToken, collateralDecimals))
@@ -282,6 +296,9 @@ async function borrowed() {
282296

283297
results.forEach((result, index) => {
284298
if (result.status === 'fulfilled') {
299+
if (result.value === null) {
300+
return
301+
}
285302
const { borrowAssetToken, totalBorrowed, borrowDecimals } = result.value
286303
sumSingleBalance(balances, borrowAssetToken, scaleTokenAmount(totalBorrowed, borrowAssetToken, borrowDecimals))
287304
} else {

0 commit comments

Comments
 (0)