@dhedge/v2-sdk 1.9.4 → 1.9.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/entities/pool.d.ts +76 -39
- package/dist/test/constants.d.ts +13 -0
- package/dist/test/utils/testingHelper.d.ts +14 -0
- package/dist/test/wallet.d.ts +1 -0
- package/dist/types.d.ts +2 -1
- package/dist/utils/contract.d.ts +2 -1
- package/dist/v2-sdk.cjs.development.js +976 -447
- package/dist/v2-sdk.cjs.development.js.map +1 -1
- package/dist/v2-sdk.cjs.production.min.js +1 -1
- package/dist/v2-sdk.cjs.production.min.js.map +1 -1
- package/dist/v2-sdk.esm.js +976 -447
- package/dist/v2-sdk.esm.js.map +1 -1
- package/package.json +3 -2
- package/src/abi/PoolLogic.json +349 -63
- package/src/config.ts +23 -7
- package/src/entities/pool.ts +334 -222
- package/src/test/constants.ts +15 -1
- package/src/test/lyra.test.ts +7 -6
- package/src/test/oneInch.test.ts +66 -3
- package/src/test/synthetix.test.ts +3 -1
- package/src/test/uniswap.test.ts +180 -148
- package/src/test/utils/testingHelper.ts +55 -1
- package/src/test/wallet.ts +2 -1
- package/src/test/zeroEx.test.ts +40 -4
- package/src/types.ts +3 -2
- package/src/utils/contract.ts +17 -1
package/src/test/constants.ts
CHANGED
|
@@ -41,7 +41,8 @@ export const KWENTA_ETH_PERP_V2 = "0x2b3bb4c683bfc5239b029131eef3b1d214478d93";
|
|
|
41
41
|
export const TEST_POOL = {
|
|
42
42
|
[Network.POLYGON]: "0x699fd4d6eadb216704c7e355cfa0a12f51813163",
|
|
43
43
|
[Network.OPTIMISM]: "0x12573bfdf764ab9d52aca20e2827497a66829716",
|
|
44
|
-
[Network.ARBITRUM]: "0x2dc2f936c8b6619facc69355d65dd93d2f4cc2bd"
|
|
44
|
+
[Network.ARBITRUM]: "0x2dc2f936c8b6619facc69355d65dd93d2f4cc2bd",
|
|
45
|
+
[Network.BASE]: "0x4842b42F68524383F609aa46eAfc18c1459cE3cD"
|
|
45
46
|
};
|
|
46
47
|
|
|
47
48
|
export const CONTRACT_ADDRESS = {
|
|
@@ -69,7 +70,20 @@ export const CONTRACT_ADDRESS = {
|
|
|
69
70
|
WSTETH: "0x5979d7b546e38e414f7e9822514be443a4800529",
|
|
70
71
|
BALANCER_WSTETH_WETH_POOL: "0x36bf227d6bac96e2ab1ebb5492ecec69c691943f",
|
|
71
72
|
BALANCER_WSTETH_WETH_GAUGE: "0x251e51b25afa40f2b6b9f05aaf1bc7eaa0551771"
|
|
73
|
+
},
|
|
74
|
+
[Network.BASE]: {
|
|
75
|
+
USDC: "0x833589fcd6edb6e08f4c7c32d4f71b54bda02913",
|
|
76
|
+
WETH: "0x4200000000000000000000000000000000000006",
|
|
77
|
+
WBTC: "",
|
|
78
|
+
SWETH: ""
|
|
72
79
|
}
|
|
73
80
|
};
|
|
74
81
|
|
|
75
82
|
export const MAX_AMOUNT = ethers.constants.MaxUint256;
|
|
83
|
+
|
|
84
|
+
export const USDC_BALANCEOF_SLOT = {
|
|
85
|
+
[Network.OPTIMISM]: 0,
|
|
86
|
+
[Network.ARBITRUM]: 9,
|
|
87
|
+
[Network.POLYGON]: 0,
|
|
88
|
+
[Network.BASE]: 9
|
|
89
|
+
};
|
package/src/test/lyra.test.ts
CHANGED
|
@@ -6,6 +6,7 @@ import { getTxOptions } from "./txOptions";
|
|
|
6
6
|
import { wallet } from "./wallet";
|
|
7
7
|
|
|
8
8
|
jest.setTimeout(100000);
|
|
9
|
+
const TEST_POOL_OP = TEST_POOL.optimism;
|
|
9
10
|
|
|
10
11
|
describe("pool", () => {
|
|
11
12
|
let dhedge: Dhedge;
|
|
@@ -16,7 +17,7 @@ describe("pool", () => {
|
|
|
16
17
|
|
|
17
18
|
it("buys 0.1 1400 calls with expiry October 28th", async () => {
|
|
18
19
|
let result;
|
|
19
|
-
const pool = await dhedge.loadPool(
|
|
20
|
+
const pool = await dhedge.loadPool(TEST_POOL_OP);
|
|
20
21
|
try {
|
|
21
22
|
result = await pool.tradeLyraOption(
|
|
22
23
|
"eth",
|
|
@@ -37,7 +38,7 @@ describe("pool", () => {
|
|
|
37
38
|
|
|
38
39
|
it("adds 0.05 1400 calls with expiry October 28th", async () => {
|
|
39
40
|
let result;
|
|
40
|
-
const pool = await dhedge.loadPool(
|
|
41
|
+
const pool = await dhedge.loadPool(TEST_POOL_OP);
|
|
41
42
|
try {
|
|
42
43
|
result = await pool.tradeLyraOption(
|
|
43
44
|
"eth",
|
|
@@ -61,7 +62,7 @@ describe("pool", () => {
|
|
|
61
62
|
|
|
62
63
|
it("sells 0.1 1300 Covered Call with expiry October 28th", async () => {
|
|
63
64
|
let result;
|
|
64
|
-
const pool = await dhedge.loadPool(
|
|
65
|
+
const pool = await dhedge.loadPool(TEST_POOL_OP);
|
|
65
66
|
try {
|
|
66
67
|
result = await pool.tradeLyraOption(
|
|
67
68
|
"eth",
|
|
@@ -86,7 +87,7 @@ describe("pool", () => {
|
|
|
86
87
|
|
|
87
88
|
it("adds 0.05 1300 Covered Call with expiry October 28th", async () => {
|
|
88
89
|
let result;
|
|
89
|
-
const pool = await dhedge.loadPool(
|
|
90
|
+
const pool = await dhedge.loadPool(TEST_POOL_OP);
|
|
90
91
|
try {
|
|
91
92
|
result = await pool.tradeLyraOption(
|
|
92
93
|
"eth",
|
|
@@ -113,7 +114,7 @@ describe("pool", () => {
|
|
|
113
114
|
|
|
114
115
|
it("closes all 0.15 1300 Covered Call with expiry October 28th", async () => {
|
|
115
116
|
let result;
|
|
116
|
-
const pool = await dhedge.loadPool(
|
|
117
|
+
const pool = await dhedge.loadPool(TEST_POOL_OP);
|
|
117
118
|
try {
|
|
118
119
|
result = await pool.tradeLyraOption(
|
|
119
120
|
"eth",
|
|
@@ -138,7 +139,7 @@ describe("pool", () => {
|
|
|
138
139
|
|
|
139
140
|
it("closes all 0.15 1400 Calls with expiry October 28th", async () => {
|
|
140
141
|
let result;
|
|
141
|
-
const pool = await dhedge.loadPool(
|
|
142
|
+
const pool = await dhedge.loadPool(TEST_POOL_OP);
|
|
142
143
|
try {
|
|
143
144
|
result = await pool.tradeLyraOption(
|
|
144
145
|
"eth",
|
package/src/test/oneInch.test.ts
CHANGED
|
@@ -1,12 +1,19 @@
|
|
|
1
1
|
/* eslint-disable @typescript-eslint/no-non-null-assertion */
|
|
2
|
+
import BigNumber from "bignumber.js";
|
|
2
3
|
import { Dhedge, Pool } from "..";
|
|
3
4
|
import { routerAddress } from "../config";
|
|
4
5
|
import { Dapp, Network } from "../types";
|
|
5
6
|
import { CONTRACT_ADDRESS, MAX_AMOUNT, TEST_POOL } from "./constants";
|
|
6
|
-
import {
|
|
7
|
+
import {
|
|
8
|
+
TestingRunParams,
|
|
9
|
+
setUSDCAmount,
|
|
10
|
+
testingHelper,
|
|
11
|
+
wait
|
|
12
|
+
} from "./utils/testingHelper";
|
|
7
13
|
import { allowanceDelta, balanceDelta } from "./utils/token";
|
|
14
|
+
import { getTxOptions } from "./txOptions";
|
|
8
15
|
|
|
9
|
-
const testOneInch = ({ wallet, network }: TestingRunParams) => {
|
|
16
|
+
const testOneInch = ({ wallet, network, provider }: TestingRunParams) => {
|
|
10
17
|
const USDC = CONTRACT_ADDRESS[network].USDC;
|
|
11
18
|
const WETH = CONTRACT_ADDRESS[network].WETH;
|
|
12
19
|
|
|
@@ -18,6 +25,19 @@ const testOneInch = ({ wallet, network }: TestingRunParams) => {
|
|
|
18
25
|
beforeAll(async () => {
|
|
19
26
|
dhedge = new Dhedge(wallet, network);
|
|
20
27
|
pool = await dhedge.loadPool(TEST_POOL[network]);
|
|
28
|
+
// top up gas
|
|
29
|
+
await provider.send("hardhat_setBalance", [
|
|
30
|
+
wallet.address,
|
|
31
|
+
"0x10000000000000000"
|
|
32
|
+
]);
|
|
33
|
+
await provider.send("evm_mine", []);
|
|
34
|
+
// top up USDC
|
|
35
|
+
await setUSDCAmount({
|
|
36
|
+
amount: new BigNumber(2).times(1e6).toFixed(0),
|
|
37
|
+
userAddress: pool.address,
|
|
38
|
+
network,
|
|
39
|
+
provider
|
|
40
|
+
});
|
|
21
41
|
});
|
|
22
42
|
|
|
23
43
|
it("approves unlimited USDC on 1Inch", async () => {
|
|
@@ -31,8 +51,46 @@ const testOneInch = ({ wallet, network }: TestingRunParams) => {
|
|
|
31
51
|
await expect(usdcAllowanceDelta.gt(0));
|
|
32
52
|
});
|
|
33
53
|
|
|
54
|
+
it("gets gas estimation for 2 USDC into WETH on 1Inch", async () => {
|
|
55
|
+
const gasEstimate = await pool.trade(
|
|
56
|
+
Dapp.ONEINCH,
|
|
57
|
+
USDC,
|
|
58
|
+
WETH,
|
|
59
|
+
"2000000",
|
|
60
|
+
0.5,
|
|
61
|
+
await getTxOptions(network),
|
|
62
|
+
true
|
|
63
|
+
);
|
|
64
|
+
expect(gasEstimate.gt(0));
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it("gets error on gas estimation for 200 USDC into WETH on 1Inch", async () => {
|
|
68
|
+
await wait(1);
|
|
69
|
+
let gasEstimate = null;
|
|
70
|
+
try {
|
|
71
|
+
gasEstimate = await pool.trade(
|
|
72
|
+
Dapp.ONEINCH,
|
|
73
|
+
USDC,
|
|
74
|
+
WETH,
|
|
75
|
+
"200000000",
|
|
76
|
+
0.5,
|
|
77
|
+
await getTxOptions(network),
|
|
78
|
+
true
|
|
79
|
+
);
|
|
80
|
+
} catch (err) {}
|
|
81
|
+
expect(gasEstimate).toBeNull();
|
|
82
|
+
});
|
|
83
|
+
|
|
34
84
|
it("trades 2 USDC into WETH on 1Inch", async () => {
|
|
35
|
-
await
|
|
85
|
+
await wait(1);
|
|
86
|
+
await pool.trade(
|
|
87
|
+
Dapp.ONEINCH,
|
|
88
|
+
USDC,
|
|
89
|
+
WETH,
|
|
90
|
+
"2000000",
|
|
91
|
+
0.5,
|
|
92
|
+
await getTxOptions(network)
|
|
93
|
+
);
|
|
36
94
|
const wethBalanceDelta = await balanceDelta(
|
|
37
95
|
pool.address,
|
|
38
96
|
WETH,
|
|
@@ -52,3 +110,8 @@ testingHelper({
|
|
|
52
110
|
network: Network.POLYGON,
|
|
53
111
|
testingRun: testOneInch
|
|
54
112
|
});
|
|
113
|
+
|
|
114
|
+
testingHelper({
|
|
115
|
+
network: Network.BASE,
|
|
116
|
+
testingRun: testOneInch
|
|
117
|
+
});
|
|
@@ -6,6 +6,8 @@ import { TEST_POOL } from "./constants";
|
|
|
6
6
|
|
|
7
7
|
import { wallet } from "./wallet";
|
|
8
8
|
|
|
9
|
+
const TEST_POOL_OP = TEST_POOL.optimism;
|
|
10
|
+
|
|
9
11
|
let dhedge: Dhedge;
|
|
10
12
|
let options: any;
|
|
11
13
|
jest.setTimeout(100000);
|
|
@@ -18,7 +20,7 @@ describe("pool", () => {
|
|
|
18
20
|
});
|
|
19
21
|
|
|
20
22
|
it("should swap sETH into sUSD on Synthetix", async () => {
|
|
21
|
-
const pool = await dhedge.loadPool(
|
|
23
|
+
const pool = await dhedge.loadPool(TEST_POOL_OP);
|
|
22
24
|
const result = await pool.trade(
|
|
23
25
|
Dapp.SYNTHETIX,
|
|
24
26
|
"sETH",
|
package/src/test/uniswap.test.ts
CHANGED
|
@@ -5,156 +5,188 @@ import { routerAddress } from "../config";
|
|
|
5
5
|
import { Dapp, Network } from "../types";
|
|
6
6
|
import { CONTRACT_ADDRESS, TEST_POOL } from "./constants";
|
|
7
7
|
import { allowanceDelta, balanceDelta } from "./utils/token";
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
const
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
8
|
+
import {
|
|
9
|
+
setUSDCAmount,
|
|
10
|
+
testingHelper,
|
|
11
|
+
TestingRunParams
|
|
12
|
+
} from "./utils/testingHelper";
|
|
13
|
+
import BigNumber from "bignumber.js";
|
|
14
|
+
|
|
15
|
+
const testUniswapV3 = ({ wallet, network, provider }: TestingRunParams) => {
|
|
16
|
+
let dhedge: Dhedge;
|
|
17
|
+
let pool: Pool;
|
|
18
|
+
|
|
19
|
+
jest.setTimeout(100000);
|
|
20
|
+
describe(`testUniswapV3 on ${network}`, () => {
|
|
21
|
+
beforeAll(async () => {
|
|
22
|
+
dhedge = new Dhedge(wallet, network);
|
|
23
|
+
pool = await dhedge.loadPool(TEST_POOL[network]);
|
|
24
|
+
|
|
25
|
+
// top up gas
|
|
26
|
+
await provider.send("hardhat_setBalance", [
|
|
27
|
+
wallet.address,
|
|
28
|
+
"0x10000000000000000"
|
|
29
|
+
]);
|
|
30
|
+
await provider.send("evm_mine", []);
|
|
31
|
+
await setUSDCAmount({
|
|
32
|
+
amount: new BigNumber(100).times(1e18).toFixed(0),
|
|
33
|
+
userAddress: pool.address,
|
|
34
|
+
network,
|
|
35
|
+
provider
|
|
36
|
+
});
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it("approves unlimited USDC on for trading on UniswapV3", async () => {
|
|
40
|
+
await pool.approve(
|
|
41
|
+
Dapp.UNISWAPV3,
|
|
42
|
+
CONTRACT_ADDRESS[network].USDC,
|
|
43
|
+
ethers.constants.MaxUint256
|
|
44
|
+
);
|
|
45
|
+
const UsdcAllowanceDelta = await allowanceDelta(
|
|
46
|
+
pool.address,
|
|
47
|
+
CONTRACT_ADDRESS[network].USDC,
|
|
48
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
49
|
+
routerAddress[network].uniswapV3!,
|
|
50
|
+
pool.signer
|
|
51
|
+
);
|
|
52
|
+
expect(UsdcAllowanceDelta.gte(0));
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it("should swap 5 USDC into WETH on UniswapV3", async () => {
|
|
56
|
+
await pool.tradeUniswapV3(
|
|
57
|
+
CONTRACT_ADDRESS[network].USDC,
|
|
58
|
+
CONTRACT_ADDRESS[network].WETH,
|
|
59
|
+
"5000000",
|
|
60
|
+
FeeAmount.LOW,
|
|
61
|
+
0.5
|
|
62
|
+
);
|
|
63
|
+
|
|
64
|
+
const wethAllowanceDelta = await balanceDelta(
|
|
65
|
+
pool.address,
|
|
66
|
+
CONTRACT_ADDRESS[network].WETH,
|
|
67
|
+
pool.signer
|
|
68
|
+
);
|
|
69
|
+
expect(wethAllowanceDelta.gt(0));
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
// it("approves unlimited WETH on for UniswapV3 LP", async () => {
|
|
73
|
+
// await pool.approveUniswapV3Liquidity(
|
|
74
|
+
// CONTRACT_ADDRESS[network].USDC,
|
|
75
|
+
// ethers.constants.MaxInt256
|
|
76
|
+
// );
|
|
77
|
+
// const UsdcAllowanceDelta = await allowanceDelta(
|
|
78
|
+
// pool.address,
|
|
79
|
+
// CONTRACT_ADDRESS[network].USDC,
|
|
80
|
+
// pool.address,
|
|
81
|
+
// pool.signer
|
|
82
|
+
// );
|
|
83
|
+
|
|
84
|
+
// expect(result).not.toBe(null);
|
|
85
|
+
// });
|
|
86
|
+
|
|
87
|
+
// it("adds WETH and WBTC to a new V3 pool", async () => {
|
|
88
|
+
// let result;
|
|
89
|
+
// const pool = await dhedge.loadPool(TEST_POOL);
|
|
90
|
+
// const usdcBalance = await dhedge.utils.getBalance(USDC, pool.address);
|
|
91
|
+
// const wethBalance = await dhedge.utils.getBalance(WETH, pool.address);
|
|
92
|
+
|
|
93
|
+
// try {
|
|
94
|
+
// result = await pool.addLiquidityUniswapV3(
|
|
95
|
+
// USDC,
|
|
96
|
+
// WETH,
|
|
97
|
+
// usdcBalance,
|
|
98
|
+
// wethBalance,
|
|
99
|
+
// 0.0003,
|
|
100
|
+
// 0.0004,
|
|
101
|
+
// null,
|
|
102
|
+
// null,
|
|
103
|
+
// FeeAmount.LOW,
|
|
104
|
+
// options
|
|
105
|
+
// );
|
|
106
|
+
// console.log(result);
|
|
107
|
+
// } catch (e) {
|
|
108
|
+
// console.log(e);
|
|
109
|
+
// }
|
|
110
|
+
// expect(result).not.toBe(null);
|
|
111
|
+
// });
|
|
112
|
+
|
|
113
|
+
// it("should remove liquidity from an existing pool ", async () => {
|
|
114
|
+
// const pool = await dhedge.loadPool(TEST_POOL);
|
|
115
|
+
// const result = await pool.decreaseLiquidity(
|
|
116
|
+
// Dapp.UNISWAPV3,
|
|
117
|
+
// "110507",
|
|
118
|
+
// 100,
|
|
119
|
+
// options
|
|
120
|
+
// );
|
|
121
|
+
// console.log("result", result);
|
|
122
|
+
// expect(result).not.toBe(null);
|
|
123
|
+
// });
|
|
124
|
+
|
|
125
|
+
// it("should increase liquidity in an existing pool WETH/WBTC pool", async () => {
|
|
126
|
+
// const pool = await dhedge.loadPool(TEST_POOL);
|
|
127
|
+
// const result = await pool.increaseLiquidity(
|
|
128
|
+
// Dapp.UNISWAPV3,
|
|
129
|
+
// "110507",
|
|
130
|
+
// "244838",
|
|
131
|
+
// "258300000000000",
|
|
132
|
+
// options
|
|
133
|
+
// );
|
|
134
|
+
// console.log("result", result);
|
|
135
|
+
// expect(result).not.toBe(null);
|
|
136
|
+
// });
|
|
137
|
+
|
|
138
|
+
// it("should claim fees an existing pool", async () => {
|
|
139
|
+
// const pool = await dhedge.loadPool(TEST_POOL);
|
|
140
|
+
// const result = await pool.claimFeesUniswapV3("54929", options);
|
|
141
|
+
// console.log("result", result);
|
|
142
|
+
// expect(result).not.toBe(null);
|
|
143
|
+
// });
|
|
144
|
+
|
|
145
|
+
// it("approves unlimited USDC to swap on UniswapV3", async () => {
|
|
146
|
+
// let result;
|
|
147
|
+
// const pool = await dhedge.loadPool(TEST_POOL);
|
|
148
|
+
// try {
|
|
149
|
+
// result = await pool.approve(
|
|
150
|
+
// Dapp.UNISWAPV3,
|
|
151
|
+
// USDC,
|
|
152
|
+
// ethers.constants.MaxInt256,
|
|
153
|
+
// options
|
|
154
|
+
// );
|
|
155
|
+
// console.log(result);
|
|
156
|
+
// } catch (e) {
|
|
157
|
+
// console.log(e);
|
|
158
|
+
// }
|
|
159
|
+
// expect(result).not.toBe(null);
|
|
160
|
+
// });
|
|
161
|
+
|
|
162
|
+
// it("should swap USDC into WETH on UniswapV3 pool", async () => {
|
|
163
|
+
// const pool = await dhedge.loadPool(TEST_POOL);
|
|
164
|
+
// const result = await pool.tradeUniswapV3(
|
|
165
|
+
// USDC,
|
|
166
|
+
// WETH,
|
|
167
|
+
// "1000000",
|
|
168
|
+
// FeeAmount.LOW,
|
|
169
|
+
// 1,
|
|
170
|
+
// options
|
|
171
|
+
// );
|
|
172
|
+
|
|
173
|
+
// console.log(result);
|
|
174
|
+
// expect(result).not.toBe(null);
|
|
175
|
+
// });
|
|
21
176
|
});
|
|
177
|
+
};
|
|
22
178
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
ethers.constants.MaxUint256
|
|
28
|
-
);
|
|
29
|
-
const UsdcAllowanceDelta = await allowanceDelta(
|
|
30
|
-
pool.address,
|
|
31
|
-
CONTRACT_ADDRESS[network].USDC,
|
|
32
|
-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
33
|
-
routerAddress[network].uniswapV3!,
|
|
34
|
-
pool.signer
|
|
35
|
-
);
|
|
36
|
-
expect(UsdcAllowanceDelta.gte(0));
|
|
37
|
-
});
|
|
179
|
+
testingHelper({
|
|
180
|
+
network: Network.OPTIMISM,
|
|
181
|
+
testingRun: testUniswapV3
|
|
182
|
+
});
|
|
38
183
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
"5000000",
|
|
44
|
-
FeeAmount.LOW,
|
|
45
|
-
0.5
|
|
46
|
-
);
|
|
47
|
-
|
|
48
|
-
const wethAllowanceDelta = await balanceDelta(
|
|
49
|
-
pool.address,
|
|
50
|
-
CONTRACT_ADDRESS[network].WETH,
|
|
51
|
-
pool.signer
|
|
52
|
-
);
|
|
53
|
-
expect(wethAllowanceDelta.gt(0));
|
|
54
|
-
});
|
|
184
|
+
testingHelper({
|
|
185
|
+
network: Network.POLYGON,
|
|
186
|
+
testingRun: testUniswapV3
|
|
187
|
+
});
|
|
55
188
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
// ethers.constants.MaxInt256
|
|
60
|
-
// );
|
|
61
|
-
// const UsdcAllowanceDelta = await allowanceDelta(
|
|
62
|
-
// pool.address,
|
|
63
|
-
// CONTRACT_ADDRESS[network].USDC,
|
|
64
|
-
// pool.address,
|
|
65
|
-
// pool.signer
|
|
66
|
-
// );
|
|
67
|
-
|
|
68
|
-
// expect(result).not.toBe(null);
|
|
69
|
-
// });
|
|
70
|
-
|
|
71
|
-
// it("adds WETH and WBTC to a new V3 pool", async () => {
|
|
72
|
-
// let result;
|
|
73
|
-
// const pool = await dhedge.loadPool(TEST_POOL);
|
|
74
|
-
// const usdcBalance = await dhedge.utils.getBalance(USDC, pool.address);
|
|
75
|
-
// const wethBalance = await dhedge.utils.getBalance(WETH, pool.address);
|
|
76
|
-
|
|
77
|
-
// try {
|
|
78
|
-
// result = await pool.addLiquidityUniswapV3(
|
|
79
|
-
// USDC,
|
|
80
|
-
// WETH,
|
|
81
|
-
// usdcBalance,
|
|
82
|
-
// wethBalance,
|
|
83
|
-
// 0.0003,
|
|
84
|
-
// 0.0004,
|
|
85
|
-
// null,
|
|
86
|
-
// null,
|
|
87
|
-
// FeeAmount.LOW,
|
|
88
|
-
// options
|
|
89
|
-
// );
|
|
90
|
-
// console.log(result);
|
|
91
|
-
// } catch (e) {
|
|
92
|
-
// console.log(e);
|
|
93
|
-
// }
|
|
94
|
-
// expect(result).not.toBe(null);
|
|
95
|
-
// });
|
|
96
|
-
|
|
97
|
-
// it("should remove liquidity from an existing pool ", async () => {
|
|
98
|
-
// const pool = await dhedge.loadPool(TEST_POOL);
|
|
99
|
-
// const result = await pool.decreaseLiquidity(
|
|
100
|
-
// Dapp.UNISWAPV3,
|
|
101
|
-
// "110507",
|
|
102
|
-
// 100,
|
|
103
|
-
// options
|
|
104
|
-
// );
|
|
105
|
-
// console.log("result", result);
|
|
106
|
-
// expect(result).not.toBe(null);
|
|
107
|
-
// });
|
|
108
|
-
|
|
109
|
-
// it("should increase liquidity in an existing pool WETH/WBTC pool", async () => {
|
|
110
|
-
// const pool = await dhedge.loadPool(TEST_POOL);
|
|
111
|
-
// const result = await pool.increaseLiquidity(
|
|
112
|
-
// Dapp.UNISWAPV3,
|
|
113
|
-
// "110507",
|
|
114
|
-
// "244838",
|
|
115
|
-
// "258300000000000",
|
|
116
|
-
// options
|
|
117
|
-
// );
|
|
118
|
-
// console.log("result", result);
|
|
119
|
-
// expect(result).not.toBe(null);
|
|
120
|
-
// });
|
|
121
|
-
|
|
122
|
-
// it("should claim fees an existing pool", async () => {
|
|
123
|
-
// const pool = await dhedge.loadPool(TEST_POOL);
|
|
124
|
-
// const result = await pool.claimFeesUniswapV3("54929", options);
|
|
125
|
-
// console.log("result", result);
|
|
126
|
-
// expect(result).not.toBe(null);
|
|
127
|
-
// });
|
|
128
|
-
|
|
129
|
-
// it("approves unlimited USDC to swap on UniswapV3", async () => {
|
|
130
|
-
// let result;
|
|
131
|
-
// const pool = await dhedge.loadPool(TEST_POOL);
|
|
132
|
-
// try {
|
|
133
|
-
// result = await pool.approve(
|
|
134
|
-
// Dapp.UNISWAPV3,
|
|
135
|
-
// USDC,
|
|
136
|
-
// ethers.constants.MaxInt256,
|
|
137
|
-
// options
|
|
138
|
-
// );
|
|
139
|
-
// console.log(result);
|
|
140
|
-
// } catch (e) {
|
|
141
|
-
// console.log(e);
|
|
142
|
-
// }
|
|
143
|
-
// expect(result).not.toBe(null);
|
|
144
|
-
// });
|
|
145
|
-
|
|
146
|
-
// it("should swap USDC into WETH on UniswapV3 pool", async () => {
|
|
147
|
-
// const pool = await dhedge.loadPool(TEST_POOL);
|
|
148
|
-
// const result = await pool.tradeUniswapV3(
|
|
149
|
-
// USDC,
|
|
150
|
-
// WETH,
|
|
151
|
-
// "1000000",
|
|
152
|
-
// FeeAmount.LOW,
|
|
153
|
-
// 1,
|
|
154
|
-
// options
|
|
155
|
-
// );
|
|
156
|
-
|
|
157
|
-
// console.log(result);
|
|
158
|
-
// expect(result).not.toBe(null);
|
|
159
|
-
// });
|
|
189
|
+
testingHelper({
|
|
190
|
+
network: Network.BASE,
|
|
191
|
+
testingRun: testUniswapV3
|
|
160
192
|
});
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import { ethers } from "ethers";
|
|
1
|
+
import { BigNumber, ethers } from "ethers";
|
|
2
2
|
import { Network } from "../../types";
|
|
3
3
|
import { getWalletData } from "../wallet";
|
|
4
|
+
import { CONTRACT_ADDRESS, USDC_BALANCEOF_SLOT } from "../constants";
|
|
4
5
|
|
|
5
6
|
export type TestingRunParams = {
|
|
6
7
|
network: Network;
|
|
@@ -40,3 +41,56 @@ export const beforeAfterReset = ({
|
|
|
40
41
|
await provider.send("evm_mine", []);
|
|
41
42
|
});
|
|
42
43
|
};
|
|
44
|
+
|
|
45
|
+
export const setTokenAmount = async ({
|
|
46
|
+
amount,
|
|
47
|
+
userAddress,
|
|
48
|
+
tokenAddress,
|
|
49
|
+
slot,
|
|
50
|
+
provider
|
|
51
|
+
}: {
|
|
52
|
+
amount: string;
|
|
53
|
+
userAddress: string;
|
|
54
|
+
tokenAddress: string;
|
|
55
|
+
slot: number;
|
|
56
|
+
provider: ethers.providers.JsonRpcProvider;
|
|
57
|
+
}): Promise<void> => {
|
|
58
|
+
const toBytes32 = (bn: string) => {
|
|
59
|
+
return ethers.utils.hexlify(
|
|
60
|
+
ethers.utils.zeroPad(BigNumber.from(bn).toHexString(), 32)
|
|
61
|
+
);
|
|
62
|
+
};
|
|
63
|
+
const index = ethers.utils.solidityKeccak256(
|
|
64
|
+
["uint256", "uint256"],
|
|
65
|
+
[userAddress, slot] // key, slot
|
|
66
|
+
);
|
|
67
|
+
await provider.send("hardhat_setStorageAt", [
|
|
68
|
+
tokenAddress,
|
|
69
|
+
index,
|
|
70
|
+
toBytes32(amount).toString()
|
|
71
|
+
]);
|
|
72
|
+
await provider.send("evm_mine", []);
|
|
73
|
+
};
|
|
74
|
+
export const setUSDCAmount = async ({
|
|
75
|
+
provider,
|
|
76
|
+
userAddress,
|
|
77
|
+
amount,
|
|
78
|
+
network
|
|
79
|
+
}: {
|
|
80
|
+
amount: string;
|
|
81
|
+
userAddress: string;
|
|
82
|
+
provider: ethers.providers.JsonRpcProvider;
|
|
83
|
+
network: Network;
|
|
84
|
+
}): Promise<void> => {
|
|
85
|
+
await setTokenAmount({
|
|
86
|
+
amount,
|
|
87
|
+
userAddress,
|
|
88
|
+
provider,
|
|
89
|
+
tokenAddress: CONTRACT_ADDRESS[network].USDC,
|
|
90
|
+
slot: USDC_BALANCEOF_SLOT[network]
|
|
91
|
+
});
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
export const wait = (seconds: number): Promise<void> => {
|
|
95
|
+
return new Promise(resolve => setTimeout(resolve, seconds * 1000));
|
|
96
|
+
};
|
package/src/test/wallet.ts
CHANGED
package/src/test/zeroEx.test.ts
CHANGED
|
@@ -1,12 +1,18 @@
|
|
|
1
1
|
/* eslint-disable @typescript-eslint/no-non-null-assertion */
|
|
2
|
+
import BigNumber from "bignumber.js";
|
|
2
3
|
import { Dhedge, Pool } from "..";
|
|
3
4
|
import { routerAddress } from "../config";
|
|
4
5
|
import { Dapp, Network } from "../types";
|
|
5
6
|
import { CONTRACT_ADDRESS, MAX_AMOUNT, TEST_POOL } from "./constants";
|
|
6
|
-
import {
|
|
7
|
+
import {
|
|
8
|
+
TestingRunParams,
|
|
9
|
+
setUSDCAmount,
|
|
10
|
+
testingHelper
|
|
11
|
+
} from "./utils/testingHelper";
|
|
7
12
|
import { allowanceDelta, balanceDelta } from "./utils/token";
|
|
13
|
+
import { getTxOptions } from "./txOptions";
|
|
8
14
|
|
|
9
|
-
const testZeroEx = ({ wallet, network }: TestingRunParams) => {
|
|
15
|
+
const testZeroEx = ({ wallet, network, provider }: TestingRunParams) => {
|
|
10
16
|
const USDC = CONTRACT_ADDRESS[network].USDC;
|
|
11
17
|
const WETH = CONTRACT_ADDRESS[network].WETH;
|
|
12
18
|
|
|
@@ -18,10 +24,28 @@ const testZeroEx = ({ wallet, network }: TestingRunParams) => {
|
|
|
18
24
|
beforeAll(async () => {
|
|
19
25
|
dhedge = new Dhedge(wallet, network);
|
|
20
26
|
pool = await dhedge.loadPool(TEST_POOL[network]);
|
|
27
|
+
// top up gas
|
|
28
|
+
await provider.send("hardhat_setBalance", [
|
|
29
|
+
wallet.address,
|
|
30
|
+
"0x10000000000000000"
|
|
31
|
+
]);
|
|
32
|
+
await provider.send("evm_mine", []);
|
|
33
|
+
// top up USDC
|
|
34
|
+
await setUSDCAmount({
|
|
35
|
+
amount: new BigNumber(100).times(1e18).toFixed(0),
|
|
36
|
+
userAddress: pool.address,
|
|
37
|
+
network,
|
|
38
|
+
provider
|
|
39
|
+
});
|
|
21
40
|
});
|
|
22
41
|
|
|
23
42
|
it("approves unlimited USDC on 0x", async () => {
|
|
24
|
-
await pool.approve(
|
|
43
|
+
await pool.approve(
|
|
44
|
+
Dapp.ZEROEX,
|
|
45
|
+
USDC,
|
|
46
|
+
MAX_AMOUNT,
|
|
47
|
+
await getTxOptions(network)
|
|
48
|
+
);
|
|
25
49
|
const usdcAllowanceDelta = await allowanceDelta(
|
|
26
50
|
pool.address,
|
|
27
51
|
USDC,
|
|
@@ -32,7 +56,14 @@ const testZeroEx = ({ wallet, network }: TestingRunParams) => {
|
|
|
32
56
|
});
|
|
33
57
|
|
|
34
58
|
it("trades 2 USDC into WETH on 0x", async () => {
|
|
35
|
-
await pool.trade(
|
|
59
|
+
await pool.trade(
|
|
60
|
+
Dapp.ZEROEX,
|
|
61
|
+
USDC,
|
|
62
|
+
WETH,
|
|
63
|
+
"2000000",
|
|
64
|
+
0.5,
|
|
65
|
+
await getTxOptions(network)
|
|
66
|
+
);
|
|
36
67
|
const wethBalanceDelta = await balanceDelta(
|
|
37
68
|
pool.address,
|
|
38
69
|
WETH,
|
|
@@ -52,3 +83,8 @@ testingHelper({
|
|
|
52
83
|
network: Network.POLYGON,
|
|
53
84
|
testingRun: testZeroEx
|
|
54
85
|
});
|
|
86
|
+
|
|
87
|
+
testingHelper({
|
|
88
|
+
network: Network.BASE,
|
|
89
|
+
testingRun: testZeroEx
|
|
90
|
+
});
|