-
Notifications
You must be signed in to change notification settings - Fork 15
/
logic.swap-token.test.ts
181 lines (176 loc) · 7.73 KB
/
logic.swap-token.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import { LogicTestCase } from 'test/types';
import { SwapTokenLogic, SwapTokenLogicFields, SwapTokenLogicOptions } from './logic.swap-token';
import * as common from '@protocolink/common';
import { constants, utils } from 'ethers';
import * as core from '@protocolink/core';
import { expect } from 'chai';
import { getConfig } from './configs';
import * as univ3 from 'src/modules/univ3';
describe('UniswapV3 SwapTokenLogic', function () {
context('Test getTokenList', async function () {
SwapTokenLogic.supportedChainIds.forEach((chainId) => {
it(`network: ${common.toNetworkId(chainId)}`, async function () {
const logic = new SwapTokenLogic(chainId);
const tokenList = await logic.getTokenList();
expect(tokenList).to.have.lengthOf.above(0);
});
});
});
context('Test build', function () {
const chainId = common.ChainId.mainnet;
const config = getConfig(chainId);
const logic = new SwapTokenLogic(chainId);
const iface = univ3.SwapRouter02__factory.createInterface();
const testCases: LogicTestCase<SwapTokenLogicFields, SwapTokenLogicOptions>[] = [
{
fields: {
tradeType: core.TradeType.exactIn,
input: new common.TokenAmount(common.mainnetTokens.ETH, '1'),
output: new common.TokenAmount(common.mainnetTokens.USDC, '1661.098116'),
fee: 500,
slippage: 100,
},
options: { account: '0xaAaAaAaaAaAaAaaAaAAAAAAAAaaaAaAaAaaAaaAa' },
},
{
fields: {
tradeType: core.TradeType.exactIn,
input: new common.TokenAmount(common.mainnetTokens.ETH, '1'),
output: new common.TokenAmount(common.mainnetTokens.USDC, '1661.098116'),
path: '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc20001f42260fac5e5542a773aa44fbcfedf7c193bc2c5990001f4a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48',
slippage: 100,
},
options: { account: '0xaAaAaAaaAaAaAaaAaAAAAAAAAaaaAaAaAaaAaaAa' },
},
{
fields: {
tradeType: core.TradeType.exactIn,
input: new common.TokenAmount(common.mainnetTokens.USDC, '1000'),
output: new common.TokenAmount(common.mainnetTokens.ETH, '0.608027615305460657'),
fee: 500,
slippage: 100,
},
options: { account: '0xaAaAaAaaAaAaAaaAaAAAAAAAAaaaAaAaAaaAaaAa' },
},
{
fields: {
tradeType: core.TradeType.exactIn,
input: new common.TokenAmount(common.mainnetTokens.USDC, '1000'),
output: new common.TokenAmount(common.mainnetTokens.ETH, '0.608027615305460657'),
path: '0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb480001f42260fac5e5542a773aa44fbcfedf7c193bc2c5990001f4c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2',
slippage: 100,
},
options: { account: '0xaAaAaAaaAaAaAaaAaAAAAAAAAaaaAaAaAaaAaaAa' },
},
{
fields: {
tradeType: core.TradeType.exactIn,
input: new common.TokenAmount(common.mainnetTokens.USDC, '1000'),
output: new common.TokenAmount(common.mainnetTokens.DAI, '1000'),
fee: 500,
slippage: 100,
},
options: { account: '0xaAaAaAaaAaAaAaaAaAAAAAAAAaaaAaAaAaaAaaAa' },
},
{
fields: {
tradeType: core.TradeType.exactIn,
input: new common.TokenAmount(common.mainnetTokens.USDC, '1000'),
output: new common.TokenAmount(common.mainnetTokens.DAI, '1000'),
path: '0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb480001f42260fac5e5542a773aa44fbcfedf7c193bc2c5990001f46b175474e89094c44da98b954eedeac495271d0f',
slippage: 100,
},
options: { account: '0xaAaAaAaaAaAaAaaAaAAAAAAAAaaaAaAaAaaAaaAa' },
},
{
fields: {
tradeType: core.TradeType.exactOut,
input: new common.TokenAmount(common.mainnetTokens.ETH, '0.608027615305460657'),
output: new common.TokenAmount(common.mainnetTokens.USDC, '1000'),
fee: 500,
},
options: { account: '0xaAaAaAaaAaAaAaaAaAAAAAAAAaaaAaAaAaaAaaAa' },
},
{
fields: {
tradeType: core.TradeType.exactOut,
input: new common.TokenAmount(common.mainnetTokens.ETH, '0.608027615305460657'),
output: new common.TokenAmount(common.mainnetTokens.USDC, '1000'),
path: '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc20001f42260fac5e5542a773aa44fbcfedf7c193bc2c5990001f4a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48',
},
options: { account: '0xaAaAaAaaAaAaAaaAaAAAAAAAAaaaAaAaAaaAaaAa' },
},
{
fields: {
tradeType: core.TradeType.exactOut,
input: new common.TokenAmount(common.mainnetTokens.USDC, '1000'),
output: new common.TokenAmount(common.mainnetTokens.ETH, '0.608027615305460657'),
fee: 500,
},
options: { account: '0xaAaAaAaaAaAaAaaAaAAAAAAAAaaaAaAaAaaAaaAa' },
},
{
fields: {
tradeType: core.TradeType.exactOut,
input: new common.TokenAmount(common.mainnetTokens.USDC, '1000'),
output: new common.TokenAmount(common.mainnetTokens.ETH, '0.608027615305460657'),
path: '0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb480001f42260fac5e5542a773aa44fbcfedf7c193bc2c5990001f4c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2',
},
options: { account: '0xaAaAaAaaAaAaAaaAaAAAAAAAAaaaAaAaAaaAaaAa' },
},
{
fields: {
tradeType: core.TradeType.exactOut,
input: new common.TokenAmount(common.mainnetTokens.USDC, '1000'),
output: new common.TokenAmount(common.mainnetTokens.DAI, '1000'),
fee: 500,
},
options: { account: '0xaAaAaAaaAaAaAaaAaAAAAAAAAaaaAaAaAaaAaaAa' },
},
{
fields: {
tradeType: core.TradeType.exactOut,
input: new common.TokenAmount(common.mainnetTokens.USDC, '1000'),
output: new common.TokenAmount(common.mainnetTokens.DAI, '1000'),
path: '0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb480001f42260fac5e5542a773aa44fbcfedf7c193bc2c5990001f4c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2',
},
options: { account: '0xaAaAaAaaAaAaAaaAaAAAAAAAAaaaAaAaAaaAaaAa' },
},
];
testCases.forEach(({ fields, options }) => {
it(`${fields.input.token.symbol} to ${fields.output.token.symbol} ${fields.tradeType} ${
univ3.isSwapTokenLogicSingleHopFields(fields) ? 'Single' : ''
}`, async function () {
const routerLogic = await logic.build(fields, options);
const sig = routerLogic.data.substring(0, 10);
const { tradeType, input, output, balanceBps } = fields;
expect(routerLogic.to).to.eq(config.swapRouterAddress);
expect(utils.isBytesLike(routerLogic.data)).to.be.true;
expect(sig).to.eq(
iface.getSighash(
`exact${tradeType === core.TradeType.exactIn ? 'Input' : 'Output'}${
univ3.isSwapTokenLogicSingleHopFields(fields) ? 'Single' : ''
}`
)
);
expect(routerLogic.inputs[0].token).to.eq(input.token.wrapped.address);
if (balanceBps) {
expect(routerLogic.inputs[0].balanceBps).to.eq(balanceBps);
expect(routerLogic.inputs[0].amountOrOffset).to.eq(common.getParamOffset(1));
} else {
expect(routerLogic.inputs[0].balanceBps).to.eq(core.BPS_NOT_USED);
expect(routerLogic.inputs[0].amountOrOffset).eq(input.amountWei);
}
expect(routerLogic.wrapMode).to.eq(
input.token.isNative
? core.WrapMode.wrapBefore
: output.token.isNative
? core.WrapMode.unwrapAfter
: core.WrapMode.none
);
expect(routerLogic.approveTo).to.eq(constants.AddressZero);
expect(routerLogic.callback).to.eq(constants.AddressZero);
});
});
});
});