Skip to content

Commit

Permalink
test: add Wagmi test and example
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeff-CCH committed Sep 18, 2024
1 parent a9cd881 commit 3accb58
Show file tree
Hide file tree
Showing 3 changed files with 159 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/odd-timers-swim.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@protocolink/api': patch
---

add Wagmi test and example
100 changes: 100 additions & 0 deletions packages/api/examples/wagmi/swap-token.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import * as api from '@protocolink/api';

// type 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;
// }
// | {
// tokenIn: {
// chainId: number;
// address: string;
// decimals: number;
// symbol: string;
// name: string;
// };
// output: {
// token: {
// chainId: number;
// address: string;
// decimals: number;
// symbol: string;
// name: string;
// };
// amount: string;
// };
// slippage?: number;
// };

// import * as core from '@protocolink/core';

// interface SwapTokenFields {
// tradeType: core.TradeType;
// 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;
// };
// fee?: number;
// path?: string;
// slippage?: number;
// }

// interface SwapTokenLogic {
// rid: string;
// fields: SwapTokenFields;
// }

(async () => {
const chainId = 8822;

const tokenList = await api.protocols.wagmi.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.wagmi.getSwapTokenQuotation(chainId, {
input: {
token: tokenIn,
amount: '10',
},
tokenOut,
slippage: 100,
});
console.log('swapTokenQuotation :>> ', JSON.stringify(swapTokenQuotation, null, 2));

const swapTokenLogic = await api.protocols.wagmi.newSwapTokenLogic(swapTokenQuotation);
console.log('swapTokenLogic :>> ', JSON.stringify(swapTokenLogic, null, 2));
})();
54 changes: 54 additions & 0 deletions packages/api/src/protocols/wagmi/swap-token.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { SwapTokenParams, getSwapTokenQuotation, getSwapTokenTokenList } from './swap-token';
import * as common from '@protocolink/common';
import { expect } from 'chai';
import * as logics from '@protocolink/logics';

describe('Wagmi SwapTokenLogic', function () {
context('Test getTokenList', async function () {
logics.wagmi.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.iota;

const testCases: SwapTokenParams[] = [
{
input: { token: common.iotaTokens.IOTA, amount: '1' },
tokenOut: common.iotaTokens.USDT,
},
{
input: { token: common.iotaTokens.USDT, amount: '1' },
tokenOut: common.iotaTokens.IOTA,
},
{
input: { token: common.iotaTokens.USDT, amount: '1' },
tokenOut: common.iotaTokens.wIOTA,
},
{
tokenIn: common.iotaTokens.IOTA,
output: { token: common.iotaTokens.USDT, amount: '1' },
},
{
tokenIn: common.iotaTokens.USDT,
output: { token: common.iotaTokens.IOTA, amount: '1' },
},
{
tokenIn: common.iotaTokens.USDT,
output: { token: common.iotaTokens.wIOTA, amount: '1' },
},
];

testCases.forEach((params, i) => {
it(`case ${i + 1}`, async function () {
const quotation = await getSwapTokenQuotation(chainId, params);
expect(quotation).to.include.all.keys('tradeType', 'input', 'output');
expect(quotation).to.have.any.keys('path', 'fee');
});
});
});
});

0 comments on commit 3accb58

Please sign in to comment.