From 44f7969380dda25e925887bd6599573de1be0436 Mon Sep 17 00:00:00 2001 From: Jeff Huang Date: Thu, 16 Nov 2023 11:31:10 +0800 Subject: [PATCH] test: add OpenOcean v2 test --- .changeset/heavy-ducks-speak.md | 5 ++ .../api/examples/openocean-v2/swap-token.ts | 76 +++++++++++++++++++ .../protocols/openocean-v2/swap-token.test.ts | 42 ++++++++++ 3 files changed, 123 insertions(+) create mode 100644 .changeset/heavy-ducks-speak.md create mode 100644 packages/api/examples/openocean-v2/swap-token.ts create mode 100644 packages/api/src/protocols/openocean-v2/swap-token.test.ts diff --git a/.changeset/heavy-ducks-speak.md b/.changeset/heavy-ducks-speak.md new file mode 100644 index 00000000..61512722 --- /dev/null +++ b/.changeset/heavy-ducks-speak.md @@ -0,0 +1,5 @@ +--- +'@protocolink/api': patch +--- + +add OpenOcean v2 test diff --git a/packages/api/examples/openocean-v2/swap-token.ts b/packages/api/examples/openocean-v2/swap-token.ts new file mode 100644 index 00000000..677ba0f8 --- /dev/null +++ b/packages/api/examples/openocean-v2/swap-token.ts @@ -0,0 +1,76 @@ +import * as api from '@protocolink/api'; + +// interface SwapTokenParams { +// input: { +// token: { +// chainId: number; +// address: string; +// decimals: number; +// symbol: string; +// name: string; +// }; +// amount: string; +// }; +// tokenOut: { +// chainId: number; +// address: string; +// decimals: number; +// symbol: string; +// name: string; +// }; +// slippage?: number; +// disabledDexIds?: string; +// } + +// interface SwapTokenFields { +// input: { +// token: { +// chainId: number; +// address: string; +// decimals: number; +// symbol: string; +// name: string; +// }; +// amount: string; +// }; +// output: { +// token: { +// chainId: number; +// address: string; +// decimals: number; +// symbol: string; +// name: string; +// }; +// amount: string; +// }; +// slippage?: number; +// disabledDexIds?: string; +// } + +// interface SwapTokenLogic { +// rid: string; +// fields: SwapTokenFields; +// } + +(async () => { + const chainId = 1088; + + const tokenList = await api.protocols.openoceanv2.getSwapTokenTokenList(chainId); + const tokenIn = tokenList[0]; + const tokenOut = tokenList[2]; + console.log('tokenIn :>> ', JSON.stringify(tokenIn, null, 2)); + console.log('tokenOut :>> ', JSON.stringify(tokenOut, null, 2)); + + const swapTokenQuotation = await api.protocols.openoceanv2.getSwapTokenQuotation(chainId, { + input: { + token: tokenIn, + amount: '10', + }, + tokenOut, + slippage: 100, + }); + console.log('swapTokenQuotation :>> ', JSON.stringify(swapTokenQuotation, null, 2)); + + const swapTokenLogic = await api.protocols.openoceanv2.newSwapTokenLogic(swapTokenQuotation); + console.log('swapTokenLogic :>> ', JSON.stringify(swapTokenLogic, null, 2)); +})(); diff --git a/packages/api/src/protocols/openocean-v2/swap-token.test.ts b/packages/api/src/protocols/openocean-v2/swap-token.test.ts new file mode 100644 index 00000000..219d76e6 --- /dev/null +++ b/packages/api/src/protocols/openocean-v2/swap-token.test.ts @@ -0,0 +1,42 @@ +import { SwapTokenParams, getSwapTokenQuotation, getSwapTokenTokenList } from './swap-token'; +import * as common from '@protocolink/common'; +import { expect } from 'chai'; +import * as logics from '@protocolink/logics'; +import { metisTokens } from '@protocolink/test-helpers'; + +describe('OpenOceanV2 SwapTokenLogic', function () { + context('Test getTokenList', async function () { + logics.openoceanv2.SwapTokenLogic.supportedChainIds.forEach((chainId) => { + it(`network: ${common.toNetworkId(chainId)}`, async function () { + const tokenList = await getSwapTokenTokenList(chainId); + expect(tokenList).to.have.lengthOf.above(0); + }); + }); + }); + + context('Test getQuotation', async function () { + const chainId = common.ChainId.metis; + + const testCases: SwapTokenParams[] = [ + { + input: { token: metisTokens.METIS, amount: '1' }, + tokenOut: metisTokens.USDC, + }, + { + input: { token: metisTokens.USDC, amount: '1' }, + tokenOut: metisTokens.METIS, + }, + { + input: { token: metisTokens.USDC, amount: '1' }, + tokenOut: metisTokens.DAI, + }, + ]; + + testCases.forEach((params, i) => { + it(`case ${i + 1}`, async function () { + const quotation = await getSwapTokenQuotation(chainId, params); + expect(quotation).to.include.all.keys('input', 'output'); + }); + }); + }); +});