Commit f1dc5f8b authored by Noah Zinsmeister's avatar Noah Zinsmeister

properly validate getInputAmount against balances

parent d10e687c
...@@ -104,6 +104,7 @@ export class Exchange { ...@@ -104,6 +104,7 @@ export class Exchange {
invariant(JSBI.greaterThan(outputAmount.raw, ZERO), 'ZERO') invariant(JSBI.greaterThan(outputAmount.raw, ZERO), 'ZERO')
invariant(JSBI.greaterThan(this.reserve0.raw, ZERO), 'ZERO') invariant(JSBI.greaterThan(this.reserve0.raw, ZERO), 'ZERO')
invariant(JSBI.greaterThan(this.reserve1.raw, ZERO), 'ZERO') invariant(JSBI.greaterThan(this.reserve1.raw, ZERO), 'ZERO')
invariant(JSBI.lessThan(outputAmount.raw, this.reserveOf(outputAmount.token).raw), 'INSUFFICIENT_RESERVE')
const inputReserve = outputAmount.token.equals(this.reserve0.token) ? this.reserve1 : this.reserve0 const inputReserve = outputAmount.token.equals(this.reserve0.token) ? this.reserve1 : this.reserve0
const outputReserve = outputAmount.token.equals(this.reserve0.token) ? this.reserve0 : this.reserve1 const outputReserve = outputAmount.token.equals(this.reserve0.token) ? this.reserve0 : this.reserve1
......
...@@ -44,9 +44,6 @@ export class Trade { ...@@ -44,9 +44,6 @@ export class Trade {
constructor(route: Route, amount: TokenAmount, tradeType: TradeType) { constructor(route: Route, amount: TokenAmount, tradeType: TradeType) {
invariant(amount.token.equals(tradeType === TradeType.EXACT_INPUT ? route.input : route.output), 'TOKEN') invariant(amount.token.equals(tradeType === TradeType.EXACT_INPUT ? route.input : route.output), 'TOKEN')
const firstExchange = route.exchanges[tradeType === TradeType.EXACT_INPUT ? 0 : route.exchanges.length - 1]
// ensure that the amount is strictly less that the exchange's balance
invariant(JSBI.lessThan(amount.raw, firstExchange.reserveOf(amount.token).raw), 'RESERVE')
const amounts: TokenAmount[] = new Array(route.path.length) const amounts: TokenAmount[] = new Array(route.path.length)
const nextExchanges: Exchange[] = new Array(route.exchanges.length) const nextExchanges: Exchange[] = new Array(route.exchanges.length)
if (tradeType === TradeType.EXACT_INPUT) { if (tradeType === TradeType.EXACT_INPUT) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment