Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions packages/host/src/__tests__/formatAxisPrice-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import {formatAxisPrice} from 'super-app-showcase-sdk';

describe('formatAxisPrice', () => {
it.each([
[
[3865, 3870, 3875, 3880],
['$3.865k', '$3.87k', '$3.875k', '$3.88k'],
],
[
[109000, 109100, 109200],
['$109k', '$109.1k', '$109.2k'],
],
[
[176.5, 177, 177.5, 178],
['$176.5', '$177', '$177.5', '$178'],
],
[
[2.31, 2.32, 2.33],
['$2.31', '$2.32', '$2.33'],
],
[
[0.652, 0.654, 0.656],
['$0.652', '$0.654', '$0.656'],
],
])('gives each tick of %j its own label', (ticks, expected) => {
expect(ticks.map(formatAxisPrice)).toEqual(expected);
});
});
2 changes: 1 addition & 1 deletion packages/sdk/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export {useHistoricalPrices} from './hooks/useHistoricalPrices';
export {useFlashAnimation} from './hooks/useFlashAnimation';
export {ASSETS, ASSET_MAP, KRAKEN_PAIR_TO_SYMBOL} from './constants';
export type {Asset, AssetSymbol, PriceUpdate, PriceMap} from './types';
export {formatPrice, formatValue} from './utils/formatPrice';
export {formatPrice, formatValue, formatAxisPrice} from './utils/formatPrice';
export {getAssetIconUri} from './utils/assetIcon';
export {colors} from './theme';
export {default as ConnectionBanner} from './components/ConnectionBanner';
14 changes: 14 additions & 0 deletions packages/sdk/src/utils/formatPrice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,17 @@ export const formatValue = (value: number): string =>
maximumFractionDigits: 2,
})
: '—';

// Chart ticks can sit closer together than a fixed number of decimals shows
// (e.g. $3,865 / $3,870 / $3,875), so keep every significant digit of the
// tick instead of rounding them all to the same label.
export const formatAxisPrice = (value: number): string => {
if (value >= 1000) {
return `$${(value / 1000).toLocaleString('en-US', {
maximumFractionDigits: 3,
})}k`;
}
return `$${value.toLocaleString('en-US', {
maximumFractionDigits: value >= 1 ? 4 : 6,
})}`;
};
12 changes: 2 additions & 10 deletions packages/trading/src/screens/AssetDetailsScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
useAssetPrice,
useHistoricalPrices,
formatPrice,
formatAxisPrice,
colors,
} from 'super-app-showcase-sdk';
import type {TradingStackParamList} from '../navigation/MainNavigator';
Expand Down Expand Up @@ -186,16 +187,7 @@ const AssetDetailsScreen = ({route, navigation}: Props) => {
tickCount: 4,
labelColor: colors.secondary,
axisSide: 'right',
formatYLabel: value => {
const n = Number(value);
if (n >= 1000) {
return `$${(n / 1000).toFixed(1)}k`;
}
if (n >= 1) {
return `$${n.toFixed(0)}`;
}
return `$${n.toFixed(3)}`;
},
formatYLabel: value => formatAxisPrice(Number(value)),
},
]}>
{({points}) => (
Expand Down