-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #64 from shapeshift/total-distribution-validation
feat: added validation surrounding the total reward distribution
- Loading branch information
Showing
3 changed files
with
85 additions
and
0 deletions.
There are no files selected for viewing
56 changes: 56 additions & 0 deletions
56
...ewards-distribution/assertValidTotalRuneAllocation/assertValidTotalRuneAllocation.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
}); | ||
}); |
22 changes: 22 additions & 0 deletions
22
...pts/rewards-distribution/assertValidTotalRuneAllocation/assertValidTotalRuneAllocation.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`, | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters