@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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dhedge/v2-sdk",
3
- "version": "1.8.3",
3
+ "version": "1.9.0",
4
4
  "license": "MIT",
5
5
  "description": "🛠 An SDK for building applications on top of dHEDGE V2",
6
6
  "main": "dist/index.js",
@@ -21,7 +21,8 @@
21
21
  "size": "size-limit",
22
22
  "analyze": "size-limit --why",
23
23
  "fork:polygon": "hardhat node --fork $(grep POLYGON_URL .env | cut -d '=' -f2)",
24
- "fork:optimism": "hardhat node --fork $(grep OPTIMISM_URL .env | cut -d '=' -f2)"
24
+ "fork:optimism": "hardhat node --fork $(grep OPTIMISM_URL .env | cut -d '=' -f2)",
25
+ "fork:arbitrum": "hardhat node --fork $(grep ARBITRUM_URL .env | cut -d '=' -f2)"
25
26
  },
26
27
  "husky": {
27
28
  "hooks": {
package/src/config.ts CHANGED
@@ -15,7 +15,8 @@ export const factoryAddress: AddressNetworkMap = {
15
15
  [Network.POLYGON]: process.env.STAGING_CONTRACTS
16
16
  ? "0xDd87eCdB10cFF7004276AAbAbd30e7a08F69bb53"
17
17
  : "0xfdc7b8bFe0DD3513Cc669bB8d601Cb83e2F69cB0",
18
- [Network.OPTIMISM]: "0x5e61a079A178f0E5784107a4963baAe0c5a680c6"
18
+ [Network.OPTIMISM]: "0x5e61a079A178f0E5784107a4963baAe0c5a680c6",
19
+ [Network.ARBITRUM]: "0xfffb5fb14606eb3a548c113026355020ddf27535"
19
20
  };
20
21
 
21
22
  export const routerAddress: AddressDappNetworkMap = {
@@ -39,6 +40,12 @@ export const routerAddress: AddressDappNetworkMap = {
39
40
  [Dapp.VELODROME]: "0x9c12939390052919aF3155f41Bf4160Fd3666A6f",
40
41
  [Dapp.LYRA]: "0xCCE7819d65f348c64B7Beb205BA367b3fE33763B",
41
42
  [Dapp.ARRAKIS]: "0x9ce88a56d120300061593eF7AD074A1B710094d5"
43
+ },
44
+ [Network.ARBITRUM]: {
45
+ [Dapp.ONEINCH]: "0x1111111254EEB25477B68fb85Ed929f73A960582",
46
+ [Dapp.UNISWAPV3]: "0xe592427a0aece92de3edee1f18e0157c05861564",
47
+ [Dapp.AAVEV3]: "0x794a61358D6845594F94dc1DB02A252b5b4814aD",
48
+ [Dapp.BALANCER]: "0xBA12222222228d8Ba445958a75a0704d566BF2C8"
42
49
  }
43
50
  };
44
51
 
@@ -47,7 +54,8 @@ export const dappFactoryAddress: AddressDappNetworkMap = {
47
54
  [Dapp.SUSHISWAP]: "0xc35DADB65012eC5796536bD9864eD8773aBc74C4",
48
55
  [Dapp.QUICKSWAP]: "0x5757371414417b8C6CAad45bAeF941aBc7d3Ab32"
49
56
  },
50
- [Network.OPTIMISM]: {}
57
+ [Network.OPTIMISM]: {},
58
+ [Network.ARBITRUM]: {}
51
59
  };
52
60
 
53
61
  export const stakingAddress: AddressDappNetworkMap = {
@@ -57,7 +65,8 @@ export const stakingAddress: AddressDappNetworkMap = {
57
65
  [Dapp.AAVE]: "0x357D51124f59836DeD84c8a1730D72B749d8BC23",
58
66
  [Dapp.AAVEV3]: "0x929EC64c34a17401F460460D4B9390518E5B473e"
59
67
  },
60
- [Network.OPTIMISM]: {}
68
+ [Network.OPTIMISM]: {},
69
+ [Network.ARBITRUM]: {}
61
70
  };
62
71
 
63
72
  export const aaveAddressProvider: AddressDappNetworkMap = {
@@ -67,27 +76,34 @@ export const aaveAddressProvider: AddressDappNetworkMap = {
67
76
  },
68
77
  [Network.OPTIMISM]: {
69
78
  [Dapp.AAVEV3]: "0xa97684ead0e402dC232d5A977953DF7ECBaB3CDb"
79
+ },
80
+ [Network.ARBITRUM]: {
81
+ [Dapp.AAVEV3]: "0xa97684ead0e402dc232d5a977953df7ecbab3cdb"
70
82
  }
71
83
  };
72
84
  export const nonfungiblePositionManagerAddress: AddressNetworkMap = {
73
85
  [Network.POLYGON]: "0xC36442b4a4522E871399CD717aBDD847Ab11FE88",
74
- [Network.OPTIMISM]: "0xC36442b4a4522E871399CD717aBDD847Ab11FE88"
86
+ [Network.OPTIMISM]: "0xC36442b4a4522E871399CD717aBDD847Ab11FE88",
87
+ [Network.ARBITRUM]: "0xC36442b4a4522E871399CD717aBDD847Ab11FE88"
75
88
  };
76
89
 
77
90
  export const networkChainIdMap: NetworkChainIdMap = {
78
91
  [Network.POLYGON]: 137,
79
- [Network.OPTIMISM]: 10
92
+ [Network.OPTIMISM]: 10,
93
+ [Network.ARBITRUM]: 42161
80
94
  };
81
95
 
82
96
  export const balancerSubgraph: AddressNetworkMap = {
83
97
  [Network.POLYGON]:
84
98
  "https://api.thegraph.com/subgraphs/name/balancer-labs/balancer-polygon-v2",
85
- [Network.OPTIMISM]: ""
99
+ [Network.OPTIMISM]: "",
100
+ [Network.ARBITRUM]: ""
86
101
  };
87
102
 
88
103
  export const multiCallAddress: AddressNetworkMap = {
89
104
  [Network.POLYGON]: "0x275617327c958bD06b5D6b871E7f491D76113dd8",
90
- [Network.OPTIMISM]: ""
105
+ [Network.OPTIMISM]: "",
106
+ [Network.ARBITRUM]: ""
91
107
  };
92
108
 
93
109
  export const lyraNetworkMap: LyraNetworkMap = {
@@ -59,6 +59,7 @@ import {
59
59
  getFuturesChangePositionTxData,
60
60
  getFuturesChangeMarginTxData
61
61
  } from "../services/futures";
62
+ import { getFuturesCancelOrderTxData } from "../services/futures/trade";
62
63
 
63
64
  export class Pool {
64
65
  public readonly poolLogic: Contract;
@@ -288,7 +289,11 @@ export class Pool {
288
289
  case Dapp.ONEINCH:
289
290
  const chainId = networkChainIdMap[this.network];
290
291
  const protocols = await getOneInchProtocols(chainId);
291
- const apiUrl = `https://api.1inch.exchange/v5.0/${chainId}/swap?fromTokenAddress=${assetFrom}&toTokenAddress=${assetTo}&amount=${amountIn.toString()}&fromAddress=${
292
+ if (!process.env.ONEINCH_API_URL)
293
+ throw new Error("ONEINCH_API_URL not configured in .env file");
294
+ const apiUrl = `${
295
+ process.env.ONEINCH_API_URL
296
+ }/${chainId}/swap?fromTokenAddress=${assetFrom}&toTokenAddress=${assetTo}&amount=${amountIn.toString()}&fromAddress=${
292
297
  this.address
293
298
  }&destReceiver=${
294
299
  this.address
@@ -1251,4 +1256,16 @@ export class Pool {
1251
1256
  const tx = await this.poolLogic.execTransaction(market, txData, options);
1252
1257
  return tx;
1253
1258
  }
1259
+
1260
+ /** Cancels an open oder on Synthetix futures market
1261
+ *
1262
+ * @param {string} market Address of futures market
1263
+ * @param {any} options Transaction options
1264
+ * @returns {Promise<any>} Transaction
1265
+ */
1266
+ async cancelFuturesOrder(market: string, options: any = null): Promise<any> {
1267
+ const txData = await getFuturesCancelOrderTxData(this);
1268
+ const tx = await this.poolLogic.execTransaction(market, txData, options);
1269
+ return tx;
1270
+ }
1254
1271
  }
@@ -24,3 +24,9 @@ export async function getFuturesChangePositionTxData(
24
24
  ethers.utils.formatBytes32String(FUTURES_TRACKING)
25
25
  ]);
26
26
  }
27
+
28
+ export async function getFuturesCancelOrderTxData(pool: Pool): Promise<string> {
29
+ return new ethers.utils.Interface(
30
+ ISynthetixFuturesMarketV2.abi
31
+ ).encodeFunctionData("cancelOffchainDelayedOrder", [pool.address]);
32
+ }
@@ -1,128 +1,87 @@
1
- import { Dhedge } from "..";
2
- import { Network } from "../types";
3
- import { OP, TEST_POOL, USDC, WBTC, WETH } from "./constants";
1
+ /* eslint-disable @typescript-eslint/no-non-null-assertion */
2
+ import { Dhedge, Pool } from "..";
3
+ import { routerAddress } from "../config";
4
+ import { Dapp, Network } from "../types";
5
+ import { CONTRACT_ADDRESS, MAX_AMOUNT, TEST_POOL } from "./constants";
6
+ import { allowanceDelta, balanceDelta } from "./utils/token";
4
7
 
5
8
  import { wallet } from "./wallet";
6
9
 
7
- let dhedge: Dhedge;
10
+ // const network = Network.OPTIMISM;
11
+ // const network = Network.POLYGON;
12
+ const network = Network.ARBITRUM;
13
+ const USDC = CONTRACT_ADDRESS[network].USDC;
14
+ const WETH = CONTRACT_ADDRESS[network].WETH;
8
15
 
16
+ let dhedge: Dhedge;
17
+ let pool: Pool;
9
18
  jest.setTimeout(100000);
10
19
 
11
- // const options = {
12
- // gasLimit: 5000000,
13
- // gasPrice: ethers.utils.parseUnits("100", "gwei")
14
- // };
15
-
16
20
  describe("pool", () => {
17
- beforeAll(() => {
18
- dhedge = new Dhedge(wallet, Network.OPTIMISM);
21
+ beforeAll(async () => {
22
+ dhedge = new Dhedge(wallet, network);
23
+ pool = await dhedge.loadPool(TEST_POOL[network]);
19
24
  });
20
25
 
21
- // it("approves USDC to Aave", async () => {
22
- // let result;
23
- // const pool = await dhedge.loadPool(TEST_POOL);
24
- // try {
25
- // result = await pool.approve(
26
- // Dapp.AAVEV3,
27
- // USDC,
28
- // ethers.constants.MaxUint256
29
- // );
30
- // console.log(result);
31
- // } catch (e) {
32
- // console.log(e);
33
- // }
34
- // expect(result).not.toBe(null);
35
- // });
36
-
37
- // it("withdraws 1 USDC from Aave lending pool", async () => {
38
- // let result;
39
- // const pool = await dhedge.loadPool(myPool);
40
- // try {
41
- // result = await pool.withdrawDeposit(
42
- // Dapp.AAVE,
43
- // weth,
44
- // "86567951006165",
45
- // options
46
- // );
47
- // console.log(result);
48
- // } catch (e) {
49
- // console.log(e);
50
- // }
51
- // expect(result).not.toBe(null);
52
- // });
53
-
54
- // it("borrows 0.001 WETH from Aave lending pool", async () => {
55
- // let result;
56
- // const pool = await dhedge.loadPool(TEST_POOL);
57
- // try {
58
- // result = await pool.borrow(Dapp.AAVEV3, WETH, "1000000000000000");
59
- // console.log(result);
60
- // } catch (e) {
61
- // console.log(e);
62
- // }
63
- // expect(result).not.toBe(null);
64
- // });
26
+ it("approves unlimited USDC on Aave Lending pool", async () => {
27
+ await pool.approve(Dapp.AAVEV3, USDC, MAX_AMOUNT);
28
+ const usdcAllowanceDelta = await allowanceDelta(
29
+ pool.address,
30
+ USDC,
31
+ routerAddress[network]["aavev3"]!,
32
+ pool.signer
33
+ );
34
+ await expect(usdcAllowanceDelta.gt(0));
35
+ });
65
36
 
66
- // it("reapys 1USDC to Aave lending pool", async () => {
67
- // let result;
68
- // const pool = await dhedge.loadPool(TEST_POOL);
69
- // try {
70
- // result = await pool.repay(Dapp.AAVEV3, USDC, "1380000");
71
- // console.log(result);
72
- // } catch (e) {
73
- // console.log(e);
74
- // }
75
- // expect(result).not.toBe(null);
76
- // });
37
+ it("approves unlimited WETH on Aave Lending pool", async () => {
38
+ await pool.approve(Dapp.AAVEV3, WETH, MAX_AMOUNT);
39
+ const wethAllowanceDelta = await allowanceDelta(
40
+ pool.address,
41
+ USDC,
42
+ routerAddress[network]["aavev3"]!,
43
+ pool.signer
44
+ );
45
+ await expect(wethAllowanceDelta.gt(0));
46
+ });
77
47
 
78
- // it("claims rewards from Aave", async () => {
79
- // let result;
80
- // const pool = await dhedge.loadPool(TEST_POOL);
81
- // try {
82
- // result = await pool.harvestAaveRewards([AMUSDC, VDEBTWETH], options);
83
- // console.log(result);
84
- // } catch (e) {
85
- // console.log(e);
86
- // }
87
- // expect(result).not.toBe(null);
88
- // });
48
+ it("lends 5 USDC into Aave lending pool", async () => {
49
+ await pool.lend(Dapp.AAVEV3, USDC, "5000000");
50
+ const usdcBalanceDelta = await balanceDelta(
51
+ pool.address,
52
+ USDC,
53
+ pool.signer
54
+ );
55
+ expect(usdcBalanceDelta.eq("-5000000"));
56
+ });
89
57
 
90
- // it("lends USDC to Aave", async () => {
91
- // let result;
92
- // const pool = await dhedge.loadPool(TEST_POOL);
93
- // try {
94
- // const balance = await dhedge.utils.getBalance(WETH, pool.address);
95
- // result = await pool.lend(Dapp.AAVEV3, WETH, balance, 196);
96
- // } catch (e) {
97
- // console.log(e);
98
- // }
99
- // expect(result).not.toBe(null);
100
- // });
58
+ it("it borrows 0.001 WETH from Aave lending pool", async () => {
59
+ await pool.borrow(Dapp.AAVEV3, WETH, "1000000000000000");
60
+ const wethBalanceDelta = await balanceDelta(
61
+ pool.address,
62
+ WETH,
63
+ pool.signer
64
+ );
65
+ expect(wethBalanceDelta.eq("1000000000000000"));
66
+ });
101
67
 
102
- // it("gets Aave assets for underlying", async () => {
103
- // let result;
104
- // const pool = await dhedge.loadPool(TEST_POOL);
105
- // try {
106
- // result = await getAaveAssetsForUnderlying(pool, Dapp.AAVEV3, [
107
- // USDC,
108
- // WETH
109
- // ]);
110
- // console.log(result);
111
- // } catch (e) {
112
- // console.log(e);
113
- // }
114
- // expect(result).not.toBe(null);
115
- // });
68
+ it("it repays 0.001 WETH to Aave lending pool", async () => {
69
+ await pool.repay(Dapp.AAVEV3, WETH, "1000000000000000");
70
+ const wethBalanceDelta = await balanceDelta(
71
+ pool.address,
72
+ WETH,
73
+ pool.signer
74
+ );
75
+ expect(wethBalanceDelta.eq("-1000000000000000"));
76
+ });
116
77
 
117
- it("claims rewards from AaveV3", async () => {
118
- let result;
119
- const pool = await dhedge.loadPool(TEST_POOL);
120
- try {
121
- result = await pool.harvestAaveV3Rewards([USDC, WETH, WBTC], OP);
122
- console.log(result);
123
- } catch (e) {
124
- console.log(e);
125
- }
126
- expect(result).not.toBe(null);
78
+ it("it withdraws 4 USDC from Aave lending pool", async () => {
79
+ await pool.withdrawDeposit(Dapp.AAVEV3, USDC, "4000000");
80
+ const usdcBalanceDelta = await balanceDelta(
81
+ pool.address,
82
+ WETH,
83
+ pool.signer
84
+ );
85
+ expect(usdcBalanceDelta.eq("4000000"));
127
86
  });
128
87
  });