1+ import { CurrencyKey } from '@centrifuge/centrifuge-js'
12import {
23 ConnectionGuard ,
4+ useCentrifugeApi ,
35 useCentrifugeTransaction ,
46 useGetExplorerUrl ,
57 useGetNetworkName ,
@@ -8,12 +10,13 @@ import {
810import { Accordion , Button , IconExternalLink , Shelf , Stack , Text } from '@centrifuge/fabric'
911import React from 'react'
1012import { useParams } from 'react-router'
13+ import { combineLatest , switchMap } from 'rxjs'
1114import { PageSection } from '../../../components/PageSection'
1215import { AnchorTextLink } from '../../../components/TextLink'
1316import { find } from '../../../utils/helpers'
1417import { useEvmTransaction } from '../../../utils/tinlake/useEvmTransaction'
1518import { Domain , useActiveDomains } from '../../../utils/useLiquidityPools'
16- import { useSuitableAccounts } from '../../../utils/usePermissions'
19+ import { usePoolAdmin } from '../../../utils/usePermissions'
1720import { usePool } from '../../../utils/usePools'
1821
1922function getDomainStatus ( domain : Domain ) {
@@ -60,13 +63,39 @@ export function LiquidityPools() {
6063
6164function 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
168202function 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 )
0 commit comments