11// Copyright (c) 2024 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
22// SPDX-License-Identifier: Apache-2.0
3- import { AmountDisplay , Loading } from '@lfdecentralizedtrust/splice-common-frontend' ;
3+
4+ import {
5+ AmountDisplay ,
6+ Loading ,
7+ medianPriceVotes ,
8+ useVotesHooks ,
9+ } from '@lfdecentralizedtrust/splice-common-frontend' ;
410import BigNumber from 'bignumber.js' ;
511import React , { useMemo } from 'react' ;
612
713import { Stack } from '@mui/material' ;
814import Typography from '@mui/material/Typography' ;
915
10- import { useAmuletPriceVotes } from '../../hooks/useAmuletPriceVotes' ;
11- import { useSvConfig } from '../../utils' ;
16+ interface MedianAmuletPriceProps {
17+ amuletName : string ;
18+ }
1219
13- const MedianAmuletPrice : React . FC = ( ) => {
14- const config = useSvConfig ( ) ;
15- const amuletPriceVotesQuery = useAmuletPriceVotes ( ) ;
16- const amuletName = config . spliceInstanceNames . amuletName ;
20+ export const MedianAmuletPrice : React . FC < MedianAmuletPriceProps > = props => {
21+ const { amuletName } = props ;
1722
18- const median = ( votedPrices : BigNumber [ ] ) => {
19- if ( votedPrices && votedPrices . length > 0 ) {
20- const sorted = [ ...votedPrices ] . sort ( ( a , b ) => {
21- return a . isEqualTo ( b ) ? 0 : a . isLessThan ( b ) ? - 1 : 1 ;
22- } ) ;
23- const length = sorted . length ;
24- const half = Math . floor ( length / 2 ) ;
25- return length % 2 !== 0
26- ? sorted [ half ]
27- : sorted [ half - 1 ] . plus ( sorted [ half ] ) . multipliedBy ( 0.5 ) ;
28- }
29- return undefined ;
30- } ;
23+ const voteHooks = useVotesHooks ( ) ;
24+ const amuletPriceVotesQuery = voteHooks . useAmuletPriceVotes ( ) ;
3125
3226 const amuletPrices = useMemo (
3327 ( ) =>
@@ -38,7 +32,7 @@ const MedianAmuletPrice: React.FC = () => {
3832 ) ;
3933
4034 const medianAmuletPrice = useMemo (
41- ( ) => ( amuletPrices ? median ( amuletPrices ) : undefined ) ,
35+ ( ) => ( amuletPrices ? medianPriceVotes ( amuletPrices ) : undefined ) ,
4236 [ amuletPrices ]
4337 ) ;
4438
@@ -64,5 +58,3 @@ const MedianAmuletPrice: React.FC = () => {
6458 </ Stack >
6559 ) ;
6660} ;
67-
68- export default MedianAmuletPrice ;
0 commit comments