Skip to content

Commit

Permalink
Merge pull request #64 from shapeshift/total-distribution-validation
Browse files Browse the repository at this point in the history
feat: added validation surrounding the total reward distribution
  • Loading branch information
0xApotheosis authored Jun 11, 2024
2 parents 29bf2a8 + d5aff7b commit 307dbbe
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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");
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import assert from "assert";
import { Address } from "viem";

export const assertValidTotalRuneAllocation = (
runeAllocationBaseUnitByAccount: Record<Address, bigint>,
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}`,
);
};
7 changes: 7 additions & 0 deletions scripts/rewards-distribution/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 307dbbe

Please sign in to comment.