Skip to content

Commit

Permalink
use a static method that indicates it's unchecked
Browse files Browse the repository at this point in the history
  • Loading branch information
moodysalem committed Apr 26, 2021
1 parent 76b9ca6 commit c0eee21
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
39 changes: 39 additions & 0 deletions src/entities/trade.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,45 @@ describe('Trade', () => {
expect(trade.outputAmount.currency).toEqual(ETHER)
})

describe('#createUncheckedTrade', () => {
it('throws if input currency does not match route', () => {
expect(() =>
Trade.createUncheckedTrade({
route: new Route([pool_0_1], token0),
inputAmount: new TokenAmount(token2, 10000),
outputAmount: new TokenAmount(token1, 10000),
tradeType: TradeType.EXACT_INPUT
})
).toThrow('INPUT_CURRENCY_MATCH')
})
it('throws if output currency does not match route', () => {
expect(() =>
Trade.createUncheckedTrade({
route: new Route([pool_0_1], token0),
inputAmount: new TokenAmount(token0, 10000),
outputAmount: new TokenAmount(token2, 10000),
tradeType: TradeType.EXACT_INPUT
})
).toThrow('OUTPUT_CURRENCY_MATCH')
})
it('can create an exact input trade without simulating', () => {
Trade.createUncheckedTrade({
route: new Route([pool_0_1], token0),
inputAmount: new TokenAmount(token0, 10000),
outputAmount: new TokenAmount(token1, 100000),
tradeType: TradeType.EXACT_INPUT
})
})
it('can create an exact output trade without simulating', () => {
Trade.createUncheckedTrade({
route: new Route([pool_0_1], token0),
inputAmount: new TokenAmount(token0, 10000),
outputAmount: new TokenAmount(token1, 100000),
tradeType: TradeType.EXACT_OUTPUT
})
})
})

describe('#bestTradeExactIn', () => {
it('throws with empty pools', async () => {
await expect(Trade.bestTradeExactIn([], new TokenAmount(token0, JSBI.BigInt(10000)), token2)).rejects.toThrow(
Expand Down
22 changes: 21 additions & 1 deletion src/entities/trade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,12 @@ export class Trade {
return Trade.fromRoute(route, amountOut, TradeType.EXACT_OUTPUT)
}

/**
* Constructs a trade by simulating swaps through the given route
* @param route route to swap through
* @param amount the amount specified, either input or output, depending on tradeType
* @param tradeType whether the trade is an exact input or exact output swap
*/
public static async fromRoute(route: Route, amount: CurrencyAmount, tradeType: TradeType): Promise<Trade> {
const amounts: TokenAmount[] = new Array(route.tokenPath.length)
if (tradeType === TradeType.EXACT_INPUT) {
Expand Down Expand Up @@ -168,14 +174,28 @@ export class Trade {
})
}

/**
* Creates a trade without computing the result of swapping through the route. Useful when you have simulated the trade
* elsewhere and do not have any tick data
* @param constructorArguments the arguments passed to the trade constructor
*/
public static createUncheckedTrade(constructorArguments: {
route: Route
inputAmount: CurrencyAmount
outputAmount: CurrencyAmount
tradeType: TradeType
}): Trade {
return new Trade(constructorArguments)
}

/**
* Construct a trade by passing in the pre-computed property values
* @param route the route through which the trade occurs
* @param inputAmount the amount of input paid in the trade
* @param outputAmount the amount of output received in the trade
* @param tradeType the type of trade, exact input or exact output
*/
public constructor({
private constructor({
route,
inputAmount,
outputAmount,
Expand Down

0 comments on commit c0eee21

Please sign in to comment.