# Blockscout MetaMask Integration

Blockscout configuration for MetaMask Portfolio compatibility.

## Overview

Blockscout must be configured to support MetaMask Portfolio's token auto-detection and balance display features.

## Required API Endpoints

### Token Metadata

Blockscout must provide token metadata via API:

```
GET /api/v2/tokens/{address}
```

**Response**:
```json
{
  "address": "0x...",
  "name": "Wrapped Ether",
  "symbol": "WETH",
  "decimals": 18,
  "total_supply": "1000000000000000000000",
  "holders_count": 100,
  "transactions_count": 1000
}
```

### Token Holders

```
GET /api/v2/tokens/{address}/holders
```

### Account Token Balances

```
GET /api/v2/addresses/{address}/token-balances
```

## CORS Configuration

### Required CORS Headers

Blockscout must allow CORS requests from MetaMask Portfolio:

```
Access-Control-Allow-Origin: https://portfolio.metamask.io
Access-Control-Allow-Methods: GET, OPTIONS
Access-Control-Allow-Headers: Content-Type
Access-Control-Max-Age: 3600
```

### Configuration

Add to Blockscout deployment:

```yaml
env:
  - name: CORS_ALLOWED_ORIGINS
    value: "https://portfolio.metamask.io,https://metamask.io,https://chainlist.org"
  - name: ENABLE_CORS
    value: "true"
```

## Token Logo Serving

### Logo URL Format

Token logos should be served from:

```
https://explorer.d-bis.org/images/tokens/{address}.png
```

### Configuration

1. **Enable Logo Serving**: Enable logo serving in Blockscout
2. **Logo Storage**: Store logos in Blockscout or CDN
3. **Fallback**: Use default token logo if not found
4. **Format**: PNG format, 512x512 pixels

## Contract Verification

### Required for Token Metadata

All token contracts should be verified on Blockscout to provide accurate metadata:

1. **Verify Contracts**: Verify all token contracts
2. **Update Metadata**: Update token metadata as needed
3. **Monitor**: Monitor for contract updates

## API Rate Limiting

### Portfolio-Specific Limits

Configure rate limiting for Portfolio requests:

- **Default**: 120 requests/minute per IP
- **Portfolio**: Higher limits for Portfolio domain
- **API Keys**: Optional API keys for higher limits

## Testing

### Test Checklist

- [ ] Token metadata API works
- [ ] Token holders API works
- [ ] Account token balances API works
- [ ] CORS headers are present
- [ ] Token logos are accessible
- [ ] Contract verification works
- [ ] Rate limiting works correctly

### Test Commands

```bash
# Test token metadata API
curl https://explorer.d-bis.org/api/v2/tokens/0xYourTokenAddress

# Test CORS headers
curl -H "Origin: https://portfolio.metamask.io" \
     -H "Access-Control-Request-Method: GET" \
     -X OPTIONS \
     https://explorer.d-bis.org/api/v2/tokens/0xYourTokenAddress

# Test token logo
curl https://explorer.d-bis.org/images/tokens/0xYourTokenAddress.png
```

## Monitoring

### Metrics

- API request rate
- CORS request rate
- Token metadata API usage
- Logo serving performance
- Error rates

### Alerts

- API errors
- CORS configuration issues
- Logo serving failures
- Rate limiting issues

## References

- [Blockscout API Documentation](https://docs.blockscout.com/for-developers/api)
- [MetaMask Portfolio](https://portfolio.metamask.io)
- [CORS Configuration](https://docs.blockscout.com/for-developers/api/cors)

