Skip to content

Commit

Permalink
fix: test validity scenario
Browse files Browse the repository at this point in the history
  • Loading branch information
clmntsnr committed Oct 16, 2023
1 parent 78f4b25 commit 6f459db
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 46 deletions.
3 changes: 2 additions & 1 deletion src/bot/validity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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}`);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/helpers/testData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export const createActiveDistribution = () => {
export const createClaims = (amount: string) => {
const claims: HolderClaims = {
'0xcaca6fE7DCD4BbA6053640Bf883bCA19d6d0eB82': {
REWARDS: amount,
'0xbac10c87B134742D15dA0F8db7Ee252Ce7318534': amount,
},
};

Expand Down
23 changes: 5 additions & 18 deletions tests/holderDiffs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand All @@ -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);
});
});
40 changes: 14 additions & 26 deletions tests/overclaims.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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 () {
Expand All @@ -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);
});
});

0 comments on commit 6f459db

Please sign in to comment.