Skip to content

Commit

Permalink
Liquidity Pools: Update token prices (#2264)
Browse files Browse the repository at this point in the history
  • Loading branch information
onnovisser authored Jul 4, 2024
1 parent 22b57c9 commit 6a4d600
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 6 deletions.
46 changes: 42 additions & 4 deletions centrifuge-app/src/pages/IssuerPool/Investors/LiquidityPools.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { CurrencyKey } from '@centrifuge/centrifuge-js'
import {
ConnectionGuard,
useCentrifugeApi,
useCentrifugeTransaction,
useGetExplorerUrl,
useGetNetworkName,
Expand All @@ -8,12 +10,13 @@ import {
import { Accordion, Button, IconExternalLink, Shelf, Stack, Text } from '@centrifuge/fabric'
import React from 'react'
import { useParams } from 'react-router'
import { combineLatest, switchMap } from 'rxjs'
import { PageSection } from '../../../components/PageSection'
import { AnchorTextLink } from '../../../components/TextLink'
import { find } from '../../../utils/helpers'
import { useEvmTransaction } from '../../../utils/tinlake/useEvmTransaction'
import { Domain, useActiveDomains } from '../../../utils/useLiquidityPools'
import { useSuitableAccounts } from '../../../utils/usePermissions'
import { usePoolAdmin } from '../../../utils/usePermissions'
import { usePool } from '../../../utils/usePools'

function getDomainStatus(domain: Domain) {
Expand Down Expand Up @@ -60,13 +63,39 @@ export function LiquidityPools() {

function PoolDomain({ poolId, domain, refetch }: { poolId: string; domain: Domain; refetch: () => void }) {
const pool = usePool(poolId)
const poolAdmin = usePoolAdmin(poolId)
const getName = useGetNetworkName()
const explorer = useGetExplorerUrl(domain.chainId)
const api = useCentrifugeApi()

const status = getDomainStatus(domain)

const { execute, isLoading } = useCentrifugeTransaction(
`Update token prices`,
(cent) => (entries: [string, CurrencyKey][], options) => {
return combineLatest(
entries.map(([tid, curKey]) =>
cent.liquidityPools.updateTokenPrice([poolId, tid, curKey, domain.chainId], { batch: true })
)
).pipe(
switchMap((txs) => {
return cent.wrapSignAndSend(api, txs.length > 1 ? api.tx.utility.batchAll(txs) : txs[0], options)
})
)
}
)

function updateTokenPrices() {
const entries = Object.entries(domain.liquidityPools).flatMap(([tid, poolsByCurrency]) => {
return domain.currencies
.filter((cur) => !!poolsByCurrency[cur.address])
.map((cur) => [tid, cur.key] satisfies [string, CurrencyKey])
})
execute(entries, { account: poolAdmin })
}

return (
<Stack>
<Stack gap={1}>
{status === 'inactive' ? (
<EnableButton poolId={poolId} domain={domain} />
) : status === 'deploying' ? (
Expand Down Expand Up @@ -104,6 +133,11 @@ function PoolDomain({ poolId, domain, refetch }: { poolId: string; domain: Domai
</AnchorTextLink>
))
)}
{domain.hasDeployedLp && (
<Button onClick={updateTokenPrices} loading={isLoading} small>
Update token prices
</Button>
)}
</Stack>
)
}
Expand Down Expand Up @@ -166,7 +200,7 @@ function DeployLPButton({
}

function EnableButton({ poolId, domain }: { poolId: string; domain: Domain }) {
const [account] = useSuitableAccounts({ poolId, poolRole: ['PoolAdmin'] })
const poolAdmin = usePoolAdmin(poolId)
const name = useNetworkName(domain.chainId)
const { execute, isLoading } = useCentrifugeTransaction(
`Enable ${name}`,
Expand All @@ -178,7 +212,11 @@ function EnableButton({ poolId, domain }: { poolId: string; domain: Domain }) {
.map((cur) => cur.key)

return (
<Button loading={isLoading} onClick={() => execute([poolId, domain.chainId, currenciesToAdd], { account })} small>
<Button
loading={isLoading}
onClick={() => execute([poolId, domain.chainId, currenciesToAdd], { account: poolAdmin })}
small
>
Enable
</Button>
)
Expand Down
16 changes: 16 additions & 0 deletions centrifuge-js/src/modules/liquidityPools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,21 @@ export function getLiquidityPoolsModule(inst: Centrifuge) {
)
}

function updateTokenPrice(
args: [poolId: string, trancheId: string, currency: CurrencyKey, evmChainId: number],
options?: TransactionOptions
) {
const [poolId, trancheId, currency, evmChainId] = args
const $api = inst.getApi()

return $api.pipe(
switchMap((api) => {
const tx = api.tx.liquidityPools.updateTokenPrice(poolId, trancheId, currency, { EVM: evmChainId })
return inst.wrapSignAndSend(api, tx, options)
})
)
}

function deployTranche(
args: [poolManager: string, poolId: string, trancheId: string],
options: TransactionRequest = {}
Expand Down Expand Up @@ -567,6 +582,7 @@ export function getLiquidityPoolsModule(inst: Centrifuge) {
withdraw,
approveForCurrency,
signPermit,
updateTokenPrice,
getDomainRouters,
getManagerFromRouter,
getPool,
Expand Down
3 changes: 1 addition & 2 deletions centrifuge-js/src/modules/pools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { StorageKey, u32 } from '@polkadot/types'
import { Codec } from '@polkadot/types-codec/types'
import { blake2AsHex } from '@polkadot/util-crypto/blake2'
import BN from 'bn.js'
import { camelCase } from 'lodash'
import { EMPTY, Observable, combineLatest, expand, firstValueFrom, forkJoin, from, of, startWith } from 'rxjs'
import { combineLatestWith, filter, map, repeatWhen, switchMap, take, takeLast } from 'rxjs/operators'
import { SolverResult, calculateOptimalSolution } from '..'
Expand Down Expand Up @@ -1069,7 +1068,7 @@ export function getPoolsModule(inst: Centrifuge) {
name: metadata.poolName,
icon: metadata.poolIcon,
asset: {
class: camelCase(metadata.assetClass) as PoolMetadata['pool']['asset']['class'],
class: metadata.assetClass,
subClass: metadata.subAssetClass,
},
issuer: {
Expand Down

0 comments on commit 6a4d600

Please sign in to comment.