or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

i18n.mdindex.mdreact-provider.mdtypes.mdutilities.mdweb3-provider.md
tile.json

utilities.mddocs/

Utility Functions

Helper functions for Web3 operations including address formatting, browser link generation, asset URL handling, and number conversion utilities.

Capabilities

Address Formatting

Utility for ensuring Ethereum addresses have the proper 0x prefix format.

/**
 * Ensures Ethereum address has 0x prefix
 * @param address - Address string with or without 0x prefix
 * @returns Address with 0x prefix guaranteed
 */
function fillAddressWith0x(address: string): `0x${string}`;

Usage Example:

import { fillAddressWith0x } from "@ant-design/web3-common";

// Add 0x prefix to address
const address1 = fillAddressWith0x("abcd1234");
// Result: "0xabcd1234"

// Address already has prefix - returns unchanged
const address2 = fillAddressWith0x("0xef567890");
// Result: "0xef567890"

Number Conversion

Safe conversion of numbers to BigInt for blockchain operations requiring high precision.

/**
 * Safely converts number to BigInt, handling undefined values
 * @param num - Number or BigInt to convert, or undefined
 * @returns BigInt value or undefined if input was undefined
 */
function parseNumberToBigint(num?: number | bigint): bigint | undefined;

Usage Example:

import { parseNumberToBigint } from "@ant-design/web3-common";

// Convert number to BigInt
const amount1 = parseNumberToBigint(1000);
// Result: 1000n

// Handle BigInt input
const amount2 = parseNumberToBigint(500n);
// Result: 500n

// Handle undefined
const amount3 = parseNumberToBigint(undefined);
// Result: undefined

Browser Link Generation

Factory function for creating blockchain explorer link generators.

/**
 * Creates a function that generates blockchain explorer links
 * @param url - Base URL of the blockchain explorer
 * @returns Function that generates explorer links for addresses and transactions
 */
function createGetBrowserLink(url: string): (address: string, type: string) => string;

Usage Example:

import { createGetBrowserLink } from "@ant-design/web3-common";

// Create Etherscan link generator
const getEtherscanLink = createGetBrowserLink("https://etherscan.io");

// Generate address link
const addressLink = getEtherscanLink(
  "0x742d35cc6634c0532925a3b8d6ac013bbce29cd4", 
  "address"
);
// Result: "https://etherscan.io/address/0x742d35cc6634c0532925a3b8d6ac013bbce29cd4"

// Generate transaction link
const txLink = getEtherscanLink(
  "0xabc123def456...", 
  "transaction"
);
// Result: "https://etherscan.io/tx/0xabc123def456..."

// Create Polygon scanner link generator
const getPolygonLink = createGetBrowserLink("https://polygonscan.com");

Web3 Asset URL Handling

Utilities for handling Web3 asset URLs, including IPFS URL conversion.

/**
 * Converts IPFS URLs to HTTP URLs for browser access
 * @param url - Asset URL, may be IPFS protocol or regular HTTP
 * @returns HTTP-accessible URL or undefined if input was undefined
 */
function getWeb3AssetUrl(url?: string): string | undefined;

/**
 * Fetches and parses JSON from a Web3 asset URL
 * @param url - Asset URL to fetch
 * @returns Promise resolving to parsed JSON data
 * @throws Error if URL is not provided or fetch fails
 */
function requestWeb3Asset<T = any>(url: string): Promise<T>;

Usage Examples:

import { getWeb3AssetUrl, requestWeb3Asset } from "@ant-design/web3-common";

// Convert IPFS URL to HTTP
const ipfsUrl = "ipfs://QmYjtig7VJQ6XsnUjqqJvj7QaMcCAwtrgNdahSiFofrE7o";
const httpUrl = getWeb3AssetUrl(ipfsUrl);
// Result: "https://ipfs.io/ipfs/QmYjtig7VJQ6XsnUjqqJvj7QaMcCAwtrgNdahSiFofrE7o"

// Regular HTTP URL passes through unchanged
const regularUrl = getWeb3AssetUrl("https://example.com/asset.json");
// Result: "https://example.com/asset.json"

// Handle undefined
const undefinedUrl = getWeb3AssetUrl(undefined);
// Result: undefined

// Fetch NFT metadata
try {
  const metadata = await requestWeb3Asset<{
    name: string;
    description: string;
    image: string;
  }>("https://api.example.com/nft/123");
  
  console.log(metadata.name);
} catch (error) {
  console.error("Failed to fetch metadata:", error);
}

Test Utilities

Helper functions for testing Web3 applications (primarily for internal use).

/**
 * Mocks the global fetch function for testing purposes
 * @param expect - Expected response object that will be returned by fetch
 */
function mockFetch(expect: any): void;

Usage Example:

import { mockFetch } from "@ant-design/web3-common";

// In test files
describe("API tests", () => {
  beforeEach(() => {
    // Mock fetch to return test data
    mockFetch({
      name: "Test NFT",
      description: "Test description",
      image: "test-image.png"
    });
  });

  it("should fetch NFT metadata", async () => {
    const result = await requestWeb3Asset("test-url");
    expect(result.name).toBe("Test NFT");
  });
});

Complete API Reference

All utility functions are exported from the main package entry point:

import { 
  fillAddressWith0x,
  parseNumberToBigint,
  createGetBrowserLink,
  getWeb3AssetUrl,
  requestWeb3Asset,
  mockFetch 
} from "@ant-design/web3-common";

These utilities provide the foundational functionality needed for Web3 applications, handling common operations like address formatting, blockchain explorer integration, and asset URL management with proper error handling and type safety.