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
The full Ethereum address
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>
);
}
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>
);
}