Skip to content

Commit

Permalink
Merge pull request #159 from rabbitholegg/mmackz-traderjoe-fix
Browse files Browse the repository at this point in the history
fix(traderjoe): fix issue with amount not working correctly using WETH
  • Loading branch information
mmackz authored Jan 5, 2024
2 parents f3c244c + 01ae21b commit d450b4e
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 21 deletions.
5 changes: 5 additions & 0 deletions .changeset/orange-pugs-vanish.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@rabbitholegg/questdk-plugin-traderjoe": patch
---

fix issue with amount not working correctly when WETH is chosen as tokenIn
29 changes: 25 additions & 4 deletions packages/traderjoe/src/TraderJoe.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,30 @@ import { CHAIN_ID_ARRAY } from './chain-ids'
import { Tokens } from './contract-addresses'
import { TOKENS_FOR_EXACT_TOKENS } from './test-transactions'
import { passingTestCases, failingTestCases } from './test-setup'
import {
EXACT_NATIVE_FOR_TOKENS_ABI,
EXACT_TOKENS_FOR_NATIVE_ABI,
EXACT_TOKENS_FOR_TOKENS_ABI,
NATIVE_FOR_EXACT_TOKENS_ABI,
TOKENS_FOR_EXACT_NATIVE_ABI,
TOKENS_FOR_EXACT_TOKENS_ABI,
} from './abi'

describe('Given the TraderJoe plugin', () => {
describe('When handling the swap action', () => {
describe('should return a valid action filter', () => {
test('when swapping tokens', async () => {
const { params } = TOKENS_FOR_EXACT_TOKENS
const filter = await swap(params)
console.log(JSON.stringify(filter, null, 2))
expect(filter).to.deep.equal({
chainId: 42161,
to: '0xb4315e873dBcf96Ffd0acd8EA43f689D8c20fB30',
value: 0,
input: {
$abi: LBRouterV21ABI,
$and: [
{
$abi: LBRouterV21ABI,
to: '0x22e798f9440f563b92aae24e94c75dfa499e3d3e',
path: {
tokenPath: {
Expand All @@ -38,6 +48,13 @@ describe('Given the TraderJoe plugin', () => {
{
$or: [
{
$abi: EXACT_NATIVE_FOR_TOKENS_ABI,
amountOutMin: {
$gte: '5315300000000000000000',
},
},
{
$abi: EXACT_TOKENS_FOR_TOKENS_ABI,
amountIn: {
$gte: '5996000000',
},
Expand All @@ -46,11 +63,13 @@ describe('Given the TraderJoe plugin', () => {
},
},
{
$abi: NATIVE_FOR_EXACT_TOKENS_ABI,
amountOut: {
$gte: '5315300000000000000000',
},
},
{
$abi: TOKENS_FOR_EXACT_TOKENS_ABI,
amountInMax: {
$gte: '5996000000',
},
Expand All @@ -59,6 +78,7 @@ describe('Given the TraderJoe plugin', () => {
},
},
{
$abi: EXACT_TOKENS_FOR_NATIVE_ABI,
amountIn: {
$gte: '5996000000',
},
Expand All @@ -67,12 +87,13 @@ describe('Given the TraderJoe plugin', () => {
},
},
{
amountNATIVEOut: {
$gte: '5315300000000000000000',
},
$abi: TOKENS_FOR_EXACT_NATIVE_ABI,
amountInMax: {
$gte: '5996000000',
},
amountNATIVEOut: {
$gte: '5315300000000000000000',
},
},
],
},
Expand Down
62 changes: 45 additions & 17 deletions packages/traderjoe/src/TraderJoe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ import { LBRouterV21ABI, LB_ROUTER_V21_ADDRESS } from '@traderjoe-xyz/sdk-v2'
import { DEFAULT_SWAP_TOKEN_LIST, Tokens } from './contract-addresses'
import { CHAIN_ID_ARRAY } from './chain-ids'
import { buildPathQuery, Chains } from './utils'
import {
EXACT_NATIVE_FOR_TOKENS_ABI,
EXACT_TOKENS_FOR_NATIVE_ABI,
EXACT_TOKENS_FOR_TOKENS_ABI,
NATIVE_FOR_EXACT_TOKENS_ABI,
TOKENS_FOR_EXACT_NATIVE_ABI,
TOKENS_FOR_EXACT_TOKENS_ABI,
} from './abi'

export const swap = async (
swap: SwapActionParams,
Expand All @@ -26,33 +34,53 @@ export const swap = async (
const nativeOut = tokenOut === NATIVE_TOKEN
const to = contractAddress ?? LB_ROUTER_V21_ADDRESS[chainId as Chains]

const input = {
$abi: LBRouterV21ABI,
to: recipient,
path: {
tokenPath: buildPathQuery(
nativeIn ? Tokens[chainId]?.WETH : tokenIn,
nativeOut ? Tokens[chainId]?.WETH : tokenOut,
),
},
}

return compressJson({
chainId,
to,
value: nativeIn ? amountIn : undefined,
value: nativeIn ? amountIn : tokenIn ? 0 : undefined,
input: {
$abi: LBRouterV21ABI,
$and: [
{
to: recipient,
path: {
tokenPath: buildPathQuery(
nativeIn ? Tokens[chainId]?.WETH : tokenIn,
nativeOut ? Tokens[chainId]?.WETH : tokenOut,
),
},
},
input,
{
$or: [
{
// exactNativeforTokens, exactTokensForTokens
amountIn: nativeIn ? undefined : amountIn,
// exactNativeforTokens
$abi: EXACT_NATIVE_FOR_TOKENS_ABI,
amountOutMin: amountOut,
},
{ amountOut: amountOut }, // nativeForExactTokens
{ amountInMax: amountIn, amountOut: amountOut }, // tokensForExactTokens
{ amountIn: amountIn, amountOutMinNATIVE: amountOut }, // exactTokensForNative
{ amountInMax: amountIn, amountNATIVEOut: amountOut }, // tokensForExactNative
{
// exactNativeforTokens
$abi: EXACT_TOKENS_FOR_TOKENS_ABI,
amountIn,
amountOutMin: amountOut,
},
{ $abi: NATIVE_FOR_EXACT_TOKENS_ABI, amountOut: amountOut }, // nativeForExactTokens
{
$abi: TOKENS_FOR_EXACT_TOKENS_ABI,
amountInMax: amountIn,
amountOut: amountOut,
}, // tokensForExactTokens
{
$abi: EXACT_TOKENS_FOR_NATIVE_ABI,
amountIn: amountIn,
amountOutMinNATIVE: amountOut,
}, // exactTokensForNative
{
$abi: TOKENS_FOR_EXACT_NATIVE_ABI,
amountInMax: amountIn,
amountNATIVEOut: amountOut,
}, // tokensForExactNative
],
},
],
Expand Down
23 changes: 23 additions & 0 deletions packages/traderjoe/src/abi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { LBRouterV21ABI } from '@traderjoe-xyz/sdk-v2'

export const EXACT_TOKENS_FOR_TOKENS_ABI = LBRouterV21ABI.filter(
(abi) =>
abi.type === 'function' && abi.name.startsWith('swapExactTokensForTokens'),
)
export const EXACT_TOKENS_FOR_NATIVE_ABI = LBRouterV21ABI.filter(
(abi) =>
abi.type === 'function' && abi.name.startsWith('swapExactTokensForNATIVE'),
)
export const TOKENS_FOR_EXACT_TOKENS_ABI = LBRouterV21ABI.filter(
(abi) => abi.type === 'function' && abi.name === 'swapTokensForExactTokens',
)
export const EXACT_NATIVE_FOR_TOKENS_ABI = LBRouterV21ABI.filter(
(abi) =>
abi.type === 'function' && abi.name.startsWith('swapExactNATIVEForTokens'),
)
export const NATIVE_FOR_EXACT_TOKENS_ABI = LBRouterV21ABI.filter(
(abi) => abi.type === 'function' && abi.name === 'swapNATIVEForExactTokens',
)
export const TOKENS_FOR_EXACT_NATIVE_ABI = LBRouterV21ABI.filter(
(abi) => abi.type === 'function' && abi.name === 'swapTokensForExactNATIVE',
)
15 changes: 15 additions & 0 deletions packages/traderjoe/src/test-setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
NATIVE_FOR_EXACT_TOKENS,
TOKENS_FOR_EXACT_NATIVE,
TOKENS_FOR_EXACT_TOKENS,
WETH_AMOUNT_IN,
} from './test-transactions'

export const passingTestCases: TestCase<SwapActionParams>[] = [
Expand Down Expand Up @@ -46,6 +47,16 @@ export const passingTestCases: TestCase<SwapActionParams>[] = [
tokenIn: undefined,
tokenOut: undefined,
}),
createTestCase(EXACT_NATIVE_FOR_TOKENS, 'when tokenIn is set to "any"', {
tokenIn: undefined,
}),
createTestCase(EXACT_NATIVE_FOR_TOKENS, 'when tokenOut is set to "any"', {
tokenOut: undefined,
}),
createTestCase(EXACT_NATIVE_FOR_TOKENS, 'when tokens are set to "any/any"', {
tokenIn: undefined,
tokenOut: undefined,
}),
]

export const failingTestCases: TestCase<SwapActionParams>[] = [
Expand All @@ -70,4 +81,8 @@ export const failingTestCases: TestCase<SwapActionParams>[] = [
createTestCase(EXACT_NATIVE_FOR_TOKENS, 'when recipient in incorrect', {
recipient: '0x7a227272e5B583c2B51B04fF5cA4FDe498368b44',
}),
createTestCase(
WETH_AMOUNT_IN,
'when using WETH and amount is not sufficient',
),
]
17 changes: 17 additions & 0 deletions packages/traderjoe/src/test-transactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,3 +188,20 @@ export const TOKENS_FOR_EXACT_TOKENS: TestParams<SwapActionParams> = {
recipient: '0x22e798f9440f563b92aae24e94c75dfa499e3d3e',
},
}

export const WETH_AMOUNT_IN: TestParams<SwapActionParams> = {
transaction: {
chainId: 42161,
from: '0xf44000901d480248b4dd1a388da2659cfdabec3f',
to: '0xb4315e873dbcf96ffd0acd8ea43f689d8c20fb30',
hash: '0xee5a4815403dfe13e487debbf65d74e16888a3d756f544a80edd8a9c4a4f7cc8',
input:
'0xb066ea7c00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000080000000000000000000000000f44000901d480248b4dd1a388da2659cfdabec3f0000000000000000000000000000000000000000000000000000000065985d61000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000e00000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000001400000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000200000000000000000000000082af49447d8a07e3bd95bd0d56f35241523fbab1000000000000000000000000371c7ec6d8039ff7933a2aa28eb827ffe1f52f07',
value: '1',
},
params: {
chainId: Chains.ARBITRUM_ONE,
tokenIn: Tokens[Chains.ARBITRUM_ONE].WETH,
amountIn: GreaterThanOrEqual(parseEther('1')),
},
}

0 comments on commit d450b4e

Please sign in to comment.