-
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.
- Loading branch information
1 parent
4971bdd
commit 9c86ca0
Showing
7 changed files
with
267 additions
and
54 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
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
90 changes: 90 additions & 0 deletions
90
src/app/collective-rewards/allocations/hooks/useAllocateVotes.test.tsx
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,90 @@ | ||
import { describe, expect, test, vi, beforeEach, afterEach } from 'vitest' | ||
import { useAllocateVotes } from './useAllocateVotes' | ||
import { | ||
useWaitForTransactionReceipt, | ||
UseWaitForTransactionReceiptReturnType, | ||
useWriteContract, | ||
UseWriteContractReturnType, | ||
} from 'wagmi' | ||
import { renderHook } from '@testing-library/react' | ||
import { useContext } from 'react' | ||
import { getVoteAllocations } from '../context/utils' | ||
|
||
vi.mock(import('wagmi'), async importOriginal => { | ||
const actual = await importOriginal() | ||
return { | ||
...actual, | ||
useWriteContract: vi.fn(), | ||
useWaitForTransactionReceipt: vi.fn(), | ||
} | ||
}) | ||
|
||
vi.mock(import('react'), async importOriginal => { | ||
const actual = await importOriginal() | ||
return { | ||
...actual, | ||
useContext: vi.fn(), | ||
} | ||
}) | ||
|
||
vi.mock('@/app/collective-rewards/allocations/context/utils', () => ({ | ||
getVoteAllocations: vi.fn(), | ||
})) | ||
|
||
describe('useAllocateVotes', () => { | ||
beforeEach(() => { | ||
vi.mocked(useWaitForTransactionReceipt).mockReturnValue({} as UseWaitForTransactionReceiptReturnType) | ||
}) | ||
|
||
describe('saveAllocations', () => { | ||
const writeContractAsyncSpy = vi.fn() | ||
|
||
beforeEach(() => { | ||
vi.mocked(useContext).mockReturnValue({ | ||
initialState: {}, | ||
state: {}, | ||
}) | ||
|
||
vi.mocked(useWriteContract).mockReturnValue({ | ||
writeContractAsync: writeContractAsyncSpy, | ||
} as unknown as UseWriteContractReturnType) | ||
}) | ||
|
||
afterEach(() => { | ||
writeContractAsyncSpy.mockClear() | ||
}) | ||
|
||
test('should return allocate function', () => { | ||
vi.mocked(getVoteAllocations).mockReturnValue([['0x123'], [1n]]) | ||
|
||
renderHook(async () => { | ||
const { saveAllocations } = useAllocateVotes() | ||
|
||
expect(typeof saveAllocations).toBe('function') | ||
|
||
saveAllocations() | ||
|
||
expect(writeContractAsyncSpy).toHaveBeenCalled() | ||
expect(writeContractAsyncSpy.mock.calls[0][0].functionName).toBe('allocate') | ||
}) | ||
}) | ||
|
||
test('should return allocateBatch function', () => { | ||
vi.mocked(getVoteAllocations).mockReturnValue([ | ||
['0x123', '0x456'], | ||
[1n, 3n], | ||
]) | ||
|
||
renderHook(async () => { | ||
const { saveAllocations } = useAllocateVotes() | ||
|
||
expect(typeof saveAllocations).toBe('function') | ||
|
||
saveAllocations() | ||
|
||
expect(writeContractAsyncSpy).toHaveBeenCalled() | ||
expect(writeContractAsyncSpy.mock.calls[0][0].functionName).toBe('allocateBatch') | ||
}) | ||
}) | ||
}) | ||
}) |
58 changes: 58 additions & 0 deletions
58
src/app/collective-rewards/allocations/hooks/useAllocateVotes.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,58 @@ | ||
import { useWaitForTransactionReceipt, useWriteContract } from 'wagmi' | ||
import { useAwaitedTxReporting } from '@/app/collective-rewards/shared/hooks' | ||
import { BackersManagerAbi } from '@/lib/abis/v2/BackersManagerAbi' | ||
import { BackersManagerAddress } from '@/lib/contracts' | ||
import { useContext } from 'react' | ||
import { AllocationsContext } from '../context' | ||
import { getVoteAllocations } from '../context/utils' | ||
|
||
export const useAllocateVotes = () => { | ||
const { writeContractAsync, error: executionError, data: hash, isPending } = useWriteContract() | ||
|
||
const { | ||
initialState: { allocations: initialAllocations }, | ||
state: { allocations, getBuilder, isValidState }, | ||
} = useContext(AllocationsContext) | ||
|
||
const { isLoading, isSuccess, data, error: receiptError } = useWaitForTransactionReceipt({ hash }) | ||
|
||
const error = executionError ?? receiptError | ||
|
||
const saveAllocations = () => { | ||
const [gauges, allocs] = getVoteAllocations({ | ||
initialAllocations, | ||
currentAllocations: allocations, | ||
getBuilder, | ||
}) | ||
|
||
const isBatchAllocation = allocs.length > 1 | ||
|
||
return writeContractAsync({ | ||
abi: BackersManagerAbi, | ||
address: BackersManagerAddress, | ||
functionName: isBatchAllocation ? 'allocateBatch' : 'allocate', | ||
args: isBatchAllocation ? [gauges, allocs] : [gauges[0], allocs[0]], | ||
}) | ||
} | ||
|
||
useAwaitedTxReporting({ | ||
hash, | ||
error, | ||
isPendingTx: isPending, | ||
isLoadingReceipt: isLoading, | ||
isSuccess, | ||
receipt: data, | ||
title: 'Saving allocations', | ||
errorContent: 'Error saving allocations', | ||
}) | ||
|
||
return { | ||
isValidState, | ||
saveAllocations: () => saveAllocations(), | ||
error, | ||
isPendingTx: isPending, | ||
isLoadingReceipt: isLoading, | ||
isSuccess, | ||
receipt: data, | ||
} | ||
} |
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
Oops, something went wrong.