Skip to main content

Overview

The shortenAddress utility function shortens Ethereum addresses to a more readable format.

Usage

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

const address = '0x742d35Cc6634C0532925a3b844Bc9e7595f0bEb7';
const short = shortenAddress(address);
// Returns: "0x742d...bEb7"

Parameters

address
string
required
The full Ethereum address
chars
number
default:"4"
Number of characters to show on each side

Examples

Default Shortening

shortenAddress('0x742d35Cc6634C0532925a3b844Bc9e7595f0bEb7');
// "0x742d...bEb7"

Custom Length

shortenAddress('0x742d35Cc6634C0532925a3b844Bc9e7595f0bEb7', 6);
// "0x742d35...5f0bEb7"

In Component

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

function AddressDisplay({ address }: { address: string }) {
  return (
    <p className="font-mono">
      {shortenAddress(address)}
    </p>
  );
}

With Copy Button

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

function CopyableAddress({ address }: { address: string }) {
  const copy = () => {
    navigator.clipboard.writeText(address);
  };

  return (
    <button onClick={copy} className="flex items-center gap-2">
      <span className="font-mono">{shortenAddress(address)}</span>
      <CopyIcon />
    </button>
  );
}