@dhedge/v2-sdk 1.3.0 → 1.4.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.3.0",
3
+ "version": "1.4.0",
4
4
  "license": "MIT",
5
5
  "description": "🛠 An SDK for building applications on top of dHEDGE V2",
6
6
  "main": "dist/index.js",
@@ -0,0 +1,107 @@
1
+ {
2
+ "abi": [
3
+ {
4
+ "inputs": [
5
+ {
6
+ "internalType": "address",
7
+ "name": "gauge",
8
+ "type": "address"
9
+ },
10
+ {
11
+ "internalType": "uint256",
12
+ "name": "amount0Max",
13
+ "type": "uint256"
14
+ },
15
+ {
16
+ "internalType": "uint256",
17
+ "name": "amount1Max",
18
+ "type": "uint256"
19
+ },
20
+ {
21
+ "internalType": "uint256",
22
+ "name": "amount0Min",
23
+ "type": "uint256"
24
+ },
25
+ {
26
+ "internalType": "uint256",
27
+ "name": "amount1Min",
28
+ "type": "uint256"
29
+ },
30
+ {
31
+ "internalType": "address",
32
+ "name": "receiver",
33
+ "type": "address"
34
+ }
35
+ ],
36
+ "name": "addLiquidityAndStake",
37
+ "outputs": [
38
+ {
39
+ "internalType": "uint256",
40
+ "name": "amount0",
41
+ "type": "uint256"
42
+ },
43
+ {
44
+ "internalType": "uint256",
45
+ "name": "amount1",
46
+ "type": "uint256"
47
+ },
48
+ {
49
+ "internalType": "uint256",
50
+ "name": "mintAmount",
51
+ "type": "uint256"
52
+ }
53
+ ],
54
+ "stateMutability": "nonpayable",
55
+ "type": "function"
56
+ },
57
+ {
58
+ "inputs": [
59
+ {
60
+ "internalType": "address",
61
+ "name": "gauge",
62
+ "type": "address"
63
+ },
64
+ {
65
+ "internalType": "uint256",
66
+ "name": "burnAmount",
67
+ "type": "uint256"
68
+ },
69
+ {
70
+ "internalType": "uint256",
71
+ "name": "amount0Min",
72
+ "type": "uint256"
73
+ },
74
+ {
75
+ "internalType": "uint256",
76
+ "name": "amount1Min",
77
+ "type": "uint256"
78
+ },
79
+ {
80
+ "internalType": "address",
81
+ "name": "receiver",
82
+ "type": "address"
83
+ }
84
+ ],
85
+ "name": "removeLiquidityAndUnstake",
86
+ "outputs": [
87
+ {
88
+ "internalType": "uint256",
89
+ "name": "amount0",
90
+ "type": "uint256"
91
+ },
92
+ {
93
+ "internalType": "uint256",
94
+ "name": "amount1",
95
+ "type": "uint256"
96
+ },
97
+ {
98
+ "internalType": "uint128",
99
+ "name": "liquidityBurned",
100
+ "type": "uint128"
101
+ }
102
+ ],
103
+ "stateMutability": "nonpayable",
104
+ "type": "function"
105
+ }
106
+ ]
107
+ }
@@ -0,0 +1,153 @@
1
+ { "abi": [
2
+ {
3
+ "inputs": [
4
+ {
5
+ "internalType": "address",
6
+ "name": "user",
7
+ "type": "address"
8
+ }
9
+ ],
10
+ "name": "claim_rewards",
11
+ "outputs": [],
12
+ "stateMutability": "nonpayable",
13
+ "type": "function"
14
+ },
15
+ {
16
+ "inputs": [
17
+ {
18
+ "internalType": "address",
19
+ "name": "user",
20
+ "type": "address"
21
+ },
22
+ {
23
+ "internalType": "address",
24
+ "name": "receiver",
25
+ "type": "address"
26
+ }
27
+ ],
28
+ "name": "claim_rewards",
29
+ "outputs": [],
30
+ "stateMutability": "nonpayable",
31
+ "type": "function"
32
+ },
33
+ {
34
+ "inputs": [],
35
+ "name": "claim_rewards",
36
+ "outputs": [],
37
+ "stateMutability": "nonpayable",
38
+ "type": "function"
39
+ },
40
+ {
41
+ "inputs": [
42
+ {
43
+ "internalType": "address",
44
+ "name": "user",
45
+ "type": "address"
46
+ },
47
+ {
48
+ "internalType": "address",
49
+ "name": "rewardToken",
50
+ "type": "address"
51
+ }
52
+ ],
53
+ "name": "claimable_reward",
54
+ "outputs": [
55
+ {
56
+ "internalType": "uint256",
57
+ "name": "",
58
+ "type": "uint256"
59
+ }
60
+ ],
61
+ "stateMutability": "view",
62
+ "type": "function"
63
+ },
64
+ {
65
+ "inputs": [],
66
+ "name": "reward_count",
67
+ "outputs": [
68
+ {
69
+ "internalType": "uint256",
70
+ "name": "",
71
+ "type": "uint256"
72
+ }
73
+ ],
74
+ "stateMutability": "view",
75
+ "type": "function"
76
+ },
77
+ {
78
+ "inputs": [
79
+ {
80
+ "internalType": "address",
81
+ "name": "token",
82
+ "type": "address"
83
+ }
84
+ ],
85
+ "name": "reward_data",
86
+ "outputs": [
87
+ {
88
+ "internalType": "address",
89
+ "name": "",
90
+ "type": "address"
91
+ },
92
+ {
93
+ "internalType": "address",
94
+ "name": "",
95
+ "type": "address"
96
+ },
97
+ {
98
+ "internalType": "uint256",
99
+ "name": "",
100
+ "type": "uint256"
101
+ },
102
+ {
103
+ "internalType": "uint256",
104
+ "name": "",
105
+ "type": "uint256"
106
+ },
107
+ {
108
+ "internalType": "uint256",
109
+ "name": "",
110
+ "type": "uint256"
111
+ },
112
+ {
113
+ "internalType": "uint256",
114
+ "name": "",
115
+ "type": "uint256"
116
+ }
117
+ ],
118
+ "stateMutability": "view",
119
+ "type": "function"
120
+ },
121
+ {
122
+ "inputs": [
123
+ {
124
+ "internalType": "uint256",
125
+ "name": "index",
126
+ "type": "uint256"
127
+ }
128
+ ],
129
+ "name": "reward_tokens",
130
+ "outputs": [
131
+ {
132
+ "internalType": "address",
133
+ "name": "",
134
+ "type": "address"
135
+ }
136
+ ],
137
+ "stateMutability": "view",
138
+ "type": "function"
139
+ },
140
+ {
141
+ "inputs": [],
142
+ "name": "staking_token",
143
+ "outputs": [
144
+ {
145
+ "internalType": "address",
146
+ "name": "",
147
+ "type": "address"
148
+ }
149
+ ],
150
+ "stateMutability": "view",
151
+ "type": "function"
152
+ }
153
+ ]}
package/src/config.ts CHANGED
@@ -20,14 +20,18 @@ export const routerAddress: AddressDappNetworkMap = {
20
20
  [Network.POLYGON]: {
21
21
  [Dapp.SUSHISWAP]: "0x1b02dA8Cb0d097eB8D57A175b88c7D8b47997506",
22
22
  [Dapp.AAVE]: "0x8dFf5E27EA6b7AC08EbFdf9eB090F32ee9a30fcf",
23
+ [Dapp.AAVEV3]: "0x794a61358D6845594F94dc1DB02A252b5b4814aD",
23
24
  [Dapp.ONEINCH]: "0x1111111254fb6c44bac0bed2854e76f90643097d",
24
25
  [Dapp.QUICKSWAP]: "0xa5E0829CaCEd8fFDD4De3c43696c57F7D7A678ff",
25
26
  [Dapp.BALANCER]: "0xBA12222222228d8Ba445958a75a0704d566BF2C8",
26
- [Dapp.UNISWAPV3]: "0x68b3465833fb72A70ecDF485E0e4C7bD8665Fc45"
27
+ [Dapp.UNISWAPV3]: "0x68b3465833fb72A70ecDF485E0e4C7bD8665Fc45",
28
+ [Dapp.ARRAKIS]: "0xbc91a120cCD8F80b819EAF32F0996daC3Fa76a6C"
27
29
  },
28
30
  [Network.OPTIMISM]: {
29
31
  [Dapp.UNISWAPV3]: "0x68b3465833fb72A70ecDF485E0e4C7bD8665Fc45",
30
- [Dapp.SYNTHETIX]: "0x8700dAec35aF8Ff88c16BdF0418774CB3D7599B4"
32
+ [Dapp.SYNTHETIX]: "0x8700dAec35aF8Ff88c16BdF0418774CB3D7599B4",
33
+ [Dapp.AAVEV3]: "0x794a61358D6845594F94dc1DB02A252b5b4814aD",
34
+ [Dapp.ONEINCH]: "0x1111111254760F7ab3F16433eea9304126DCd199"
31
35
  }
32
36
  };
33
37
 
@@ -11,9 +11,12 @@ import IUniswapV2Router from "../abi/IUniswapV2Router.json";
11
11
  import INonfungiblePositionManager from "../abi/INonfungiblePositionManager.json";
12
12
  import IBalancerMerkleOrchard from "../abi/IBalancerMerkleOrchard.json";
13
13
  import IAaveIncentivesController from "../abi/IAaveIncentivesController.json";
14
+ import IArrakisV1RouterStaking from "../abi/IArrakisV1RouterStaking.json";
15
+ import ILiquidityGaugeV4 from "../abi/ILiquidityGaugeV4.json";
14
16
  import {
15
17
  deadline,
16
18
  MaxUint128,
19
+ networkChainIdMap,
17
20
  nonfungiblePositionManagerAddress,
18
21
  routerAddress,
19
22
  stakingAddress,
@@ -234,7 +237,8 @@ export class Pool {
234
237
  ): Promise<any> {
235
238
  let swapTxData: string;
236
239
  if (dapp === Dapp.ONEINCH) {
237
- const apiUrl = `https://api.1inch.exchange/v4.0/137/swap?fromTokenAddress=${assetFrom}&toTokenAddress=${assetTo}&amount=${amountIn.toString()}&fromAddress=${
240
+ const chainId = networkChainIdMap[this.network];
241
+ const apiUrl = `https://api.1inch.exchange/v4.0/${chainId}/swap?fromTokenAddress=${assetFrom}&toTokenAddress=${assetTo}&amount=${amountIn.toString()}&fromAddress=${
238
242
  this.address
239
243
  }&destReceiver=${
240
244
  this.address
@@ -770,99 +774,149 @@ export class Pool {
770
774
  }
771
775
 
772
776
  /**
773
- * Remove liquidity from an UniswapV3 liquidity pool
777
+ * Remove liquidity from an UniswapV3 or Arrakis liquidity pool
778
+ * @param {Dapp} dapp Platform either UniswapV3 or Arrakis
774
779
  * @param {string} tokenId Token Id of UniswapV3 position
775
780
  * @param {number} amount Amount in percent of assets to be removed
776
781
  * @param {any} options Transaction options
777
782
  * @returns {Promise<any>} Transaction
778
783
  */
779
- async removeLiquidityUniswapV3(
784
+ async decreaseLiquidity(
785
+ dapp: Dapp,
780
786
  tokenId: string,
781
787
  amount = 100,
782
788
  options: any = null
783
789
  ): Promise<any> {
784
- const iNonfungiblePositionManager = new ethers.utils.Interface(
785
- INonfungiblePositionManager.abi
786
- );
787
- const liquidity = (await getUniswapV3Liquidity(tokenId, this))
788
- .mul(amount)
789
- .div(100);
790
- const decreaseLiquidityTxData = iNonfungiblePositionManager.encodeFunctionData(
791
- Transaction.DECREASE_LIQUIDITY,
792
- [[tokenId, liquidity, 0, 0, deadline]]
793
- );
794
- const collectTxData = iNonfungiblePositionManager.encodeFunctionData(
795
- Transaction.COLLECT,
796
- [[tokenId, this.address, MaxUint128, MaxUint128]]
797
- );
790
+ let txData;
791
+ let dappAddress;
792
+ if (dapp === Dapp.UNISWAPV3) {
793
+ dappAddress = nonfungiblePositionManagerAddress[this.network];
794
+ const abi = new ethers.utils.Interface(INonfungiblePositionManager.abi);
795
+ const liquidity = (await getUniswapV3Liquidity(tokenId, this))
796
+ .mul(amount)
797
+ .div(100);
798
+ const decreaseLiquidityTxData = abi.encodeFunctionData(
799
+ Transaction.DECREASE_LIQUIDITY,
800
+ [[tokenId, liquidity, 0, 0, deadline]]
801
+ );
802
+ const collectTxData = abi.encodeFunctionData(Transaction.COLLECT, [
803
+ [tokenId, this.address, MaxUint128, MaxUint128]
804
+ ]);
798
805
 
799
- const multicallParams = [decreaseLiquidityTxData, collectTxData];
806
+ const multicallParams = [decreaseLiquidityTxData, collectTxData];
800
807
 
801
- if (amount === 100) {
802
- const burnTxData = iNonfungiblePositionManager.encodeFunctionData(
803
- Transaction.BURN,
804
- [tokenId]
805
- );
806
- multicallParams.push(burnTxData);
808
+ if (amount === 100) {
809
+ const burnTxData = abi.encodeFunctionData(Transaction.BURN, [tokenId]);
810
+ multicallParams.push(burnTxData);
811
+ }
812
+ txData = abi.encodeFunctionData(Transaction.MULTI_CALL, [
813
+ multicallParams
814
+ ]);
815
+ } else if (dapp === Dapp.ARRAKIS) {
816
+ dappAddress = routerAddress[this.network][dapp];
817
+ const abi = new ethers.utils.Interface(IArrakisV1RouterStaking.abi);
818
+ const liquidity = (await this.utils.getBalance(tokenId, this.address))
819
+ .mul(amount)
820
+ .div(100);
821
+ txData = abi.encodeFunctionData(Transaction.REMOVE_LIQUIDITY_UNSTAKE, [
822
+ tokenId,
823
+ liquidity,
824
+ 0,
825
+ 0,
826
+ this.address
827
+ ]);
828
+ } else {
829
+ throw new Error("dapp not supported");
807
830
  }
808
- const multicallTxData = iNonfungiblePositionManager.encodeFunctionData(
809
- Transaction.MULTI_CALL,
810
- [multicallParams]
811
- );
831
+
812
832
  const tx = await this.poolLogic.execTransaction(
813
- nonfungiblePositionManagerAddress[this.network],
814
- multicallTxData,
833
+ dappAddress,
834
+ txData,
815
835
  options
816
836
  );
817
837
  return tx;
818
838
  }
819
839
 
820
840
  /**
821
- * Increase liquidity of an UniswapV3 liquidity pool
841
+ * Increase liquidity of an UniswapV3 or Arrakis liquidity pool
842
+ * @param {Dapp} dapp Platform either UniswapV3 or Arrakis
822
843
  * @param {string} tokenId Token Id of UniswapV3 position
823
844
  * @param {BigNumber | string} amountA Amount first asset
824
845
  * @param {BigNumber | string} amountB Amount second asset
825
846
  * @param {any} options Transaction options
826
847
  * @returns {Promise<any>} Transaction
827
848
  */
828
- async increaseLiquidityUniswapV3(
849
+ async increaseLiquidity(
850
+ dapp: Dapp,
829
851
  tokenId: string,
830
852
  amountA: BigNumber | string,
831
853
  amountB: BigNumber | string,
832
854
  options: any = null
833
855
  ): Promise<any> {
834
- const iNonfungiblePositionManager = new ethers.utils.Interface(
835
- INonfungiblePositionManager.abi
836
- );
837
- const increaseLiquidityTxData = iNonfungiblePositionManager.encodeFunctionData(
838
- Transaction.INCREASE_LIQUIDITY,
839
- [[tokenId, amountA, amountB, 0, 0, deadline]]
840
- );
856
+ let txData;
857
+ let dappAddress;
858
+ if (dapp === Dapp.UNISWAPV3) {
859
+ dappAddress = nonfungiblePositionManagerAddress[this.network];
860
+ const abi = new ethers.utils.Interface(INonfungiblePositionManager.abi);
861
+ txData = abi.encodeFunctionData(Transaction.INCREASE_LIQUIDITY, [
862
+ [tokenId, amountA, amountB, 0, 0, deadline]
863
+ ]);
864
+ } else if (dapp === Dapp.ARRAKIS) {
865
+ dappAddress = routerAddress[this.network][dapp];
866
+ const abi = new ethers.utils.Interface(IArrakisV1RouterStaking.abi);
867
+ txData = abi.encodeFunctionData(Transaction.ADD_LIQUIDITY_STAKE, [
868
+ tokenId,
869
+ amountA,
870
+ amountB,
871
+ 0,
872
+ 0,
873
+ this.address
874
+ ]);
875
+ } else {
876
+ throw new Error("dapp not supported");
877
+ }
878
+
841
879
  const tx = await this.poolLogic.execTransaction(
842
- nonfungiblePositionManagerAddress[this.network],
843
- increaseLiquidityTxData,
880
+ dappAddress,
881
+ txData,
844
882
  options
845
883
  );
846
884
  return tx;
847
885
  }
848
886
 
849
887
  /**
850
- * Claim fees of an UniswapV3 liquidity pool
851
- * @param {string} tokenId Token Id of UniswapV3 position
888
+ * Claim fees of an UniswapV3 liquidity or Arrakis pool
889
+ * @param {Dapp} dapp Platform either UniswapV3 or Arrakis
890
+ * @param {string} tokenId Token Id of UniswapV3 or Arrakis position
852
891
  * @param {any} options Transaction options
853
892
  * @returns {Promise<any>} Transaction
854
893
  */
855
- async claimFeesUniswapV3(tokenId: string, options: any = null): Promise<any> {
856
- const iNonfungiblePositionManager = new ethers.utils.Interface(
857
- INonfungiblePositionManager.abi
858
- );
859
- const collectTxData = iNonfungiblePositionManager.encodeFunctionData(
860
- Transaction.COLLECT,
861
- [[tokenId, this.address, MaxUint128, MaxUint128]]
862
- );
894
+ async claimFees(
895
+ dapp: Dapp,
896
+ tokenId: string,
897
+ options: any = null
898
+ ): Promise<any> {
899
+ let txData;
900
+ let contractAddress;
901
+ if (dapp === Dapp.UNISWAPV3) {
902
+ contractAddress = nonfungiblePositionManagerAddress[this.network];
903
+ const iNonfungiblePositionManager = new ethers.utils.Interface(
904
+ INonfungiblePositionManager.abi
905
+ );
906
+ txData = iNonfungiblePositionManager.encodeFunctionData(
907
+ Transaction.COLLECT,
908
+ [[tokenId, this.address, MaxUint128, MaxUint128]]
909
+ );
910
+ } else if (dapp === Dapp.ARRAKIS) {
911
+ contractAddress = tokenId;
912
+ const abi = new ethers.utils.Interface(ILiquidityGaugeV4.abi);
913
+ txData = abi.encodeFunctionData("claim_rewards()", []);
914
+ } else {
915
+ throw new Error("dapp not supported");
916
+ }
863
917
  const tx = await this.poolLogic.execTransaction(
864
- nonfungiblePositionManagerAddress[this.network],
865
- collectTxData,
918
+ contractAddress,
919
+ txData,
866
920
  options
867
921
  );
868
922
  return tx;
@@ -1,6 +1,6 @@
1
- import { Dhedge, ethers } from "..";
2
- import { Network } from "../types";
3
- import { AMUSDC, TEST_POOL, VDEBTWETH } from "./constants";
1
+ import { Dhedge } from "..";
2
+ import { Dapp, Network } from "../types";
3
+ import { TEST_POOL, WETH } from "./constants";
4
4
 
5
5
  import { wallet } from "./wallet";
6
6
 
@@ -8,16 +8,32 @@ let dhedge: Dhedge;
8
8
 
9
9
  jest.setTimeout(100000);
10
10
 
11
- const options = {
12
- gasLimit: 5000000,
13
- gasPrice: ethers.utils.parseUnits("100", "gwei")
14
- };
11
+ // const options = {
12
+ // gasLimit: 5000000,
13
+ // gasPrice: ethers.utils.parseUnits("100", "gwei")
14
+ // };
15
15
 
16
16
  describe("pool", () => {
17
17
  beforeAll(() => {
18
- dhedge = new Dhedge(wallet, Network.POLYGON);
18
+ dhedge = new Dhedge(wallet, Network.OPTIMISM);
19
19
  });
20
20
 
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
+
21
37
  // it("withdraws 1 USDC from Aave lending pool", async () => {
22
38
  // let result;
23
39
  // const pool = await dhedge.loadPool(myPool);
@@ -35,11 +51,23 @@ describe("pool", () => {
35
51
  // expect(result).not.toBe(null);
36
52
  // });
37
53
 
38
- // it("borrows 0.0001 WETH from Aave lending pool", async () => {
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
+ });
65
+
66
+ // it("reapys 1USDC to Aave lending pool", async () => {
39
67
  // let result;
40
- // const pool = await dhedge.loadPool(myPool);
68
+ // const pool = await dhedge.loadPool(TEST_POOL);
41
69
  // try {
42
- // result = await pool.borrow(Dapp.AAVE, weth, "100000000000000");
70
+ // result = await pool.repay(Dapp.AAVEV3, USDC, "1380000");
43
71
  // console.log(result);
44
72
  // } catch (e) {
45
73
  // console.log(e);
@@ -47,11 +75,11 @@ describe("pool", () => {
47
75
  // expect(result).not.toBe(null);
48
76
  // });
49
77
 
50
- // it("reapys 0.0001 WETH to Aave lending pool", async () => {
78
+ // it("claims rewards from Aave", async () => {
51
79
  // let result;
52
- // const pool = await dhedge.loadPool(myPool);
80
+ // const pool = await dhedge.loadPool(TEST_POOL);
53
81
  // try {
54
- // result = await pool.repay(Dapp.AAVE, weth, "100000000000000", options);
82
+ // result = await pool.harvestAaveRewards([AMUSDC, VDEBTWETH], options);
55
83
  // console.log(result);
56
84
  // } catch (e) {
57
85
  // console.log(e);
@@ -59,15 +87,15 @@ describe("pool", () => {
59
87
  // expect(result).not.toBe(null);
60
88
  // });
61
89
 
62
- it("claims rewards from Aave", async () => {
63
- let result;
64
- const pool = await dhedge.loadPool(TEST_POOL);
65
- try {
66
- result = await pool.harvestAaveRewards([AMUSDC, VDEBTWETH], options);
67
- console.log(result);
68
- } catch (e) {
69
- console.log(e);
70
- }
71
- expect(result).not.toBe(null);
72
- });
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
+ // });
73
101
  });