Skip to content

Commit

Permalink
fix: tests scenarios and how they are tested (#28)
Browse files Browse the repository at this point in the history
* feat: cleaning and removing scripts

* feat: cleaning and removing scripts

* fix: history tests not able to run

* fix: no problem test checks

* fix: test validity scenario

---------

Co-authored-by: picodes <[email protected]>
Co-authored-by: Picodes <[email protected]>
  • Loading branch information
3 people authored Oct 16, 2023
1 parent 3a963f7 commit 8cdd27d
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 46 deletions.
46 changes: 46 additions & 0 deletions github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# .github/workflows/ci.yml

name: CI

on:
push:
branches:
- main
pull_request:
branches:
- main

jobs:
build-and-test:
runs-on: ubuntu-latest

strategy:
matrix:
node-version: [14.x] # Use the version of Node.js that matches your project's requirements

steps:
- name: Checkout repository
uses: actions/checkout@v2

- name: Setup Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}

- name: Cache Node.js modules
uses: actions/cache@v2
with:
# path should point to the location of your node_modules directory
path: ~/.cache/yarn
key: ${{ runner.OS }}-node-${{ matrix.node-version }}-yarn-${{ hashFiles('**/yarn.lock') }}
restore-keys: |
${{ runner.OS }}-node-${{ matrix.node-version }}-yarn-
- name: Install dependencies
run: yarn install --frozen-lockfile

- name: Compile TypeScript
run: yarn tsc --noEmit

- name: Run tests
run: yarn test
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
24 changes: 6 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,9 @@ 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);
resolve(!report.err);
})
).to.equal(true);
const report = await checkHolderValidity(testContext, testReport);

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 @@ -8,12 +8,13 @@ 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);
};

await result(await checkOverclaimedRewards(testContext, testReport));
resolve(false);
})
).to.equal(true);
const holdersReport = await checkHolderValidity(testContext, testReport);

Check failure on line 32 in tests/overclaims.test.ts

View workflow job for this annotation

GitHub Actions / build-and-test (14.x)

Cannot find name 'checkHolderValidity'.
const report = await checkOverclaimedRewards(testContext, holdersReport.res.report);

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 8cdd27d

Please sign in to comment.