0.1.4 • Published 10 months ago

@chrom-ar/utils v0.1.4

Weekly downloads
-
License
MIT
Repository
github
Last release
10 months ago

@chrom-ar/utils

Version License

Utility functions used across Chrom.ar projects.

Installation

npm install @chrom-ar/utils
# or
yarn add @chrom-ar/utils

Usage

Token helpers

import { getTokenAddress, getTokenDecimals, getTokenAmount } from "@chrom-ar/utils";

const chain = "base";
const token = "USDC";

const address = getTokenAddress(chain, token); // "0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913"
const decimals = getTokenDecimals(chain, token); // 6
const amount = getTokenAmount("42", chain, token); // "42000000"

Chain helpers

import { getChainId, getChainName, getEnvironment } from "@chrom-ar/utils";

getChainId("base"); // 8453
getChainName("mainnet"); // "ethereum"
getEnvironment("optimism"); // "Mainnet"

Prices

import { getTokenPrice } from "@chrom-ar/utils";

// USDC, you can get the address by calling getTokenAddress, see above
const price = await getTokenPrice("base", "0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913");

Logger

import { logger, setLogger } from "@chrom-ar/utils";

setLogger({
  ...console,
  success: (msg: string) => console.log("SUCCESS:", msg),
});

logger.info("Ready");

API Reference

Token Functions

  • getTokenAddress(chain: string, token: string): string | null Returns the token address for the specified chain and token symbol.

  • getTokenDecimals(chain: string, token: string): number | null Returns the number of decimals for the specified token on the given chain.

  • getTokenAmount(amount: string, chain: string, token: string): string | null Converts a human-readable amount to raw token units based on token decimals.

  • getTokenInfo(network?: string, address?: string): { chainId: number, symbol: string, decimals: number } Returns token information for a given network and address.

Chain Functions

  • getChainId(network: string): ChainId Returns the chain ID for the given network name.

  • getChainName(chain: string): string | null Returns the standardized chain name for the given chain identifier.

  • isEvmChain(chain: string): boolean Checks if the specified chain is an EVM-compatible chain.

  • getEnvironment(chain: string): "Mainnet" | "Testnet" | "Devnet" | null Returns the environment type for the given chain.

  • getAlchemyChainName(chain: string): string | null Returns the Alchemy-specific chain name for the given chain.

Price Functions

  • getTokenPrice(network: string, tokenAddress: string, currency?: string): Promise Retrieves the current price of a token in the specified currency (default: USD).

  • getTokenPrices(network: string, tokenAddresses: string[], currency?: string): Promise<Record<string, number>> Retrieves prices for multiple tokens in the specified currency (default: USD).

Logger Functions

  • logger: Logger The global logger instance with methods: debug, info, error, warn, and success.

  • setLogger(customLogger: Logger): void Sets a custom logger implementation.

  • resetLogger(): void Resets the logger to the default implementation.

Development

npm run lint
npm run build
0.1.4

10 months ago

0.1.3

10 months ago

0.1.2

10 months ago

0.1.1

10 months ago

0.1.0

11 months ago