Skip to content

Commit 0e5f52d

Browse files
committed
Update 681 to use scientific notation
1 parent 53eebbb commit 0e5f52d

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

app/util.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,19 @@
11
import { USDC_ADDRESS, BASE_CHAIN_ID } from './constants/index';
22

3+
export const formatUsdcAmount = (amount: number) => {
4+
// Convert to 6 decimal places, remove decimal point
5+
const rawAmount = Math.round(amount * 1_000_000);
6+
// Convert to scientific notation if it ends in zeros
7+
const trailingZeros = (rawAmount.toString().match(/0+$/) || [''])[0].length;
8+
if (trailingZeros > 0) {
9+
return `${rawAmount/10**trailingZeros}e${trailingZeros}`;
10+
}
11+
return rawAmount.toString();
12+
}
13+
314
export const GeneratePaymentLink = (amount: number, address: string) : string => {
4-
return `ethereum:${USDC_ADDRESS}@${BASE_CHAIN_ID}/transfer?value=${amount}e10&address=${address}`;
15+
const amountAsUsdc = formatUsdcAmount(amount);
16+
return `ethereum:${USDC_ADDRESS}@${BASE_CHAIN_ID}/transfer?value=${amountAsUsdc}&address=${address}`;
517
}
618

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

0 commit comments

Comments
 (0)