Skip to content

Commit

Permalink
pending order
Browse files Browse the repository at this point in the history
  • Loading branch information
onnovisser committed Sep 18, 2023
1 parent 71781df commit f82315c
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -830,7 +830,7 @@ const PendingOrder: React.FC<{
<AnchorTextLink href="https://docs.centrifuge.io/learn/epoch/">Learn more</AnchorTextLink>
</Text>
</Stack>
<Grid gap="1px" columns={2} equalColumns>
<Grid gap="1px" columns={state.canChangeOrder ? 2 : 1} equalColumns>
<LightButton type="button" onClick={onCancelOrder} disabled={isCancelling || calculatingOrders}>
{isCancelling ? (
<Spinner size="iconSmall" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import * as React from 'react'
import { Dec } from '../../utils/Decimal'
import { useEvmTransaction } from '../../utils/tinlake/useEvmTransaction'
import { useAddress } from '../../utils/useAddress'
import { useLiquidityPoolInvestment, useLiquidityPools, useLPEvents } from '../../utils/useLiquidityPools'
import { useLiquidityPoolInvestment, useLiquidityPools } from '../../utils/useLiquidityPools'
import { usePendingCollect, usePool, usePoolMetadata } from '../../utils/usePools'
import { InvestRedeemContext } from './InvestRedeemProvider'
import { InvestRedeemAction, InvestRedeemActions, InvestRedeemProviderProps as Props, InvestRedeemState } from './types'
Expand All @@ -18,7 +18,7 @@ export function InvestRedeemLiquidityPoolsProvider({ poolId, trancheId, children
console.log('evmAddress', evmAddress)
const { data: evmNativeBalance } = useEvmNativeBalance(evmAddress)
const evmNativeCurrency = useEvmNativeCurrency()
const order = usePendingCollect(poolId, trancheId, centAddress)
const centOrder = usePendingCollect(poolId, trancheId, centAddress)
const pool = usePool(poolId) as Pool
const cent = useCentrifuge()
const [pendingActionState, setPendingAction] = React.useState<
Expand All @@ -32,7 +32,7 @@ export function InvestRedeemLiquidityPoolsProvider({ poolId, trancheId, children
} = useLiquidityPoolInvestment(poolId, trancheId)
const provider = useEvmProvider()

const { data: lpEvents } = useLPEvents(poolId, trancheId, lpInvest?.lpAddress)
// const { data: lpEvents } = useLPEvents(poolId, trancheId, lpInvest?.lpAddress)
const isAllowedToInvest = lpInvest?.isAllowedToInvest
const tranche = pool.tranches.find((t) => t.id === trancheId)
const { data: metadata, isLoading: isMetadataLoading } = usePoolMetadata(pool)
Expand All @@ -45,13 +45,11 @@ export function InvestRedeemLiquidityPoolsProvider({ poolId, trancheId, children
const price = lpInvest?.tokenPrice?.toDecimal() ?? Dec(1)
const investToCollect = lpInvest?.maxMint.toDecimal() ?? Dec(0)
const currencyToCollect = lpInvest?.maxWithdraw.toDecimal() ?? Dec(0)
const pendingRedeem = order?.remainingRedeemToken.toDecimal() ?? Dec(0)
const pendingRedeem = lpInvest?.pendingRedeem.toDecimal() ?? Dec(0)
const combinedTrancheBalance = trancheBalance.add(investToCollect).add(pendingRedeem)
const investmentValue = combinedTrancheBalance.mul(price)
const poolCurBalance = lpInvest?.currencyBalance.toDecimal() ?? Dec(0)
const poolCurBalanceCombined = poolCurBalance
.add(currencyToCollect)
.add(order?.remainingInvestCurrency.toDecimal() ?? 0)
const poolCurBalanceCombined = poolCurBalance.add(currencyToCollect).add(lpInvest?.pendingInvest.toDecimal() ?? 0)

const isCalculatingOrders = pool.epoch.status !== 'ongoing'

Expand All @@ -62,7 +60,7 @@ export function InvestRedeemLiquidityPoolsProvider({ poolId, trancheId, children
const redeem = useEvmTransaction('Redeem', (cent) => cent.liquidityPools.updateRedeemOrder)
const redeemWithPermit = useEvmTransaction('Redeem', (cent) => cent.liquidityPools.updateRedeemOrderWithPermit)
const collectInvest = useEvmTransaction('Collect', (cent) => cent.liquidityPools.mint)
const collectRedeem = useEvmTransaction('Collect', (cent) => cent.liquidityPools.withdraw)
const collectRedeem = useEvmTransaction('Withdraw', (cent) => cent.liquidityPools.withdraw)
const approve = useEvmTransaction('Approve', (cent) => cent.liquidityPools.approveForCurrency)
const cancelInvest = useEvmTransaction('Cancel order', (cent) => cent.liquidityPools.updateInvestOrder)
const cancelRedeem = useEvmTransaction('Cancel order', (cent) => cent.liquidityPools.updateRedeemOrder)
Expand All @@ -85,12 +83,12 @@ export function InvestRedeemLiquidityPoolsProvider({ poolId, trancheId, children
? 'redeem'
: pendingActionState
const pendingTransaction = pendingActionState && txActions[pendingActionState]?.lastCreatedTransaction
let statusMessage
if (order?.remainingInvestCurrency.isZero() && lpEvents?.find((e) => e.event === 'DepositRequested')) {
statusMessage = 'Investment order is currently being bridged and will show up soon'
} else if (order?.remainingRedeemToken.isZero() && lpEvents?.find((e) => e.event === 'RedeemRequested')) {
statusMessage = 'Redemption order is currently being bridged and will show up soon'
}
// let statusMessage
// if (order?.remainingInvestCurrency.isZero() && lpEvents?.find((e) => e.event === 'DepositRequested')) {
// statusMessage = 'Investment order is currently being bridged and will show up soon'
// } else if (order?.remainingRedeemToken.isZero() && lpEvents?.find((e) => e.event === 'RedeemRequested')) {
// statusMessage = 'Redemption order is currently being bridged and will show up soon'
// }

function doAction<T = any>(
name: InvestRedeemAction,
Expand Down Expand Up @@ -125,7 +123,7 @@ export function InvestRedeemLiquidityPoolsProvider({ poolId, trancheId, children
isDataLoading: isLpsLoading || isInvestmentLoading || isMetadataLoading,
isAllowedToInvest,
isPoolBusy: isCalculatingOrders,
isFirstInvestment: order?.submittedAt === 0 && order.investCurrency.isZero(),
isFirstInvestment: centOrder?.submittedAt === 0 && centOrder.investCurrency.isZero(),
nativeCurrency: evmNativeCurrency,
trancheCurrency: tranche.currency,
poolCurrency: lpInvest && {
Expand All @@ -144,19 +142,20 @@ export function InvestRedeemLiquidityPoolsProvider({ poolId, trancheId, children
trancheBalanceWithPending: combinedTrancheBalance,
investmentValue,
tokenPrice: price,
order: order
? {
investCurrency: order.investCurrency.toDecimal(),
redeemToken: order.redeemToken.toDecimal(),
payoutCurrencyAmount: order.payoutCurrencyAmount.toDecimal(),
payoutTokenAmount: order.payoutTokenAmount.toDecimal(),
remainingInvestCurrency: order.remainingInvestCurrency.toDecimal(),
remainingRedeemToken: order.remainingRedeemToken.toDecimal(),
}
: null,
order:
lpInvest && (!lpInvest?.pendingInvest.isZero() || !lpInvest?.pendingRedeem.isZero())
? {
investCurrency: lpInvest.pendingInvest.toDecimal(),
redeemToken: lpInvest.pendingRedeem.toDecimal(),
payoutCurrencyAmount: lpInvest.maxWithdraw.toDecimal(),
payoutTokenAmount: lpInvest.maxMint.toDecimal(),
remainingInvestCurrency: lpInvest.pendingInvest.toDecimal(),
remainingRedeemToken: lpInvest.pendingRedeem.toDecimal(),
}
: null,
collectAmount: investToCollect.gt(0) ? investToCollect : currencyToCollect,
collectType,
needsToCollectBeforeOrder: investToCollect.gt(0) || currencyToCollect.gt(0),
needsToCollectBeforeOrder: false,
needsPoolCurrencyApproval: (amount) =>
lpInvest ? lpInvest.managerCurrencyAllowance.toFloat() < amount && !lpInvest.currencySupportsPermit : false,
needsTrancheTokenApproval: (amount) =>
Expand All @@ -166,7 +165,7 @@ export function InvestRedeemLiquidityPoolsProvider({ poolId, trancheId, children
canChangeOrder: false,
pendingAction,
pendingTransaction,
statusMessage,
statusMessage: undefined,
}

const actions: InvestRedeemActions = {
Expand All @@ -181,7 +180,10 @@ export function InvestRedeemLiquidityPoolsProvider({ poolId, trancheId, children
) {
const signer = provider!.getSigner()
const connectedCent = cent.connectEvm(evmAddress!, signer)
const permit = await connectedCent.liquidityPools.signPermit([lpInvest.lpAddress, lpInvest.currencyAddress])
const permit = await connectedCent.liquidityPools.signPermit([
lpInvest.managerAddress,
lpInvest.currencyAddress,
])
investWithPermit.execute([lpInvest.lpAddress, newOrder, permit])
setPendingAction('investWithPermit')
} else {
Expand All @@ -198,7 +200,10 @@ export function InvestRedeemLiquidityPoolsProvider({ poolId, trancheId, children
) {
const signer = provider!.getSigner()
const connectedCent = cent.connectEvm(evmAddress!, signer)
const permit = await connectedCent.liquidityPools.signPermit([lpInvest.lpAddress, lpInvest.currencyAddress])
const permit = await connectedCent.liquidityPools.signPermit([
lpInvest.managerAddress,
lpInvest.currencyAddress,
])
redeemWithPermit.execute([lpInvest.lpAddress, newOrder, permit])
setPendingAction('redeemWithPermit')
} else {
Expand All @@ -210,8 +215,7 @@ export function InvestRedeemLiquidityPoolsProvider({ poolId, trancheId, children
collectType === 'invest' ? [lpInvest?.lpAddress, lpInvest?.maxMint] : [lpInvest?.lpAddress, lpInvest?.maxWithdraw]
),
approvePoolCurrency: doAction('approvePoolCurrency', () => [lpInvest?.managerAddress, lpInvest?.currencyAddress]),
// approveTrancheToken: doAction('approveTrancheToken', () => [lpInvest?.managerAddress, lpInvest?.lpAddress]),
approveTrancheToken: () => {},
approveTrancheToken: doAction('approveTrancheToken', () => [lpInvest?.managerAddress, lpInvest?.lpAddress]),
cancelInvest: doAction('cancelInvest', () => [lpInvest?.lpAddress, new BN(0)]),
cancelRedeem: doAction('cancelRedeem', () => [lpInvest?.lpAddress, new BN(0)]),
}
Expand Down
12 changes: 12 additions & 0 deletions centrifuge-js/src/modules/liquidityPools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,16 @@ export function getLiquidityPoolsModule(inst: Centrifuge) {
call: ['function allowance(address, address) view returns (uint)', user, manager],
returns: [['managerTrancheTokenAllowance', toTokenBalance(currency.trancheDecimals)]],
},
{
target: manager,
call: ['function userDepositRequest(address, address) view returns (uint256)', lp, user],
returns: [['pendingInvest', toCurrencyBalance(currency.currencyDecimals)]],
},
{
target: manager,
call: ['function userRedeemRequest(address, address) view returns (uint)', lp, user],
returns: [['pendingRedeem', toTokenBalance(currency.trancheDecimals)]],
},
]

const pool = await multicall<{
Expand All @@ -462,6 +472,8 @@ export function getLiquidityPoolsModule(inst: Centrifuge) {
maxWithdraw: CurrencyBalance
managerCurrencyAllowance: CurrencyBalance
managerTrancheTokenAllowance: CurrencyBalance
pendingInvest: CurrencyBalance
pendingRedeem: TokenBalance
}>(calls, {
rpcProvider: options?.rpcProvider ?? inst.config.evmSigner?.provider!,
})
Expand Down

0 comments on commit f82315c

Please sign in to comment.