|
| 1 | +# Token Caching Script |
| 2 | + |
| 3 | +This script fetches token metadata from the blockchain for all unique tokens available on Mento Protocol and caches them in static TypeScript files. It dynamically generates type-safe enums, address mappings, and helper functions. |
| 4 | + |
| 5 | +## Purpose |
| 6 | + |
| 7 | +- **Synchronous Access**: Enables synchronous access to token data without async blockchain calls |
| 8 | +- **Type Safety**: Dynamically generates type-safe `TokenSymbol` enum and `TOKEN_ADDRESSES_BY_CHAIN` mapping |
| 9 | +- **Performance**: Eliminates network calls for token metadata |
| 10 | +- **Offline Support**: Token data available without network connection |
| 11 | +- **Zero Hardcoding**: Everything auto-generated from blockchain data |
| 12 | + |
| 13 | +## Usage |
| 14 | + |
| 15 | +### Cache tokens for all supported chains |
| 16 | + |
| 17 | +```bash |
| 18 | +yarn cacheTokens |
| 19 | +``` |
| 20 | + |
| 21 | +### Cache tokens for specific chains |
| 22 | + |
| 23 | +```bash |
| 24 | +yarn cacheTokens --chain-ids=42220,44787 |
| 25 | +``` |
| 26 | + |
| 27 | +## Output |
| 28 | + |
| 29 | +The script generates TypeScript files in `src/constants/`: |
| 30 | + |
| 31 | +**Individual token files (per chain):** |
| 32 | + |
| 33 | +- `tokens.42220.ts` - Celo Mainnet tokens (readonly Token[]) |
| 34 | +- `tokens.44787.ts` - Alfajores Testnet tokens (readonly Token[]) |
| 35 | +- `tokens.11142220.ts` - Celo Sepolia Testnet tokens (readonly Token[]) |
| 36 | + |
| 37 | +**Main index file (dynamically generated):** |
| 38 | + |
| 39 | +- `tokens.ts` - Contains: |
| 40 | + - `TokenSymbol` enum - All unique token symbols across all chains |
| 41 | + - `TOKEN_ADDRESSES_BY_CHAIN` - Address mapping by chain and symbol |
| 42 | + - `getCachedTokens()` - Async token loading function |
| 43 | + - `getCachedTokensSync()` - Synchronous token loading function |
| 44 | + - `getTokenAddress()` - Helper to get token address by symbol |
| 45 | + - `findTokenBySymbol()` - Helper to find token by symbol |
| 46 | + |
| 47 | +## What Gets Generated |
| 48 | + |
| 49 | +Everything is computed dynamically from blockchain data: |
| 50 | + |
| 51 | +✅ **Token enum** - Unique symbols across all chains (e.g., `TokenSymbol.CELO`, `TokenSymbol.cUSD`) |
| 52 | +✅ **Address mappings** - Complete `TOKEN_ADDRESSES_BY_CHAIN` for all chains |
| 53 | +✅ **Helper functions** - Type-safe address lookups and token search |
| 54 | +✅ **Chain support** - Auto-detects supported chains from network config |
| 55 | +✅ **Error messages** - Dynamic list of supported chains in error messages |
| 56 | + |
| 57 | +## Configuration |
| 58 | + |
| 59 | +RPC URLs can be configured via environment variables: |
| 60 | + |
| 61 | +- `CELO_RPC_URL` - Celo Mainnet RPC URL (default: <https://forno.celo.org>) |
| 62 | +- `ALFAJORES_RPC_URL` - Alfajores RPC URL (default: <https://alfajores-forno.celo-testnet.org>) |
| 63 | +- `CELO_SEPOLIA_RPC_URL` - Celo Sepolia RPC URL (default: <https://forno.celo-sepolia.celo-testnet.org>) |
| 64 | + |
| 65 | +## When to Regenerate |
| 66 | + |
| 67 | +Regenerate cached tokens when: |
| 68 | + |
| 69 | +- New tokens are added to the Mento Protocol |
| 70 | +- Existing tokens are removed from the Mento Protocol |
| 71 | +- Token metadata changes (symbol, name, decimals) |
| 72 | +- New chains are added to the protocol |
| 73 | + |
| 74 | +## Example Output |
| 75 | + |
| 76 | +```bash |
| 77 | +📡 Cache tokens for chain(s): 42220, 44787, 11142220 |
| 78 | +🔄 Generating tokens for chain 42220... |
| 79 | +✅ Successfully cached 20 tokens to tokens.42220.ts |
| 80 | + |
| 81 | +🔄 Generating tokens for chain 44787... |
| 82 | +✅ Successfully cached 20 tokens to tokens.44787.ts |
| 83 | + |
| 84 | +🔄 Generating tokens for chain 11142220... |
| 85 | +✅ Successfully cached 20 tokens to tokens.11142220.ts |
| 86 | + |
| 87 | +🔄 Generating tokens.ts index file... |
| 88 | +✅ Generated tokens.ts with 23 unique token symbols |
| 89 | +``` |
0 commit comments