Skip to content

Commit

Permalink
Updating SDK addresses for xDai support (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
crisog committed Feb 17, 2021
1 parent d8cc586 commit fe09a72
Show file tree
Hide file tree
Showing 28 changed files with 2,431 additions and 1,406 deletions.
1 change: 1 addition & 0 deletions .yarnrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ignore-scripts true
21 changes: 14 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@uniswap/sdk",
"name": "@1hive/honeyswap-sdk",
"license": "MIT",
"version": "2.0.6",
"version": "1.0.0",
"description": "🛠 An SDK for building applications on top of Uniswap.",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
Expand All @@ -22,11 +22,6 @@
"prepublishOnly": "tsdx build"
},
"dependencies": {
"@ethersproject/address": "^5.0.0-beta.134",
"@ethersproject/contracts": "^5.0.0-beta.143",
"@ethersproject/networks": "^5.0.0-beta.135",
"@ethersproject/providers": "^5.0.0-beta.153",
"@ethersproject/solidity": "^5.0.0-beta.131",
"@uniswap/v2-core": "^1.0.0",
"big.js": "^5.2.2",
"decimal.js-light": "^2.5.0",
Expand All @@ -35,7 +30,19 @@
"tiny-warning": "^1.0.3",
"toformat": "^2.0.0"
},
"peerDependencies": {
"@ethersproject/address": "^5.0.0-beta",
"@ethersproject/contracts": "^5.0.0-beta",
"@ethersproject/networks": "^5.0.0-beta",
"@ethersproject/providers": "^5.0.0-beta",
"@ethersproject/solidity": "^5.0.0-beta"
},
"devDependencies": {
"@ethersproject/address": "^5.0.2",
"@ethersproject/contracts": "^5.0.2",
"@ethersproject/networks": "^5.0.2",
"@ethersproject/providers": "^5.0.5",
"@ethersproject/solidity": "^5.0.2",
"@types/big.js": "^4.0.5",
"@types/jest": "^24.0.25",
"babel-plugin-transform-jsbi-to-bigint": "^1.3.1",
Expand Down
7 changes: 4 additions & 3 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ export enum ChainId {
ROPSTEN = 3,
RINKEBY = 4,
GÖRLI = 5,
KOVAN = 42
KOVAN = 42,
XDAI = 100
}

export enum TradeType {
Expand All @@ -22,9 +23,9 @@ export enum Rounding {
ROUND_UP
}

export const FACTORY_ADDRESS = '0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f'
export const FACTORY_ADDRESS = '0xA818b4F111Ccac7AA31D0BCc0806d64F2E0737D7'

export const INIT_CODE_HASH = '0x96e8ac4277198ff8b6f785478aa9a39f403cb768dd02cbee326c3e7da348845f'
export const INIT_CODE_HASH = '0x3f88503e8580ab941773b59034fb4b2a63e86dbc031b3633a925533ad3ed2b93'

export const MINIMUM_LIQUIDITY = JSBI.BigInt(1000)

Expand Down
37 changes: 37 additions & 0 deletions src/entities/currency.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import JSBI from 'jsbi'

import { SolidityType } from '../constants'
import { validateSolidityTypeInstance } from '../utils'

/**
* A currency is any fungible financial instrument on Ethereum, including Ether and all ERC20 tokens.
*
* The only instance of the base class `Currency` is Ether.
*/
export class Currency {
public readonly decimals: number
public readonly symbol?: string
public readonly name?: string

/**
* The only instance of the base class `Currency`.
*/
public static readonly ETHER: Currency = new Currency(18, 'XDAI', 'xDai')

/**
* Constructs an instance of the base class `Currency`. The only instance of the base class `Currency` is `Currency.ETHER`.
* @param decimals decimals of the currency
* @param symbol symbol of the currency
* @param name of the currency
*/
protected constructor(decimals: number, symbol?: string, name?: string) {
validateSolidityTypeInstance(JSBI.BigInt(decimals), SolidityType.uint8)

this.decimals = decimals
this.symbol = symbol
this.name = name
}
}

const ETHER = Currency.ETHER
export { ETHER }
69 changes: 69 additions & 0 deletions src/entities/fractions/currencyAmount.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { currencyEquals } from '../token'
import { Currency, ETHER } from '../currency'
import invariant from 'tiny-invariant'
import JSBI from 'jsbi'
import _Big from 'big.js'
import toFormat from 'toformat'

import { BigintIsh, Rounding, TEN, SolidityType } from '../../constants'
import { parseBigintIsh, validateSolidityTypeInstance } from '../../utils'
import { Fraction } from './fraction'

const Big = toFormat(_Big)

export class CurrencyAmount extends Fraction {
public readonly currency: Currency

/**
* Helper that calls the constructor with the ETHER currency
* @param amount ether amount in wei
*/
public static ether(amount: BigintIsh): CurrencyAmount {
return new CurrencyAmount(ETHER, amount)
}

// amount _must_ be raw, i.e. in the native representation
protected constructor(currency: Currency, amount: BigintIsh) {
const parsedAmount = parseBigintIsh(amount)
validateSolidityTypeInstance(parsedAmount, SolidityType.uint256)

super(parsedAmount, JSBI.exponentiate(TEN, JSBI.BigInt(currency.decimals)))
this.currency = currency
}

public get raw(): JSBI {
return this.numerator
}

public add(other: CurrencyAmount): CurrencyAmount {
invariant(currencyEquals(this.currency, other.currency), 'TOKEN')
return new CurrencyAmount(this.currency, JSBI.add(this.raw, other.raw))
}

public subtract(other: CurrencyAmount): CurrencyAmount {
invariant(currencyEquals(this.currency, other.currency), 'TOKEN')
return new CurrencyAmount(this.currency, JSBI.subtract(this.raw, other.raw))
}

public toSignificant(
significantDigits: number = 6,
format?: object,
rounding: Rounding = Rounding.ROUND_DOWN
): string {
return super.toSignificant(significantDigits, format, rounding)
}

public toFixed(
decimalPlaces: number = this.currency.decimals,
format?: object,
rounding: Rounding = Rounding.ROUND_DOWN
): string {
invariant(decimalPlaces <= this.currency.decimals, 'DECIMALS')
return super.toFixed(decimalPlaces, format, rounding)
}

public toExact(format: object = { groupSeparator: '' }): string {
Big.DP = this.currency.decimals
return new Big(this.numerator.toString()).div(this.denominator.toString()).toFormat(format)
}
}
26 changes: 13 additions & 13 deletions src/entities/fractions/fraction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,26 +27,26 @@ export class Fraction {
public readonly numerator: JSBI
public readonly denominator: JSBI

constructor(numerator: BigintIsh, denominator: BigintIsh = ONE) {
public constructor(numerator: BigintIsh, denominator: BigintIsh = ONE) {
this.numerator = parseBigintIsh(numerator)
this.denominator = parseBigintIsh(denominator)
}

// performs floor division
get quotient(): JSBI {
public get quotient(): JSBI {
return JSBI.divide(this.numerator, this.denominator)
}

// remainder after floor division
get remainder(): Fraction {
public get remainder(): Fraction {
return new Fraction(JSBI.remainder(this.numerator, this.denominator), this.denominator)
}

invert(): Fraction {
public invert(): Fraction {
return new Fraction(this.denominator, this.numerator)
}

add(other: Fraction | BigintIsh): Fraction {
public add(other: Fraction | BigintIsh): Fraction {
const otherParsed = other instanceof Fraction ? other : new Fraction(parseBigintIsh(other))
if (JSBI.equal(this.denominator, otherParsed.denominator)) {
return new Fraction(JSBI.add(this.numerator, otherParsed.numerator), this.denominator)
Expand All @@ -60,7 +60,7 @@ export class Fraction {
)
}

subtract(other: Fraction | BigintIsh): Fraction {
public subtract(other: Fraction | BigintIsh): Fraction {
const otherParsed = other instanceof Fraction ? other : new Fraction(parseBigintIsh(other))
if (JSBI.equal(this.denominator, otherParsed.denominator)) {
return new Fraction(JSBI.subtract(this.numerator, otherParsed.numerator), this.denominator)
Expand All @@ -74,47 +74,47 @@ export class Fraction {
)
}

lessThan(other: Fraction | BigintIsh): boolean {
public lessThan(other: Fraction | BigintIsh): boolean {
const otherParsed = other instanceof Fraction ? other : new Fraction(parseBigintIsh(other))
return JSBI.lessThan(
JSBI.multiply(this.numerator, otherParsed.denominator),
JSBI.multiply(otherParsed.numerator, this.denominator)
)
}

equalTo(other: Fraction | BigintIsh): boolean {
public equalTo(other: Fraction | BigintIsh): boolean {
const otherParsed = other instanceof Fraction ? other : new Fraction(parseBigintIsh(other))
return JSBI.equal(
JSBI.multiply(this.numerator, otherParsed.denominator),
JSBI.multiply(otherParsed.numerator, this.denominator)
)
}

greaterThan(other: Fraction | BigintIsh): boolean {
public greaterThan(other: Fraction | BigintIsh): boolean {
const otherParsed = other instanceof Fraction ? other : new Fraction(parseBigintIsh(other))
return JSBI.greaterThan(
JSBI.multiply(this.numerator, otherParsed.denominator),
JSBI.multiply(otherParsed.numerator, this.denominator)
)
}

multiply(other: Fraction | BigintIsh): Fraction {
public multiply(other: Fraction | BigintIsh): Fraction {
const otherParsed = other instanceof Fraction ? other : new Fraction(parseBigintIsh(other))
return new Fraction(
JSBI.multiply(this.numerator, otherParsed.numerator),
JSBI.multiply(this.denominator, otherParsed.denominator)
)
}

divide(other: Fraction | BigintIsh): Fraction {
public divide(other: Fraction | BigintIsh): Fraction {
const otherParsed = other instanceof Fraction ? other : new Fraction(parseBigintIsh(other))
return new Fraction(
JSBI.multiply(this.numerator, otherParsed.denominator),
JSBI.multiply(this.denominator, otherParsed.numerator)
)
}

toSignificant(
public toSignificant(
significantDigits: number,
format: object = { groupSeparator: '' },
rounding: Rounding = Rounding.ROUND_HALF_UP
Expand All @@ -129,7 +129,7 @@ export class Fraction {
return quotient.toFormat(quotient.decimalPlaces(), format)
}

toFixed(
public toFixed(
decimalPlaces: number,
format: object = { groupSeparator: '' },
rounding: Rounding = Rounding.ROUND_HALF_UP
Expand Down
1 change: 1 addition & 0 deletions src/entities/fractions/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from './fraction'
export * from './percent'
export * from './tokenAmount'
export * from './currencyAmount'
export * from './price'
4 changes: 2 additions & 2 deletions src/entities/fractions/percent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import { Fraction } from './fraction'
const _100_PERCENT = new Fraction(_100)

export class Percent extends Fraction {
toSignificant(significantDigits: number = 5, format?: object, rounding?: Rounding): string {
public toSignificant(significantDigits: number = 5, format?: object, rounding?: Rounding): string {
return this.multiply(_100_PERCENT).toSignificant(significantDigits, format, rounding)
}

toFixed(decimalPlaces: number = 2, format?: object, rounding?: Rounding): string {
public toFixed(decimalPlaces: number = 2, format?: object, rounding?: Rounding): string {
return this.multiply(_100_PERCENT).toFixed(decimalPlaces, format, rounding)
}
}
54 changes: 30 additions & 24 deletions src/entities/fractions/price.ts
Original file line number Diff line number Diff line change
@@ -1,70 +1,76 @@
import { Token } from '../token'
import { TokenAmount } from './tokenAmount'
import { currencyEquals } from '../token'
import invariant from 'tiny-invariant'
import JSBI from 'jsbi'

import { BigintIsh, Rounding, TEN } from '../../constants'
import { Token } from '../token'
import { Currency } from '../currency'
import { Route } from '../route'
import { Fraction } from './fraction'
import { TokenAmount } from './tokenAmount'
import { CurrencyAmount } from './currencyAmount'

export class Price extends Fraction {
public readonly baseToken: Token // input i.e. denominator
public readonly quoteToken: Token // output i.e. numerator
public readonly baseCurrency: Currency // input i.e. denominator
public readonly quoteCurrency: Currency // output i.e. numerator
public readonly scalar: Fraction // used to adjust the raw fraction w/r/t the decimals of the {base,quote}Token

static fromRoute(route: Route): Price {
public static fromRoute(route: Route): Price {
const prices: Price[] = []
for (const [i, pair] of route.pairs.entries()) {
prices.push(
route.path[i].equals(pair.token0)
? new Price(pair.reserve0.token, pair.reserve1.token, pair.reserve0.raw, pair.reserve1.raw)
: new Price(pair.reserve1.token, pair.reserve0.token, pair.reserve1.raw, pair.reserve0.raw)
? new Price(pair.reserve0.currency, pair.reserve1.currency, pair.reserve0.raw, pair.reserve1.raw)
: new Price(pair.reserve1.currency, pair.reserve0.currency, pair.reserve1.raw, pair.reserve0.raw)
)
}
return prices.slice(1).reduce((accumulator, currentValue) => accumulator.multiply(currentValue), prices[0])
}

// denominator and numerator _must_ be raw, i.e. in the native representation
constructor(baseToken: Token, quoteToken: Token, denominator: BigintIsh, numerator: BigintIsh) {
public constructor(baseCurrency: Currency, quoteCurrency: Currency, denominator: BigintIsh, numerator: BigintIsh) {
super(numerator, denominator)

this.baseToken = baseToken
this.quoteToken = quoteToken
this.baseCurrency = baseCurrency
this.quoteCurrency = quoteCurrency
this.scalar = new Fraction(
JSBI.exponentiate(TEN, JSBI.BigInt(baseToken.decimals)),
JSBI.exponentiate(TEN, JSBI.BigInt(quoteToken.decimals))
JSBI.exponentiate(TEN, JSBI.BigInt(baseCurrency.decimals)),
JSBI.exponentiate(TEN, JSBI.BigInt(quoteCurrency.decimals))
)
}

get raw(): Fraction {
public get raw(): Fraction {
return new Fraction(this.numerator, this.denominator)
}

get adjusted(): Fraction {
public get adjusted(): Fraction {
return super.multiply(this.scalar)
}

invert(): Price {
return new Price(this.quoteToken, this.baseToken, this.numerator, this.denominator)
public invert(): Price {
return new Price(this.quoteCurrency, this.baseCurrency, this.numerator, this.denominator)
}

multiply(other: Price): Price {
invariant(this.quoteToken.equals(other.baseToken), 'BASE')
public multiply(other: Price): Price {
invariant(currencyEquals(this.quoteCurrency, other.baseCurrency), 'TOKEN')
const fraction = super.multiply(other)
return new Price(this.baseToken, other.quoteToken, fraction.denominator, fraction.numerator)
return new Price(this.baseCurrency, other.quoteCurrency, fraction.denominator, fraction.numerator)
}

// performs floor division on overflow
quote(tokenAmount: TokenAmount): TokenAmount {
invariant(tokenAmount.token.equals(this.baseToken), 'TOKEN')
return new TokenAmount(this.quoteToken, super.multiply(tokenAmount.raw).quotient)
public quote(currencyAmount: CurrencyAmount): CurrencyAmount {
invariant(currencyEquals(currencyAmount.currency, this.baseCurrency), 'TOKEN')
if (this.quoteCurrency instanceof Token) {
return new TokenAmount(this.quoteCurrency, super.multiply(currencyAmount.raw).quotient)
}
return CurrencyAmount.ether(super.multiply(currencyAmount.raw).quotient)
}

toSignificant(significantDigits: number = 6, format?: object, rounding?: Rounding): string {
public toSignificant(significantDigits: number = 6, format?: object, rounding?: Rounding): string {
return this.adjusted.toSignificant(significantDigits, format, rounding)
}

toFixed(decimalPlaces: number = 4, format?: object, rounding?: Rounding): string {
public toFixed(decimalPlaces: number = 4, format?: object, rounding?: Rounding): string {
return this.adjusted.toFixed(decimalPlaces, format, rounding)
}
}
Loading

0 comments on commit fe09a72

Please sign in to comment.