2022-12-28 17:37:19 +00:00
import { useCallback , useMemo , useState } from 'react' ;
import {
NetworkParams ,
useNetworkParams ,
2023-05-08 01:01:07 +00:00
} from '@vegaprotocol/network-parameters' ;
import { useDataProvider } from '@vegaprotocol/data-provider' ;
2023-05-18 11:22:54 +00:00
import type { MarketData } from '@vegaprotocol/markets' ;
import { marketDataProvider , marketProvider } from '@vegaprotocol/markets' ;
2022-12-28 17:37:19 +00:00
import { HeaderStat } from '../header' ;
2023-02-28 18:56:29 +00:00
import {
2023-04-07 17:00:57 +00:00
ExternalLink ,
2023-02-28 18:56:29 +00:00
Indicator ,
KeyValueTable ,
KeyValueTableRow ,
Link ,
} from '@vegaprotocol/ui-toolkit' ;
2022-12-28 17:37:19 +00:00
import BigNumber from 'bignumber.js' ;
import { useCheckLiquidityStatus } from '@vegaprotocol/liquidity' ;
2023-01-09 14:39:03 +00:00
import { AuctionTrigger , MarketTradingMode } from '@vegaprotocol/types' ;
2023-02-28 18:56:29 +00:00
import {
addDecimalsFormatNumber ,
formatNumberPercentage ,
} from '@vegaprotocol/utils' ;
import { t } from '@vegaprotocol/i18n' ;
2023-05-17 10:10:31 +00:00
import { DocsLinks } from '@vegaprotocol/environment' ;
2022-12-28 17:37:19 +00:00
interface Props {
marketId? : string ;
noUpdate? : boolean ;
assetDecimals : number ;
}
export const MarketLiquiditySupplied = ( {
marketId ,
assetDecimals ,
noUpdate = false ,
} : Props ) = > {
const [ market , setMarket ] = useState < MarketData > ( ) ;
const { params } = useNetworkParams ( [
2023-01-06 15:35:32 +00:00
NetworkParams . market_liquidity_stakeToCcyVolume ,
2022-12-28 17:37:19 +00:00
NetworkParams . market_liquidity_targetstake_triggering_ratio ,
] ) ;
2023-01-06 15:35:32 +00:00
const stakeToCcyVolume = params . market_liquidity_stakeToCcyVolume ;
2022-12-28 17:37:19 +00:00
const triggeringRatio = Number (
params . market_liquidity_targetstake_triggering_ratio
) ;
const variables = useMemo (
( ) = > ( {
2023-02-09 14:20:31 +00:00
marketId : marketId || '' ,
2022-12-28 17:37:19 +00:00
} ) ,
[ marketId ]
) ;
2023-02-09 14:20:31 +00:00
const { data } = useDataProvider ( {
2022-12-28 17:37:19 +00:00
dataProvider : marketProvider ,
variables ,
skip : ! marketId ,
} ) ;
const update = useCallback (
( { data : marketData } : { data : MarketData | null } ) = > {
if ( ! noUpdate && marketData ) {
setMarket ( marketData ) ;
}
return true ;
} ,
[ noUpdate ]
) ;
2023-03-09 10:03:50 +00:00
useDataProvider ( {
2022-12-28 17:37:19 +00:00
dataProvider : marketDataProvider ,
update ,
variables ,
skip : noUpdate || ! marketId || ! data ,
} ) ;
const supplied = market ? . suppliedStake
? addDecimalsFormatNumber (
new BigNumber ( market ? . suppliedStake )
. multipliedBy ( stakeToCcyVolume || 1 )
. toString ( ) ,
assetDecimals
)
: '-' ;
2023-01-06 16:58:55 +00:00
const { percentage , status } = useCheckLiquidityStatus ( {
2022-12-28 17:37:19 +00:00
suppliedStake : market?.suppliedStake || 0 ,
targetStake : market?.targetStake || 0 ,
triggeringRatio ,
} ) ;
2023-01-09 14:39:03 +00:00
const showMessage =
percentage . gte ( 100 ) &&
market ? . marketTradingMode ===
MarketTradingMode . TRADING_MODE_MONITORING_AUCTION &&
2023-03-21 16:19:21 +00:00
market . trigger ===
AuctionTrigger . AUCTION_TRIGGER_UNABLE_TO_DEPLOY_LP_ORDERS ;
2023-01-09 14:39:03 +00:00
2023-01-16 17:36:06 +00:00
const description = marketId ? (
2022-12-28 17:37:19 +00:00
< section >
2023-02-28 18:56:29 +00:00
< KeyValueTable >
< KeyValueTableRow >
< span > { t ( 'Supplied stake' ) } < / span >
< span >
{ market ? . suppliedStake
? addDecimalsFormatNumber (
new BigNumber ( market ? . suppliedStake ) . toString ( ) ,
assetDecimals
)
: '-' }
< / span >
< / KeyValueTableRow >
< KeyValueTableRow >
< span > { t ( 'Target stake' ) } < / span >
< span >
{ market ? . targetStake
? addDecimalsFormatNumber (
new BigNumber ( market ? . targetStake ) . toString ( ) ,
assetDecimals
)
: '-' }
< / span >
< / KeyValueTableRow >
< / KeyValueTable >
2022-12-28 17:37:19 +00:00
< br / >
2023-01-16 17:36:06 +00:00
< Link href = { ` /#/liquidity/ ${ marketId } ` } data - testid = "view-liquidity-link" >
{ t ( 'View liquidity provision table' ) }
< / Link >
2023-05-17 10:10:31 +00:00
{ DocsLinks && (
< ExternalLink href = { DocsLinks . LIQUIDITY } className = "mt-2" >
2023-04-07 17:00:57 +00:00
{ t ( 'Learn about providing liquidity' ) }
< / ExternalLink >
) }
2023-01-09 14:39:03 +00:00
{ showMessage && (
< p className = "mt-4" >
{ t (
2023-02-22 17:05:31 +00:00
'The market has sufficient liquidity but there are not enough priced limit orders in the order book, which are required to deploy liquidity commitment pegged orders.'
2023-01-09 14:39:03 +00:00
) }
< / p >
) }
2022-12-28 17:37:19 +00:00
< / section >
2023-01-16 17:36:06 +00:00
) : (
'-'
2022-12-28 17:37:19 +00:00
) ;
2023-01-16 17:36:06 +00:00
return marketId ? (
2022-12-28 17:37:19 +00:00
< HeaderStat
heading = { t ( 'Liquidity supplied' ) }
description = { description }
testId = "liquidity-supplied"
>
2023-01-06 16:58:55 +00:00
< Indicator variant = { status } / >
2023-01-09 14:39:03 +00:00
{ supplied } (
{ percentage . gt ( 100 ) ? '>100%' : formatNumberPercentage ( percentage , 2 ) } )
2022-12-28 17:37:19 +00:00
< / HeaderStat >
2023-01-16 17:36:06 +00:00
) : (
< HeaderStat heading = { t ( 'Liquidity supplied' ) } testId = "liquidity-supplied" >
{ '-' }
< / HeaderStat >
2022-12-28 17:37:19 +00:00
) ;
} ;