Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/sdk/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
6 changes: 4 additions & 2 deletions packages/sdk/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ export enum ChainId {
SOMNIA_TESTNET = 50312,
BASE = 8453,
SOMNIA = 5031,
MANTRA = 5888
MANTRA = 5888,
ANUBIS = 6714,
}

export enum TradeType {
Expand Down Expand Up @@ -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'
Expand Down
3 changes: 2 additions & 1 deletion packages/sdk/src/entities/currency.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
}

/**
Expand Down
8 changes: 4 additions & 4 deletions packages/sdk/src/entities/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand All @@ -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')
Expand Down
9 changes: 8 additions & 1 deletion packages/sdk/src/entities/token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export function currencyEquals(currencyA: Currency, currencyB: Currency): boolea
}
}

export const WETH = {
export const WETH: Partial<Record<ChainId, Token>> = {
[ChainId.MUMBAI]: new Token(
ChainId.MUMBAI,
'0x9c3C9283D3e44854697Cd22D3Faa240Cfb032889',
Expand Down Expand Up @@ -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
}
6 changes: 3 additions & 3 deletions packages/sdk/src/entities/trade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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')
}

Expand Down
29 changes: 29 additions & 0 deletions packages/sdk/test/native-currency.test.ts
Original file line number Diff line number Diff line change
@@ -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')
})
})
})
Loading