Skip to content

Commit

Permalink
Final fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
deniszagumennov committed Sep 4, 2024
1 parent 25d8e4b commit 1df3b07
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 15 deletions.
5 changes: 3 additions & 2 deletions constants/index.ts
Original file line number Diff line number Diff line change
@@ -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 };
11 changes: 8 additions & 3 deletions crawler/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
6 changes: 3 additions & 3 deletions lib/sources/astroport/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
20 changes: 17 additions & 3 deletions lib/sources/mars/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<never>;
Expand Down Expand Up @@ -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');
}
Expand Down Expand Up @@ -143,6 +150,7 @@ export default class MarsSource implements SourceInterface {
};

getBalanceAndDebt = (
height: number,
positions: MarsPositionResponse,
denom: string,
assetId: string,
Expand All @@ -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,
};
}
}
Expand Down Expand Up @@ -216,6 +228,7 @@ export default class MarsSource implements SourceInterface {
break;
}
const { balance, boost: debted } = this.getBalanceAndDebt(
height,
positions,
denom,
assetId,
Expand Down Expand Up @@ -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,
Expand All @@ -284,7 +298,7 @@ export default class MarsSource implements SourceInterface {
return result;
};

getAtroLpExchangeRate = async (
getAstroLpExchangeRate = async (
height: number,
contract: string,
): Promise<number> => {
Expand Down Expand Up @@ -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],
);
Expand Down
Empty file removed recalc_config.toml
Empty file.
41 changes: 37 additions & 4 deletions recalculate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string[]> {
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);
Expand Down Expand Up @@ -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`;
Expand All @@ -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`);
}
}
}

Expand Down

0 comments on commit 1df3b07

Please sign in to comment.