@evolutionland/evolution-js
Version:
evolution evolution-js evolutionland evolution-js-sdk evolution-land metaverse
69 lines (68 loc) • 3.54 kB
JavaScript
import invariant from "tiny-invariant";
import { ChainId } from "../constants";
import { validateAndParseAddress } from "../utils";
import { Currency } from "./currency";
/**
* Represents an ERC20 token with a unique address and some metadata.
*/
export class Token extends Currency {
constructor(chainId, address, decimals, symbol, name, projectLink) {
super(decimals, symbol, name);
this.chainId = chainId;
this.address = validateAndParseAddress(address);
this.projectLink = projectLink;
}
/**
* Returns true if the two tokens are equivalent, i.e. have the same chainId and address.
* @param other other token to compare
*/
equals(other) {
// short circuit on reference equality
if (this === other) {
return true;
}
return this.chainId === other.chainId && this.address === other.address;
}
/**
* Returns true if the address of this token sorts before the address of the other token
* @param other other token to compare
* @throws if the tokens have the same address
* @throws if the tokens are on different chains
*/
sortsBefore(other) {
invariant(this.chainId === other.chainId, "CHAIN_IDS");
invariant(this.address !== other.address, "ADDRESSES");
return this.address.toLowerCase() < other.address.toLowerCase();
}
}
/**
* Compares two currencies for equality
*/
export function currencyEquals(currencyA, currencyB) {
if (currencyA instanceof Token && currencyB instanceof Token) {
return currencyA.equals(currencyB);
}
else if (currencyA instanceof Token) {
return false;
}
else if (currencyB instanceof Token) {
return false;
}
else {
return currencyA === currencyB;
}
}
export const WETH = {
[ChainId.MAINNET]: new Token(ChainId.MAINNET, "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2", 18, "WETH", "Wrapped ETH", "https://weth.io/"),
[ChainId.ETHEREUM]: new Token(ChainId.ETHEREUM, "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2", 18, "WETH", "Wrapped ETH", "https://weth.io/"),
[ChainId.ROPSTEN]: new Token(ChainId.ROPSTEN, "0xc778417E063141139Fce010982780140Aa0cD5Ab", 18, "WETH", "Test-Wrapped ETH", "https://weth.io/"),
[ChainId.CRAB]: new Token(ChainId.CRAB, "0x2D2b97EA380b0185e9fDF8271d1AFB5d2Bf18329", 18, "WCRAB", "Wrapped CRAB", "https://crab.network/"),
[ChainId.CRABTESTNET]: new Token(ChainId.CRABTESTNET, "0xD4C2F962B8b94cdD2e0B2e8E765d39f32980a1c1", 18, "WCRAB", "Wrapped CRAB", "https://crab.network/"),
[ChainId.HECO]: new Token(ChainId.HECO, "0x5545153CCFcA01fbd7Dd11C0b23ba694D9509A6F", 18, "WHT", "Wrapped HT", "https://www.htokens.finance/zh-cn/"),
[ChainId.HECOTESTNET]: new Token(ChainId.HECOTESTNET, "0xD4C2F962B8b94cdD2e0B2e8E765d39f32980a1c1", 18, "WHT", "Test-Wrapped HT", "https://www.htokens.finance/zh-cn/"),
[ChainId.POLYGON]: new Token(ChainId.POLYGON, "0x0d500B1d8E8eF31E21C99d1Db9A6444d3ADf1270", 18, "WMATIC", "Wrapped MATIC", "https://polygon.technology/"),
[ChainId.MUMBAI]: new Token(ChainId.MUMBAI, "0x3277B7BABbeadF5dC43D5350df25310f3c819965", 18, "WMATIC", "Test-Wrapped MATIC", "https://polygon.technology/"),
[ChainId.TRON]: new Token(ChainId.TRON, "0x891cdb91d149f23B1a45D9c5Ca78a88d0cB44C18", 6, "WTRX", "Wrapped TRX", "https://just.network/"),
// todo
[ChainId.SHASTA]: new Token(ChainId.SHASTA, "0x891cdb91d149f23B1a45D9c5Ca78a88d0cB44C18", 6, "WTRX", "Test-Wrapped TRX", "https://just.network/"),
};