diff --git a/scripts/rewards-distribution/assertValidTotalRuneAllocation/assertValidTotalRuneAllocation.test.ts b/scripts/rewards-distribution/assertValidTotalRuneAllocation/assertValidTotalRuneAllocation.test.ts new file mode 100644 index 0000000..048df56 --- /dev/null +++ b/scripts/rewards-distribution/assertValidTotalRuneAllocation/assertValidTotalRuneAllocation.test.ts @@ -0,0 +1,56 @@ +import { assertValidTotalRuneAllocation } from "./assertValidTotalRuneAllocation"; + +describe("assertValidTotalRuneAllocation", () => { + test("doesn't throw when sum of allocations is equal to the total amount to distribute", () => { + const totalRuneAmountToDistroBaseUnit = 100n; + const runeAllocationBaseUnitByAccount = { + "0x1": 50n, + "0x2": 50n, + }; + expect(() => + assertValidTotalRuneAllocation( + runeAllocationBaseUnitByAccount, + totalRuneAmountToDistroBaseUnit, + ), + ).not.toThrow(); + }); + + test("throws when sum of allocations is not equal to the total amount to distribute", () => { + const totalRuneAmountToDistroBaseUnit = 100n; + const runeAllocationBaseUnitByAccount = { + "0x1": 50n, + "0x2": 51n, + }; + expect(() => + assertValidTotalRuneAllocation( + runeAllocationBaseUnitByAccount, + totalRuneAmountToDistroBaseUnit, + ), + ).toThrow("Expected total allocated amount to be 100, got 101"); + }); + + test("throws when no allocations and non-zero distribution amount", () => { + const totalRuneAmountToDistroBaseUnit = 100n; + const runeAllocationBaseUnitByAccount = {}; + expect(() => + assertValidTotalRuneAllocation( + runeAllocationBaseUnitByAccount, + totalRuneAmountToDistroBaseUnit, + ), + ).toThrow("Expected total allocation > 0"); + }); + + test("throws when all allocations are zero and non-zero distribution amount", () => { + const totalRuneAmountToDistroBaseUnit = 100n; + const runeAllocationBaseUnitByAccount = { + "0x1": 0n, + "0x2": 0n, + }; + expect(() => + assertValidTotalRuneAllocation( + runeAllocationBaseUnitByAccount, + totalRuneAmountToDistroBaseUnit, + ), + ).toThrow("Expected total allocation > 0"); + }); +}); diff --git a/scripts/rewards-distribution/assertValidTotalRuneAllocation/assertValidTotalRuneAllocation.ts b/scripts/rewards-distribution/assertValidTotalRuneAllocation/assertValidTotalRuneAllocation.ts new file mode 100644 index 0000000..70fd0a1 --- /dev/null +++ b/scripts/rewards-distribution/assertValidTotalRuneAllocation/assertValidTotalRuneAllocation.ts @@ -0,0 +1,22 @@ +import assert from "assert"; +import { Address } from "viem"; + +export const assertValidTotalRuneAllocation = ( + runeAllocationBaseUnitByAccount: Record, + totalRuneAmountToDistroBaseUnit: bigint, +) => { + const totalAllocatedRuneBaseUnitAfterRemainder = Object.values( + runeAllocationBaseUnitByAccount, + ).reduce((sum, runeAllocationBaseUnit) => sum + runeAllocationBaseUnit, 0n); + + assert( + totalAllocatedRuneBaseUnitAfterRemainder > 0n, + "Expected total allocation > 0", + ); + + assert( + totalAllocatedRuneBaseUnitAfterRemainder === + totalRuneAmountToDistroBaseUnit, + `Expected total allocated amount to be ${totalRuneAmountToDistroBaseUnit}, got ${totalAllocatedRuneBaseUnitAfterRemainder}`, + ); +}; diff --git a/scripts/rewards-distribution/index.ts b/scripts/rewards-distribution/index.ts index 686d25a..cf34685 100644 --- a/scripts/rewards-distribution/index.ts +++ b/scripts/rewards-distribution/index.ts @@ -16,6 +16,7 @@ import { distributeAmount } from "./distributeAmount/distributeAmount"; import { Address } from "viem"; import { orderBy } from "lodash"; import { getStakingInfoByAccount } from "./getStakingInfoByAccount"; +import { assertValidTotalRuneAllocation } from "./assertValidTotalRuneAllocation/assertValidTotalRuneAllocation"; const main = async () => { const [currentBlock, [initLog]] = await Promise.all([ @@ -102,6 +103,12 @@ const main = async () => { earnedRewardsByAccount, ); + // Validate the sum of the allocations is exactly the total amount + assertValidTotalRuneAllocation( + runeAllocationBaseUnitByAccount, + totalRuneAmountToDistroBaseUnit, + ); + console.log("Rewards distribution calculated successfully!"); const tableRows = Object.entries(epochEndStakingInfoByAccount).map(