From 62dabf9b9e78c7ba754957a22dbbede77aa1ff10 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Leszczyk?= Date: Wed, 4 Dec 2024 16:46:57 +0100 Subject: [PATCH] fix: max button using rounded up value (#103) --- src/components/common/BNInput.tsx | 43 +++++++-------------- src/pages/Send/components/ContactSelect.tsx | 8 ++-- 2 files changed, 19 insertions(+), 32 deletions(-) diff --git a/src/components/common/BNInput.tsx b/src/components/common/BNInput.tsx index a78e14d7..4554c396 100644 --- a/src/components/common/BNInput.tsx +++ b/src/components/common/BNInput.tsx @@ -1,5 +1,4 @@ import React, { WheelEvent, useEffect, useState } from 'react'; -import Big from 'big.js'; import { Stack, TextField, @@ -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; @@ -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]; } @@ -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]); @@ -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); @@ -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; diff --git a/src/pages/Send/components/ContactSelect.tsx b/src/pages/Send/components/ContactSelect.tsx index 82612932..dce90124 100644 --- a/src/pages/Send/components/ContactSelect.tsx +++ b/src/pages/Send/components/ContactSelect.tsx @@ -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 : '', @@ -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