From 23dd66021202daa2a2c1259a933f865b7a1a5f5b Mon Sep 17 00:00:00 2001 From: Sophia Date: Fri, 23 Feb 2024 11:52:20 -0500 Subject: [PATCH 1/2] Fix breaking change since rt upgrade 1041 (#1979) --- centrifuge-js/src/modules/liquidityPools.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/centrifuge-js/src/modules/liquidityPools.ts b/centrifuge-js/src/modules/liquidityPools.ts index 1af915d0a9..f8cb1e486f 100644 --- a/centrifuge-js/src/modules/liquidityPools.ts +++ b/centrifuge-js/src/modules/liquidityPools.ts @@ -70,7 +70,7 @@ export function getLiquidityPoolsModule(inst: Centrifuge) { // Ensure the domain currencies are enabled // Using a batch, because theoretically they could have been enabled already for a different domain api.tx.utility.batch( - currencies.map((cur) => api.tx.liquidityPools.allowInvestmentCurrency(poolId, trancheId, cur.key)) + currencies.map((cur) => api.tx.liquidityPools.allowInvestmentCurrency(poolId, cur.key)) ), ]), ]) @@ -418,7 +418,6 @@ export function getLiquidityPoolsModule(inst: Centrifuge) { } ) - const currencyData = await multicall<{ currencies: { currencySupportsPermit?: boolean }[] trancheTokenSymbol: string From 3868795491c8da0c71e50b187c361bc3ae45e815 Mon Sep 17 00:00:00 2001 From: Sophia Date: Mon, 26 Feb 2024 10:36:21 -0500 Subject: [PATCH 2/2] Remove deprecated orderBook.tradingPair query (#1981) --- .../InvestRedeemLiquidityPoolsProvider.tsx | 8 ++- .../src/components/Swaps/Orders.tsx | 35 ++++++------- centrifuge-app/src/utils/useCurrencies.ts | 51 ++++++++----------- 3 files changed, 38 insertions(+), 56 deletions(-) diff --git a/centrifuge-app/src/components/InvestRedeem/InvestRedeemLiquidityPoolsProvider.tsx b/centrifuge-app/src/components/InvestRedeem/InvestRedeemLiquidityPoolsProvider.tsx index a1b0122e51..b3546e7532 100644 --- a/centrifuge-app/src/components/InvestRedeem/InvestRedeemLiquidityPoolsProvider.tsx +++ b/centrifuge-app/src/components/InvestRedeem/InvestRedeemLiquidityPoolsProvider.tsx @@ -5,15 +5,14 @@ import { useEvmNativeBalance, useEvmNativeCurrency, useEvmProvider, - useWallet + useWallet, } from '@centrifuge/centrifuge-react' import { TransactionRequest } from '@ethersproject/providers' import BN from 'bn.js' import * as React from 'react' -import { Dec, max } from '../../utils/Decimal' +import { Dec } from '../../utils/Decimal' import { useEvmTransaction } from '../../utils/tinlake/useEvmTransaction' import { useAddress } from '../../utils/useAddress' -import { useAssetPair } from '../../utils/useCurrencies' import { useLPEvents, useLiquidityPoolInvestment, useLiquidityPools } from '../../utils/useLiquidityPools' import { usePendingCollect, usePool, usePoolMetadata } from '../../utils/usePools' import { useDebugFlags } from '../DebugFlags' @@ -69,8 +68,7 @@ export function InvestRedeemLiquidityPoolsProvider({ poolId, trancheId, children const collectType = currencyToCollect.gt(0) ? 'redeem' : investToCollect.gt(0) ? 'invest' : null - const assetPairMinOrder = useAssetPair(pool.currency, lpInvest?.currency) - const minOrder = max(assetPairMinOrder?.toDecimal() ?? Dec(0), consts.orderBook.minFulfillment.toDecimal()) + const minOrder = consts.orderBook.minFulfillment.toDecimal() const invest = useEvmTransaction('Invest', (cent) => cent.liquidityPools.increaseInvestOrder) const decreaseInvest = useEvmTransaction('Invest', (cent) => cent.liquidityPools.decreaseInvestOrder) diff --git a/centrifuge-app/src/components/Swaps/Orders.tsx b/centrifuge-app/src/components/Swaps/Orders.tsx index bd95f21a50..4c0f5a5baa 100644 --- a/centrifuge-app/src/components/Swaps/Orders.tsx +++ b/centrifuge-app/src/components/Swaps/Orders.tsx @@ -44,9 +44,9 @@ import * as React from 'react' import { filter, map, repeatWhen, switchMap } from 'rxjs' import { parachainNames } from '../../config' import { copyToClipboard } from '../../utils/copyToClipboard' -import { Dec, max as maxDecimal } from '../../utils/Decimal' +import { Dec } from '../../utils/Decimal' import { formatBalance } from '../../utils/formatting' -import { findAssetPairPrice, useAssetPair, useAssetPairPrices, useCurrencies } from '../../utils/useCurrencies' +import { findAssetPairPrice, useAssetPairPrices, useCurrencies } from '../../utils/useCurrencies' import { useSuitableAccounts } from '../../utils/usePermissions' import { address, combine, max, min, required } from '../../utils/validation' import { ButtonGroup } from '../ButtonGroup' @@ -149,7 +149,7 @@ export function Orders({ buyOrSell }: OrdersProps) { maxSellAmount: string amountOut: string amountOutInitial: string - ratio: {custom: string} | {market: null} + ratio: { custom: string } | { market: null } } const currencyIn = parseCurrencyKey(order.currencyIn) @@ -157,11 +157,13 @@ export function Orders({ buyOrSell }: OrdersProps) { const buyCurrency = findCurrency(currencies!, currencyIn)! const sellCurrency = findCurrency(currencies!, currencyOut)! const sellAmount = new CurrencyBalance(order.amountOut, sellCurrency!.decimals) - const price = "market" in order.ratio ? null : new Price(order.ratio.custom) + const price = 'market' in order.ratio ? null : new Price(order.ratio.custom) return { id: String(order.orderId), account: addressToHex(order.placingAccount), - buyAmount: price ? CurrencyBalance.fromFloat(sellAmount.toDecimal().mul(price.toDecimal()), buyCurrency!.decimals) : null, + buyAmount: price + ? CurrencyBalance.fromFloat(sellAmount.toDecimal().mul(price.toDecimal()), buyCurrency!.decimals) + : null, sellAmount, price, buyCurrency, @@ -209,12 +211,9 @@ export function SwapAndSendDialog({ open, onClose, order }: { open: boolean; onC const api = useCentrifugeApi() const consts = useCentrifugeConsts() const getNetworkName = useGetNetworkName() - const assetPairMinOrder = useAssetPair(order.buyCurrency, order.sellCurrency) - - const minFulfillDec = maxDecimal( - assetPairMinOrder?.toDecimal() ?? Dec(0), - consts.orderBook.minFulfillment.toDecimal().mul(order.price.toDecimal()) - ) + + const minFulfillDec = consts.orderBook.minFulfillment.toDecimal().mul(order.price.toDecimal()) + const balanceDec = (balances && findBalance(balances.currencies, order.buyCurrency.key))?.balance.toDecimal() || Dec(0) const orderBuyDec = order.buyAmount.toDecimal() @@ -241,14 +240,7 @@ export function SwapAndSendDialog({ open, onClose, order }: { open: boolean; onC if (transferTo) { return cent.pools .withdraw( - [ - amount - ? amount - : order.sellAmount, - order.sellCurrency.key, - transferTo, - orderSellCurrencyLocation, - ], + [amount ? amount : order.sellAmount, order.sellCurrency.key, transferTo, orderSellCurrencyLocation], { batch: true } ) .pipe( @@ -281,7 +273,10 @@ export function SwapAndSendDialog({ open, onClose, order }: { open: boolean; onC [ values.isTransferEnabled ? values.tranferReceiverAddress : null, values.isPartialEnabled && values.partialTransfer - ? CurrencyBalance.fromFloat(Dec(values.partialTransfer).div(order.price.toDecimal()), order.sellCurrency.decimals) + ? CurrencyBalance.fromFloat( + Dec(values.partialTransfer).div(order.price.toDecimal()), + order.sellCurrency.decimals + ) : null, ], { account } diff --git a/centrifuge-app/src/utils/useCurrencies.ts b/centrifuge-app/src/utils/useCurrencies.ts index b4f9586dda..9b5863a11f 100644 --- a/centrifuge-app/src/utils/useCurrencies.ts +++ b/centrifuge-app/src/utils/useCurrencies.ts @@ -1,4 +1,4 @@ -import { CurrencyBalance, CurrencyMetadata, Price, isSameCurrency } from '@centrifuge/centrifuge-js' +import { CurrencyMetadata, Price, isSameCurrency } from '@centrifuge/centrifuge-js' import { useCentrifugeApi, useCentrifugeQuery } from '@centrifuge/centrifuge-react' import { map, switchMap } from 'rxjs' @@ -12,47 +12,36 @@ export function usePoolCurrencies() { return data?.filter((c) => c.isPoolCurrency) } -export function useAssetPair(currency: CurrencyMetadata, otherCurrency?: CurrencyMetadata) { - const api = useCentrifugeApi() - const [data] = useCentrifugeQuery( - ['assetPair', currency.key, otherCurrency?.key], - () => - api.query.orderBook.tradingPair(currency.key, otherCurrency!.key).pipe( - map((minOrderData) => { - return new CurrencyBalance(minOrderData.toPrimitive() as string, otherCurrency!.decimals) - }) - ), - { enabled: !!otherCurrency } - ) - return data -} - export function useAssetPairPrices() { const api = useCentrifugeApi() - const [data] = useCentrifugeQuery( - ['assetPairPrices'], - () => - api.query.orderBook.marketFeederId().pipe( - switchMap(feeder => api.query.oraclePriceFeed.fedValues.entries(feeder.toPrimitive())), - map((oracleValues) => { - return oracleValues.map(([keys, value]) => { + const [data] = useCentrifugeQuery(['assetPairPrices'], () => + api.query.orderBook.marketFeederId().pipe( + switchMap((feeder) => api.query.oraclePriceFeed.fedValues.entries(feeder.toPrimitive())), + map((oracleValues) => { + return oracleValues + .map(([keys, value]) => { const key = (keys.toHuman() as any)[1] as { ConversionRatio?: [any, any] } if (!key.ConversionRatio) return null as never const pair = key.ConversionRatio const price = new Price((value.toPrimitive() as [string, number])[0]) return { pair, - price + price, } - }).filter(Boolean) - }) - ), + }) + .filter(Boolean) + }) + ) ) return data } -export function findAssetPairPrice(prices: {pair: [any, any], price: Price}[], currency: CurrencyMetadata, otherCurrency: CurrencyMetadata) { - return prices.find(price => ( - isSameCurrency(price.pair[1], currency.key) && isSameCurrency(price.pair[0], otherCurrency.key) - ))?.price +export function findAssetPairPrice( + prices: { pair: [any, any]; price: Price }[], + currency: CurrencyMetadata, + otherCurrency: CurrencyMetadata +) { + return prices.find( + (price) => isSameCurrency(price.pair[1], currency.key) && isSameCurrency(price.pair[0], otherCurrency.key) + )?.price }