Skip to content

Commit 8721e27

Browse files
committed
refactor: use big.js to handle large numbers that JS cannot support
1 parent 802fff3 commit 8721e27

33 files changed

+315
-253
lines changed

app/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@
2121
"@emotion/core": "10.0.28",
2222
"@emotion/styled": "10.0.27",
2323
"@improbable-eng/grpc-web": "0.12.0",
24+
"@types/big.js": "4.0.5",
2425
"@types/file-saver": "2.0.1",
2526
"@types/react-virtualized": "^9.21.9",
27+
"big.js": "5.2.2",
2628
"debug": "4.1.1",
2729
"emotion-theming": "10.0.27",
2830
"file-saver": "2.0.2",

app/src/__stories__/ChannelBalance.stories.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ export default {
1212

1313
const getChannel = (store: Store, ratio: number) => {
1414
const channel = new Channel(store, lndListChannelsOne.channelsList[0]);
15-
channel.localBalance = channel.capacity * ratio;
16-
channel.remoteBalance = channel.capacity * (1 - ratio);
15+
channel.localBalance = channel.capacity.mul(ratio);
16+
channel.remoteBalance = channel.capacity.mul(1 - ratio);
1717
return channel;
1818
};
1919

app/src/__stories__/ChannelRow.stories.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ const renderStory = (
2020
},
2121
) => {
2222
if (options && options.ratio) {
23-
channel.localBalance = channel.capacity * options.ratio;
24-
channel.remoteBalance = channel.capacity * (1 - options.ratio);
23+
channel.localBalance = channel.capacity.mul(options.ratio);
24+
channel.remoteBalance = channel.capacity.mul(1 - options.ratio);
2525
}
2626
return useObserver(() => (
2727
<div style={{ paddingTop: 50 }}>

app/src/__stories__/LoopActions.stories.tsx

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@ export default {
77
title: 'Components/Loop Actions',
88
component: LoopActions,
99
parameters: { contained: true },
10-
decorators: [
11-
(storyFn: any) => <div style={{ width: 600, margin: 'auto' }}>{storyFn()}</div>,
12-
],
1310
};
1411

1512
export const Default = () => {
@@ -19,7 +16,15 @@ export const Default = () => {
1916
export const Opened = () => {
2017
const store = useStore();
2118
store.buildSwapStore.startSwap();
22-
// select 3 channels
19+
lndListChannels.channelsList.slice(0, 1).forEach(c => {
20+
store.buildSwapStore.toggleSelectedChannel(c.chanId);
21+
});
22+
return <LoopActions />;
23+
};
24+
25+
export const LoopInWarn = () => {
26+
const store = useStore();
27+
store.buildSwapStore.startSwap();
2328
lndListChannels.channelsList.slice(0, 3).forEach(c => {
2429
store.buildSwapStore.toggleSelectedChannel(c.chanId);
2530
});

app/src/__stories__/NodeStatus.stories.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import React, { useEffect } from 'react';
2+
import Big from 'big.js';
23
import { useStore } from 'store';
34
import NodeStatus from 'components/NodeStatus';
45

@@ -12,7 +13,7 @@ export const Default = () => {
1213
const store = useStore();
1314
useEffect(() => {
1415
const { channelBalance, walletBalance } = store.nodeStore.wallet;
15-
store.nodeStore.wallet = { channelBalance: 0, walletBalance: 0 };
16+
store.nodeStore.wallet = { channelBalance: Big(0), walletBalance: Big(0) };
1617

1718
// change back to sample data when the component is unmounted
1819
return () => {

app/src/__stories__/Range.stories.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import React, { useState } from 'react';
2+
import Big from 'big.js';
23
import Range from 'components/common/Range';
34

45
export default {
@@ -8,16 +9,16 @@ export default {
89
};
910

1011
export const Default = () => {
11-
const [value, setValue] = useState(50);
12+
const [value, setValue] = useState(Big(50));
1213
return <Range value={value} onChange={setValue} />;
1314
};
1415

1516
export const StepByFive = () => {
16-
const [value, setValue] = useState(50);
17+
const [value, setValue] = useState(Big(50));
1718
return <Range value={value} onChange={setValue} step={5} />;
1819
};
1920

2021
export const WithMinMax = () => {
21-
const [value, setValue] = useState(50);
22+
const [value, setValue] = useState(Big(50));
2223
return <Range value={value} onChange={setValue} showRadios />;
2324
};

app/src/__stories__/SwapWizard.stories.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React from 'react';
22
import { observable } from 'mobx';
33
import { SwapDirection } from 'types/state';
4+
import Big from 'big.js';
45
import { lndListChannels } from 'util/tests/sampleData';
56
import { useStore } from 'store';
67
import SwapWizard from 'components/loop/swap/SwapWizard';
@@ -35,7 +36,7 @@ export const Step2Fees = () => {
3536
build.startSwap();
3637
build.selectedChanIds = selectedChannels;
3738
build.setDirection(SwapDirection.OUT);
38-
build.setAmount(500000);
39+
build.setAmount(Big(500000));
3940
build.goToNextStep();
4041
return <SwapWizard />;
4142
};
@@ -45,7 +46,7 @@ export const Step3Processing = () => {
4546
build.startSwap();
4647
build.selectedChanIds = selectedChannels;
4748
build.setDirection(SwapDirection.OUT);
48-
build.setAmount(500000);
49+
build.setAmount(Big(500000));
4950
build.goToNextStep();
5051
build.goToNextStep();
5152
return <SwapWizard />;

app/src/__tests__/components/NodeStatus.spec.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import React from 'react';
2+
import Big from 'big.js';
23
import { renderWithProviders } from 'util/tests';
34
import NodeStatus from 'components/NodeStatus';
45

@@ -14,13 +15,13 @@ describe('NodeStatus component', () => {
1415

1516
it('should display the lightning balance', () => {
1617
const { getByText, store } = render();
17-
store.nodeStore.wallet = { channelBalance: 123, walletBalance: 0 };
18+
store.nodeStore.wallet = { channelBalance: Big(123), walletBalance: Big(0) };
1819
expect(getByText('123 sats')).toBeInTheDocument();
1920
});
2021

2122
it('should display the bitcoin balance', () => {
2223
const { getByText, store } = render();
23-
store.nodeStore.wallet = { channelBalance: 0, walletBalance: 234 };
24+
store.nodeStore.wallet = { channelBalance: Big(0), walletBalance: Big(234) };
2425
expect(getByText('234')).toBeInTheDocument();
2526
});
2627
});

app/src/__tests__/components/common/Range.spec.tsx

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
import React from 'react';
22
import { fireEvent } from '@testing-library/react';
3+
import Big from 'big.js';
34
import { renderWithProviders } from 'util/tests';
45
import Range from 'components/common/Range';
56

67
describe('Range component', () => {
78
const handleChange = jest.fn();
89

910
const render = (options?: {
10-
value?: number;
11-
min?: number;
12-
max?: number;
11+
value?: Big;
12+
min?: Big;
13+
max?: Big;
1314
step?: number;
1415
showRadios?: boolean;
1516
}) => {
@@ -34,9 +35,9 @@ describe('Range component', () => {
3435

3536
it('should accept custom props', () => {
3637
const { getByText, getByLabelText } = render({
37-
value: 5000,
38-
min: 2500,
39-
max: 7500,
38+
value: Big(5000),
39+
min: Big(2500),
40+
max: Big(7500),
4041
step: 100,
4142
showRadios: true,
4243
});
@@ -49,30 +50,30 @@ describe('Range component', () => {
4950
});
5051

5152
it('should highlight Min radio when value equals min', () => {
52-
const { getByText } = render({ value: 0, showRadios: true });
53+
const { getByText } = render({ value: Big(0), showRadios: true });
5354
expect(getByText('Min')).toHaveAttribute('aria-checked', 'true');
5455
});
5556

5657
it('should highlight Max radio when value equals max', () => {
57-
const { getByText } = render({ value: 100, showRadios: true });
58+
const { getByText } = render({ value: Big(100), showRadios: true });
5859
expect(getByText('Max')).toHaveAttribute('aria-checked', 'true');
5960
});
6061

6162
it('should trigger the event when slider changed', () => {
6263
const { getByLabelText } = render();
6364
fireEvent.change(getByLabelText('range-slider'), { target: { value: '25' } });
64-
expect(handleChange).toBeCalledWith(25);
65+
expect(handleChange).toBeCalledWith(Big(25));
6566
});
6667

6768
it('should trigger the event when min button clicked', () => {
6869
const { getByText } = render({ showRadios: true });
6970
fireEvent.click(getByText('Min'));
70-
expect(handleChange).toBeCalledWith(0);
71+
expect(handleChange).toBeCalledWith(Big(0));
7172
});
7273

7374
it('should trigger the event when max button clicked', () => {
7475
const { getByText } = render({ showRadios: true });
7576
fireEvent.click(getByText('Max'));
76-
expect(handleChange).toBeCalledWith(100);
77+
expect(handleChange).toBeCalledWith(Big(100));
7778
});
7879
});

app/src/__tests__/components/loop/ChannelBalance.spec.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ describe('ChannelBalance component', () => {
1414
const render = (ratio: number, active = true) => {
1515
const store = createStore();
1616
channel = new Channel(store, lndListChannelsOne.channelsList[0]);
17-
channel.localBalance = channel.capacity * ratio;
18-
channel.remoteBalance = channel.capacity * (1 - ratio);
17+
channel.localBalance = channel.capacity.mul(ratio);
18+
channel.remoteBalance = channel.capacity.mul(1 - ratio);
1919
channel.active = active;
2020

2121
const result = renderWithProviders(<ChannelBalance channel={channel} />, store);

0 commit comments

Comments
 (0)