-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(cr): improve allocs invalid state
- Loading branch information
Showing
3 changed files
with
131 additions
and
12 deletions.
There are no files selected for viewing
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
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,29 @@ | ||
import { Address } from 'viem' | ||
import { Allocations, Backer } from './AllocationsContext' | ||
|
||
export type ValidateAllocationsStateParams = { | ||
backer: Pick<Backer, 'amountToAllocate' | 'balance' | 'cumulativeAllocation'> | ||
initialAllocations: Allocations | ||
currentAllocations: Allocations | ||
totalOnchainAllocation: bigint | ||
} | ||
export const validateAllocationsState = ({ | ||
backer, | ||
initialAllocations, | ||
currentAllocations, | ||
totalOnchainAllocation = 0n, | ||
}: ValidateAllocationsStateParams): boolean => { | ||
const { balance, cumulativeAllocation, amountToAllocate } = backer | ||
|
||
if (cumulativeAllocation > balance || amountToAllocate > balance) { | ||
return false | ||
} | ||
|
||
if (totalOnchainAllocation === cumulativeAllocation) { | ||
return Object.entries(initialAllocations).some(([builder, allocation]) => { | ||
return allocation !== currentAllocations[builder as Address] | ||
}) | ||
} | ||
|
||
return true | ||
} |
85 changes: 85 additions & 0 deletions
85
src/app/collective-rewards/allocations/context/utis.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,85 @@ | ||
import { useContext } from 'react' | ||
import { describe, it, expect } from 'vitest' | ||
import { Allocations, AllocationsContext, Backer } from './AllocationsContext' | ||
import { parseEther } from 'viem' | ||
import { validateAllocationsState, ValidateAllocationsStateParams } from './utils' | ||
|
||
describe.only('Allocations context utils', () => { | ||
describe('validateAllocationsState', () => { | ||
const backer: Backer = { | ||
balance: 1000n, | ||
cumulativeAllocation: 10n, | ||
amountToAllocate: 10n, | ||
allocationsCount: 2, | ||
} | ||
const initialAllocations: Allocations = { | ||
'0x1': 3n, | ||
} | ||
const currentAllocations: Allocations = { | ||
'0x1': 5n, | ||
'0x2': 5n, | ||
} | ||
const inputs: ValidateAllocationsStateParams = { | ||
backer, | ||
initialAllocations, | ||
currentAllocations, | ||
totalOnchainAllocation: 3n, | ||
} | ||
|
||
it('should return false if cumulative allocation > balance', () => { | ||
const isValidState = validateAllocationsState({ | ||
...inputs, | ||
backer: { | ||
amountToAllocate: 1n, | ||
cumulativeAllocation: 10n, | ||
balance: 5n, | ||
}, | ||
}) | ||
|
||
expect(isValidState).toBe(false) | ||
}) | ||
|
||
it('should return false if amount to allocate > balance', () => { | ||
const isValidState = validateAllocationsState({ | ||
...inputs, | ||
backer: { | ||
cumulativeAllocation: 1n, | ||
amountToAllocate: 10n, | ||
balance: 5n, | ||
}, | ||
}) | ||
|
||
expect(isValidState).toBe(false) | ||
}) | ||
|
||
it('should return true if totalOnchainAllocation and cumulativeAllocation are different', () => { | ||
const isValidState = validateAllocationsState({ | ||
...inputs, | ||
totalOnchainAllocation: 1n, | ||
backer: { | ||
...backer, | ||
cumulativeAllocation: 2n, | ||
}, | ||
}) | ||
|
||
expect(isValidState).toBe(true) | ||
}) | ||
|
||
it('should return false if initialAllocations and currentAllocations are the same', () => { | ||
const isValidState = validateAllocationsState({ | ||
...inputs, | ||
totalOnchainAllocation: 1n, | ||
backer: { | ||
...backer, | ||
cumulativeAllocation: 1n, | ||
}, | ||
initialAllocations, | ||
currentAllocations: { | ||
...initialAllocations, | ||
}, | ||
}) | ||
|
||
expect(isValidState).toBe(false) | ||
}) | ||
}) | ||
}) |