From 1df3b075ebe31f9d3bb4b78223f861a6d6b2b91b Mon Sep 17 00:00:00 2001 From: deniszagumennov Date: Wed, 4 Sep 2024 20:52:45 +0400 Subject: [PATCH] Final fixes --- constants/index.ts | 5 +++-- crawler/index.ts | 11 ++++++--- lib/sources/astroport/index.ts | 6 ++--- lib/sources/mars/index.ts | 20 ++++++++++++++--- recalc_config.toml | 0 recalculate.ts | 41 ++++++++++++++++++++++++++++++---- 6 files changed, 68 insertions(+), 15 deletions(-) delete mode 100644 recalc_config.toml diff --git a/constants/index.ts b/constants/index.ts index af3be56..c25f4e1 100644 --- a/constants/index.ts +++ b/constants/index.ts @@ -1,6 +1,7 @@ const RECALCULATE = { - FIRST_MARKED_AS_PROCESSED_BATCH: 4, - FIRST_ASTROPORT_CORRECT_BATCH_HEIGHT: 13464964, + LAST_NOT_MARKED_AS_PROCESSED_BATCH: 3, + FIRST_ASTROPORT_CORRECT_EXCHANGE_RATE_HEIGHT: 13464964, + FIRST_MARS_LP_CORRECT_HEIGHT: 14036905, }; export { RECALCULATE }; diff --git a/crawler/index.ts b/crawler/index.ts index f87b556..e10db67 100644 --- a/crawler/index.ts +++ b/crawler/index.ts @@ -476,10 +476,15 @@ program if (options.recalculate) { const lastBatchId = processedBatchesIds.at(-1); if (lastBatchId) { - if (lastBatchId < RECALCULATE.FIRST_MARKED_AS_PROCESSED_BATCH) + if (lastBatchId < RECALCULATE.LAST_NOT_MARKED_AS_PROCESSED_BATCH) processedBatchesIds = []; - if (lastBatchId >= RECALCULATE.FIRST_MARKED_AS_PROCESSED_BATCH) - processedBatchesIds = processedBatchesIds.concat([1, 2, 3]); + if (lastBatchId >= RECALCULATE.LAST_NOT_MARKED_AS_PROCESSED_BATCH) + processedBatchesIds = processedBatchesIds.concat( + Array.from( + { length: RECALCULATE.LAST_NOT_MARKED_AS_PROCESSED_BATCH }, + (_, i) => i, + ), + ); } } db.exec( diff --git a/lib/sources/astroport/index.ts b/lib/sources/astroport/index.ts index c68e890..2b1bd04 100644 --- a/lib/sources/astroport/index.ts +++ b/lib/sources/astroport/index.ts @@ -214,9 +214,9 @@ export default class AstroportSource implements SourceInterface { do { const multiplier = this.recalculate && - height < RECALCULATE.FIRST_ASTROPORT_CORRECT_BATCH_HEIGHT - ? (multipliers[assetId] || 1) * exchangeRate - : multipliers[assetId] || 1; + height < RECALCULATE.FIRST_ASTROPORT_CORRECT_EXCHANGE_RATE_HEIGHT + ? multipliers[assetId] || 1 + : (multipliers[assetId] || 1) * exchangeRate; const { results, nextKey: newNextKey } = await this.getDenomBalances( lpToken, multiplier, diff --git a/lib/sources/mars/index.ts b/lib/sources/mars/index.ts index be2bffe..ac97b2f 100644 --- a/lib/sources/mars/index.ts +++ b/lib/sources/mars/index.ts @@ -5,9 +5,11 @@ import pLimit from 'p-limit'; import { SourceInterface } from '../../../types/sources/source'; import { MarsPositionResponse } from '../../../types/sources/marsPositionResponse'; import { Logger } from 'pino'; +import { RECALCULATE } from "../../../constants"; export default class MarsSource implements SourceInterface { rpc: string; + recalculate: boolean; concurrencyLimit: number; paginationLimit: number; logger: Logger; @@ -38,6 +40,11 @@ export default class MarsSource implements SourceInterface { } this.assets = params.assets; + if (!params.recalculate) { + throw new Error('No recalculation flag configured'); + } + this.recalculate = params.recalculate; + if (!params.nft_contract) { throw new Error('No mars nft contract configured in params'); } @@ -143,6 +150,7 @@ export default class MarsSource implements SourceInterface { }; getBalanceAndDebt = ( + height: number, positions: MarsPositionResponse, denom: string, assetId: string, @@ -158,7 +166,11 @@ export default class MarsSource implements SourceInterface { balance: BigInt( Math.floor(assetAmount * this.lpToDATOMRate[assetId]), ), - boost: true, + boost: + this.recalculate && + height < RECALCULATE.FIRST_MARS_LP_CORRECT_HEIGHT + ? positions.debts.length > 0 + : true, }; } } @@ -216,6 +228,7 @@ export default class MarsSource implements SourceInterface { break; } const { balance, boost: debted } = this.getBalanceAndDebt( + height, positions, denom, assetId, @@ -262,6 +275,7 @@ export default class MarsSource implements SourceInterface { for (const accountToken of accountTokensOwned) { const tokenPosition = ownerPositions[accountToken]; const { balance, boost: debted } = this.getBalanceAndDebt( + height, tokenPosition, denom, assetId, @@ -284,7 +298,7 @@ export default class MarsSource implements SourceInterface { return result; }; - getAtroLpExchangeRate = async ( + getAstroLpExchangeRate = async ( height: number, contract: string, ): Promise => { @@ -321,7 +335,7 @@ export default class MarsSource implements SourceInterface { let accountTokens: string[] = []; for (const [assetId, asset] of Object.entries(this.assets)) { if (asset.lp) { - const lpRate = await this.getAtroLpExchangeRate( + const lpRate = await this.getAstroLpExchangeRate( height, asset.denom.split('/')[1], ); diff --git a/recalc_config.toml b/recalc_config.toml deleted file mode 100644 index e69de29..0000000 diff --git a/recalculate.ts b/recalculate.ts index 988bc71..ac7cde1 100644 --- a/recalculate.ts +++ b/recalculate.ts @@ -5,6 +5,20 @@ import { constants, Database } from 'bun:sqlite'; const OLD_DATABASE = 'recalculate/old_data.db'; const NEW_DATABASE = 'recalculate/new_data.db'; +const TESTNET_PARTICIPANTS_FILE = 'recalculate/testnet_participants.csv'; + +async function parseCsvToArray(filePath: string): Promise { + try { + const data = await fsPromises.readFile(filePath, 'utf8'); + + const lines = data.trim().split('\n'); + + return lines; + } catch (error) { + console.error('Error reading CSV file:', error); + throw error; + } +} async function main() { const currentDbFile = path.join(__dirname, OLD_DATABASE); @@ -88,7 +102,7 @@ async function main() { ); const batchProtocols = tasks.map((task) => task.protocol_id); - console.log(batchProtocols); + console.log(`Enabled protocols are ${batchProtocols}`); for (const batchProtocol of batchProtocols) { const crawlCommand = `bun crawl crawl ${batchProtocol} --recalculate`; @@ -101,17 +115,36 @@ async function main() { } let finishCommand; - if (batch.status === 'processed') { - finishCommand = `bun crawl finish --publish --recalculate`; - } else { + + if (batch.batch_id === 1 || batch.status === 'new') { finishCommand = `bun crawl finish --recalculate`; + } else { + finishCommand = `bun crawl finish --publish --recalculate`; } + console.log(`Running ${finishCommand}`); try { execSync(finishCommand); } catch (error) { console.error(`Error executing finish command: ${error}`); } + + if (batch.batch_id === 1) { + const testnetParticipants = await parseCsvToArray( + TESTNET_PARTICIPANTS_FILE, + ); + + testnetParticipants.forEach((participant) => { + newDb.exec(` + INSERT INTO user_points (batch_id, address, asset_id, points) + VALUES (1, '${participant}', 'dATOM', 10000000000) + ON CONFLICT (batch_id, address, asset_id) DO UPDATE SET + points = user_points.points + 10000000000 + `); + }); + + console.log(`Bonus 10_000_000_000 were added to testnet participants`); + } } }