-
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.
* save * feat: display vestings
- Loading branch information
Showing
14 changed files
with
633 additions
and
8 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
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,125 @@ | ||
import { useState } from 'react'; | ||
|
||
import { chunk } from 'lodash-es'; | ||
import { MulticallResult } from 'viem'; | ||
import { useContractReads, useContractWrite, usePublicClient } from 'wagmi'; | ||
|
||
import { useSquidNetworkHeightHooks } from '@hooks/useSquidNetworkHeightHooks'; | ||
import { useContracts } from '@network/useContracts'; | ||
|
||
import { errorMessage, WriteContractRes } from './utils'; | ||
import { VESTING_CONTRACT_ABI } from './vesting.abi'; | ||
|
||
export function useVestings({ addresses }: { addresses?: `0x${string}`[] }) { | ||
const contracts = useContracts(); | ||
const { currentHeight } = useSquidNetworkHeightHooks(); | ||
|
||
const { data, isLoading } = useContractReads({ | ||
contracts: addresses?.flatMap(address => { | ||
const vestingContract = { abi: VESTING_CONTRACT_ABI, address } as const; | ||
return [ | ||
{ | ||
...vestingContract, | ||
functionName: 'start', | ||
}, | ||
{ | ||
...vestingContract, | ||
functionName: 'end', | ||
}, | ||
{ | ||
...vestingContract, | ||
functionName: 'depositedIntoProtocol', | ||
}, | ||
{ | ||
...vestingContract, | ||
functionName: 'releasable', | ||
args: [contracts.SQD], | ||
}, | ||
{ | ||
...vestingContract, | ||
functionName: 'released', | ||
args: [contracts.SQD], | ||
}, | ||
{ | ||
...vestingContract, | ||
functionName: 'expectedTotalAmount', | ||
}, | ||
{ | ||
...vestingContract, | ||
functionName: 'immediateReleaseBIP', | ||
}, | ||
] as const; | ||
}), | ||
allowFailure: true, | ||
enabled: !!addresses, | ||
blockNumber: currentHeight ? BigInt(currentHeight) : undefined, | ||
}); | ||
|
||
return { | ||
data: data | ||
? chunk(data, 7).map(ch => ({ | ||
start: unwrapResult(ch[0])?.toString(), | ||
end: unwrapResult(ch[1])?.toString(), | ||
deposited: unwrapResult(ch[2])?.toString(), | ||
releasable: unwrapResult(ch[3])?.toString(), | ||
released: unwrapResult(ch[4])?.toString(), | ||
total: unwrapResult(ch[5])?.toString(), | ||
initialRelease: Number(unwrapResult(ch[6]) || 0) / 100, | ||
})) | ||
: undefined, | ||
isLoading, | ||
}; | ||
} | ||
|
||
export function useVesting({ address }: { address?: `0x${string}` }) { | ||
const { data, isLoading } = useVestings({ addresses: address ? [address] : undefined }); | ||
|
||
return { | ||
data: data?.[0], | ||
isLoading, | ||
}; | ||
} | ||
|
||
function unwrapResult<T>(result?: MulticallResult<T>): T | undefined { | ||
return result?.status === 'success' ? (result.result as T) : undefined; | ||
} | ||
|
||
export function useVestingRelease({ address }: { address?: `0x${string}` }) { | ||
const client = usePublicClient(); | ||
const { setWaitHeight } = useSquidNetworkHeightHooks(); | ||
const [isLoading, setLoading] = useState(false); | ||
const [error, setError] = useState<string | null>(null); | ||
const { SQD } = useContracts(); | ||
|
||
const { writeAsync } = useContractWrite({ | ||
abi: VESTING_CONTRACT_ABI, | ||
functionName: 'release', | ||
args: [SQD], | ||
address, | ||
}); | ||
|
||
const release = async (): Promise<WriteContractRes> => { | ||
setLoading(true); | ||
|
||
try { | ||
const tx = await writeAsync(); | ||
|
||
const receipt = await client.waitForTransactionReceipt(tx); | ||
setWaitHeight(receipt.blockNumber, []); | ||
|
||
return { success: true }; | ||
} catch (e) { | ||
const failedReason = errorMessage(e); | ||
setError(failedReason); | ||
return { success: false, failedReason }; | ||
} finally { | ||
setLoading(false); | ||
} | ||
}; | ||
|
||
return { | ||
release, | ||
isLoading, | ||
error, | ||
}; | ||
} |
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
File renamed without changes.
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,20 @@ | ||
import React from 'react'; | ||
|
||
import { Box } from '@mui/material'; | ||
import { Outlet } from 'react-router-dom'; | ||
|
||
import { CenteredPageWrapper } from '@layouts/NetworkLayout'; | ||
|
||
import { MyAssets } from './Assets'; | ||
import { MyVestings } from './Vestings'; | ||
|
||
export function AssetsPage() { | ||
return ( | ||
<CenteredPageWrapper className="wide"> | ||
<MyAssets /> | ||
<Box sx={{ height: 64 }} /> | ||
<MyVestings /> | ||
<Outlet /> | ||
</CenteredPageWrapper> | ||
); | ||
} |
File renamed without changes.
Oops, something went wrong.