Skip to main content

Overview

The parseTokenAmount utility function converts human-readable token amounts to wei (smallest unit).

Usage

import { parseTokenAmount } from '@sanketsaagar/polygon-kit';

const amount = parseTokenAmount('1.5', 18);
// Returns: BigInt('1500000000000000000')

Parameters

amount
string
required
The human-readable amount (e.g., “1.5”)
decimals
number
default:"18"
The number of decimals for the token

Examples

Parse MATIC Amount

parseTokenAmount('1', 18);
// BigInt('1000000000000000000')

parseTokenAmount('0.001', 18);
// BigInt('1000000000000000')

Parse USDC Amount

parseTokenAmount('100', 6);
// BigInt('100000000')

In Form

import { parseTokenAmount } from '@sanketsaagar/polygon-kit';
import { useState } from 'react';

function SendForm() {
  const [amount, setAmount] = useState('');

  const handleSend = () => {
    const value = parseTokenAmount(amount, 18);
    // Send transaction with value
  };

  return (
    <div>
      <input
        type="number"
        value={amount}
        onChange={(e) => setAmount(e.target.value)}
        placeholder="Amount"
      />
      <button onClick={handleSend}>Send</button>
    </div>
  );
}