Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial update conditions checking #39

Merged
merged 6 commits into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"eslint:fix": "pnpm run eslint:check --fix",
"prettier:check": "prettier --check \"./**/*.{js,ts,md,json}\"",
"prettier:fix": "prettier --write \"./**/*.{js,ts,md,json}\"",
"test": "jest --verbose --runInBand --bail --detectOpenHandles --silent",
"test": "jest --verbose --runInBand --bail --detectOpenHandles",
"test:e2e": "jest --selectProjects e2e --runInBand",
"tsc": "tsc --project .",
"docker:build": "docker build -t api3/airseekerv2:latest -f docker/Dockerfile .",
Expand Down
6 changes: 3 additions & 3 deletions src/condition-check/condition-check.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ describe('checkUpdateCondition', () => {
it('checks all update conditions | heartbeat exceeded', () => {
const result = checkUpdateConditions(
BigNumber.from(10),
Date.now() / 1000,
Date.now() / 1000 - 60 * 60 * 24,
BigNumber.from(10),
Date.now() + 60 * 60 * 25,
86_400,
Date.now() / 1000,
60 * 60 * 23,
2
);

Expand Down
20 changes: 14 additions & 6 deletions src/condition-check/condition-check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,29 @@ export const calculateUpdateInPercentage = (initialValue: ethers.BigNumber, upda

export const calculateMedian = (arr: ethers.BigNumber[]) => {
const mid = Math.floor(arr.length / 2);

const nums = [...arr].sort((a, b) => {
if (a.lt(b)) return -1;
else if (a.gt(b)) return 1;
else return 0;
});

if (arr.length % 2 !== 0) {
return nums[mid];
}
const midNumber = nums[mid];
aquarat marked this conversation as resolved.
Show resolved Hide resolved
if (arr.length % 2 === 0) {
const baseNumber = nums[mid - 1];

if (!baseNumber) {
throw new Error('Invalid base number');
}

if (!midNumber) {
throw new Error('Invalid mid number');
}

if (mid - 1 > 0) {
return nums[mid - 1]!.add(nums[mid]!).div(2);
return baseNumber.add(midNumber).div(2);
}

throw new Error('Invalid scenario');
return midNumber;
};

export const checkDeviationThresholdExceeded = (
Expand Down