diff --git a/packages/sdk/package.json b/packages/sdk/package.json index 8840057..0fa5d9e 100644 --- a/packages/sdk/package.json +++ b/packages/sdk/package.json @@ -1,7 +1,7 @@ { "name": "@quickswap-defi/sdk", "license": "MIT", - "version": "1.0.1", + "version": "1.0.2", "description": "🛠 An SDK for building applications on top of Quickswap.", "main": "dist/index.js", "typings": "dist/index.d.ts", diff --git a/packages/sdk/src/constants.ts b/packages/sdk/src/constants.ts index 76a2cfb..4f41e15 100755 --- a/packages/sdk/src/constants.ts +++ b/packages/sdk/src/constants.ts @@ -25,7 +25,8 @@ export enum ChainId { SOMNIA_TESTNET = 50312, BASE = 8453, SOMNIA = 5031, - MANTRA = 5888 + MANTRA = 5888, + ANUBIS = 6714, } export enum TradeType { @@ -61,7 +62,8 @@ export const FACTORY_ADDRESS = { [ChainId.SOMNIA_TESTNET]: '0x5757371414417b8C6CAad45bAeF941aBc7d3Ab32', //DUMMY [ChainId.BASE]: '0xEC6540261aaaE13F236A032d454dc9287E52e56A', [ChainId.SOMNIA]: '0x5757371414417b8C6CAad45bAeF941aBc7d3Ab32', //DUMMY - [ChainId.MANTRA]: '0x5757371414417b8C6CAad45bAeF941aBc7d3Ab32' //DUMMY + [ChainId.MANTRA]: '0x5757371414417b8C6CAad45bAeF941aBc7d3Ab32', //DUMMY + [ChainId.ANUBIS]: '0x5757371414417b8C6CAad45bAeF941aBc7d3Ab32' //DUMMY } export const INIT_CODE_HASH = '0x96e8ac4277198ff8b6f785478aa9a39f403cb768dd02cbee326c3e7da348845f' diff --git a/packages/sdk/src/entities/currency.ts b/packages/sdk/src/entities/currency.ts index a9e392b..7358044 100755 --- a/packages/sdk/src/entities/currency.ts +++ b/packages/sdk/src/entities/currency.ts @@ -39,7 +39,8 @@ export class Currency { [ChainId.SOMNIA_TESTNET]: new Currency(18, 'STT', 'Somnia Testnet Tokens'), [ChainId.BASE]: new Currency(18, 'ETH', 'Ether'), [ChainId.SOMNIA]: new Currency(18, 'SOMI', 'Somnia Tokens'), - [ChainId.MANTRA]: new Currency(18, 'MANTRA', 'Mantra Token') + [ChainId.MANTRA]: new Currency(18, 'MANTRA', 'Mantra Token'), + [ChainId.ANUBIS]: new Currency(18, 'DAI', 'Dai Stablecoin') } /** diff --git a/packages/sdk/src/entities/route.ts b/packages/sdk/src/entities/route.ts index 816cde9..33ca5ff 100755 --- a/packages/sdk/src/entities/route.ts +++ b/packages/sdk/src/entities/route.ts @@ -2,7 +2,7 @@ import { ChainId } from '../constants' import invariant from 'tiny-invariant' import { Currency, ETHER } from './currency' -import { Token, WETH } from './token' +import { Token, wrappedNative } from './token' import { Pair } from './pair' import { Price } from './fractions/price' @@ -21,17 +21,17 @@ export class Route { ) invariant( (input instanceof Token && pairs[0].involvesToken(input)) || - (input === ETHER[pairs[0].chainId] && pairs[0].involvesToken(WETH[pairs[0].chainId])), + (input === ETHER[pairs[0].chainId] && pairs[0].involvesToken(wrappedNative(pairs[0].chainId))), 'INPUT' ) invariant( typeof output === 'undefined' || (output instanceof Token && pairs[pairs.length - 1].involvesToken(output)) || - (output === ETHER[pairs[0].chainId] && pairs[pairs.length - 1].involvesToken(WETH[pairs[0].chainId])), + (output === ETHER[pairs[0].chainId] && pairs[pairs.length - 1].involvesToken(wrappedNative(pairs[0].chainId))), 'OUTPUT' ) - const path: Token[] = [input instanceof Token ? input : WETH[pairs[0].chainId]] + const path: Token[] = [input instanceof Token ? input : wrappedNative(pairs[0].chainId)] for (const [i, pair] of pairs.entries()) { const currentInput = path[i] invariant(currentInput.equals(pair.token0) || currentInput.equals(pair.token1), 'PATH') diff --git a/packages/sdk/src/entities/token.ts b/packages/sdk/src/entities/token.ts index 18ce637..cdb7773 100755 --- a/packages/sdk/src/entities/token.ts +++ b/packages/sdk/src/entities/token.ts @@ -56,7 +56,7 @@ export function currencyEquals(currencyA: Currency, currencyB: Currency): boolea } } -export const WETH = { +export const WETH: Partial> = { [ChainId.MUMBAI]: new Token( ChainId.MUMBAI, '0x9c3C9283D3e44854697Cd22D3Faa240Cfb032889', @@ -157,4 +157,11 @@ export const WETH = { 'WMANTRA', 'Wrapped MANTRA' ) + // ChainId.ANUBIS has no entry: its gas token is the ERC-20 DAI and there is no wrapped-native contract. +} + +export function wrappedNative(chainId: ChainId): Token { + const token = WETH[chainId] + invariant(token, 'WRAPPED_NATIVE') + return token } diff --git a/packages/sdk/src/entities/trade.ts b/packages/sdk/src/entities/trade.ts index f13c89e..36ba5ef 100755 --- a/packages/sdk/src/entities/trade.ts +++ b/packages/sdk/src/entities/trade.ts @@ -11,7 +11,7 @@ import { Price } from './fractions/price' import { TokenAmount } from './fractions/tokenAmount' import { Pair } from './pair' import { Route } from './route' -import { currencyEquals, Token, WETH } from './token' +import { currencyEquals, Token, wrappedNative } from './token' /** * Returns the percent difference between the mid price and the execution price, i.e. price impact. @@ -90,13 +90,13 @@ export interface BestTradeOptions { */ function wrappedAmount(currencyAmount: CurrencyAmount, chainId: ChainId): TokenAmount { if (currencyAmount instanceof TokenAmount) return currencyAmount - if (currencyAmount.currency === ETHER[chainId]) return new TokenAmount(WETH[chainId], currencyAmount.raw) + if (currencyAmount.currency === ETHER[chainId]) return new TokenAmount(wrappedNative(chainId), currencyAmount.raw) invariant(false, 'CURRENCY') } function wrappedCurrency(currency: Currency, chainId: ChainId): Token { if (currency instanceof Token) return currency - if (currency === ETHER[chainId]) return WETH[chainId] + if (currency === ETHER[chainId]) return wrappedNative(chainId) invariant(false, 'CURRENCY') } diff --git a/packages/sdk/test/native-currency.test.ts b/packages/sdk/test/native-currency.test.ts new file mode 100644 index 0000000..c98f0b5 --- /dev/null +++ b/packages/sdk/test/native-currency.test.ts @@ -0,0 +1,29 @@ +import { ChainId, Currency, WETH, wrappedNative } from '../src' + +describe('The native currency', () => { + describe('on a chain whose gas token is an ERC-20 with no wrapped contract', () => { + it('describes the gas token so wallets can display balances', () => { + const native = Currency.ETHER[ChainId.ANUBIS] + + expect(native.symbol).toBe('DAI') + expect(native.decimals).toBe(18) + }) + + it('exposes no wrapped token', () => { + expect(WETH[ChainId.ANUBIS]).toBeUndefined() + }) + + it('rejects wrapping instead of returning a phantom token', () => { + expect(() => wrappedNative(ChainId.ANUBIS)).toThrow('WRAPPED_NATIVE') + }) + }) + + describe('on a chain with a wrapped native token', () => { + it('resolves the wrapped token for routing', () => { + const wrapped = wrappedNative(ChainId.ETHEREUM) + + expect(wrapped.chainId).toBe(ChainId.ETHEREUM) + expect(wrapped.symbol).toBe('WETH') + }) + }) +})