From f82315c5ba203593eeb4f76a181335c53b21c13b Mon Sep 17 00:00:00 2001 From: Onno Visser <23527729+onnovisser@users.noreply.github.com> Date: Mon, 18 Sep 2023 11:26:14 +0200 Subject: [PATCH] pending order --- .../components/InvestRedeem/InvestRedeem.tsx | 2 +- .../InvestRedeemLiquidityPoolsProvider.tsx | 66 ++++++++++--------- centrifuge-js/src/modules/liquidityPools.ts | 12 ++++ 3 files changed, 48 insertions(+), 32 deletions(-) diff --git a/centrifuge-app/src/components/InvestRedeem/InvestRedeem.tsx b/centrifuge-app/src/components/InvestRedeem/InvestRedeem.tsx index bfbbbf4102..51e2683bbe 100644 --- a/centrifuge-app/src/components/InvestRedeem/InvestRedeem.tsx +++ b/centrifuge-app/src/components/InvestRedeem/InvestRedeem.tsx @@ -830,7 +830,7 @@ const PendingOrder: React.FC<{ Learn more - + {isCancelling ? ( diff --git a/centrifuge-app/src/components/InvestRedeem/InvestRedeemLiquidityPoolsProvider.tsx b/centrifuge-app/src/components/InvestRedeem/InvestRedeemLiquidityPoolsProvider.tsx index 74422dfc60..6897a4bb2f 100644 --- a/centrifuge-app/src/components/InvestRedeem/InvestRedeemLiquidityPoolsProvider.tsx +++ b/centrifuge-app/src/components/InvestRedeem/InvestRedeemLiquidityPoolsProvider.tsx @@ -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' @@ -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< @@ -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) @@ -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' @@ -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) @@ -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( name: InvestRedeemAction, @@ -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 && { @@ -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) => @@ -166,7 +165,7 @@ export function InvestRedeemLiquidityPoolsProvider({ poolId, trancheId, children canChangeOrder: false, pendingAction, pendingTransaction, - statusMessage, + statusMessage: undefined, } const actions: InvestRedeemActions = { @@ -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 { @@ -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 { @@ -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)]), } diff --git a/centrifuge-js/src/modules/liquidityPools.ts b/centrifuge-js/src/modules/liquidityPools.ts index 4cd0f1c385..46b41a7dc2 100644 --- a/centrifuge-js/src/modules/liquidityPools.ts +++ b/centrifuge-js/src/modules/liquidityPools.ts @@ -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<{ @@ -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!, })