Skip to content

Commit

Permalink
Update 681 to use scientific notation
Browse files Browse the repository at this point in the history
  • Loading branch information
amhedcb committed Dec 12, 2024
1 parent 53eebbb commit 0e5f52d
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion app/util.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
import { USDC_ADDRESS, BASE_CHAIN_ID } from './constants/index';

export const formatUsdcAmount = (amount: number) => {
// Convert to 6 decimal places, remove decimal point
const rawAmount = Math.round(amount * 1_000_000);
// Convert to scientific notation if it ends in zeros
const trailingZeros = (rawAmount.toString().match(/0+$/) || [''])[0].length;
if (trailingZeros > 0) {
return `${rawAmount/10**trailingZeros}e${trailingZeros}`;
}
return rawAmount.toString();
}

export const GeneratePaymentLink = (amount: number, address: string) : string => {
return `ethereum:${USDC_ADDRESS}@${BASE_CHAIN_ID}/transfer?value=${amount}e10&address=${address}`;
const amountAsUsdc = formatUsdcAmount(amount);
return `ethereum:${USDC_ADDRESS}@${BASE_CHAIN_ID}/transfer?value=${amountAsUsdc}&address=${address}`;
}

export const copyToClipboard = (text: string, toast:any): void => {
Expand Down

0 comments on commit 0e5f52d

Please sign in to comment.