Skip to content

Commit

Permalink
Merge pull request #207 from rabbitholegg/mmackz-camelot-utils
Browse files Browse the repository at this point in the history
refactor(camelot): use built-in utils
  • Loading branch information
mmackz authored Feb 7, 2024
2 parents 17f09b9 + d3f22a7 commit 4185050
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 67 deletions.
5 changes: 5 additions & 0 deletions .changeset/spotty-squids-marry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@rabbitholegg/questdk-plugin-camelot": minor
---

use built-in utils
4 changes: 2 additions & 2 deletions packages/camelot/src/Camelot.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
CAMELOT_V3_EXACT_OUTPUT_ABI,
PARASWAP_ABI,
} from './abi'
import { ARBITRUM_CHAIN_ID } from './chain-ids'
import { Chains } from '@rabbitholegg/questdk-plugin-utils'
import { parseEther, getAddress } from 'viem'
import { swap } from './Camelot'
import { Tokens } from './utils'
Expand All @@ -22,7 +22,7 @@ describe('Given the camelot plugin', () => {
describe('should return a valid action filter', () => {
test('for a swap using ERC-20 token as tokenIn', async () => {
const filter = await swap({
chainId: ARBITRUM_CHAIN_ID,
chainId: Chains.ARBITRUM_ONE,
tokenIn: Tokens.USDT,
tokenOut: Tokens.WETH,
amountIn: GreaterThanOrEqual(1000000n),
Expand Down
5 changes: 3 additions & 2 deletions packages/camelot/src/Camelot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
compressJson,
} from '@rabbitholegg/questdk'
import { type Address } from 'viem'
import { CHAIN_ID_ARRAY, ARBITRUM_CHAIN_ID } from './chain-ids'
import { CHAIN_ID_ARRAY } from './chain-ids'
import { buildV2PathQuery, buildV3PathQuery, Tokens } from './utils'
import {
CAMELOT_V2_ABI,
Expand All @@ -19,6 +19,7 @@ import {
PARASWAP_ROUTER,
INTERNAL_ETH_ADDRESS,
} from './contract-addresses'
import { Chains } from '@rabbitholegg/questdk-plugin-utils'

const PARASWAP_PARTNER = '0x353D2d14Bb674892910685520Ac040f560CcBC06'

Expand Down Expand Up @@ -159,7 +160,7 @@ export const getSupportedTokenAddresses = async (
_chainId: number,
): Promise<Address[]> => {
// Only return supported tokens for ARBITRUM_CHAIN_ID
return _chainId === ARBITRUM_CHAIN_ID ? DEFAULT_TOKEN_LIST : []
return _chainId === Chains.ARBITRUM_ONE ? DEFAULT_TOKEN_LIST : []
}

export const getSupportedChainIds = async (): Promise<number[]> => {
Expand Down
5 changes: 3 additions & 2 deletions packages/camelot/src/chain-ids.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export const ARBITRUM_CHAIN_ID = 42161
export const CHAIN_ID_ARRAY = [ARBITRUM_CHAIN_ID]
import { Chains } from '@rabbitholegg/questdk-plugin-utils'

export const CHAIN_ID_ARRAY = [Chains.ARBITRUM_ONE]
3 changes: 0 additions & 3 deletions packages/camelot/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
// This file is standard for more projects.
// The main degree of nuance is in the

import {
type IActionPlugin,
PluginActionNotImplementedError,
Expand Down
3 changes: 2 additions & 1 deletion packages/camelot/src/test-setup.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { GreaterThanOrEqual } from '@rabbitholegg/questdk'
import { parseEther, parseUnits } from 'viem'
import { Tokens, createTestCase } from './utils'
import { Tokens } from './utils'
import { createTestCase } from '@rabbitholegg/questdk-plugin-utils'
import {
V2_SWAP_ETH,
V2_SWAP_TOKENS,
Expand Down
20 changes: 10 additions & 10 deletions packages/camelot/src/test-transactions.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ARBITRUM_CHAIN_ID } from './chain-ids'
import { Chains, type TestParams } from '@rabbitholegg/questdk-plugin-utils'
import { CAMELOT_V2_ROUTER } from './contract-addresses'
import { parseEther, parseUnits } from 'viem'
import { Tokens, type TestParams } from './utils'
import { Tokens } from './utils'
import {
GreaterThanOrEqual,
type SwapActionParams,
Expand All @@ -18,7 +18,7 @@ export const V2_SWAP_ETH: TestParams<SwapActionParams> = {
value: '550000000000000',
},
params: {
chainId: ARBITRUM_CHAIN_ID,
chainId: Chains.ARBITRUM_ONE,
contractAddress: CAMELOT_V2_ROUTER,
tokenIn: Tokens.ETH,
tokenOut: '0xBfbCFe8873fE28Dfa25f1099282b088D52bbAD9C', // EQB Token
Expand All @@ -38,7 +38,7 @@ export const V2_TOKENS_TO_ETH: TestParams<SwapActionParams> = {
value: '0',
},
params: {
chainId: ARBITRUM_CHAIN_ID,
chainId: Chains.ARBITRUM_ONE,
tokenIn: '0x939727d85d99d0ac339bf1b76dfe30ca27c19067', // SIZE
tokenOut: Tokens.ETH,
amountIn: GreaterThanOrEqual(parseUnits('20000000', 18)),
Expand All @@ -57,7 +57,7 @@ export const V2_SWAP_TOKENS: TestParams<SwapActionParams> = {
value: '0',
},
params: {
chainId: ARBITRUM_CHAIN_ID,
chainId: Chains.ARBITRUM_ONE,
tokenIn: '0x5190F06EaceFA2C552dc6BD5e763b81C73293293', // WOMBEX
tokenOut: Tokens.USDT,
amountIn: GreaterThanOrEqual(parseUnits('750', 18)),
Expand All @@ -77,7 +77,7 @@ export const V3_SWAP_ETH: TestParams<SwapActionParams> = {
value: '1000000000000000',
},
params: {
chainId: ARBITRUM_CHAIN_ID,
chainId: Chains.ARBITRUM_ONE,
tokenIn: Tokens.ETH,
tokenOut: Tokens.USDCE,
amountIn: GreaterThanOrEqual(parseEther('0.001')),
Expand All @@ -97,7 +97,7 @@ export const V3_TOKEN_TO_ETH: TestParams<SwapActionParams> = {
value: '0',
},
params: {
chainId: ARBITRUM_CHAIN_ID,
chainId: Chains.ARBITRUM_ONE,
tokenIn: Tokens.USDT,
tokenOut: Tokens.ETH,
amountIn: GreaterThanOrEqual(parseUnits('20', 6)),
Expand All @@ -117,7 +117,7 @@ export const V3_EXACT_OUTPUT_SINGLE: TestParams<SwapActionParams> = {
value: '0',
},
params: {
chainId: ARBITRUM_CHAIN_ID,
chainId: Chains.ARBITRUM_ONE,
tokenIn: Tokens.ARB,
tokenOut: Tokens.ETH,
amountIn: GreaterThanOrEqual(parseUnits('2758', 18)),
Expand All @@ -137,7 +137,7 @@ export const V3_EXACT_OUTPUT: TestParams<SwapActionParams> = {
value: '0',
},
params: {
chainId: ARBITRUM_CHAIN_ID,
chainId: Chains.ARBITRUM_ONE,
tokenIn: Tokens.USDCE,
tokenOut: '0xf97f4df75117a78c1a5a0dbb814af92458539fb4',
amountIn: GreaterThanOrEqual(parseUnits('4317', 6)),
Expand Down Expand Up @@ -196,7 +196,7 @@ export const PARASWAP_SIMPLESWAP: TestParams<SwapActionParams> = {
value: '21000000000000000',
},
params: {
chainId: ARBITRUM_CHAIN_ID,
chainId: Chains.ARBITRUM_ONE,
tokenIn: Tokens.ETH,
tokenOut: Tokens.USDCE,
amountIn: GreaterThanOrEqual(parseEther('0.021')),
Expand Down
49 changes: 2 additions & 47 deletions packages/camelot/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { ActionParams, FilterOperator } from '@rabbitholegg/questdk'
import { type Address, type Hash, getAddress } from 'viem'
import type { FilterOperator } from '@rabbitholegg/questdk'
import { getAddress } from 'viem'

export enum Tokens {
ARB = '0x912CE59144191C1204E64559FE8253a0e49E6548',
Expand All @@ -16,51 +16,6 @@ export enum Tokens {
SIZE = '0x939727d85D99d0aC339bF1B76DfE30Ca27C19067',
}

interface Transaction {
chainId: number
from: Address
hash?: Hash
input: string
to: Address
value: string
}

export interface TestCase<T extends ActionParams> {
transaction: Transaction
params: T
description: string
}

export type TestParams<T extends ActionParams> = {
transaction: Transaction
params: T
}

/**
* Creates a test case object for a given action and transaction.
*
* This function takes a `TestParams` object that includes both a `Transaction` and
* `ActionParams`, a description of the test case, and an optional set of overrides
* for the action parameters. It returns a `TestCase` object that contains the transaction,
* the combined action parameters with any overrides applied, and the description.
*
* @param {TestParams<T>} testParams - An object containing the transaction and action parameters.
* @param {string} description - A brief description of the test case.
* @param {Partial<T>} [overrides] - Optional overrides for the action parameters.
* @returns {TestCase<T>} A test case object with the transaction, params, and description.
*/
export function createTestCase<T extends ActionParams>(
testParams: TestParams<T>,
description: string,
overrides: Partial<T> = {},
): TestCase<T> {
return {
transaction: testParams.transaction,
params: { ...testParams.params, ...overrides },
description,
}
}

export const buildV2PathQuery = (tokenIn?: string, tokenOut?: string) => {
// v2 paths are formatted as [<token>, <token>]
const conditions: FilterOperator[] = []
Expand Down

0 comments on commit 4185050

Please sign in to comment.