UNPKG

@cobaltx/sdk-v2

Version:

An SDK for building applications on top of CobaltX.

57 lines (49 loc) 1.59 kB
import { NetworkName } from "@/config"; import { getSolInfo } from "../cobaltx/token/constant"; import { Token } from "./token"; interface CurrencyProps { decimals: number; symbol?: string; name?: string; } /** * A currency is any fungible financial instrument on Solana, including SOL and all SPL tokens. * The only instance of the base class `Currency` is SOL. */ export class Currency { public readonly symbol?: string; public readonly name?: string; public readonly decimals: number; /** * The only instance of the base class `Currency`. */ /** * Constructs an instance of the base class `Currency`. The only instance of the base class `Currency` is `Currency.SOL`. * @param decimals - decimals of the currency * @param symbol - symbol of the currency * @param name - name of the currency */ public constructor({ decimals, symbol = "UNKNOWN", name = "UNKNOWN" }: CurrencyProps) { this.decimals = decimals; this.symbol = symbol; this.name = name; } public equals(other: Currency): boolean { return this === other; } public static getSOL(network: NetworkName): Currency { return new Currency(getSolInfo(network)); } } /** * Compares two currencies for equality */ export function currencyEquals(currencyA: Currency, currencyB: Currency): boolean { if (currencyA instanceof Token && currencyB instanceof Token) { return currencyA.equals(currencyB); } else if (currencyA instanceof Token || currencyB instanceof Token) { return false; } else { return currencyA === currencyB; } }