Skip to content

Commit

Permalink
Merge branch 'runtime-upgrade-1038' of github.com:centrifuge/apps int…
Browse files Browse the repository at this point in the history
…o runtime-upgrade-1038
  • Loading branch information
sophialittlejohn committed Feb 26, 2024
2 parents 1bd4d79 + 3868795 commit e49bf01
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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)
Expand Down
35 changes: 15 additions & 20 deletions centrifuge-app/src/components/Swaps/Orders.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -149,19 +149,21 @@ 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)
const currencyOut = parseCurrencyKey(order.currencyOut)
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,
Expand Down Expand Up @@ -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()
Expand All @@ -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(
Expand Down Expand Up @@ -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 }
Expand Down
51 changes: 20 additions & 31 deletions centrifuge-app/src/utils/useCurrencies.ts
Original file line number Diff line number Diff line change
@@ -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'

Expand All @@ -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
}
3 changes: 1 addition & 2 deletions centrifuge-js/src/modules/liquidityPools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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))
),
]),
])
Expand Down Expand Up @@ -418,7 +418,6 @@ export function getLiquidityPoolsModule(inst: Centrifuge) {
}
)


const currencyData = await multicall<{
currencies: { currencySupportsPermit?: boolean }[]
trancheTokenSymbol: string
Expand Down

0 comments on commit e49bf01

Please sign in to comment.