( value: unknown, uri: string, )
| 428 | } |
| 429 | |
| 430 | function assertTokenlist( |
| 431 | value: unknown, |
| 432 | uri: string, |
| 433 | ): asserts value is Tokenlist { |
| 434 | if (!isRecord(value)) |
| 435 | throw new Error(`Tokenlist response from ${uri} must be an object.`) |
| 436 | if (typeof value.name !== 'string' || value.name.length === 0) |
| 437 | throw new Error(`Tokenlist response from ${uri} must include a name.`) |
| 438 | if (typeof value.timestamp !== 'string' || value.timestamp.length === 0) |
| 439 | throw new Error(`Tokenlist response from ${uri} must include a timestamp.`) |
| 440 | if (!isRecord(value.version)) |
| 441 | throw new Error(`Tokenlist response from ${uri} must include a version.`) |
| 442 | for (const key of ['major', 'minor', 'patch'] as const) { |
| 443 | if (!Number.isInteger(value.version[key])) |
| 444 | throw new Error( |
| 445 | `Tokenlist response from ${uri} must include version.${key}.`, |
| 446 | ) |
| 447 | } |
| 448 | if (!Array.isArray(value.tokens)) |
| 449 | throw new Error( |
| 450 | `Tokenlist response from ${uri} must include a tokens array.`, |
| 451 | ) |
| 452 | |
| 453 | for (const token of value.tokens) { |
| 454 | if (!isRecord(token)) |
| 455 | throw new Error(`Tokenlist token from ${uri} must be an object.`) |
| 456 | if (!Number.isInteger(token.chainId)) |
| 457 | throw new Error(`Invalid token chain id from ${uri}.`) |
| 458 | if (typeof token.address !== 'string' || !isAddress(token.address)) |
| 459 | throw new Error( |
| 460 | `Invalid token address from ${uri}: ${String(token.address)}.`, |
| 461 | ) |
| 462 | const decimals = token.decimals |
| 463 | if ( |
| 464 | typeof decimals !== 'number' || |
| 465 | !Number.isInteger(decimals) || |
| 466 | decimals < 0 |
| 467 | ) |
| 468 | throw new Error( |
| 469 | `Invalid token decimals from ${uri} for ${token.address}.`, |
| 470 | ) |
| 471 | if (typeof token.name !== 'string' || token.name.length === 0) |
| 472 | throw new Error(`Invalid token name from ${uri} for ${token.address}.`) |
| 473 | if (typeof token.symbol !== 'string' || token.symbol.length === 0) |
| 474 | throw new Error(`Invalid token symbol from ${uri} for ${token.address}.`) |
| 475 | if ('currency' in token && typeof token.currency !== 'string') |
| 476 | throw new Error( |
| 477 | `Invalid token currency from ${uri} for ${token.address}.`, |
| 478 | ) |
| 479 | if ('popular' in token && typeof token.popular !== 'boolean') |
| 480 | throw new Error( |
| 481 | `Invalid token popular flag from ${uri} for ${token.address}.`, |
| 482 | ) |
| 483 | if ('extensions' in token && token.extensions !== undefined) { |
| 484 | if (!isRecord(token.extensions)) |
| 485 | throw new Error( |
| 486 | `Invalid token extensions from ${uri} for ${token.address}.`, |
| 487 | ) |
no test coverage detected