@dhedge/v2-sdk 1.8.3 → 1.9.0

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.
@@ -1,218 +1,126 @@
1
- import { Dhedge } from "..";
2
- import { Network } from "../types";
3
- import { TEST_POOL, WMATIC } from "./constants";
4
- import { getTxOptions } from "./txOptions";
1
+ /* eslint-disable @typescript-eslint/no-non-null-assertion */
2
+ /* eslint-disable @typescript-eslint/no-explicit-any */
3
+ import { Dhedge, Pool } from "..";
4
+ import { routerAddress } from "../config";
5
+ import { Dapp, Network } from "../types";
6
+ import { CONTRACT_ADDRESS, MAX_AMOUNT, TEST_POOL } from "./constants";
7
+ import { allowanceDelta, balanceDelta } from "./utils/token";
5
8
 
6
9
  import { wallet } from "./wallet";
7
10
 
8
- let dhedge: Dhedge;
9
- let options: any;
11
+ const network = Network.ARBITRUM;
12
+ const USDC = CONTRACT_ADDRESS[network].USDC;
13
+ const WETH = CONTRACT_ADDRESS[network].WETH;
14
+ const WSTETH = CONTRACT_ADDRESS[network].WSTETH;
15
+ const BALANCER_POOL = CONTRACT_ADDRESS[network].BALANCER_WSTETH_WETH_POOL;
16
+ const BLANCER_GAUGE = CONTRACT_ADDRESS[network].BALANCER_WSTETH_WETH_GAUGE;
10
17
 
18
+ let dhedge: Dhedge;
19
+ let pool: Pool;
11
20
  jest.setTimeout(100000);
12
21
 
13
22
  describe("pool", () => {
14
23
  beforeAll(async () => {
15
- dhedge = new Dhedge(wallet, Network.POLYGON);
16
- options = await getTxOptions(Network.POLYGON);
24
+ dhedge = new Dhedge(wallet, network);
25
+ pool = await dhedge.loadPool(TEST_POOL[network]);
26
+ await pool.approve(Dapp.ONEINCH, USDC, MAX_AMOUNT);
27
+ await pool.trade(Dapp.ONEINCH, USDC, WETH, "1000000", 0.5);
28
+ await pool.trade(Dapp.ONEINCH, USDC, WSTETH, "1000000", 0.5);
17
29
  });
18
30
 
19
- // it("approves unlimited USDC on Balancer", async () => {
20
- // let result;
21
- // const pool = await dhedge.loadPool(TEST_POOL);
22
- // try {
23
- // result = await pool.approve(
24
- // Dapp.BALANCER,
25
- // USDC,
26
- // ethers.constants.MaxInt256,
27
- // options
28
- // );
29
- // console.log(result);
30
- // } catch (e) {
31
- // console.log(e);
32
- // }
33
- // expect(result).not.toBe(null);
34
- // });
35
-
36
- // it("trades 2 USDC into SUSHI on Balancer", async () => {
37
- // let result;
38
- // const pool = await dhedge.loadPool(myPool);
39
- // try {
40
- // result = await pool.trade(
41
- // Dapp.BALANCER,
42
- // usdc,
43
- // sushi,
44
- // "2000000",
45
- // 0.5,
46
- // options
47
- // );
48
- // console.log(result);
49
- // } catch (e) {
50
- // console.log(e);
51
- // }
52
- // expect(result).not.toBe(null);
53
- // });
54
-
55
- // it("adds 1 USDC to a USDC/TUSD/DAI/USDT balancer pool", async () => {
56
- // let result;
57
- // const pool = await dhedge.loadPool(TEST_POOL);
58
- // const assets = [USDC, TUSD, DAI, USDT];
59
- // const amounts = ["1000000", "0", "0", "0"];
60
- // try {
61
- // result = await pool.joinBalancerPool(
62
- // "0x0d34e5dd4d8f043557145598e4e2dc286b35fd4f000000000000000000000068",
63
- // assets,
64
- // amounts,
65
- // options
66
- // );
67
- // console.log("result", result);
68
- // } catch (e) {
69
- // console.log(e);
70
- // }
71
- // expect(result).not.toBe(null);
72
- // });
73
-
74
- // it("exits entire balance of WBTC/USDC/WETH balancer pool", async () => {
75
- // let result;
76
- // const pool = await dhedge.loadPool(myPool);
77
- // const assets = [wbtc, usdc, weth];
78
- // const amount = await dhedge.utils.getBalance(
79
- // "0x03cd191f589d12b0582a99808cf19851e468e6b5",
80
- // pool.address
81
- // );
82
- // try {
83
- // result = await pool.exitBalancerPool(
84
- // "0x03cd191f589d12b0582a99808cf19851e468e6b500010000000000000000000a",
85
- // assets,
86
- // amount,
87
- // options
88
- // );
89
- // console.log("result", result);
90
- // } catch (e) {
91
- // console.log(e);
92
- // }
93
- // expect(result).not.toBe(null);
94
- // });
95
-
96
- // it("claims balancer rewards", async () => {
97
- // let result;
98
- // const pool = await dhedge.loadPool(TEST_POOL);
99
- // try {
100
- // result = await pool.harvestBalancerRewards(options);
101
- // console.log("result", result);
102
- // } catch (e) {
103
- // console.log(e);
104
- // }
105
- // expect(result).not.toBe(null);
106
- // });
31
+ it("approves unlimited stWETH on Balancer", async () => {
32
+ await pool.approve(Dapp.BALANCER, WSTETH, MAX_AMOUNT);
33
+ const stWETHAllowanceDelta = await allowanceDelta(
34
+ pool.address,
35
+ WSTETH,
36
+ routerAddress[network].balancer!,
37
+ pool.signer
38
+ );
39
+ await expect(stWETHAllowanceDelta.gt(0));
40
+ });
107
41
 
108
- it("adds 5 WMATIC to a WMATIC/stMATIC balancer pool", async () => {
109
- let result;
110
- const pool = await dhedge.loadPool(TEST_POOL);
111
- const assets = [
112
- WMATIC,
113
- "0xb20fC01D21A50d2C734C4a1262B4404d41fA7BF0",
114
- "0xfa68FB4628DFF1028CFEc22b4162FCcd0d45efb6"
115
- ];
116
- const amounts = ["29500317230801455961187", "0", "0"];
117
- try {
118
- result = await pool.joinBalancerPool(
119
- "0xb20fc01d21a50d2c734c4a1262b4404d41fa7bf000000000000000000000075c",
120
- assets,
121
- amounts,
122
- options
123
- );
124
- console.log("result", result);
125
- } catch (e) {
126
- console.log(e);
127
- }
128
- expect(result).not.toBe(null);
42
+ it("approves unlimited WETH on Balancer", async () => {
43
+ await pool.approve(Dapp.BALANCER, WETH, MAX_AMOUNT);
44
+ const wethAllowanceDelta = await allowanceDelta(
45
+ pool.address,
46
+ WETH,
47
+ routerAddress[network].balancer!,
48
+ pool.signer
49
+ );
50
+ await expect(wethAllowanceDelta.gt(0));
129
51
  });
130
52
 
131
- // it("allows unlimited WMATIC-stMATIC LP on gauge", async () => {
132
- // let result;
133
- // const pool = await dhedge.loadPool(TEST_POOL);
134
- // try {
135
- // result = await pool.approveSpender(
136
- // "0x9928340f9E1aaAd7dF1D95E27bd9A5c715202a56",
137
- // WMATIC_STMATIC_LP,
138
- // ethers.constants.MaxUint256,
139
- // options
140
- // );
141
- // console.log("result", result);
142
- // } catch (e) {
143
- // console.log(e);
144
- // }
145
- // expect(result).not.toBe(null);
146
- // });
53
+ it("should add liquidity in a Balancer pool", async () => {
54
+ const wstETHBalance = await pool.utils.getBalance(WSTETH, pool.address);
55
+ const wethBalance = await pool.utils.getBalance(WETH, pool.address);
56
+ await pool.joinBalancerPool(
57
+ "0x36bf227d6bac96e2ab1ebb5492ecec69c691943f000200000000000000000316", //wstETH-WETH on Arbitrum
58
+ [WSTETH, WETH],
59
+ [wstETHBalance.toString(), wethBalance.toString()]
60
+ );
61
+ const lpBalanceDelta = await balanceDelta(
62
+ pool.address,
63
+ BALANCER_POOL,
64
+ pool.signer
65
+ );
66
+ expect(lpBalanceDelta.gt(0));
67
+ });
147
68
 
148
- // it("stakes WMATIC-stMATIC LP in gauge", async () => {
149
- // let result;
150
- // const pool = await dhedge.loadPool(TEST_POOL);
151
- // try {
152
- // result = await pool.stakeInGauge(
153
- // "0x9928340f9E1aaAd7dF1D95E27bd9A5c715202a56",
154
- // "4978534455005333156",
155
- // options
156
- // );
157
- // console.log("result", result);
158
- // } catch (e) {
159
- // console.log(e);
160
- // }
161
- // expect(result).not.toBe(null);
162
- // });
69
+ it("approves unlimited LP Token on Balancer Vault", async () => {
70
+ await pool.approveSpender(BLANCER_GAUGE, BALANCER_POOL, MAX_AMOUNT);
71
+ const gaugeAllowanceDelta = await allowanceDelta(
72
+ pool.address,
73
+ BALANCER_POOL,
74
+ BLANCER_GAUGE,
75
+ pool.signer
76
+ );
77
+ await expect(gaugeAllowanceDelta.gt(0));
78
+ });
163
79
 
164
- // it("claim fess pf staked WMATIC-stMATIC LP in gauge", async () => {
165
- // let result;
166
- // const pool = await dhedge.loadPool(TEST_POOL);
167
- // try {
168
- // result = await pool.claimFees(
169
- // Dapp.BALANCER,
170
- // "0x9928340f9E1aaAd7dF1D95E27bd9A5c715202a56",
171
- // options
172
- // );
173
- // console.log("result", result);
174
- // } catch (e) {
175
- // console.log(e);
176
- // }
177
- // expect(result).not.toBe(null);
178
- // });
80
+ it("stakes LP tokens in Balancer vault", async () => {
81
+ const lpTokenBalance = await pool.utils.getBalance(
82
+ BALANCER_POOL,
83
+ pool.address
84
+ );
85
+ await pool.stakeInGauge(Dapp.BALANCER, BLANCER_GAUGE, lpTokenBalance);
86
+ const lpBalanceDelta = await balanceDelta(
87
+ pool.address,
88
+ BALANCER_POOL,
89
+ pool.signer
90
+ );
91
+ expect(lpBalanceDelta.lt(0));
92
+ });
179
93
 
180
- // it("unstakes WMATIC-stMATIC LP from gauge", async () => {
181
- // let result;
182
- // const pool = await dhedge.loadPool(TEST_POOL);
183
- // try {
184
- // result = await pool.unstakeFromGauge(
185
- // "0x9928340f9E1aaAd7dF1D95E27bd9A5c715202a56",
186
- // "4978534455005333156",
187
- // options
188
- // );
189
- // console.log("result", result);
190
- // } catch (e) {
191
- // console.log(e);
192
- // }
193
- // expect(result).not.toBe(null);
194
- // });
94
+ it("unstakes LP tokens from Balancer vault", async () => {
95
+ const vaultTokenBalance = await pool.utils.getBalance(
96
+ BLANCER_GAUGE,
97
+ pool.address
98
+ );
99
+ await pool.unstakeFromGauge(BLANCER_GAUGE, vaultTokenBalance);
100
+ const lpBalanceDelta = await balanceDelta(
101
+ pool.address,
102
+ BALANCER_POOL,
103
+ pool.signer
104
+ );
105
+ expect(lpBalanceDelta.gt(0));
106
+ });
195
107
 
196
- // it("exits from WMATIC-stMATIC LP into WMATIC", async () => {
197
- // let result;
198
- // const pool = await dhedge.loadPool(TEST_POOL);
199
- // const assets = [WMATIC, STMATIC];
200
- // const amount = await dhedge.utils.getBalance(
201
- // "0xaF5E0B5425dE1F5a630A8cB5AA9D97B8141C908D",
202
- // pool.address
203
- // );
204
- // try {
205
- // result = await pool.exitBalancerPool(
206
- // "0xaf5e0b5425de1f5a630a8cb5aa9d97b8141c908d000200000000000000000366",
207
- // assets,
208
- // amount,
209
- // 1,
210
- // options
211
- // );
212
- // console.log("result", result);
213
- // } catch (e) {
214
- // console.log(e);
215
- // }
216
- // expect(result).not.toBe(null);
217
- // });
108
+ it("should remove liquidity from an Balancer pool ", async () => {
109
+ const lpTokenBalance = await pool.utils.getBalance(
110
+ BALANCER_POOL,
111
+ pool.address
112
+ );
113
+ await pool.approve(Dapp.BALANCER, BALANCER_POOL, MAX_AMOUNT);
114
+ await pool.exitBalancerPool(
115
+ "0x36bf227d6bac96e2ab1ebb5492ecec69c691943f000200000000000000000316",
116
+ [WSTETH, WETH],
117
+ lpTokenBalance
118
+ );
119
+ const wethBalanceDelta = await balanceDelta(
120
+ pool.address,
121
+ CONTRACT_ADDRESS[network].WETH,
122
+ pool.signer
123
+ );
124
+ expect(wethBalanceDelta.gt(0));
125
+ });
218
126
  });
@@ -40,7 +40,8 @@ export const KWENTA_ETH_PERP_V2 = "0x2b3bb4c683bfc5239b029131eef3b1d214478d93";
40
40
 
41
41
  export const TEST_POOL = {
42
42
  [Network.POLYGON]: "0x699fd4d6eadb216704c7e355cfa0a12f51813163",
43
- [Network.OPTIMISM]: "0x12573bfdf764ab9d52aca20e2827497a66829716"
43
+ [Network.OPTIMISM]: "0x12573bfdf764ab9d52aca20e2827497a66829716",
44
+ [Network.ARBITRUM]: "0x2dc2f936c8b6619facc69355d65dd93d2f4cc2bd"
44
45
  };
45
46
 
46
47
  export const CONTRACT_ADDRESS = {
@@ -55,9 +56,15 @@ export const CONTRACT_ADDRESS = {
55
56
  USDC: "0x7F5c764cBc14f9669B88837ca1490cCa17c31607",
56
57
  SUSD: "0x8c6f28f2f1a3c87f0f938b96d27520d9751ec8d9",
57
58
  WETH: "0x4200000000000000000000000000000000000006",
58
- KWENTA_ETH_PERP_V2: "0x2b3bb4c683bfc5239b029131eef3b1d214478d93",
59
- ARRAKIS_USDC_WETH_GAUGE: "0xd3a3fbae792c4ed0aa909ec032d3f14c999b2402",
60
- ARRAKIS_USDC_WETH_LP: "0xc59d8fbc3d9a92d24a923722f0244a50daa2ea33"
59
+ KWENTA_ETH_PERP_V2: "0x2b3bb4c683bfc5239b029131eef3b1d214478d93"
60
+ },
61
+ [Network.ARBITRUM]: {
62
+ USDC: "0xff970a61a04b1ca14834a43f5de4533ebddb5cc8",
63
+ WETH: "0x82af49447d8a07e3bd95bd0d56f35241523fbab1",
64
+ WBTC: "0x2f2a2543b76a4166549f7aab2e75bef0aefc5b0f",
65
+ WSTETH: "0x5979d7b546e38e414f7e9822514be443a4800529",
66
+ BALANCER_WSTETH_WETH_POOL: "0x36bf227d6bac96e2ab1ebb5492ecec69c691943f",
67
+ BALANCER_WSTETH_WETH_GAUGE: "0x251e51b25afa40f2b6b9f05aaf1bc7eaa0551771"
61
68
  }
62
69
  };
63
70
 
@@ -1,6 +1,7 @@
1
1
  import { Dhedge, Pool } from "..";
2
2
  import { Network } from "../types";
3
3
  import { CONTRACT_ADDRESS, TEST_POOL } from "./constants";
4
+ import { getDelayedOrders } from "./utils/futures";
4
5
  import { balanceDelta } from "./utils/token";
5
6
  import { wallet } from "./wallet";
6
7
 
@@ -32,11 +33,13 @@ describe("pool", () => {
32
33
  it("goes long ETH-PERP about 2x leverage", async () => {
33
34
  //size 50*2/2000 (margin * leverage / price)
34
35
  const size = (0.05 * 1e18).toString();
35
- const tx = await pool.changeFuturesPosition(perp, size);
36
- expect(tx).not.toBe(null);
36
+ await pool.changeFuturesPosition(perp, size);
37
+ const orders = await getDelayedOrders(perp, pool);
38
+ expect(orders.length).toBeGreaterThan(0);
37
39
  });
38
40
 
39
41
  it("removes 20 sUSD margin from ETH future market", async () => {
42
+ await getDelayedOrders(perp, pool);
40
43
  await pool.changeFuturesMargin(perp, (-20 * 1e18).toString());
41
44
  const sUSDBalanceDelta = await balanceDelta(
42
45
  pool.address,
@@ -8,7 +8,8 @@ import { allowanceDelta, balanceDelta } from "./utils/token";
8
8
  import { wallet } from "./wallet";
9
9
 
10
10
  // const network = Network.OPTIMISM;
11
- const network = Network.POLYGON;
11
+ // const network = Network.POLYGON;
12
+ const network = Network.ARBITRUM;
12
13
  const USDC = CONTRACT_ADDRESS[network].USDC;
13
14
  const WETH = CONTRACT_ADDRESS[network].WETH;
14
15
 
@@ -33,8 +34,8 @@ describe("pool", () => {
33
34
  await expect(usdcAllowanceDelta.gt(0));
34
35
  });
35
36
 
36
- it("trades 5 USDC into WETH on 1Inch", async () => {
37
- await pool.trade(Dapp.ONEINCH, USDC, WETH, "5000000", 0.5);
37
+ it("trades 2 USDC into WETH on 1Inch", async () => {
38
+ await pool.trade(Dapp.ONEINCH, USDC, WETH, "2000000", 0.5);
38
39
  const wethBalanceDelta = await balanceDelta(
39
40
  pool.address,
40
41
  WETH,
@@ -0,0 +1,14 @@
1
+ /* eslint-disable @typescript-eslint/no-explicit-any */
2
+ import { Pool, ethers } from "../..";
3
+ import ISynthetixFuturesMarketV2 from "../../abi/ISynthetixFuturesMarketV2.json";
4
+ export async function getDelayedOrders(
5
+ market: string,
6
+ pool: Pool
7
+ ): Promise<any> {
8
+ const futuresMarket = new ethers.Contract(
9
+ market,
10
+ ISynthetixFuturesMarketV2.abi,
11
+ pool.signer
12
+ );
13
+ return await futuresMarket.delayedOrders(pool.address);
14
+ }
@@ -12,7 +12,7 @@ require("dotenv").config();
12
12
  // );
13
13
 
14
14
  // const provider = new ethers.providers.JsonRpcProvider(
15
- // `https://polygon-mainnet.infura.io/v3/${process.env.INFURA_PROJECT_ID}`
15
+ // `https://arb-mainnet.g.alchemy.com/v2/${process.env.ALCHEMY_PROJECT_ID}`
16
16
  // );
17
17
 
18
18
  // const provider = new ethers.providers.JsonRpcProvider(
package/src/types.ts CHANGED
@@ -3,7 +3,8 @@ import { BigNumber } from "ethers";
3
3
 
4
4
  export enum Network {
5
5
  POLYGON = "polygon",
6
- OPTIMISM = "optimism"
6
+ OPTIMISM = "optimism",
7
+ ARBITRUM = "arbitrum"
7
8
  }
8
9
 
9
10
  export enum Dapp {