Files
smom-dbis-138/metamask-sdk
defiQUG 304b3b4377
Some checks failed
CI/CD Pipeline / Solidity Contracts (push) Failing after 1m45s
CI/CD Pipeline / Security Scanning (push) Successful in 2m41s
CI/CD Pipeline / Lint and Format (push) Failing after 52s
CI/CD Pipeline / Terraform Validation (push) Failing after 30s
CI/CD Pipeline / Kubernetes Validation (push) Successful in 28s
HYBX OMNL TypeScript & anchor / token-aggregation build + reconcile artifact (push) Failing after 32s
Validation / validate-genesis (push) Successful in 29s
Validation / validate-terraform (push) Failing after 31s
Validation / validate-kubernetes (push) Failing after 14s
Validation / validate-smart-contracts (push) Failing after 12s
Validation / validate-security (push) Failing after 1m28s
Validation / validate-documentation (push) Failing after 20s
Align Chain 138 public RPC URLs for wallets and token-aggregation API.
Use rpc.public-0138.defi-oracle.io as canonical MetaMask addEthereumChain RPC; expand tokenDetection tokenListUrls; update tests.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-06-27 19:13:03 -07:00
..

MetaMask SDK for ChainID 138

MetaMask integration helpers for DeFi Oracle Meta Mainnet (ChainID 138).

Installation

npm install @defi-oracle/metamask-sdk

Or using the source:

cd metamask-sdk
npm install
npm run build

Usage

Add Network to MetaMask

import { addNetwork, addOrSwitchNetwork } from '@defi-oracle/metamask-sdk';

// Add ChainID 138 to MetaMask
await addNetwork();

// Or add if not added, switch if already added
await addOrSwitchNetwork();

Switch to ChainID 138

import { switchNetwork, isOnChain138 } from '@defi-oracle/metamask-sdk';

// Check if already on ChainID 138
const isOn138 = await isOnChain138();

if (!isOn138) {
  await switchNetwork();
}

Add Token to MetaMask

import { addToken } from '@defi-oracle/metamask-sdk';

// Add WETH token
await addToken(
  '0xYourTokenAddress',
  'WETH',
  18,
  'https://explorer.d-bis.org/images/tokens/weth.png'
);

Complete Example

import { addOrSwitchNetwork, addToken, isOnChain138 } from '@defi-oracle/metamask-sdk';

async function connectToChain138() {
  try {
    // Add or switch to ChainID 138
    await addOrSwitchNetwork();
    
    // Verify we're on the correct chain
    const isOn138 = await isOnChain138();
    if (!isOn138) {
      throw new Error('Failed to switch to ChainID 138');
    }
    
    console.log('Connected to ChainID 138');
  } catch (error) {
    console.error('Error connecting to ChainID 138:', error);
  }
}

async function addWETHToken() {
  try {
    await addToken(
      '0xYourWETHAddress',
      'WETH',
      18,
      'https://explorer.d-bis.org/images/tokens/weth.png'
    );
    console.log('WETH token added to MetaMask');
  } catch (error) {
    console.error('Error adding token:', error);
  }
}

API Reference

Network Functions

  • addNetwork(customMetadata?) - Add ChainID 138 to MetaMask
  • switchNetwork() - Switch to ChainID 138
  • addOrSwitchNetwork() - Add if not added, switch if already added
  • isNetworkAdded() - Check if ChainID 138 is added
  • isOnChain138() - Check if currently on ChainID 138
  • getCurrentChainId() - Get current chain ID

Token Functions

  • addToken(address, symbol, decimals, image?) - Add ERC-20 token
  • addTokenFromList(token) - Add token from token list entry

Configuration

  • CHAIN_ID - ChainID (138)
  • CHAIN_ID_HEX - ChainID in hex (0x8a)
  • CHAIN_NAME - Chain name
  • RPC_URLS - RPC endpoint URLs
  • BLOCK_EXPLORER_URL - Blockscout explorer URL
  • NETWORK_METADATA - Network metadata for wallet_addEthereumChain
  • CAIP2_IDENTIFIER - CAIP-2 identifier (eip155:138)

Error Handling

All functions throw errors that should be caught:

try {
  await addNetwork();
} catch (error) {
  if (error.message.includes('MetaMask is not installed')) {
    // Handle MetaMask not installed
  } else if (error.message.includes('User rejected')) {
    // Handle user rejection
  } else {
    // Handle other errors
  }
}

Browser Compatibility

  • Chrome/Chromium (MetaMask Extension)
  • Firefox (MetaMask Extension)
  • Edge (MetaMask Extension)
  • Mobile browsers (MetaMask Mobile)

Requirements

  • MetaMask extension or mobile app installed
  • MetaMask unlocked
  • User approval for network addition/switching

License

MIT