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

Release/1.2.2 #52

Merged
merged 2 commits into from
Oct 24, 2023
Merged
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "safe-dao-governance-app",
"homepage": "https://github.com/safe-global/safe-dao-governance-app",
"license": "GPL-3.0",
"version": "1.2.1",
"version": "1.2.2",
"scripts": {
"build": "next build && next export",
"lint": "tsc && next lint",
Expand Down
32 changes: 31 additions & 1 deletion src/hooks/__tests__/useSafeTokenAllocation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { BigNumber } from 'ethers'
import { hexZeroPad, Deferrable, keccak256, toUtf8Bytes, defaultAbiCoder, parseEther } from 'ethers/lib/utils'

import { ZERO_ADDRESS } from '@/config/constants'
import { _getVestingData, _getVotingPower } from '@/hooks/useSafeTokenAllocation'
import { _getRedeemDeadline, _getVestingData, _getVotingPower } from '@/hooks/useSafeTokenAllocation'
import type { Vesting } from '@/hooks/useSafeTokenAllocation'
import type { Allocation } from '@/hooks/useAllocations'

Expand Down Expand Up @@ -38,6 +38,36 @@ describe('useSafeTokenAllocation', () => {

beforeEach(() => {
jest.resetAllMocks()

// Clear memoization cache
_getRedeemDeadline.cache.clear?.()
})

describe('_getRedeemDeadline', () => {
it('should should only call the provider once per address on a chain', async () => {
for await (const _ of Array.from({ length: 10 })) {
await _getRedeemDeadline({ chainId: 1, contract: hexZeroPad('0x1', 20) } as Allocation, web3Provider)
}

expect(web3Provider.call).toHaveBeenCalledTimes(1)
})

it('should not memoize different addresses on the same chain', async () => {
const chainId = 1

await _getRedeemDeadline({ chainId, contract: hexZeroPad('0x1', 20) } as Allocation, web3Provider)
await _getRedeemDeadline({ chainId, contract: hexZeroPad('0x2', 20) } as Allocation, web3Provider)

expect(web3Provider.call).toHaveBeenCalledTimes(2)
})

it('should not memoize the same address on difference chains', async () => {
for await (const i of Array.from({ length: 10 }, (_, i) => i + 1)) {
await _getRedeemDeadline({ chainId: i, contract: hexZeroPad('0x1', 20) } as Allocation, web3Provider)
}

expect(web3Provider.call).toHaveBeenCalledTimes(10)
})
})

describe('_getVestingData', () => {
Expand Down
18 changes: 13 additions & 5 deletions src/hooks/useSafeTokenAllocation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { BigNumber } from 'ethers'
import { defaultAbiCoder } from 'ethers/lib/utils'
import useSWR from 'swr'
import { useRouter } from 'next/router'
import memoize from 'lodash/memoize'
import type { JsonRpcProvider } from '@ethersproject/providers'

import { getAirdropInterface } from '@/services/contracts/Airdrop'
Expand All @@ -24,6 +25,16 @@ export type Vesting = Allocation & {

const airdropInterface = getAirdropInterface()

export const _getRedeemDeadline = memoize(
async (allocation: Allocation, provider: JsonRpcProvider): Promise<string> => {
return provider.call({
to: allocation.contract,
data: airdropInterface.encodeFunctionData('redeemDeadline'),
})
},
({ chainId, contract }) => chainId + contract,
)

/**
* Add on-chain information to allocation.
* Fetches if the redeem deadline is expired and the claimed tokens from on-chain
Expand Down Expand Up @@ -52,10 +63,7 @@ const completeAllocation = async (allocation: Allocation, provider: JsonRpcProvi
}

// Allocation is not yet redeemed => check the redeemDeadline
const redeemDeadline = await provider.call({
to: allocation.contract,
data: airdropInterface.encodeFunctionData('redeemDeadline'),
})
const redeemDeadline = await _getRedeemDeadline(allocation, provider)

const redeemDeadlineDate = new Date(BigNumber.from(redeemDeadline).mul(1000).toNumber())

Expand Down Expand Up @@ -104,7 +112,7 @@ const computeVotingPower = (validVestingData: Vesting[], balance: string): BigNu
)

// add balance
return tokensInVesting.add(balance || '0')
return tokensInVesting.add(BigNumber.from(balance))
}

export const _getVotingPower = async ({
Expand Down
2 changes: 1 addition & 1 deletion src/utils/airdrop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,5 @@ export const canRedeemSep5Airdrop = (allocation: ReturnType<typeof useSafeTokenA
return false
}

return !sep5Allocation.isRedeemed
return !sep5Allocation.isRedeemed && !sep5Allocation.isExpired
}
Loading