Skip to content
This repository has been archived by the owner on Apr 25, 2024. It is now read-only.

Commit

Permalink
prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
zhongeric committed Mar 28, 2024
1 parent 000fa6b commit ee0376d
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 41 deletions.
6 changes: 3 additions & 3 deletions src/entities/protocols/uniswap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,13 @@ interface Swap<TInput extends Currency, TOutput extends Currency> {
// also translates trade objects from previous (v2, v3) SDKs
export class UniswapTrade implements Command {
readonly tradeType: RouterTradeType = RouterTradeType.UniswapTrade
readonly payerIsUser: boolean;
readonly payerIsUser: boolean

constructor(public trade: RouterTrade<Currency, Currency, TradeType>, public options: SwapOptions) {
if (!!options.fee && !!options.flatFee) throw new Error('Only one fee option permitted')

if(this.inputRequiresWrap) this.payerIsUser = false;
else if (this.options.directSend) this.payerIsUser = false;
if (this.inputRequiresWrap) this.payerIsUser = false
else if (this.options.directSend) this.payerIsUser = false
else this.payerIsUser = true
}

Expand Down
6 changes: 3 additions & 3 deletions src/utils/routerTradeAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,10 @@ export class RouterTradeAdapter {

if (!route) throw new Error('Expected route to be present')
if (!route.length) throw new Error('Expected there to be at least one route')
if(route.some((r) => !r.length)) throw new Error('Expected all routes to have at least one pool')
if (route.some((r) => !r.length)) throw new Error('Expected all routes to have at least one pool')
const firstRoute = route[0]
if(!firstRoute.length) throw new Error('Expected route to have at least one pool')
if (!firstRoute.length) throw new Error('Expected route to have at least one pool')

const tokenInData = firstRoute[0].tokenIn
const tokenOutData = firstRoute[firstRoute.length - 1].tokenOut

Expand Down
2 changes: 1 addition & 1 deletion test/forge/interop.json
Original file line number Diff line number Diff line change
Expand Up @@ -247,4 +247,4 @@
"calldata": "0x24856bc30000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000030b000400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000c00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000de0b6b3a76400000000000000000000000000000000000000000000000000000000000000000120000000000000000000000000aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa0000000000000000000000000000000000000000000000000de0b6b3a764000000000000000000000000000000000000000000000000003eb3459f0ce6ae000b00000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000042c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2000bb8a0b86991c6218b36c1d19d4a2e9eb0ce3606eb480001f46b175474e89094c44da98b954eedeac495271d0f00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa0000000000000000000000000000000000000000000000000000000000000000",
"value": "1000000000000000000"
}
}
}
174 changes: 140 additions & 34 deletions test/uniswapTrades.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ describe('Uniswap', () => {
CurrencyAmount.fromRawAmount(USDC, outputUSDC),
TradeType.EXACT_OUTPUT
)
const routerTrade = buildTrade([trade]);
const routerTrade = buildTrade([trade])
const opts = swapOptions({})
const methodParameters = SwapRouter.swapERC20CallParameters(buildTrade([trade]), opts)
const methodParametersV2 = SwapRouter.swapCallParameters(new UniswapTrade(buildTrade([trade]), opts))
Expand Down Expand Up @@ -706,10 +706,19 @@ describe('Uniswap', () => {

for (let tradeType of [TradeType.EXACT_INPUT, TradeType.EXACT_OUTPUT]) {
describe('RouterTradeAdapter ' + tradeType, () => {
const getAmountToken = (tokenIn: Token | NativeCurrency, tokenOut: Token | NativeCurrency, tradeType: TradeType): Token | NativeCurrency => {
const getAmountToken = (
tokenIn: Token | NativeCurrency,
tokenOut: Token | NativeCurrency,
tradeType: TradeType
): Token | NativeCurrency => {
return tradeType === TradeType.EXACT_INPUT ? tokenIn : tokenOut
}
const getAmount = (tokenIn: Token | NativeCurrency, tokenOut: Token | NativeCurrency, amount: string, tradeType: TradeType): CurrencyAmount<Token | NativeCurrency> => {
const getAmount = (
tokenIn: Token | NativeCurrency,
tokenOut: Token | NativeCurrency,
amount: string,
tradeType: TradeType
): CurrencyAmount<Token | NativeCurrency> => {
return tradeType === TradeType.EXACT_INPUT
? CurrencyAmount.fromRawAmount(tokenIn, amount)
: CurrencyAmount.fromRawAmount(tokenOut, amount)
Expand Down Expand Up @@ -794,7 +803,9 @@ describe('Uniswap', () => {
}
it('v2 - erc20 <> erc20', async () => {
const [tokenIn, tokenOut] = [DAI, USDC]
const inputAmount = ethers.utils.parseUnits("1000", getAmountToken(tokenIn, tokenOut, tradeType).decimals).toString();
const inputAmount = ethers.utils
.parseUnits('1000', getAmountToken(tokenIn, tokenOut, tradeType).decimals)
.toString()
const rawInputAmount = getAmount(tokenIn, tokenOut, inputAmount, tradeType)

const opts = swapOptions({})
Expand All @@ -806,7 +817,17 @@ describe('Uniswap', () => {
tokenIn: DAI.address,
tokenOut: USDC.address,
tradeType,
route: [[mockV2PoolInRoute(USDC_DAI_V2, tokenIn, tokenOut, trade.inputAmount.quotient.toString(), trade.outputAmount.quotient.toString())]],
route: [
[
mockV2PoolInRoute(
USDC_DAI_V2,
tokenIn,
tokenOut,
trade.inputAmount.quotient.toString(),
trade.outputAmount.quotient.toString()
),
],
],
}
const routerTrade = RouterTradeAdapter.fromClassicQuote(classicQuote)
const uniswapTrade = new UniswapTrade(routerTrade, opts)
Expand All @@ -821,7 +842,9 @@ describe('Uniswap', () => {

it('v3 - erc20 <> erc20', async () => {
const [tokenIn, tokenOut] = [DAI, USDC]
const inputAmount = ethers.utils.parseUnits("1000", getAmountToken(tokenIn, tokenOut, tradeType).decimals).toString();
const inputAmount = ethers.utils
.parseUnits('1000', getAmountToken(tokenIn, tokenOut, tradeType).decimals)
.toString()
const rawInputAmount = getAmount(tokenIn, tokenOut, inputAmount, tradeType)

const opts = swapOptions({})
Expand All @@ -832,7 +855,17 @@ describe('Uniswap', () => {
tokenIn: DAI.address,
tokenOut: USDC.address,
tradeType,
route: [[mockV3PoolInRoute(USDC_DAI_V3, tokenIn, tokenOut, trade.inputAmount.quotient.toString(), trade.outputAmount.quotient.toString())]],
route: [
[
mockV3PoolInRoute(
USDC_DAI_V3,
tokenIn,
tokenOut,
trade.inputAmount.quotient.toString(),
trade.outputAmount.quotient.toString()
),
],
],
}
const routerTrade = RouterTradeAdapter.fromClassicQuote(classicQuote)
const uniswapTrade = new UniswapTrade(routerTrade, opts)
Expand All @@ -847,7 +880,9 @@ describe('Uniswap', () => {

it('v2 - handles weth input properly', async () => {
const [tokenIn, tokenOut] = [WETH, USDC]
const inputAmount = ethers.utils.parseUnits("1", getAmountToken(tokenIn, tokenOut, tradeType).decimals).toString();
const inputAmount = ethers.utils
.parseUnits('1', getAmountToken(tokenIn, tokenOut, tradeType).decimals)
.toString()
const rawInputAmount = getAmount(tokenIn, tokenOut, inputAmount, tradeType)

const opts = swapOptions({})
Expand All @@ -858,7 +893,17 @@ describe('Uniswap', () => {
tokenIn: WETH.address,
tokenOut: USDC.address,
tradeType,
route: [[mockV2PoolInRoute(WETH_USDC_V2, WETH, USDC, trade.inputAmount.quotient.toString(), trade.outputAmount.quotient.toString())]],
route: [
[
mockV2PoolInRoute(
WETH_USDC_V2,
WETH,
USDC,
trade.inputAmount.quotient.toString(),
trade.outputAmount.quotient.toString()
),
],
],
}
const routerTrade = RouterTradeAdapter.fromClassicQuote(classicQuote)
const uniswapTrade = new UniswapTrade(routerTrade, opts)
Expand All @@ -873,7 +918,9 @@ describe('Uniswap', () => {

it('v3 - handles weth input properly', async () => {
const [tokenIn, tokenOut] = [WETH, USDC]
const inputAmount = ethers.utils.parseUnits("1", getAmountToken(tokenIn, tokenOut, tradeType).decimals).toString();
const inputAmount = ethers.utils
.parseUnits('1', getAmountToken(tokenIn, tokenOut, tradeType).decimals)
.toString()
const rawInputAmount = getAmount(tokenIn, tokenOut, inputAmount, tradeType)

const opts = swapOptions({})
Expand All @@ -884,7 +931,17 @@ describe('Uniswap', () => {
tokenIn: WETH.address,
tokenOut: USDC.address,
tradeType,
route: [[mockV3PoolInRoute(WETH_USDC_V3, WETH, USDC, trade.inputAmount.quotient.toString(), trade.outputAmount.quotient.toString())]],
route: [
[
mockV3PoolInRoute(
WETH_USDC_V3,
WETH,
USDC,
trade.inputAmount.quotient.toString(),
trade.outputAmount.quotient.toString()
),
],
],
}
const routerTrade = RouterTradeAdapter.fromClassicQuote(classicQuote)
const uniswapTrade = new UniswapTrade(routerTrade, opts)
Expand All @@ -899,7 +956,9 @@ describe('Uniswap', () => {

it('v2 - handles eth input properly', async () => {
const [tokenIn, tokenOut] = [Ether.onChain(1), USDC]
const inputAmount = ethers.utils.parseUnits("1", getAmountToken(tokenIn, tokenOut, tradeType).decimals).toString();
const inputAmount = ethers.utils
.parseUnits('1', getAmountToken(tokenIn, tokenOut, tradeType).decimals)
.toString()
const rawInputAmount = getAmount(tokenIn, tokenOut, inputAmount, tradeType)

const opts = swapOptions({})
Expand All @@ -913,7 +972,13 @@ describe('Uniswap', () => {
route: [
[
// WETH here since all pairs use WETH
mockV2PoolInRoute(WETH_USDC_V2, WETH, USDC, trade.inputAmount.quotient.toString(), trade.outputAmount.quotient.toString())
mockV2PoolInRoute(
WETH_USDC_V2,
WETH,
USDC,
trade.inputAmount.quotient.toString(),
trade.outputAmount.quotient.toString()
),
],
],
}
Expand All @@ -930,7 +995,9 @@ describe('Uniswap', () => {

it('v2 - handles eth input properly - 0xeeee...eeee address', async () => {
const [tokenIn, tokenOut] = [Ether.onChain(1), USDC]
const inputAmount = ethers.utils.parseUnits("1", getAmountToken(tokenIn, tokenOut, tradeType).decimals).toString();
const inputAmount = ethers.utils
.parseUnits('1', getAmountToken(tokenIn, tokenOut, tradeType).decimals)
.toString()
const rawInputAmount = getAmount(tokenIn, tokenOut, inputAmount, tradeType)

const opts = swapOptions({})
Expand All @@ -944,7 +1011,13 @@ describe('Uniswap', () => {
route: [
[
// WETH here since all pairs use WETH
mockV2PoolInRoute(WETH_USDC_V2, WETH, USDC, trade.inputAmount.quotient.toString(), trade.outputAmount.quotient.toString())
mockV2PoolInRoute(
WETH_USDC_V2,
WETH,
USDC,
trade.inputAmount.quotient.toString(),
trade.outputAmount.quotient.toString()
),
],
],
}
Expand All @@ -961,7 +1034,9 @@ describe('Uniswap', () => {

it('v3 - handles eth input properly', async () => {
const [tokenIn, tokenOut] = [Ether.onChain(1), USDC]
const inputAmount = ethers.utils.parseUnits("1", getAmountToken(tokenIn, tokenOut, tradeType).decimals).toString();
const inputAmount = ethers.utils
.parseUnits('1', getAmountToken(tokenIn, tokenOut, tradeType).decimals)
.toString()
const rawInputAmount = getAmount(tokenIn, tokenOut, inputAmount, tradeType)

const opts = swapOptions({})
Expand All @@ -979,7 +1054,13 @@ describe('Uniswap', () => {
route: [
[
// WETH here since all pools use WETH
mockV3PoolInRoute(WETH_USDC_V3, WETH, USDC, trade.inputAmount.quotient.toString(), trade.outputAmount.quotient.toString())
mockV3PoolInRoute(
WETH_USDC_V3,
WETH,
USDC,
trade.inputAmount.quotient.toString(),
trade.outputAmount.quotient.toString()
),
],
],
}
Expand All @@ -996,7 +1077,9 @@ describe('Uniswap', () => {

it('v2 - handles eth output properly', async () => {
const [tokenIn, tokenOut] = [USDC, Ether.onChain(1)]
const inputAmount = ethers.utils.parseUnits("1", getAmountToken(tokenIn, tokenOut, tradeType).decimals).toString();
const inputAmount = ethers.utils
.parseUnits('1', getAmountToken(tokenIn, tokenOut, tradeType).decimals)
.toString()
const rawInputAmount = getAmount(tokenIn, tokenOut, inputAmount, tradeType)

const opts = swapOptions({})
Expand All @@ -1010,7 +1093,13 @@ describe('Uniswap', () => {
route: [
[
// WETH here since all pairs use WETH
mockV2PoolInRoute(WETH_USDC_V2, USDC, WETH, trade.inputAmount.quotient.toString(), trade.outputAmount.quotient.toString()),
mockV2PoolInRoute(
WETH_USDC_V2,
USDC,
WETH,
trade.inputAmount.quotient.toString(),
trade.outputAmount.quotient.toString()
),
],
],
}
Expand All @@ -1027,15 +1116,13 @@ describe('Uniswap', () => {

it('v3 - handles eth output properly', async () => {
const [tokenIn, tokenOut] = [USDC, Ether.onChain(1)]
const inputAmount = ethers.utils.parseUnits("1", getAmountToken(tokenIn, tokenOut, tradeType).decimals).toString();
const inputAmount = ethers.utils
.parseUnits('1', getAmountToken(tokenIn, tokenOut, tradeType).decimals)
.toString()
const rawInputAmount = getAmount(tokenIn, tokenOut, inputAmount, tradeType)

const opts = swapOptions({})
const trade = await V3Trade.fromRoute(
new RouteV3([WETH_USDC_V3], tokenIn, tokenOut),
rawInputAmount,
tradeType
)
const trade = await V3Trade.fromRoute(new RouteV3([WETH_USDC_V3], tokenIn, tokenOut), rawInputAmount, tradeType)
const SDKMethodParameters = SwapRouter.swapCallParameters(new UniswapTrade(buildTrade([trade]), opts))

const classicQuote: PartialClassicQuote = {
Expand All @@ -1045,7 +1132,13 @@ describe('Uniswap', () => {
route: [
[
// WETH here since all pairs use WETH
mockV3PoolInRoute(WETH_USDC_V3, USDC, WETH, trade.inputAmount.quotient.toString(), trade.outputAmount.quotient.toString()),
mockV3PoolInRoute(
WETH_USDC_V3,
USDC,
WETH,
trade.inputAmount.quotient.toString(),
trade.outputAmount.quotient.toString()
),
],
],
}
Expand All @@ -1062,7 +1155,9 @@ describe('Uniswap', () => {

it('v3 - multi pool erc20 <> erc20', async () => {
const [tokenIn, tokenOut] = [DAI, WETH]
const inputAmount = ethers.utils.parseUnits("1", getAmountToken(tokenIn, tokenOut, tradeType).decimals).toString();
const inputAmount = ethers.utils
.parseUnits('1', getAmountToken(tokenIn, tokenOut, tradeType).decimals)
.toString()
const rawInputAmount = getAmount(tokenIn, tokenOut, inputAmount, tradeType)

const opts = swapOptions({})
Expand All @@ -1079,8 +1174,20 @@ describe('Uniswap', () => {
tradeType,
route: [
[
mockV3PoolInRoute(USDC_DAI_V3, DAI, USDC, trade.inputAmount.quotient.toString(), trade.outputAmount.quotient.toString()),
mockV3PoolInRoute(WETH_USDC_V3, USDC, WETH, trade.inputAmount.quotient.toString(), trade.outputAmount.quotient.toString()),
mockV3PoolInRoute(
USDC_DAI_V3,
DAI,
USDC,
trade.inputAmount.quotient.toString(),
trade.outputAmount.quotient.toString()
),
mockV3PoolInRoute(
WETH_USDC_V3,
USDC,
WETH,
trade.inputAmount.quotient.toString(),
trade.outputAmount.quotient.toString()
),
],
],
}
Expand All @@ -1097,7 +1204,9 @@ describe('Uniswap', () => {

it('v3 - handles split routes properly', async () => {
const [tokenIn, tokenOut] = [WETH, USDC]
const inputAmount = ethers.utils.parseUnits("1", getAmountToken(tokenIn, tokenOut, tradeType).decimals).toString();
const inputAmount = ethers.utils
.parseUnits('1', getAmountToken(tokenIn, tokenOut, tradeType).decimals)
.toString()
const rawInputAmount = getAmount(tokenIn, tokenOut, inputAmount, tradeType)

const opts = swapOptions({})
Expand All @@ -1113,10 +1222,7 @@ describe('Uniswap', () => {
)
const SDKMethodParameters = SwapRouter.swapCallParameters(new UniswapTrade(buildTrade([trade1, trade2]), opts))

const splitRouteInputAmounts = [
trade1.inputAmount.quotient.toString(),
trade2.inputAmount.quotient.toString(),
]
const splitRouteInputAmounts = [trade1.inputAmount.quotient.toString(), trade2.inputAmount.quotient.toString()]

const splitRouteOutputAmounts = [
trade1.outputAmount.quotient.toString(),
Expand Down

0 comments on commit ee0376d

Please sign in to comment.