# MetaMask SDK for ChainID 138

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

## Installation

```bash
npm install @defi-oracle/metamask-sdk
```

Or using the source:

```bash
cd metamask-sdk
npm install
npm run build
```

## Usage

### Add Network to MetaMask

```typescript
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

```typescript
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

```typescript
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

```typescript
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:

```typescript
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

