-
Notifications
You must be signed in to change notification settings - Fork 17
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
e85138c
commit 2a56d25
Showing
4 changed files
with
103 additions
and
43 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,104 @@ | ||
import { useAccount, useDisconnect, useEnsName } from 'wagmi' | ||
import { useAccountBalance } from '../hooks/useAccount' | ||
import { useActiveNetworks, useVaults } from '../hooks/usePool' | ||
import { Button, Card, CurrencyInput, Shelf, Spinner, Stack, Text } from '@centrifuge/fabric' | ||
import { PoolNetwork, Vault } from '@centrifuge/sdk' | ||
import { useEffect, useRef, useState } from 'react' | ||
import { useAccount } from 'wagmi' | ||
import { useCentrifugeTransaction } from '../hooks/useCentrifugeTransaction' | ||
import { useActiveNetworks, useVaultInvestment, useVaults } from '../hooks/usePool' | ||
import { ConnectionGuard } from './ConnectionGuard' | ||
|
||
const poolId = '2779829532' | ||
const trancheId = '0xac6bffc5fd68f7772ceddec7b0a316ca' | ||
|
||
export function Invest() { | ||
const { data: networks } = useActiveNetworks(poolId) | ||
console.log('active networks', networks) | ||
const { data: networks, isLoading } = useActiveNetworks(poolId) | ||
const ref = useRef<PoolNetwork[]>() | ||
if ((networks?.length ?? 0) > (ref.current?.length ?? 0)) { | ||
ref.current = networks | ||
} | ||
return ( | ||
<ConnectionGuard networks={networks?.map((n) => n.chainId) || []}> | ||
<InvestInner /> | ||
</ConnectionGuard> | ||
<Card maxWidth={400} mx="auto" p={2} backgroundColor="backgroundSecondary"> | ||
<Stack gap={2}> | ||
{isLoading ? ( | ||
<Shelf justifyContent="center"> | ||
<Spinner /> | ||
</Shelf> | ||
) : ( | ||
<> | ||
<Text variant="heading1">Invest</Text> | ||
<ConnectionGuard networks={ref.current?.map((n) => n.chainId) || []}> | ||
<InvestInner /> | ||
</ConnectionGuard> | ||
</> | ||
)} | ||
</Stack> | ||
</Card> | ||
) | ||
} | ||
|
||
function InvestInner() { | ||
const { address } = useAccount() | ||
const { disconnect } = useDisconnect() | ||
const { data: ensName } = useEnsName({ address }) | ||
const { data: balance, error, retry, isError } = useAccountBalance() | ||
const { data: vaults } = useVaults(poolId, trancheId, 11155111) | ||
const { execute: executeClaim, isLoading: isClaiming } = useCentrifugeTransaction() | ||
const { execute: executeInvest, isLoading: isInvesting } = useCentrifugeTransaction() | ||
const [selectedVault, setVault] = useState<Vault>() | ||
const [amount, setAmount] = useState<number | ''>(0) | ||
|
||
useEffect(() => { | ||
if (vaults?.length && (!selectedVault || !vaults.includes(selectedVault))) { | ||
setVault(vaults[0]) | ||
} | ||
}, [vaults]) | ||
|
||
function submit() { | ||
if (!selectedVault || !amount) return | ||
executeInvest(selectedVault.increaseInvestOrder(amount)) | ||
} | ||
|
||
function claim() { | ||
if (!selectedVault) return | ||
executeClaim(selectedVault.claim()) | ||
} | ||
|
||
const { data: investment } = useVaultInvestment(selectedVault, address) | ||
|
||
return ( | ||
<div> | ||
{address && <div>{ensName ? `${ensName} (${address})` : address}</div>} | ||
<h1>Your balance is {balance?.toFloat() ?? 'Loading...'}</h1> | ||
{error ? ( | ||
<div> | ||
Error {isError && 'fatal'} | ||
<button type="button" onClick={() => retry()}> | ||
Retry | ||
</button> | ||
</div> | ||
) : null} | ||
<button onClick={() => disconnect()}>Disconnect</button> | ||
</div> | ||
<> | ||
{investment?.isAllowedToInvest === false && ( | ||
<Text variant="body2" color="statusCritical"> | ||
You are not allowed to invest in this vault | ||
</Text> | ||
)} | ||
{investment?.pendingInvestCurrency.gt(0n) && ( | ||
<Text variant="body2" color="statusWarning"> | ||
You have a pending investment of {investment.pendingInvestCurrency.toFloat().toFixed(2)}{' '} | ||
{investment.investmentCurrency.symbol} | ||
</Text> | ||
)} | ||
{(investment?.claimableInvestShares.gt(0n) || investment?.claimableCancelInvestCurrency.gt(0n)) && ( | ||
<Stack gap={1}> | ||
<Text variant="body2" color="statusWarning"> | ||
You have claimable shares or pending cancelation | ||
</Text> | ||
<Button loading={isClaiming} onClick={claim} small> | ||
Claim | ||
</Button> | ||
</Stack> | ||
)} | ||
<CurrencyInput | ||
value={amount} | ||
onChange={(value) => setAmount(value)} | ||
label="Amount" | ||
currency={investment?.investmentCurrency.symbol} | ||
secondaryLabel={`${investment?.investmentCurrencyBalance.toFloat().toFixed(2)} balance`} | ||
disabled={ | ||
investment?.isAllowedToInvest === false || | ||
investment?.claimableInvestShares.gt(0n) || | ||
investment?.claimableCancelInvestCurrency.gt(0n) | ||
} | ||
/> | ||
<Button onClick={submit} disabled={!amount} loading={isInvesting}> | ||
Invest | ||
</Button> | ||
</> | ||
) | ||
} |
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