Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SWA-134][IMPROVEMENT] - Fix CoW Trade type errors #335

Merged
25,725 changes: 6,200 additions & 19,525 deletions package-lock.json

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,13 @@
"eslint": "^8.19.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-import": "^2.25.4",
"eslint-plugin-prettier": "^4.0.0",
"eslint-plugin-prettier": "5.0.0",
"eslint-plugin-react": "^7.30.1",
"eslint-plugin-simple-import-sort": "^7.0.0",
"eslint-plugin-unused-imports": "^2.0.0",
"install-peers-cli": "^2.2.0",
"jest": "^27.5.1",
"prettier": "^3.2.2",
"ts-jest": "^27.1.3",
"ts-node": "^10.8.2",
"typescript": "^4.7.4",
Expand Down
30 changes: 15 additions & 15 deletions src/entities/DistributionCampaignBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,59 +11,59 @@ import { Token } from './token'
const MINIMUM_STAKED_AMOUNT_NATIVE_CURRENCY: Record<ChainId, CurrencyAmount> = {
[ChainId.RINKEBY]: CurrencyAmount.nativeCurrency(
parseUnits('0.05', Token.getNative(ChainId.RINKEBY).decimals).toString(),
ChainId.RINKEBY
ChainId.RINKEBY,
),
[ChainId.MAINNET]: CurrencyAmount.nativeCurrency(
parseUnits('0.1', Token.getNative(ChainId.MAINNET).decimals).toString(),
ChainId.MAINNET
ChainId.MAINNET,
),
[ChainId.XDAI]: CurrencyAmount.nativeCurrency(
parseUnits('1000', Token.getNative(ChainId.XDAI).decimals).toString(),
ChainId.XDAI
ChainId.XDAI,
),
[ChainId.ARBITRUM_ONE]: CurrencyAmount.nativeCurrency(
parseUnits('0.1', Token.getNative(ChainId.ARBITRUM_ONE).decimals).toString(),
ChainId.ARBITRUM_ONE
ChainId.ARBITRUM_ONE,
),
[ChainId.ARBITRUM_RINKEBY]: CurrencyAmount.nativeCurrency(
parseUnits('0.05', Token.getNative(ChainId.ARBITRUM_RINKEBY).decimals).toString(),
ChainId.ARBITRUM_RINKEBY
ChainId.ARBITRUM_RINKEBY,
),
[ChainId.ARBITRUM_GOERLI]: CurrencyAmount.nativeCurrency(
parseUnits('0.05', Token.getNative(ChainId.ARBITRUM_GOERLI).decimals).toString(),
ChainId.ARBITRUM_GOERLI
ChainId.ARBITRUM_GOERLI,
),
[ChainId.POLYGON]: CurrencyAmount.nativeCurrency(
parseUnits('0', Token.getNative(ChainId.POLYGON).decimals).toString(),
ChainId.POLYGON
ChainId.POLYGON,
),
[ChainId.GOERLI]: CurrencyAmount.nativeCurrency(
parseUnits('0.05', Token.getNative(ChainId.GOERLI).decimals).toString(),
ChainId.GOERLI
ChainId.GOERLI,
),
[ChainId.OPTIMISM_MAINNET]: CurrencyAmount.nativeCurrency(
parseUnits('0.05', Token.getNative(ChainId.OPTIMISM_MAINNET).decimals).toString(),
ChainId.OPTIMISM_MAINNET
ChainId.OPTIMISM_MAINNET,
),
[ChainId.OPTIMISM_GOERLI]: CurrencyAmount.nativeCurrency(
parseUnits('0.05', Token.getNative(ChainId.OPTIMISM_GOERLI).decimals).toString(),
ChainId.OPTIMISM_GOERLI
ChainId.OPTIMISM_GOERLI,
),
[ChainId.BSC_MAINNET]: CurrencyAmount.nativeCurrency(
parseUnits('0.05', Token.getNative(ChainId.BSC_MAINNET).decimals).toString(),
ChainId.BSC_MAINNET
ChainId.BSC_MAINNET,
),
[ChainId.BSC_TESTNET]: CurrencyAmount.nativeCurrency(
parseUnits('0.05', Token.getNative(ChainId.BSC_TESTNET).decimals).toString(),
ChainId.BSC_TESTNET
ChainId.BSC_TESTNET,
),
[ChainId.ZK_SYNC_ERA_MAINNET]: CurrencyAmount.nativeCurrency(
parseUnits('0', Token.getNative(ChainId.ZK_SYNC_ERA_MAINNET).decimals).toString(),
ChainId.ZK_SYNC_ERA_MAINNET
ChainId.ZK_SYNC_ERA_MAINNET,
),
[ChainId.ZK_SYNC_ERA_TESTNET]: CurrencyAmount.nativeCurrency(
parseUnits('0', Token.getNative(ChainId.ZK_SYNC_ERA_TESTNET).decimals).toString(),
ChainId.ZK_SYNC_ERA_TESTNET
ChainId.ZK_SYNC_ERA_TESTNET,
),
}

Expand Down Expand Up @@ -152,7 +152,7 @@ export class DistributionCampaignBase {
(accumulator, remainingRewardAmount) => {
return accumulator.add(remainingRewardAmount.nativeCurrencyAmount)
},
CurrencyAmount.nativeCurrency('0', this.chainId)
CurrencyAmount.nativeCurrency('0', this.chainId),
)

const yieldInPeriod = cumulativeRemainingRewardAmountNativeCurrency.divide(stakedValueNativeCurrency)
Expand Down
2 changes: 1 addition & 1 deletion src/entities/currency.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export class Currency {
18,
'ETH',
'Ether',
'0xDeadDeAddeAddEAddeadDEaDDEAdDeaDDeAD0000'
'0xDeadDeAddeAddEAddeadDEaDDEAdDeaDDeAD0000',
)
/**
* Gnosis Chain native currency
Expand Down
2 changes: 1 addition & 1 deletion src/entities/fractions/currencyAmount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export class CurrencyAmount extends Fraction {
public toFixed(
decimalPlaces: number = this.currency.decimals,
format?: object,
rounding: Rounding = Rounding.ROUND_DOWN
rounding: Rounding = Rounding.ROUND_DOWN,
): string {
invariant(decimalPlaces <= this.currency.decimals, 'DECIMALS')
return super.toFixed(decimalPlaces, format, rounding)
Expand Down
22 changes: 11 additions & 11 deletions src/entities/fractions/fraction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ export class Fraction {
return new Fraction(
JSBI.add(
JSBI.multiply(this.numerator, otherParsed.denominator),
JSBI.multiply(otherParsed.numerator, this.denominator)
JSBI.multiply(otherParsed.numerator, this.denominator),
),
JSBI.multiply(this.denominator, otherParsed.denominator)
JSBI.multiply(this.denominator, otherParsed.denominator),
)
}

Expand All @@ -68,56 +68,56 @@ export class Fraction {
return new Fraction(
JSBI.subtract(
JSBI.multiply(this.numerator, otherParsed.denominator),
JSBI.multiply(otherParsed.numerator, this.denominator)
JSBI.multiply(otherParsed.numerator, this.denominator),
),
JSBI.multiply(this.denominator, otherParsed.denominator)
JSBI.multiply(this.denominator, otherParsed.denominator),
)
}

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)
JSBI.multiply(otherParsed.numerator, this.denominator),
)
}

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)
JSBI.multiply(otherParsed.numerator, this.denominator),
)
}

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)
JSBI.multiply(otherParsed.numerator, this.denominator),
)
}

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)
JSBI.multiply(this.denominator, otherParsed.denominator),
)
}

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)
JSBI.multiply(this.denominator, otherParsed.numerator),
)
}

public toSignificant(
significantDigits: number,
format: object = { groupSeparator: '' },
rounding: Rounding = Rounding.ROUND_HALF_UP
rounding: Rounding = Rounding.ROUND_HALF_UP,
): string {
invariant(Number.isInteger(significantDigits), `${significantDigits} is not an integer.`)
invariant(significantDigits > 0, `${significantDigits} is not positive.`)
Expand All @@ -132,7 +132,7 @@ export class Fraction {
public toFixed(
decimalPlaces: number,
format: object = { groupSeparator: '' },
rounding: Rounding = Rounding.ROUND_HALF_UP
rounding: Rounding = Rounding.ROUND_HALF_UP,
): string {
invariant(Number.isInteger(decimalPlaces), `${decimalPlaces} is not an integer.`)
invariant(decimalPlaces >= 0, `${decimalPlaces} is negative.`)
Expand Down
4 changes: 2 additions & 2 deletions src/entities/fractions/price.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export class Price extends Fraction {
quoteCurrency: pair.reserve0.currency,
denominator: pair.reserve1.raw,
numerator: pair.reserve0.raw,
})
}),
)
}
return prices.slice(1).reduce((accumulator, currentValue) => accumulator.multiply(currentValue), prices[0])
Expand All @@ -52,7 +52,7 @@ export class Price extends Fraction {
this.quoteCurrency = quoteCurrency
this.scalar = new Fraction(
JSBI.exponentiate(TEN, JSBI.BigInt(baseCurrency.decimals)),
JSBI.exponentiate(TEN, JSBI.BigInt(quoteCurrency.decimals))
JSBI.exponentiate(TEN, JSBI.BigInt(quoteCurrency.decimals)),
)
}

Expand Down
4 changes: 2 additions & 2 deletions src/entities/fractions/priced-token-amount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ export class PricedTokenAmount extends TokenAmount {
this.token.price.quoteCurrency,
parseUnits(
this.multiply(this.token.price).toFixed(this.token.price.quoteCurrency.decimals),
this.token.price.quoteCurrency.decimals
).toString()
this.token.price.quoteCurrency.decimals,
).toString(),
)
}
}
4 changes: 2 additions & 2 deletions src/entities/kpi-token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export class KpiToken extends PricedToken {
collateral: PricedTokenAmount,
kpiId: string,
symbol?: string,
name?: string
name?: string,
) {
const collateralTokenNativeCurrency = collateral.nativeCurrencyAmount
const kpiTokenPrice = new Decimal(collateralTokenNativeCurrency.raw.toString()).dividedBy(totalSupply.toString())
Expand All @@ -37,7 +37,7 @@ export class KpiToken extends PricedToken {
numerator: parseUnits(kpiTokenPrice.toFixed(nativeCurrency.decimals), nativeCurrency.decimals).toString(),
}),
symbol,
name
name,
) // decimals are always 18 for kpi tokens
invariant(collateral.token.chainId === chainId, 'inconsistent chain id in collateral')
this.totalSupply = new TokenAmount(this, totalSupply)
Expand Down
22 changes: 11 additions & 11 deletions src/entities/pair.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export class Pair {
public static getAddress(
tokenA: Token,
tokenB: Token,
platform: UniswapV2RoutablePlatform = UniswapV2RoutablePlatform.SWAPR
platform: UniswapV2RoutablePlatform = UniswapV2RoutablePlatform.SWAPR,
): string {
const tokens = tokenA.sortsBefore(tokenB) ? [tokenA, tokenB] : [tokenB, tokenA] // does safety checks
const chainId = tokenA.chainId
Expand All @@ -115,7 +115,7 @@ export class Pair {
[tokens[1].address]: getCreate2Address(
platform.factoryAddress[chainId] as string,
keccak256(['bytes'], [pack(['address', 'address'], [tokens[0].address, tokens[1].address])]),
platform.initCodeHash
platform.initCodeHash,
),
},
},
Expand All @@ -131,7 +131,7 @@ export class Pair {
swapFee?: BigintIsh,
protocolFeeDenominator?: BigintIsh,
platform: UniswapV2RoutablePlatform = UniswapV2RoutablePlatform.SWAPR,
liquidityMiningCampaigns: LiquidityMiningCampaign[] = []
liquidityMiningCampaigns: LiquidityMiningCampaign[] = [],
) {
invariant(tokenAmountA.token.chainId === tokenAmountB.token.chainId, 'CHAIN_ID')
const tokenAmounts = tokenAmountA.token.sortsBefore(tokenAmountB.token) // does safety checks
Expand Down Expand Up @@ -228,7 +228,7 @@ export class Pair {
const denominator = JSBI.add(JSBI.multiply(inputReserve.raw, _10000), inputAmountWithFee)
const outputAmount = new TokenAmount(
inputAmount.token.equals(this.token0) ? this.token1 : this.token0,
JSBI.divide(numerator, denominator)
JSBI.divide(numerator, denominator),
)
if (JSBI.equal(outputAmount.raw, ZERO)) {
throw new InsufficientInputAmountError()
Expand All @@ -241,7 +241,7 @@ export class Pair {
this.swapFee,
this.protocolFeeDenominator,
this.platform,
this.liquidityMiningCampaigns
this.liquidityMiningCampaigns,
),
]
}
Expand All @@ -261,11 +261,11 @@ export class Pair {
const numerator = JSBI.multiply(JSBI.multiply(inputReserve.raw, outputAmount.raw), _10000)
const denominator = JSBI.multiply(
JSBI.subtract(outputReserve.raw, outputAmount.raw),
JSBI.subtract(_10000, parseBigintIsh(this.swapFee))
JSBI.subtract(_10000, parseBigintIsh(this.swapFee)),
)
const inputAmount = new TokenAmount(
outputAmount.token.equals(this.token0) ? this.token1 : this.token0,
JSBI.add(JSBI.divide(numerator, denominator), ONE)
JSBI.add(JSBI.divide(numerator, denominator), ONE),
)
return [
inputAmount,
Expand All @@ -275,15 +275,15 @@ export class Pair {
this.swapFee,
this.protocolFeeDenominator,
this.platform,
this.liquidityMiningCampaigns
this.liquidityMiningCampaigns,
),
]
}

public getLiquidityMinted(
totalSupply: TokenAmount,
tokenAmountA: TokenAmount,
tokenAmountB: TokenAmount
tokenAmountB: TokenAmount,
): TokenAmount {
invariant(totalSupply.token.equals(this.liquidityToken), 'LIQUIDITY')
const tokenAmounts = tokenAmountA.token.sortsBefore(tokenAmountB.token) // does safety checks
Expand All @@ -310,7 +310,7 @@ export class Pair {
totalSupply: TokenAmount,
liquidity: TokenAmount,
feeOn = false,
kLast?: BigintIsh
kLast?: BigintIsh,
): TokenAmount {
invariant(this.involvesToken(token), 'TOKEN')
invariant(totalSupply.token.equals(this.liquidityToken), 'TOTAL_SUPPLY')
Expand Down Expand Up @@ -341,7 +341,7 @@ export class Pair {

return new TokenAmount(
token,
JSBI.divide(JSBI.multiply(liquidity.raw, this.reserveOf(token).raw), totalSupplyAdjusted.raw)
JSBI.divide(JSBI.multiply(liquidity.raw, this.reserveOf(token).raw), totalSupplyAdjusted.raw),
)
}
}
2 changes: 1 addition & 1 deletion src/entities/platforms-breakdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export class Breakdown {
platforms
.reduce((accumulator, platform) => accumulator.add(platform.percentage), new Percent('0', '100'))
.toFixed(2) === '1.00',
'Inconsistent breakdown percentage'
'Inconsistent breakdown percentage',
)
if (input instanceof Token && output instanceof Token)
invariant(input.chainId === output.chainId, 'Input and output tokens must be on the same chain')
Expand Down
8 changes: 4 additions & 4 deletions src/entities/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,22 @@ export class Route {
invariant(pairs.length > 0, 'PAIRS')
invariant(
pairs.every((pair) => pair.chainId === pairs[0].chainId),
'CHAIN_IDS'
'CHAIN_IDS',
)
invariant(
pairs.every((pair) => pair.platform === pairs[0].platform),
'PLATFORM'
'PLATFORM',
)
invariant(
(input instanceof Token && pairs[0].involvesToken(input)) ||
(Currency.isNative(input) && pairs[0].involvesToken(Token.getNativeWrapper(pairs[0].chainId))),
'INPUT'
'INPUT',
)
invariant(
typeof output === 'undefined' ||
(output instanceof Token && pairs[pairs.length - 1].involvesToken(output)) ||
(Currency.isNative(output) && pairs[pairs.length - 1].involvesToken(Token.getNativeWrapper(pairs[0].chainId))),
'OUTPUT'
'OUTPUT',
)

const path: Token[] = [input instanceof Token ? input : Token.getNativeWrapper(pairs[0].chainId)]
Expand Down
2 changes: 1 addition & 1 deletion src/entities/single-sided-liquidity-mining-campaign.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export class SingleSidedLiquidityMiningCampaign extends DistributionCampaignBase
staked: PricedTokenAmount,
locked: boolean,
stakingCap: TokenAmount,
address?: string
address?: string,
) {
invariant(staked.token.equals(stakeToken), 'STAKED_LP_TOKEN')
super({
Expand Down
Loading
Loading