This repository has been archived by the owner on Nov 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 362
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Display total allocation instead of balance in safe token widget (…
…#4081) * fix: Display total allocation instead of balance in safe token widget * fix: Add function return type * chore: bump version to 3.31.1
- Loading branch information
1 parent
133732e
commit e1216ae
Showing
3 changed files
with
75 additions
and
6 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
66 changes: 66 additions & 0 deletions
66
src/components/AppLayout/Header/components/SafeTokenWidget/useSafeTokenAllocation.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,66 @@ | ||
import { useEffect, useState } from 'react' | ||
import useAsync from 'src/logic/hooks/useAsync' | ||
import { useSelector } from 'react-redux' | ||
import { currentChainId } from 'src/logic/config/store/selectors' | ||
import useSafeAddress from 'src/logic/currentSession/hooks/useSafeAddress' | ||
import { BigNumber } from 'bignumber.js' | ||
|
||
export const VESTING_URL = 'https://safe-claiming-app-data.gnosis-safe.io/allocations/' | ||
|
||
export type VestingData = { | ||
tag: 'user' | 'ecosystem' | ||
account: string | ||
chainId: number | ||
contract: string | ||
vestingId: string | ||
durationWeeks: number | ||
startDate: number | ||
amount: string | ||
curve: 0 | 1 | ||
proof: string[] | ||
} | ||
|
||
const fetchAllocation = async (chainId: string, safeAddress: string) => { | ||
try { | ||
const response = await fetch(`${VESTING_URL}${chainId}/${safeAddress}.json`) | ||
|
||
if (response.ok) { | ||
return response.json() as Promise<VestingData[]> | ||
} | ||
|
||
if (response.status === 404) { | ||
// No file exists => the safe is not part of any vesting | ||
return Promise.resolve([]) as Promise<VestingData[]> | ||
} | ||
} catch (err) { | ||
throw Error(`Error fetching vestings: ${err}`) | ||
} | ||
} | ||
|
||
const useSafeTokenAllocation = (): string => { | ||
const [allocation, setAllocation] = useState<string>('0') | ||
const chainId = useSelector(currentChainId) | ||
const { safeAddress } = useSafeAddress() | ||
|
||
const [allocationData] = useAsync<VestingData[] | undefined>( | ||
() => fetchAllocation(chainId, safeAddress), | ||
[chainId, safeAddress], | ||
) | ||
|
||
useEffect(() => { | ||
if (!allocationData) return | ||
|
||
const userAllocation = allocationData.find((data) => data.tag === 'user') | ||
const ecosystemAllocation = allocationData.find((data) => data.tag === 'ecosystem') | ||
|
||
const totalAllocation = new BigNumber(userAllocation?.amount || '0') | ||
.plus(ecosystemAllocation?.amount || '0') | ||
.toString() | ||
|
||
setAllocation(totalAllocation) | ||
}, [allocationData]) | ||
|
||
return allocation | ||
} | ||
|
||
export default useSafeTokenAllocation |