import type { Currency } from './currency-type.js'; import { NativeCurrency, nativeCurrencyToSerializableErc20TokenType } from './native-currency.js'; import { WETH } from './common-tokens.js'; import type { SerializableErc20TokenType } from './base-currency.js'; /** * Ether is the main usage of a 'native' currency, i.e. for Ethereum mainnet and all testnets */ export class Ether extends NativeCurrency { protected constructor(chainId: number) { super({ chainId, decimals: 18, symbol: 'ETH', name: 'Ether', }); } get wrapped() { const chainId = this.chainId; const weth = WETH[chainId]; if (!weth) { throw new Error(`No WETH available on chain ${chainId}`); } return weth; } private static _etherCache: { [chainId: number]: Ether } = {}; static onChain(chainId: number): Ether { return this._etherCache[chainId] || (this._etherCache[chainId] = new Ether(chainId)); } /** * Returns true if the two tokens are equivalent, i.e. have the same chainId and address. * @param other other token to compare */ public equals(other: Currency): boolean { return other.isNative && this.chainId === other.chainId && this.address === other.address; } public toObject(): SerializableErc20TokenType { return nativeCurrencyToSerializableErc20TokenType(this); } }