Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixes separators logic disparities #199

Merged
merged 4 commits into from
Nov 15, 2023
Merged
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
100 changes: 17 additions & 83 deletions __tests__/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { displayAmount, displayNumber } from "../src/app/utils";
import { displayNumber } from "../src/app/utils";

// the separators are set to "." and " " for testing purposes
// inside jest.setup.js
describe("displayAmount", () => {
it("sends error message if noDigits is less than 4", () => {
let digits = 3;
Expand All @@ -8,23 +10,23 @@ describe("displayAmount", () => {
];

inputs.forEach(([input, expected]) => {
expect(displayAmount(input, digits)).toBe(expected);
expect(displayNumber(input, digits)).toBe(expected);
});
digits = 0;
inputs.forEach(([input, expected]) => {
expect(displayAmount(input, digits)).toBe(expected);
expect(displayNumber(input, digits)).toBe(expected);
});
digits = -3;
inputs.forEach(([input, expected]) => {
expect(displayAmount(input, digits)).toBe(expected);
expect(displayNumber(input, digits)).toBe(expected);
});
digits = -10;
inputs.forEach(([input, expected]) => {
expect(displayAmount(input, digits)).toBe(expected);
expect(displayNumber(input, digits)).toBe(expected);
});
});

it("displays amounts in 4 digits with thousands_separator", () => {
it("displays amounts in 4 digits", () => {
const digits = 4;
const inputs: [number, string][] = [
[0, "0"],
Expand Down Expand Up @@ -54,47 +56,11 @@ describe("displayAmount", () => {
];

inputs.forEach(([input, expected]) => {
expect(displayAmount(input, digits)).toBe(expected);
expect(displayNumber(input, digits)).toBe(expected);
});
});

it("displays amounts in 4 digits without thousands_separator", () => {
const digits = 4;
const inputs: [number, string][] = [
[0, "0"],
[0.1, "0.1"],
[0.101, "0.1"],
[0.12, "0.12"],
[0.123, "0.12"],
[0.1234, "0.12"],
[0.123456, "0.12"],
[0.1234567, "0.12"],
[0.12345678, "0.12"],
[0.123456789, "0.12"],
[1.101, "1.1"],
[1.1234567, "1.12"],
[12.1234567, "12.1"],
[123.1234567, "123"],
[1234.1234567, "1234"],
[1, "1"],
[12, "12"],
[123, "123"],
[1234, "1234"],
[1356, "1356"],
[34567, "34K"],
[123456, "123K"],
[1234567, "1.2M"],
[12345678, "12M"],
[123456789, "123M"],
[1234567890, "1.2B"],
];

inputs.forEach(([input, expected]) => {
expect(displayAmount(input, digits, -1, ".", "")).toBe(expected);
});
});

it("displays amounts in 6 digits with thousands_separator", () => {
it("displays amounts in 6 digits", () => {
const digits = 6;
const inputs: [number, string][] = [
[0, "0"],
Expand Down Expand Up @@ -127,11 +93,11 @@ describe("displayAmount", () => {
];

inputs.forEach(([input, expected]) => {
expect(displayAmount(input, digits, -1, ".", " ")).toBe(expected);
expect(displayNumber(input, digits, -1)).toBe(expected);
});
});

it("displays amounts in 10 digits with thousands_separator", () => {
it("displays amounts in 10 digits", () => {
const digits = 10;
const inputs: [number, string][] = [
[0, "0"],
Expand Down Expand Up @@ -165,11 +131,11 @@ describe("displayAmount", () => {
];

inputs.forEach(([input, expected]) => {
expect(displayAmount(input, digits, -1, ".", " ")).toBe(expected);
expect(displayNumber(input, digits, -1)).toBe(expected);
});
});

it("displays amounts in 6 digits with thousands_separator and fixed decimals = 3", () => {
it("displays amounts in 6 digits with fixed decimals = 3", () => {
const digits = 6;
const inputs: [number, string][] = [
[0, "0.000"],
Expand Down Expand Up @@ -203,11 +169,11 @@ describe("displayAmount", () => {
];

inputs.forEach(([input, expected]) => {
expect(displayAmount(input, digits, 3, ".", " ")).toBe(expected);
expect(displayNumber(input, digits, 3)).toBe(expected);
});
});

it("displays amounts in 10 digits with thousands_separator and fixed decimals = 3", () => {
it("displays amounts in 10 digits with fixed decimals = 3", () => {
const digits = 10;
const inputs: [number, string][] = [
[0, "0.000"],
Expand Down Expand Up @@ -241,39 +207,7 @@ describe("displayAmount", () => {
];

inputs.forEach(([input, expected]) => {
expect(displayAmount(input, digits, 3, ".", " ")).toBe(expected);
});
});
});

describe("displayNumber", () => {
it("formats all numbers to fixed decimal places", () => {
const inputs = [
1002, 1248, 99.95, 88, 77.7, 1000000.1, 0.03, 0.000004, 0.1,
];
const expected = [
"1K",
"1.25K",
"99.95000",
"88.00000",
"77.70000",
"1M",
"0.03000",
"0.00000",
"0.10000",
];

inputs.forEach((input, index) => {
expect(displayNumber(input, 5)).toBe(expected[index]);
});
});

it("rounds decimals correctly", () => {
const inputs = [0.233, 0.235];
const expected = ["0.23", "0.24"];

inputs.forEach((input, index) => {
expect(displayNumber(input, 2)).toBe(expected[index]);
expect(displayNumber(input, digits, 3)).toBe(expected);
});
});
});
10 changes: 7 additions & 3 deletions src/app/components/OrderBook.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import * as utils from "../utils";
import { OrderBookRowProps, orderBookSlice } from "../redux/orderBookSlice";
import { useAppDispatch, useAppSelector } from "../hooks";

const N_DIGITS = 8;

function OrderBookRow(props: OrderBookRowProps) {
const { barColor, orderCount, price, size, total, maxTotal } = props;
if (
Expand All @@ -16,9 +18,9 @@ function OrderBookRow(props: OrderBookRowProps) {
typeof maxTotal !== "undefined"
) {
const charactersToDisplay = 6;
const priceString = utils.displayAmount(price, charactersToDisplay);
const sizeString = utils.displayAmount(size, charactersToDisplay);
const totalString = utils.displayAmount(total, charactersToDisplay);
const priceString = utils.displayNumber(price, charactersToDisplay);
const sizeString = utils.displayNumber(size, charactersToDisplay);
const totalString = utils.displayNumber(total, charactersToDisplay);
const barWidth = `${(total / maxTotal) * 100}%`;

const barStyle = {
Expand Down Expand Up @@ -70,10 +72,12 @@ function CurrentPriceRow() {
if (orderBook.spreadPercent !== null && orderBook.spread !== null) {
const spread = utils.displayPositiveNumber(
orderBook.spread,
N_DIGITS,
priceMaxDecimals
);
const spreadPercent = utils.displayPositiveNumber(
orderBook.spreadPercent,
N_DIGITS,
2
);

Expand Down
16 changes: 8 additions & 8 deletions src/app/components/PriceChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
initializeLegend,
} from "../redux/priceChartSlice";
import { useAppDispatch, useAppSelector } from "../hooks";
import { displayAmount } from "../utils";
import { displayNumber } from "../utils";
import * as tailwindConfig from "../../../tailwind.config";

interface PriceChartProps {
Expand Down Expand Up @@ -37,29 +37,29 @@ function PriceChartCanvas(props: PriceChartProps) {
const noDigits = 8;
const fixedDecimals = 6;

const volume = displayAmount(props.volume || 0, noDigits, 2);
const percChange = displayAmount(props.percChange || 0, noDigits, 2);
const change = displayAmount(props.change || 0, noDigits, 2);
const volume = displayNumber(props.volume || 0, noDigits, 2);
const percChange = displayNumber(props.percChange || 0, noDigits, 2);
const change = displayNumber(props.change || 0, noDigits, 2);

const candleOpen = displayAmount(
const candleOpen = displayNumber(
candlePrice?.open || 0,
noDigits,
fixedDecimals
);

const candleHigh = displayAmount(
const candleHigh = displayNumber(
candlePrice?.high || 0,
noDigits,
fixedDecimals
);

const candleLow = displayAmount(
const candleLow = displayNumber(
candlePrice?.low || 0,
noDigits,
fixedDecimals
);

const candleClose = displayAmount(
const candleClose = displayNumber(
candlePrice?.close || 0,
noDigits,
fixedDecimals
Expand Down
12 changes: 6 additions & 6 deletions src/app/components/PriceInfo.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import React from "react";
import { useAppSelector } from "../hooks";
import { displayAmount } from "../utils";
import { displayNumber } from "../utils";

export function PriceInfo() {
const priceInfo = useAppSelector((state) => state.priceInfo);
const noDigits = 4;
const fixedDecimals = 3;
const lastPrice = displayAmount(priceInfo.lastPrice, noDigits, fixedDecimals);
const change = displayAmount(priceInfo.change24h, noDigits, fixedDecimals);
const high = displayAmount(priceInfo.high24h, noDigits, fixedDecimals);
const low = displayAmount(priceInfo.low24h, noDigits, fixedDecimals);
const volume = displayAmount(priceInfo.value24h, noDigits, fixedDecimals);
const lastPrice = displayNumber(priceInfo.lastPrice, noDigits, fixedDecimals);
const change = displayNumber(priceInfo.change24h, noDigits, fixedDecimals);
const high = displayNumber(priceInfo.high24h, noDigits, fixedDecimals);
const low = displayNumber(priceInfo.low24h, noDigits, fixedDecimals);
const volume = displayNumber(priceInfo.value24h, noDigits, fixedDecimals);
const isNegativeOrZero = priceInfo.isNegativeOrZero;
const basePair = "XRD";

Expand Down
11 changes: 3 additions & 8 deletions src/app/components/order_input/AmountInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
selectValidationByAddress,
} from "redux/orderInputSlice";
import { BottomRightErrorLabel } from "components/BottomRightErrorLabel";
import { getLocaleSeparators } from "utils";

export const enum PayReceive {
PAY = "YOU PAY:",
Expand Down Expand Up @@ -43,14 +44,8 @@ export function AmountInput(props: TokenInputFiledProps) {
onChange,
} = props;

// TODO: after https://github.com/DeXter-on-Radix/website/pull/159
// read this from the state instead
const getDecimalSeparator = () => {
const numberWithDecimal = 1.1;
return numberWithDecimal.toLocaleString().substring(1, 2);
};

const placeholder = `0${getDecimalSeparator()}0`;
const { decimalSeparator } = getLocaleSeparators();
const placeholder = `0${decimalSeparator}0`;

return (
<div className="form-control my-2">
Expand Down
6 changes: 3 additions & 3 deletions src/app/redux/orderInputSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
import * as adex from "alphadex-sdk-js";
import { SdkResult } from "alphadex-sdk-js/lib/models/sdk-result";
import { RDT, getRdt } from "../subscriptions";
import { RoundType, displayAmount, roundTo } from "../utils";
import { RoundType, displayNumber, roundTo } from "../utils";
import { fetchAccountHistory } from "./accountHistorySlice";
import { selectBestBuy, selectBestSell } from "./orderBookSlice";
import { fetchBalances } from "./pairSelectorSlice";
Expand Down Expand Up @@ -438,10 +438,10 @@ function toDescription(quote: Quote): string {

if (quote.fromAmount > 0 && quote.toAmount > 0) {
description +=
`Sending ${displayAmount(quote.fromAmount, 8)} ${
`Sending ${displayNumber(quote.fromAmount, 8)} ${
quote.fromToken.symbol
} ` +
`to receive ${displayAmount(quote.toAmount, 8)} ${
`to receive ${displayNumber(quote.toAmount, 8)} ${
quote.toToken.symbol
}.\n`;
}
Expand Down
Loading