Files
radicle-explorer/src/utils.ts
T

62 lines
1.8 KiB
TypeScript

import { ethers } from "ethers";
import type { BigNumber } from "ethers";
import type { Config } from '@app/config';
export function formatBalance(n: BigNumber) {
return ethers.utils.commify(parseFloat(ethers.utils.formatUnits(n)).toFixed(2));
}
export function formatAddress(addr: string) {
return addr.substring(0, 6)
+ '...'
+ addr.substring(addr.length - 4, addr.length);
}
export function capitalize(s: string) {
if (s === "") return s;
return s[0].toUpperCase() + s.substr(1);
}
// Takes a domain name, eg. 'cloudhead.radicle.eth' and
// returns the label, eg. 'cloudhead', otherwise `null`.
export function parseEnsLabel(name: string, config: Config): string {
let domain = config.registrar.domain.replace(".", "\\.");
let label = name.replace(new RegExp(`\\.${domain}$`), "");
return label;
}
// Return the current unix time.
export function unixTime(): number {
return Math.floor(Date.now() / 1000);
}
// Check whether the input is a Radicle ID.
export function isRadicleId(input: string): boolean {
return /^rad:[a-z]+:[a-zA-Z0-9]+$/.test(input);
}
// Check whether the input is a URL.
export function isUrl(input: string): boolean {
return /^https?:\/\//.test(input);
}
// Check whether the input is an Ethereum address.
export function isAddress(input: string): boolean {
return ethers.utils.isAddress(input);
}
// Get search parameters from location.
export function getSearchParam(key: string, location: RouteLocation): string | null {
let params = new URLSearchParams(location.search);
return params.get(key);
}
// Get the explorer link of an address, eg. Etherscan.
export function explorerLink(addr: string, config: Config): string {
if (config.network.name == "ropsten") {
return `https://ropsten.etherscan.io/address/${addr}`;
}
return `https://etherscan.io/address/${addr}`;
}