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

fix: ⚡ fixed the decimal point issue #1352

Merged
merged 3 commits into from
Jan 10, 2025
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
3 changes: 2 additions & 1 deletion src/components/NegligibleTransactionFees.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export type props = {
refMessage: React.RefObject<any>;
onClose: () => void;
transactionFee: TransactionFeeData;
precision: number;
};

const NegligibleTransactionFees = (props: props) => {
Expand All @@ -31,7 +32,7 @@ const NegligibleTransactionFees = (props: props) => {
<View style={styles.content}>
<View>
<Text style={{ ...commonStyles.primaryFontFamily, fontSize: 17 }}>
{props.transactionFee.fee}
{props.transactionFee.fee.toString(props.precision)}
</Text>
<Text style={{ color: theme.colors.secondary2, fontSize: 15 }}>
${formatCurrencyValue(props.transactionFee.usdFee, 2)}
Expand Down
19 changes: 15 additions & 4 deletions src/components/Transaction.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { formatCurrencyValue } from '../utils/numbers';
import Decimal from 'decimal.js';

export type TransactionFeeData = {
fee: string;
fee: IAsset;
usdFee: number;
show: boolean;
isFree: boolean;
Expand Down Expand Up @@ -219,7 +219,13 @@ export function showFee(operations: OperationData[] | unknown, asset: IAsset, us

const NEGLIGIBLE_FEE_USD = 0.001;

export function TransactionFee({ transactionFee }: { transactionFee: TransactionFeeData }) {
export function TransactionFee({
transactionFee,
precision,
}: {
transactionFee: TransactionFeeData;
precision: number;
}) {
const [toolTipVisible, setToolTipVisible] = useState(false);
const refMessage = useRef<{ open: () => void; close: () => void }>(null);
const isNegligible = transactionFee.usdFee <= NEGLIGIBLE_FEE_USD;
Expand All @@ -246,7 +252,7 @@ export function TransactionFee({ transactionFee }: { transactionFee: Transaction
} else {
return (
<>
<Text>{transactionFee.fee}</Text>
<Text>{transactionFee.fee.toString()}</Text>
<Text style={[styles.secondaryColor, commonStyles.secondaryFontFamily]}>
(${formatCurrencyValue(transactionFee.usdFee, 2)})
</Text>
Expand All @@ -257,7 +263,12 @@ export function TransactionFee({ transactionFee }: { transactionFee: Transaction

return (
<View style={styles.appDialog}>
<NegligibleTransactionFees transactionFee={transactionFee} onClose={onClose} refMessage={refMessage} />
<NegligibleTransactionFees
precision={precision}
transactionFee={transactionFee}
onClose={onClose}
refMessage={refMessage}
/>
<View style={{ flexDirection: 'row', justifyContent: 'space-between' }}>
<View style={{ flexDirection: 'row', alignItems: 'center' }}>
<Text style={styles.secondaryColor}>Transaction fee:</Text>
Expand Down
16 changes: 10 additions & 6 deletions src/containers/SignTransactionConsentContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,7 @@ export default function SignTransactionConsentContainer({
if (type === TransactionType.TRANSFER) {
const to = chain.formatShortAccountName((await operation.getTo()).getName());
const value = await operation.getValue();
const precision = chain.getNativeToken().getPrecision();
const amount = value.toString(precision <= 4 ? 4 : 6).replace(/\.?0+$/, '');
const amount = value.toString();

const usdValue = formatCurrencyValue(await value.getUsdValue(), 2);

Expand Down Expand Up @@ -111,7 +110,7 @@ export default function SignTransactionConsentContainer({
const usdFee = await fee.getUsdValue();

const transactionFee: TransactionFeeData = {
fee: fee.toString(4),
fee: fee,
usdFee: usdFee,
show: showFee(operations, fee, usdFee),
isFree: isFree(fee, usdFee),
Expand All @@ -138,15 +137,15 @@ export default function SignTransactionConsentContainer({

const transactionTotal = {
show: showFee(operations, total, usdTotal),
total: total.toString(4),
total: total.toString(),
totalUsd: formatCurrencyValue(usdTotal, 2),
balanceError,
};

setTransactionTotalData(transactionTotal);
debug('fetchTransactionTotal() done', transactionTotal);
},
[chain, request.account, request.transaction]
[chain, request]
);

const fetchOperations = useCallback(async () => {
Expand Down Expand Up @@ -345,7 +344,12 @@ export default function SignTransactionConsentContainer({
{!operations && <TSpinner />}
{operations && <Operations operations={operations} />}
{!transactionFeeData && <TSpinner />}
{transactionFeeData && <TransactionFee transactionFee={transactionFeeData} />}
{transactionFeeData && (
<TransactionFee
transactionFee={transactionFeeData}
precision={chain.getNativeToken().getPrecision()}
/>
)}
{!transactionTotalData && <TSpinner />}
{transactionTotalData && (
<TransactionTotal
Expand Down
13 changes: 7 additions & 6 deletions src/containers/SignTransactionConsentSuccessContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export default function SignTransactionConsentSuccessContainer({
const [total, setTotal] = useState<{ total: string; totalUsd: string } | null>(null);
const [fee, setFee] = useState<TransactionFeeData | null>(null);
const [date, setDate] = useState<Date | null>(null);
const chain = request.transaction.getChain();

const errorStore = useErrorStore();

Expand All @@ -60,17 +61,15 @@ export default function SignTransactionConsentSuccessContainer({
const total = await request.transaction.estimateTransactionTotal();
const usdTotal = await total.getUsdValue();

const totalString = total.toString(4);
const totalString = total.toString();
const usdTotalString = formatCurrencyValue(usdTotal);

setTotal({ total: totalString, totalUsd: usdTotalString });

const fee = await receipt.getFee();
const usdFee = await fee.getUsdValue();

const feeString = fee.toString(4);

setFee({ fee: feeString, usdFee, show: showFee(operations, fee, usdFee), isFree: isFree(fee, usdFee) });
setFee({ fee: fee, usdFee, show: showFee(operations, fee, usdFee), isFree: isFree(fee, usdFee) });
} catch (e) {
errorStore.setError({
title: 'Error fetching total',
Expand All @@ -81,7 +80,7 @@ export default function SignTransactionConsentSuccessContainer({
}

fetchTransactionDetail();
}, [errorStore, receipt, request.account, request.transaction]);
}, [errorStore, receipt, request.account, request.transaction, operations]);

return (
<LayoutComponent
Expand All @@ -104,7 +103,9 @@ export default function SignTransactionConsentSuccessContainer({
<Operations operations={operations} />
)}
{!fee && <TSpinner />}
{fee && <TransactionFee transactionFee={fee} />}
{fee && (
<TransactionFee transactionFee={fee} precision={chain.getNativeToken().getPrecision()} />
)}
<TButtonSecondaryContained
theme="secondary"
style={{ width: '100%', marginTop: 25 }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export abstract class AssetStorageManager {
const existingAsset = await this.repository.findAssetByName(name);

if (existingAsset) {
const balance = asset.toString(4);
const balance = asset.toString();

debug(`updateAccountBalance() updating ${name} balance to ${balance}`);

Expand Down
4 changes: 3 additions & 1 deletion src/utils/chain/etherum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,9 @@ export class EthereumTransactionReceipt extends AbstractTransactionReceipt {
const receipt = await this.receipt.wait();

if (!receipt) throw new Error('Failed to fetch receipt');
return new Asset(this.chain.getNativeToken(), new Decimal(receipt.fee.toString()));
const precisionMultiplier = new Decimal(10).pow(this.chain.getNativeToken().getPrecision());

return new Asset(this.chain.getNativeToken(), new Decimal(receipt.fee.toString()).div(precisionMultiplier));
}

async getTimestamp(): Promise<Date> {
Expand Down
20 changes: 19 additions & 1 deletion src/utils/chain/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,26 @@ export abstract class AbstractAsset implements IAsset {
getPrecision(): number {
return this.token.getPrecision();
}

printValue(precision?: number): string {
return this.amount.toFixed(precision ?? this.getPrecision());
if (precision) {
return this.amount.toFixed(precision);
} else {
let formattedAmount: string;
const decimalPart = this.amount.toFixed().split('.')[1] || '';

if (this.amount.equals(this.amount.floor())) {
formattedAmount = this.amount.toFixed(1);
} else if (decimalPart.length > 4) {
// If the decimal part exceeds 4 digits, display only 4 decimal places
formattedAmount = this.amount.toFixed(4, Decimal.ROUND_DOWN);
} else {
// If the decimal part is 4 digits or fewer, display as-is
formattedAmount = this.amount.toString();
}

return formattedAmount;
}
}

toString(precision?: number): string {
Expand Down
6 changes: 3 additions & 3 deletions test/utils/antelope.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ describe('AntelopeTransaction', () => {
expect(transaction.getType()).rejects.toThrow(
'Antelope transactions have multiple operations, call getOperations()'
);
expect((await transaction.estimateTransactionFee()).toString()).toBe('0.0000 JungleEOS');
expect((await transaction.estimateTransactionTotal()).toString()).toBe('0.0000 JungleEOS');
expect((await transaction.estimateTransactionFee()).toString()).toBe('0.0 JungleEOS');
expect((await transaction.estimateTransactionTotal()).toString()).toBe('0.0 JungleEOS');

const operations = await transaction.getOperations();
const createAssetOperation = operations[0];
Expand All @@ -57,7 +57,7 @@ describe('AntelopeTransaction', () => {
expect(await createAssetOperation.getType()).toBe(TransactionType.CONTRACT);
expect((await createAssetOperation.getFrom()).getName()).toEqual(jungleAccountName);
expect((await createAssetOperation.getTo()).getName()).toEqual(SIMPLE_ASSSET_CONTRACT_NAME);
expect((await createAssetOperation.getValue()).toString()).toEqual('0.0000 JungleEOS');
expect((await createAssetOperation.getValue()).toString()).toEqual('0.0 JungleEOS');
expect(await createAssetOperation.getFunction()).toEqual('create');
expect(await createAssetOperation.getArguments()).toEqual({
author: jungleAccountName,
Expand Down
Loading