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

TOK-511: improve allocs invalid state #440

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { Address, zeroAddress } from 'viem'
import { useAccount } from 'wagmi'
import { createActions } from './allocationsActions'
import { useGetBackerRewards } from '../hooks/useBuildersWithBackerRewardPercentage'
import { validateAllocationsState } from './utils'

export type Allocations = {
[K: Address]: bigint
Expand Down Expand Up @@ -146,18 +147,6 @@ export const AllocationsContextProvider: FC<{ children: ReactNode }> = ({ childr
}, {} as Builders)
}, [rawBuilders, backerRewards])

/**
* Retrieval functions
*/
const getBuilder = useCallback((address: Address) => builders[address], [builders])

const isValidState = useCallback(() => {
const { balance, cumulativeAllocation, amountToAllocate } = backer
// FIXME: verify that the initial state has changed compared to what we want to save

return cumulativeAllocation <= balance && amountToAllocate <= balance
}, [backer])

/**
* Reactive state updates
*/
Expand Down Expand Up @@ -240,6 +229,22 @@ export const AllocationsContextProvider: FC<{ children: ReactNode }> = ({ childr
}
}, [rawAllocations, rawBuilders, totalOnchainAllocation, selections, votingPower, isContextLoading])

/**
* Getters
*/
const getBuilder = useCallback((address: Address) => builders[address], [builders])

const isValidState = useCallback(
() =>
validateAllocationsState({
backer,
initialAllocations: initialState.allocations,
currentAllocations: allocations,
totalOnchainAllocation: totalOnchainAllocation as bigint,
}),
[backer, allocations, totalOnchainAllocation, initialState.allocations],
)

const state: State = useMemo(() => {
return {
selections,
Expand Down
29 changes: 29 additions & 0 deletions src/app/collective-rewards/allocations/context/utils.ts
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
}
83 changes: 83 additions & 0 deletions src/app/collective-rewards/allocations/context/utis.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import { describe, expect, it } from 'vitest'
import { Allocations, Backer } from './AllocationsContext'
import { validateAllocationsState, ValidateAllocationsStateParams } from './utils'

describe('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)
})
})
})
Loading