fix(explorer): switch decimals used in lp commitment views (#2747)

This commit is contained in:
Edd 2023-01-26 14:47:31 +00:00 committed by GitHub
parent e10945eeda
commit 33e528c569
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 86 additions and 35 deletions

View File

@ -8,6 +8,9 @@ query ExplorerMarket($id: ID!) {
product { product {
... on Future { ... on Future {
quoteName quoteName
settlementAsset {
decimals
}
} }
} }
} }

View File

@ -8,7 +8,7 @@ export type ExplorerMarketQueryVariables = Types.Exact<{
}>; }>;
export type ExplorerMarketQuery = { __typename?: 'Query', market?: { __typename?: 'Market', id: string, decimalPlaces: number, state: Types.MarketState, tradableInstrument: { __typename?: 'TradableInstrument', instrument: { __typename?: 'Instrument', name: string, product: { __typename?: 'Future', quoteName: string } } } } | null }; export type ExplorerMarketQuery = { __typename?: 'Query', market?: { __typename?: 'Market', id: string, decimalPlaces: number, state: Types.MarketState, tradableInstrument: { __typename?: 'TradableInstrument', instrument: { __typename?: 'Instrument', name: string, product: { __typename?: 'Future', quoteName: string, settlementAsset: { __typename?: 'Asset', decimals: number } } } } } | null };
export const ExplorerMarketDocument = gql` export const ExplorerMarketDocument = gql`
@ -22,6 +22,9 @@ export const ExplorerMarketDocument = gql`
product { product {
... on Future { ... on Future {
quoteName quoteName
settlementAsset {
decimals
}
} }
} }
} }

View File

@ -97,6 +97,10 @@ describe('Order TX Summary component', () => {
product: { product: {
__typename: 'Future', __typename: 'Future',
quoteName: 'TEST', quoteName: 'TEST',
settlementAsset: {
__typeName: 'SettlementAsset',
decimals: 18,
},
}, },
}, },
}, },

View File

@ -3,30 +3,29 @@ import { MockedProvider } from '@apollo/client/testing';
import type { MockedResponse } from '@apollo/client/testing'; import type { MockedResponse } from '@apollo/client/testing';
import { render } from '@testing-library/react'; import { render } from '@testing-library/react';
import PriceInMarket from './price-in-market'; import PriceInMarket from './price-in-market';
import type { DecimalSource } from './price-in-market';
import { ExplorerMarketDocument } from '../links/market-link/__generated__/Market'; import { ExplorerMarketDocument } from '../links/market-link/__generated__/Market';
function renderComponent( function renderComponent(
price: string, price: string,
marketId: string, marketId: string,
mocks: MockedResponse[] mocks: MockedResponse[],
decimalSource: DecimalSource = 'MARKET'
) { ) {
return ( return (
<MockedProvider mocks={mocks} addTypename={false}> <MockedProvider mocks={mocks} addTypename={false}>
<MemoryRouter> <MemoryRouter>
<PriceInMarket marketId={marketId} price={price} /> <PriceInMarket
marketId={marketId}
price={price}
decimalSource={decimalSource}
/>
</MemoryRouter> </MemoryRouter>
</MockedProvider> </MockedProvider>
); );
} }
describe('Price in Market component', () => { const fullMock = {
it('Renders the raw price when there is no market data', () => {
const res = render(renderComponent('100', '123', []));
expect(res.getByText('100')).toBeInTheDocument();
});
it('Renders the formatted price when market data is fetched', async () => {
const mock = {
request: { request: {
query: ExplorerMarketDocument, query: ExplorerMarketDocument,
variables: { variables: {
@ -45,19 +44,37 @@ describe('Price in Market component', () => {
product: { product: {
__typename: 'Future', __typename: 'Future',
quoteName: 'dai', quoteName: 'dai',
settlementAsset: {
decimals: 18,
}, },
}, },
}, },
}, },
}, },
}, },
}; },
};
const res = render(renderComponent('100', '123', [mock])); describe('Price in Market component', () => {
it('Renders the raw price when there is no market data', () => {
const res = render(renderComponent('100', '123', []));
expect(res.getByText('100')).toBeInTheDocument();
});
it('Renders the formatted price when market data is fetched, using market decimals by default', async () => {
const res = render(renderComponent('100', '123', [fullMock]));
expect(await res.findByText('1.00')).toBeInTheDocument(); expect(await res.findByText('1.00')).toBeInTheDocument();
expect(await res.findByText('dai')).toBeInTheDocument(); expect(await res.findByText('dai')).toBeInTheDocument();
}); });
it('Renders the formatted price when market data is fetched, using settlement decimals', async () => {
const res = render(
renderComponent('100', '123', [fullMock], 'SETTLEMENT_ASSET')
);
expect(await res.findByText('0.0000000000000001')).toBeInTheDocument();
expect(await res.findByText('dai')).toBeInTheDocument();
});
it('Leaves the market id when the market is not found', async () => { it('Leaves the market id when the market is not found', async () => {
const mock = { const mock = {
request: { request: {

View File

@ -3,16 +3,23 @@ import isUndefined from 'lodash/isUndefined';
import { useExplorerMarketQuery } from '../links/market-link/__generated__/Market'; import { useExplorerMarketQuery } from '../links/market-link/__generated__/Market';
import get from 'lodash/get'; import get from 'lodash/get';
export type DecimalSource = 'MARKET' | 'SETTLEMENT_ASSET';
export type PriceInMarketProps = { export type PriceInMarketProps = {
marketId: string; marketId: string;
price: string; price: string;
decimalSource?: DecimalSource;
}; };
/** /**
* Given a market ID and a price it will fetch the market * Given a market ID and a price it will fetch the market
* and format the price in that market's decimal places. * and format the price in that market's decimal places.
*/ */
const PriceInMarket = ({ marketId, price }: PriceInMarketProps) => { const PriceInMarket = ({
marketId,
price,
decimalSource = 'MARKET',
}: PriceInMarketProps) => {
const { data } = useExplorerMarketQuery({ const { data } = useExplorerMarketQuery({
variables: { id: marketId }, variables: { id: marketId },
fetchPolicy: 'cache-first', fetchPolicy: 'cache-first',
@ -20,8 +27,19 @@ const PriceInMarket = ({ marketId, price }: PriceInMarketProps) => {
let label = price; let label = price;
if (data && data.market?.decimalPlaces) { if (data) {
if (decimalSource === 'MARKET' && data.market?.decimalPlaces) {
label = addDecimalsFormatNumber(price, data.market.decimalPlaces); label = addDecimalsFormatNumber(price, data.market.decimalPlaces);
} else if (
decimalSource === 'SETTLEMENT_ASSET' &&
data.market?.tradableInstrument.instrument.product.settlementAsset
) {
label = addDecimalsFormatNumber(
price,
data.market?.tradableInstrument.instrument.product.settlementAsset
.decimals
);
}
} }
const suffix = get( const suffix = get(

View File

@ -55,6 +55,7 @@ export const TxDetailsLiquidityAmendment = ({
<PriceInMarket <PriceInMarket
price={amendment.commitmentAmount} price={amendment.commitmentAmount}
marketId={marketId} marketId={marketId}
decimalSource="SETTLEMENT_ASSET"
/> />
</TableCell> </TableCell>
</TableRow> </TableRow>

View File

@ -54,6 +54,7 @@ export const TxDetailsLiquiditySubmission = ({
<PriceInMarket <PriceInMarket
price={submission.commitmentAmount} price={submission.commitmentAmount}
marketId={marketId} marketId={marketId}
decimalSource="SETTLEMENT_ASSET"
/> />
</TableCell> </TableCell>
</TableRow> </TableRow>

View File

@ -3894,6 +3894,10 @@ export type Statistics = {
chainVersion: Scalars['String']; chainVersion: Scalars['String'];
/** RFC3339Nano current time (real) */ /** RFC3339Nano current time (real) */
currentTime: Scalars['Timestamp']; currentTime: Scalars['Timestamp'];
/** Total number of events on the last block */
eventCount: Scalars['String'];
/** The number of events per second on the last block */
eventsPerSecond: Scalars['String'];
/** RFC3339Nano genesis time of the chain */ /** RFC3339Nano genesis time of the chain */
genesisTime: Scalars['Timestamp']; genesisTime: Scalars['Timestamp'];
/** Number of orders per seconds */ /** Number of orders per seconds */