Skip to content

Commit 6a4d600

Browse files
authored
Liquidity Pools: Update token prices (#2264)
1 parent 22b57c9 commit 6a4d600

3 files changed

Lines changed: 59 additions & 6 deletions

File tree

centrifuge-app/src/pages/IssuerPool/Investors/LiquidityPools.tsx

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
import { CurrencyKey } from '@centrifuge/centrifuge-js'
12
import {
23
ConnectionGuard,
4+
useCentrifugeApi,
35
useCentrifugeTransaction,
46
useGetExplorerUrl,
57
useGetNetworkName,
@@ -8,12 +10,13 @@ import {
810
import { Accordion, Button, IconExternalLink, Shelf, Stack, Text } from '@centrifuge/fabric'
911
import React from 'react'
1012
import { useParams } from 'react-router'
13+
import { combineLatest, switchMap } from 'rxjs'
1114
import { PageSection } from '../../../components/PageSection'
1215
import { AnchorTextLink } from '../../../components/TextLink'
1316
import { find } from '../../../utils/helpers'
1417
import { useEvmTransaction } from '../../../utils/tinlake/useEvmTransaction'
1518
import { Domain, useActiveDomains } from '../../../utils/useLiquidityPools'
16-
import { useSuitableAccounts } from '../../../utils/usePermissions'
19+
import { usePoolAdmin } from '../../../utils/usePermissions'
1720
import { usePool } from '../../../utils/usePools'
1821

1922
function getDomainStatus(domain: Domain) {
@@ -60,13 +63,39 @@ export function LiquidityPools() {
6063

6164
function PoolDomain({ poolId, domain, refetch }: { poolId: string; domain: Domain; refetch: () => void }) {
6265
const pool = usePool(poolId)
66+
const poolAdmin = usePoolAdmin(poolId)
6367
const getName = useGetNetworkName()
6468
const explorer = useGetExplorerUrl(domain.chainId)
69+
const api = useCentrifugeApi()
6570

6671
const status = getDomainStatus(domain)
6772

73+
const { execute, isLoading } = useCentrifugeTransaction(
74+
`Update token prices`,
75+
(cent) => (entries: [string, CurrencyKey][], options) => {
76+
return combineLatest(
77+
entries.map(([tid, curKey]) =>
78+
cent.liquidityPools.updateTokenPrice([poolId, tid, curKey, domain.chainId], { batch: true })
79+
)
80+
).pipe(
81+
switchMap((txs) => {
82+
return cent.wrapSignAndSend(api, txs.length > 1 ? api.tx.utility.batchAll(txs) : txs[0], options)
83+
})
84+
)
85+
}
86+
)
87+
88+
function updateTokenPrices() {
89+
const entries = Object.entries(domain.liquidityPools).flatMap(([tid, poolsByCurrency]) => {
90+
return domain.currencies
91+
.filter((cur) => !!poolsByCurrency[cur.address])
92+
.map((cur) => [tid, cur.key] satisfies [string, CurrencyKey])
93+
})
94+
execute(entries, { account: poolAdmin })
95+
}
96+
6897
return (
69-
<Stack>
98+
<Stack gap={1}>
7099
{status === 'inactive' ? (
71100
<EnableButton poolId={poolId} domain={domain} />
72101
) : status === 'deploying' ? (
@@ -104,6 +133,11 @@ function PoolDomain({ poolId, domain, refetch }: { poolId: string; domain: Domai
104133
</AnchorTextLink>
105134
))
106135
)}
136+
{domain.hasDeployedLp && (
137+
<Button onClick={updateTokenPrices} loading={isLoading} small>
138+
Update token prices
139+
</Button>
140+
)}
107141
</Stack>
108142
)
109143
}
@@ -166,7 +200,7 @@ function DeployLPButton({
166200
}
167201

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

180214
return (
181-
<Button loading={isLoading} onClick={() => execute([poolId, domain.chainId, currenciesToAdd], { account })} small>
215+
<Button
216+
loading={isLoading}
217+
onClick={() => execute([poolId, domain.chainId, currenciesToAdd], { account: poolAdmin })}
218+
small
219+
>
182220
Enable
183221
</Button>
184222
)

centrifuge-js/src/modules/liquidityPools.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,21 @@ export function getLiquidityPoolsModule(inst: Centrifuge) {
7777
)
7878
}
7979

80+
function updateTokenPrice(
81+
args: [poolId: string, trancheId: string, currency: CurrencyKey, evmChainId: number],
82+
options?: TransactionOptions
83+
) {
84+
const [poolId, trancheId, currency, evmChainId] = args
85+
const $api = inst.getApi()
86+
87+
return $api.pipe(
88+
switchMap((api) => {
89+
const tx = api.tx.liquidityPools.updateTokenPrice(poolId, trancheId, currency, { EVM: evmChainId })
90+
return inst.wrapSignAndSend(api, tx, options)
91+
})
92+
)
93+
}
94+
8095
function deployTranche(
8196
args: [poolManager: string, poolId: string, trancheId: string],
8297
options: TransactionRequest = {}
@@ -567,6 +582,7 @@ export function getLiquidityPoolsModule(inst: Centrifuge) {
567582
withdraw,
568583
approveForCurrency,
569584
signPermit,
585+
updateTokenPrice,
570586
getDomainRouters,
571587
getManagerFromRouter,
572588
getPool,

centrifuge-js/src/modules/pools.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { StorageKey, u32 } from '@polkadot/types'
44
import { Codec } from '@polkadot/types-codec/types'
55
import { blake2AsHex } from '@polkadot/util-crypto/blake2'
66
import BN from 'bn.js'
7-
import { camelCase } from 'lodash'
87
import { EMPTY, Observable, combineLatest, expand, firstValueFrom, forkJoin, from, of, startWith } from 'rxjs'
98
import { combineLatestWith, filter, map, repeatWhen, switchMap, take, takeLast } from 'rxjs/operators'
109
import { SolverResult, calculateOptimalSolution } from '..'
@@ -1069,7 +1068,7 @@ export function getPoolsModule(inst: Centrifuge) {
10691068
name: metadata.poolName,
10701069
icon: metadata.poolIcon,
10711070
asset: {
1072-
class: camelCase(metadata.assetClass) as PoolMetadata['pool']['asset']['class'],
1071+
class: metadata.assetClass,
10731072
subClass: metadata.subAssetClass,
10741073
},
10751074
issuer: {

0 commit comments

Comments
 (0)