From 6f459dbfd192deeae5e06acf40461bd59abe9818 Mon Sep 17 00:00:00 2001 From: Clement SAUNIER Date: Mon, 16 Oct 2023 12:52:04 +0200 Subject: [PATCH] fix: test validity scenario --- src/bot/validity.ts | 3 ++- tests/helpers/testData.ts | 2 +- tests/holderDiffs.test.ts | 23 +++++----------------- tests/overclaims.test.ts | 40 ++++++++++++++------------------------- 4 files changed, 22 insertions(+), 46 deletions(-) diff --git a/src/bot/validity.ts b/src/bot/validity.ts index 91f7b9c..0c60196 100644 --- a/src/bot/validity.ts +++ b/src/bot/validity.ts @@ -118,7 +118,7 @@ export async function validateClaims(onChainProvider: OnChainProvider, holdersRe const alreadyClaimed: HolderClaims = await onChainProvider.fetchClaimed(details); const overclaimed: string[] = []; - + // Sort details by distribution and format numbers const expandedDetails = await Promise.all( details @@ -128,6 +128,7 @@ export async function validateClaims(onChainProvider: OnChainProvider, holdersRe .map(async (d) => { const alreadyClaimedValue = round(Int256.from(alreadyClaimed[d.holder][d.tokenAddress], d.decimals).toNumber(), 2); const totalCumulated = round(unclaimed[d.holder][d.symbol].toNumber(), 2); + if (totalCumulated < alreadyClaimedValue) { overclaimed.push(`${d.holder}: ${alreadyClaimedValue} / ${totalCumulated} ${d.symbol}`); } diff --git a/tests/helpers/testData.ts b/tests/helpers/testData.ts index 4ed0818..4565309 100644 --- a/tests/helpers/testData.ts +++ b/tests/helpers/testData.ts @@ -80,7 +80,7 @@ export const createActiveDistribution = () => { export const createClaims = (amount: string) => { const claims: HolderClaims = { '0xcaca6fE7DCD4BbA6053640Bf883bCA19d6d0eB82': { - REWARDS: amount, + '0xbac10c87B134742D15dA0F8db7Ee252Ce7318534': amount, }, }; diff --git a/tests/holderDiffs.test.ts b/tests/holderDiffs.test.ts index 085f6fa..29fc0cd 100644 --- a/tests/holderDiffs.test.ts +++ b/tests/holderDiffs.test.ts @@ -28,19 +28,10 @@ describe('Errors in the differences between two trees', async function () { merkleRootsProvider: new ManualMerkleRootsProvider(), }; - expect( - await new Promise(async function (resolve) { - const result = (res: StepResult) => { - - expect(res.err).to.equal(true); - res.err && expect(res.res.code).to.equal(BotError.NegativeDiff); - resolve(true); - }; + const report = await checkHolderValidity(testContext, testReport); - await result(await checkHolderValidity(testContext, testReport)); - resolve(false); - }) - ).to.equal(true); + expect(report.err).to.equal(true); + report.err && expect(report.res.code).to.equal(BotError.NegativeDiff); }); it('Should not catch a negative diff if none', async function () { @@ -61,12 +52,8 @@ describe('Errors in the differences between two trees', async function () { merkleRootsProvider: new ManualMerkleRootsProvider(), }; - expect( - await new Promise(async function (resolve) { + const report = await checkHolderValidity(testContext, testReport); - const report = await checkHolderValidity(testContext, testReport); - resolve(!report.err); - }) - ).to.equal(true); + expect(report.err).to.equal(false); }); }); diff --git a/tests/overclaims.test.ts b/tests/overclaims.test.ts index f16043a..380b43b 100644 --- a/tests/overclaims.test.ts +++ b/tests/overclaims.test.ts @@ -2,18 +2,19 @@ import { expect } from 'chai'; import { describe, it } from 'node:test'; import { DisputeContext } from '../src/bot/context'; -import { checkOverclaimedRewards } from '../src/bot/runner'; +import { checkHolderValidity, checkOverclaimedRewards } from '../src/bot/runner'; import ConsoleLogger from '../src/helpers/logger/ConsoleLogger'; import { BotError, MerklReport, Resolver, StepResult } from '../src/types/bot'; import ManualChainProvider from './helpers/ManualChainProvider'; import ManualMerkleRootsProvider from './helpers/ManualMerkleRootsProvider'; import { createActiveDistribution, createClaims, createTree } from './helpers/testData'; +import { validateHolders } from '../src/bot/validity'; describe('Overclaim detections', async function () { it('Should catch on holder having overclaimed', async function () { const testReport: MerklReport = { startTree: createTree('1000000000000000000000'), - endTree: createTree('1000000000000000000000'), + endTree: createTree('1000000000000000000001'), }; const testContext: DisputeContext = { @@ -22,24 +23,17 @@ describe('Overclaim detections', async function () { logger: new ConsoleLogger(), onChainProvider: new ManualChainProvider( createActiveDistribution, - () => createClaims('1000000000000000000001'), + () => createClaims('1001000000000000000000'), () => 'PESOS-STERLING' ), merkleRootsProvider: new ManualMerkleRootsProvider(), }; - expect( - await new Promise(async function (resolve) { - const result = (res: StepResult) => { - if (!res.err) resolve(false); - if (res.err && res.res.code === BotError.AlreadyClaimed) resolve(true); - resolve(false); - }; + const holdersReport = await checkHolderValidity(testContext, testReport); + const report = await checkOverclaimedRewards(testContext, holdersReport.res.report); - await result(await checkOverclaimedRewards(testContext, testReport)); - resolve(false); - }) - ).to.equal(true); + expect(report.err).to.equal(true); + report.err && expect(report.res.code).to.equal(BotError.AlreadyClaimed); }); it('Should not catch on holder not having overclaimed', async function () { @@ -54,23 +48,17 @@ describe('Overclaim detections', async function () { logger: new ConsoleLogger(), onChainProvider: new ManualChainProvider( createActiveDistribution, - () => createClaims('1000000000000000000000'), + () => createClaims('1000000000000000000002'), () => 'PESOS-STERLING' ), merkleRootsProvider: new ManualMerkleRootsProvider(), }; - expect( - await new Promise(async function (resolve) { - const result = (res: StepResult) => { - if (!res.err) resolve(false); - if (res.err && res.res.code === BotError.AlreadyClaimed) resolve(true); - resolve(false); - }; + const holdersReport = await validateHolders(testContext.onChainProvider, testReport.startTree, testReport.endTree); + testReport.holdersReport = holdersReport; - await result(await checkOverclaimedRewards(testContext, testReport)); - resolve(false); - }) - ).to.equal(true); + const report = await checkOverclaimedRewards(testContext, testReport); + console.log(report); + expect(report.err).to.equal(false); }); });