currencies.js
1 import networks, { getNetworkConfig, getEnabledNetworks, getEnabledNetworksForType } from "@/data/crypto/networks.js"; 2 3 export const coins = { 4 ETH: { 5 id: "ETH", 6 symbol: "ETH", 7 name: "Ethereum", 8 priceIds: { 9 coingecko: "ethereum", 10 binance: "ETHUSDT", 11 kraken: "XETHZUSD", 12 }, 13 networks: [ 14 { networkId: "ethereum", decimals: 18, minDeposit: 0.005, maxDeposit: 10, isNative: true, tokenAddress: null }, 15 ], 16 }, 17 SOL: { 18 id: "SOL", 19 symbol: "SOL", 20 name: "Solana", 21 priceIds: { 22 coingecko: "solana", 23 binance: "SOLUSDT", 24 kraken: "SOLUSD", 25 }, 26 networks: [ 27 { networkId: "solana", decimals: 9, minDeposit: 0.05, maxDeposit: 100, isNative: true, tokenAddress: null }, 28 ], 29 }, 30 SUI: { 31 id: "SUI", 32 symbol: "SUI", 33 name: "Sui", 34 priceIds: { 35 coingecko: "sui", 36 binance: "SUIUSDT", 37 kraken: "SUIUSD", 38 }, 39 networks: [ 40 { networkId: "sui", decimals: 9, minDeposit: 5, maxDeposit: 1000, isNative: true, tokenAddress: null }, 41 ], 42 }, 43 USDC: { 44 id: "USDC", 45 symbol: "USDC", 46 name: "USD Coin", 47 priceIds: { 48 coingecko: "usd-coin", 49 binance: "USDCUSDT", 50 kraken: "USDCUSD", 51 }, 52 networks: [ 53 { networkId: "ethereum", decimals: 6, minDeposit: 5, maxDeposit: 10000, isNative: false, tokenAddress: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48" }, 54 { networkId: "solana", decimals: 6, minDeposit: 5, maxDeposit: 10000, isNative: false, tokenAddress: "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v" }, 55 { networkId: "sui", decimals: 6, minDeposit: 5, maxDeposit: 10000, isNative: false, tokenAddress: "0xdba34672e30cb065b1f93e3ab55318768fd6fef66c15942c9f7cb846e2f900e7::usdc::USDC" }, 56 ], 57 }, 58 }; 59 60 function validateCoinId(coinId) { 61 if (typeof coinId !== "string" || !/^[A-Z0-9]{2,10}$/.test(coinId)) { 62 throw new Error("Invalid coin ID format"); 63 } 64 return coinId; 65 } 66 67 function validateNetworkId(networkId) { 68 if (typeof networkId !== "string" || !/^[a-z0-9-]{3,20}$/.test(networkId)) { 69 throw new Error("Invalid network ID format"); 70 } 71 return networkId; 72 } 73 74 export default coins; 75 76 export function getTokenConfig(coinId, networkId) { 77 const validCoinId = validateCoinId(coinId); 78 const validNetworkId = validateNetworkId(networkId); 79 80 if (!Object.prototype.hasOwnProperty.call(coins, validCoinId)) { 81 return null; 82 } 83 84 const coin = coins[validCoinId]; 85 if (!coin) { 86 return null; 87 } 88 89 return coin.networks.find(n => n.networkId === validNetworkId) || null; 90 } 91 92 export function getCoinConfig(coinId) { 93 const validCoinId = validateCoinId(coinId); 94 if (!Object.prototype.hasOwnProperty.call(coins, validCoinId)) { 95 return null; 96 } 97 98 return coins[validCoinId] || null; 99 } 100 101 export function getNetworksForCoin(coinId) { 102 const validCoinId = validateCoinId(coinId); 103 104 if (!Object.prototype.hasOwnProperty.call(coins, validCoinId)) { 105 return []; 106 } 107 108 const coin = coins[validCoinId]; 109 if (!coin) { 110 return []; 111 } 112 return coin.networks.map(n => networks[n.networkId]).filter(Boolean); 113 } 114 115 export function getEnabledNetworksForCoin(coinId) { 116 const validCoinId = validateCoinId(coinId); 117 118 if (!Object.prototype.hasOwnProperty.call(coins, validCoinId)) { 119 return []; 120 } 121 122 const coin = coins[validCoinId]; 123 if (!coin) { 124 return []; 125 } 126 return coin.networks.map(n => networks[n.networkId]).filter(network => network && network.enabled !== false); 127 } 128 129 export function getCoinsForNetwork(networkId) { 130 const validNetworkId = validateNetworkId(networkId); 131 132 if (!Object.prototype.hasOwnProperty.call(networks, validNetworkId)) { 133 return []; 134 } 135 136 return Object.values(coins).filter(coin => coin.networks.some(n => n.networkId === validNetworkId && networks[validNetworkId]?.enabled !== false)); 137 } 138 139 export function getFullTokenConfig(coinId, networkId) { 140 const validCoinId = validateCoinId(coinId); 141 const validNetworkId = validateNetworkId(networkId); 142 143 if (!Object.prototype.hasOwnProperty.call(coins, validCoinId)) { 144 return null; 145 } 146 147 const coin = coins[validCoinId]; 148 if (!coin) { 149 return null; 150 } 151 152 const networkConfig = coin.networks.find(n => n.networkId === validNetworkId); 153 if (!networkConfig) { 154 return null; 155 } 156 157 if (!Object.prototype.hasOwnProperty.call(networks, validNetworkId)) { 158 return null; 159 } 160 161 const network = networks[validNetworkId]; 162 if (!network || network.enabled === false) { 163 return null; 164 } 165 166 return { 167 coinId, 168 networkId, 169 decimals: networkConfig.decimals, 170 minDeposit: networkConfig.minDeposit, 171 maxDeposit: networkConfig.maxDeposit, 172 isNative: networkConfig.isNative, 173 tokenAddress: networkConfig.tokenAddress, 174 coin, 175 network, 176 code: coinId, 177 chain: networkId, 178 symbol: coin.symbol, 179 displayName: coin.name, 180 }; 181 } 182 183 export function getAllTokenConfigs() { 184 const configs = []; 185 186 for (const [coinId, coin] of Object.entries(coins)) { 187 for (const networkConfig of coin.networks) { 188 const fullConfig = getFullTokenConfig(coinId, networkConfig.networkId); 189 if (fullConfig) { 190 configs.push(fullConfig); 191 } 192 } 193 } 194 195 return configs; 196 } 197 198 let _supportedCurrencies = null; 199 export function getSupportedCurrencies() { 200 if (!_supportedCurrencies) { 201 _supportedCurrencies = getAllTokenConfigs(); 202 } 203 return _supportedCurrencies; 204 } 205 206 export function getEnabledNetworksForCoinFromNetworks(coinId) { 207 const validCoinId = validateCoinId(coinId); 208 209 if (!Object.prototype.hasOwnProperty.call(coins, validCoinId)) { 210 return []; 211 } 212 213 const coin = coins[validCoinId]; 214 if (!coin) { 215 return []; 216 } 217 return coin.networks.map(n => networks[n.networkId]).filter(network => network && network.enabled !== false); 218 } 219 220 export function getCurrencyConfig(code, chain) { 221 return getFullTokenConfig(code, chain); 222 }