Skip to content

Commit

Permalink
Merge pull request #10 from Jeff-CCH/feat/integrate-open-ocean
Browse files Browse the repository at this point in the history
Add OpenOcean v2 test
  • Loading branch information
Jeff-CCH authored Nov 16, 2023
2 parents 452d7ed + 44f7969 commit 4605ce2
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/heavy-ducks-speak.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@protocolink/api': patch
---

add OpenOcean v2 test
76 changes: 76 additions & 0 deletions packages/api/examples/openocean-v2/swap-token.ts
Original file line number Diff line number Diff line change
@@ -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));
})();
42 changes: 42 additions & 0 deletions packages/api/src/protocols/openocean-v2/swap-token.test.ts
Original file line number Diff line number Diff line change
@@ -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');
});
});
});
});

0 comments on commit 4605ce2

Please sign in to comment.