Skip to content

Commit

Permalink
fix: max button using rounded up value (#103)
Browse files Browse the repository at this point in the history
  • Loading branch information
meeh0w authored Dec 4, 2024
1 parent f7c9f43 commit 62dabf9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 32 deletions.
43 changes: 15 additions & 28 deletions src/components/common/BNInput.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import React, { WheelEvent, useEffect, useState } from 'react';
import Big from 'big.js';
import {
Stack,
TextField,
Expand All @@ -8,12 +7,9 @@ import {
styled,
CircularProgress,
} from '@avalabs/core-k2-components';
import { TokenUnit } from '@avalabs/core-utils-sdk';
import { bigIntToString } from '@avalabs/core-utils-sdk';
import { stringToBigint } from '@src/utils/stringToBigint';

Big.PE = 99;
Big.NE = -18;

export interface BNInputProps {
value?: bigint;
denomination: number;
Expand All @@ -37,7 +33,7 @@ const InputNumber = styled(TextField)`
padding: 0;
`;

function splitBN(val: string) {
function splitValue(val: string) {
return val.includes('.') ? val.split('.') : [val, null];
}

Expand Down Expand Up @@ -69,16 +65,15 @@ export function BNInput({
}

if (value) {
const valueAsBig = new Big(value.toString()).div(
Math.pow(10, denomination)
);
const valueAsString = bigIntToString(value, denomination);
const oldValue = stringToBigint(valStr, denomination);
/**
* When deleting zeros after decimal, all zeros delete without this check.
* This also preserves zeros in the input ui.
*/

if (!valStr || !valueAsBig.eq(valStr)) {
setValStr(valueAsBig.toString());
if (!valStr || value !== oldValue) {
setValStr(valueAsString);
}
}
}, [denomination, valStr, value]);
Expand All @@ -88,28 +83,21 @@ export function BNInput({
* Split the input and make sure the right side never exceeds
* the denomination length
*/
const [, endValue] = splitBN(newValueString); // renamed callback param
const [, endValue] = splitValue(newValueString); // renamed callback param

if (!endValue || endValue.length <= denomination) {
const newValue = new TokenUnit(
stringToBigint(newValueString || '0', denomination),
denomination,
''
);
const newValue = stringToBigint(newValueString || '0', denomination);

if (newValue.toSubUnit() < min) {
if (newValue < min) {
return;
}

const oldValue = new TokenUnit(
stringToBigint(valStr || '0', denomination),
denomination,
''
);
if (!newValue.eq(oldValue)) {
const oldValue = stringToBigint(valStr || '0', denomination);

if (newValue !== oldValue) {
onChange?.({
amount: newValue.toDisplay(),
bigint: newValue.toSubUnit(),
amount: bigIntToString(newValue || 0n, denomination),
bigint: newValue,
});
}
setValStr(newValueString);
Expand All @@ -122,8 +110,7 @@ export function BNInput({
return;
}

const big = new Big(max.toString()).div(Math.pow(10, denomination));
onValueChanged(big.toString());
onValueChanged(bigIntToString(max, denomination));
};

const isMaxBtnVisible = max && max > 0n;
Expand Down
8 changes: 4 additions & 4 deletions src/pages/Send/components/ContactSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,8 @@ export const ContactSelect = ({
return;
}
const result = walletAccount.map(
({ addressC, name, addressBTC, addressPVM, addressAVM }) => ({
id: '',
({ id, addressC, name, addressBTC, addressPVM, addressAVM }) => ({
id,
address: network?.vmName == NetworkVMType.EVM ? addressC : '',
addressBTC:
network?.vmName === NetworkVMType.BITCOIN ? addressBTC : '',
Expand All @@ -166,8 +166,8 @@ export const ContactSelect = ({
}

const formattedImported = importedAccountToPrep?.map(
({ addressC, name, addressBTC, addressPVM, addressAVM }) => ({
id: '',
({ id, addressC, name, addressBTC, addressPVM, addressAVM }) => ({
id,
address: network?.vmName == NetworkVMType.EVM ? addressC : '',
addressBTC:
network?.vmName === NetworkVMType.BITCOIN && addressBTC
Expand Down

0 comments on commit 62dabf9

Please sign in to comment.