@dhedge/v2-sdk 2.1.2 → 2.1.4

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": "2.1.2",
3
+ "version": "2.1.4",
4
4
  "license": "MIT",
5
5
  "description": "🛠 An SDK for building applications on top of dHEDGE V2",
6
6
  "main": "dist/index.js",
@@ -825,6 +825,32 @@
825
825
  "outputs": [],
826
826
  "stateMutability": "nonpayable",
827
827
  "type": "function"
828
+ },
829
+ {
830
+ "inputs": [
831
+ {
832
+ "internalType": "uint256",
833
+ "name": "_maxSupplyCapD18",
834
+ "type": "uint256"
835
+ }
836
+ ],
837
+ "name": "setMaxSupplyCap",
838
+ "outputs": [],
839
+ "stateMutability": "nonpayable",
840
+ "type": "function"
841
+ },
842
+ {
843
+ "inputs": [],
844
+ "name": "maxSupplyCap",
845
+ "outputs": [
846
+ {
847
+ "internalType": "uint256",
848
+ "name": "",
849
+ "type": "uint256"
850
+ }
851
+ ],
852
+ "stateMutability": "view",
853
+ "type": "function"
828
854
  }
829
855
  ]
830
- }
856
+ }
@@ -11,5 +11,18 @@
11
11
  ],
12
12
  "stateMutability": "view",
13
13
  "type": "function"
14
+ },
15
+ {
16
+ "inputs": [],
17
+ "name": "YT",
18
+ "outputs": [
19
+ {
20
+ "internalType": "address",
21
+ "name": "",
22
+ "type": "address"
23
+ }
24
+ ],
25
+ "stateMutability": "view",
26
+ "type": "function"
14
27
  }
15
28
  ]
@@ -1,6 +1,6 @@
1
1
  /* eslint-disable @typescript-eslint/explicit-module-boundary-types */
2
2
  /* eslint-disable @typescript-eslint/no-explicit-any */
3
- import { Contract, ethers, Wallet, BigNumber } from "ethers";
3
+ import { Contract, ethers, Wallet, BigNumber, BigNumberish } from "ethers";
4
4
 
5
5
  import IERC20 from "../abi/IERC20.json";
6
6
 
@@ -84,7 +84,7 @@ import {
84
84
  getPancakeUnStakeTxData
85
85
  } from "../services/pancake/staking";
86
86
  import { getOdosSwapTxData } from "../services/odos";
87
- import { getPendleSwapTxData } from "../services/pendle";
87
+ import { getPendleMintTxData, getPendleSwapTxData } from "../services/pendle";
88
88
  import { getCompleteWithdrawalTxData } from "../services/toros/completeWithdrawal";
89
89
  import { getKyberSwapTxData } from "../services/kyberSwap";
90
90
 
@@ -1025,6 +1025,31 @@ export class Pool {
1025
1025
  return tx;
1026
1026
  }
1027
1027
 
1028
+ /**
1029
+ * Sets max supply cap for a pool
1030
+ * @param {BigNumberish} _maxSupplyCapD18 Max supply cap with 18 decimals
1031
+ * @param {any} options Transaction options
1032
+ * @param {boolean} estimateGas Simulate/estimate gas
1033
+ * @returns {Promise<any>} Transaction
1034
+ */
1035
+ async setMaxCap(
1036
+ _maxSupplyCapD18: BigNumberish,
1037
+ options: any = null,
1038
+ estimateGas = false
1039
+ ): Promise<any> {
1040
+ if (estimateGas) {
1041
+ return await this.managerLogic.estimateGas.setMaxSupplyCap(
1042
+ _maxSupplyCapD18,
1043
+ options
1044
+ );
1045
+ }
1046
+ const tx = await this.managerLogic.setMaxSupplyCap(
1047
+ _maxSupplyCapD18,
1048
+ options
1049
+ );
1050
+ return tx;
1051
+ }
1052
+
1028
1053
  /**
1029
1054
  * Invest into a Balancer pool
1030
1055
  * @param {string} poolId Balancer pool id
@@ -2082,4 +2107,44 @@ export class Pool {
2082
2107
  );
2083
2108
  return tx;
2084
2109
  }
2110
+
2111
+ /**
2112
+ * Mint PT and YT tokens on Pendle
2113
+ * @param {string} assetFrom Asset to mint from (only underlying asset)
2114
+ * @param {string} pt PT address
2115
+ * @param {BigNumber | string} amountIn Amount underlying asset
2116
+ * @param {number} slippage Slippage tolerance in %
2117
+ * @param {any} options Transaction options
2118
+ * @param {SDKOptions} sdkOptions SDK options including estimateGas
2119
+ * @returns {Promise<any>} Transaction
2120
+ */
2121
+ async mintPendle(
2122
+ assetFrom: string,
2123
+ pt: string,
2124
+ amountIn: BigNumber | string,
2125
+ slippage = 0.5,
2126
+ options: any = null,
2127
+ sdkOptions: SDKOptions = {
2128
+ estimateGas: false
2129
+ }
2130
+ ): Promise<any> {
2131
+ const { swapTxData, minAmountOut } = await getPendleMintTxData(
2132
+ this,
2133
+ assetFrom,
2134
+ pt,
2135
+ amountIn,
2136
+ slippage
2137
+ );
2138
+ const tx = await getPoolTxOrGasEstimate(
2139
+ this,
2140
+ [
2141
+ routerAddress[this.network][Dapp.PENDLE],
2142
+ swapTxData,
2143
+ options,
2144
+ minAmountOut
2145
+ ],
2146
+ sdkOptions
2147
+ );
2148
+ return tx;
2149
+ }
2085
2150
  }
@@ -56,6 +56,39 @@ export async function getPendleSwapTxData(
56
56
  }
57
57
  }
58
58
 
59
+ export async function getPendleMintTxData(
60
+ pool: Pool,
61
+ tokenIn: string,
62
+ pt: string,
63
+ amountIn: ethers.BigNumber | string,
64
+ slippage: number
65
+ ): Promise<{ swapTxData: string; minAmountOut: string | null }> {
66
+ const PTcontract = new ethers.Contract(pt, PTAbi, pool.signer);
67
+ const ytAddress = await PTcontract.YT();
68
+ const params = {
69
+ receiver: pool.address,
70
+ tokensIn: tokenIn,
71
+ tokensOut: `${pt},${ytAddress}`,
72
+ amountsIn: amountIn.toString(),
73
+ slippage: slippage / 100
74
+ };
75
+ try {
76
+ const swapResult = await axios.get(
77
+ `${pendleBaseUrl}/v2/sdk/${networkChainIdMap[pool.network]}/convert`,
78
+ { params }
79
+ );
80
+ return {
81
+ swapTxData: swapResult.data.routes[0].tx.data,
82
+ minAmountOut: swapResult.data.routes[0].outputs.filter(
83
+ (e: { token: string }) => e.token === pt.toLowerCase()
84
+ )[0].amount
85
+ };
86
+ } catch (e) {
87
+ console.error("Error in Pendle API request:", e);
88
+ throw new ApiError("Pendle api request failed");
89
+ }
90
+ }
91
+
59
92
  const checkUnderlying = (market: any, token: string, networkId: number) => {
60
93
  if (market.underlyingAsset !== `${networkId}-${token.toLocaleLowerCase()}`) {
61
94
  throw new Error("Can only trade in or out of the underlying asset");
@@ -0,0 +1,59 @@
1
+ /* eslint-disable @typescript-eslint/no-non-null-assertion */
2
+
3
+ import { Dhedge, Pool } from "..";
4
+
5
+ import { Network } from "../types";
6
+ import { CONTRACT_ADDRESS } from "./constants";
7
+ import { getTxOptions } from "./txOptions";
8
+ import { TestingRunParams, testingHelper } from "./utils/testingHelper";
9
+
10
+ const testPendle = ({ wallet, network }: TestingRunParams) => {
11
+ const USDE = CONTRACT_ADDRESS[network].USDE;
12
+ const PTJan26Usde = "0x93b544c330f60a2aa05ced87aeeffb8d38fd8c9a";
13
+
14
+ let dhedge: Dhedge;
15
+ let pool: Pool;
16
+ jest.setTimeout(100000);
17
+
18
+ describe(`pool on ${network}`, () => {
19
+ beforeAll(async () => {
20
+ dhedge = new Dhedge(wallet, network);
21
+ pool = await dhedge.loadPool(
22
+ "0xdad21646ebb0997eb59de1f6a68a67059daf4c31"
23
+ );
24
+ });
25
+
26
+ it("can get TX Data for mint PT and SY", async () => {
27
+ const usdeBalance = await pool.utils.getBalance(USDE, pool.address);
28
+ const { txData, minAmountOut } = await pool.mintPendle(
29
+ USDE,
30
+ PTJan26Usde,
31
+ usdeBalance,
32
+ 0.5,
33
+ null,
34
+ { onlyGetTxData: true, estimateGas: true }
35
+ );
36
+ expect(txData).not.toBeNull();
37
+ expect(minAmountOut).not.toBeNull();
38
+ });
39
+
40
+ it("can get for mint PT and SY", async () => {
41
+ const usdeBalance = await pool.utils.getBalance(USDE, pool.address);
42
+ await pool.mintPendle(
43
+ USDE,
44
+ PTJan26Usde,
45
+ usdeBalance,
46
+ 0.5,
47
+ await getTxOptions(network)
48
+ );
49
+ const ptBalance = await pool.utils.getBalance(PTJan26Usde, pool.address);
50
+ expect(ptBalance.gt(0)).toBe(true);
51
+ });
52
+ });
53
+ };
54
+
55
+ testingHelper({
56
+ network: Network.PLASMA,
57
+ onFork: false,
58
+ testingRun: testPendle
59
+ });
@@ -1,16 +1,15 @@
1
+ import { BigNumber } from "ethers";
1
2
  import { Dhedge, Pool } from "..";
2
3
  import { Network } from "../types";
3
- import { CONTRACT_ADDRESS, TEST_POOL } from "./constants";
4
+ import { TEST_POOL } from "./constants";
4
5
 
5
6
  import { testingHelper, TestingRunParams } from "./utils/testingHelper";
6
- import { balanceDelta } from "./utils/token";
7
+ // import { balanceDelta } from "./utils/token";
7
8
 
8
- const testPool = ({ wallet, network }: TestingRunParams) => {
9
+ const testPool = ({ wallet, network, provider }: TestingRunParams) => {
9
10
  let dhedge: Dhedge;
10
11
  let pool: Pool;
11
12
 
12
- const USDT = CONTRACT_ADDRESS[network].USDT;
13
-
14
13
  jest.setTimeout(200000);
15
14
 
16
15
  describe(`pool on ${network}`, () => {
@@ -18,18 +17,31 @@ const testPool = ({ wallet, network }: TestingRunParams) => {
18
17
  dhedge = new Dhedge(wallet, network);
19
18
  pool = await dhedge.loadPool(TEST_POOL[network]);
20
19
 
21
- // await provider.send("hardhat_setBalance", [
22
- // wallet.address,
23
- // "0x10000000000000000"
24
- // ]);
20
+ await provider.send("hardhat_setBalance", [
21
+ wallet.address,
22
+ "0x10000000000000000"
23
+ ]);
25
24
  });
26
25
 
27
26
  it("checks fund composition", async () => {
28
27
  const result = await pool.getComposition();
29
- console.log(result);
28
+ // console.log(result);
30
29
  expect(result.length).toBeGreaterThan(0);
31
30
  });
32
31
 
32
+ it("sets max supply cap", async () => {
33
+ const totalSupply: BigNumber = await pool.poolLogic.totalSupply();
34
+ let initCap = totalSupply;
35
+ if (totalSupply.eq(0)) {
36
+ initCap = BigNumber.from(1000).mul(BigNumber.from(10).pow(18));
37
+ }
38
+ await pool.setMaxCap(initCap.mul(2), null, true);
39
+ const tx = await pool.setMaxCap(initCap.mul(2));
40
+ await tx.wait(1);
41
+ const maxCapAfter: BigNumber = await pool.managerLogic.maxSupplyCap();
42
+ expect(maxCapAfter).toEqual(initCap.mul(2));
43
+ });
44
+
33
45
  // it("sets pool private", async () => {
34
46
  // const result = await pool.setPrivate(true);
35
47
  // expect(result).not.toBeNull();
@@ -65,18 +77,18 @@ const testPool = ({ wallet, network }: TestingRunParams) => {
65
77
  // expect(usdtAllowanceDelta.gt(0));
66
78
  // });
67
79
 
68
- it("deposits 200 USDT into Pool", async () => {
69
- await pool.deposit(
70
- CONTRACT_ADDRESS[network].USDT,
71
- (200000000).toString()
72
- );
73
- const poolTokenDelta = await balanceDelta(
74
- pool.address,
75
- USDT,
76
- pool.signer
77
- );
78
- expect(poolTokenDelta.gt(0));
79
- });
80
+ // it("deposits 200 USDT into Pool", async () => {
81
+ // await pool.deposit(
82
+ // CONTRACT_ADDRESS[network].USDT,
83
+ // (200000000).toString()
84
+ // );
85
+ // const poolTokenDelta = await balanceDelta(
86
+ // pool.address,
87
+ // USDT,
88
+ // pool.signer
89
+ // );
90
+ // expect(poolTokenDelta.gt(0));
91
+ // });
80
92
 
81
93
  // it("get available Manager Fee", async () => {
82
94
  // const result = await pool.getAvailableManagerFee();
@@ -108,7 +120,6 @@ const testPool = ({ wallet, network }: TestingRunParams) => {
108
120
  // });
109
121
 
110
122
  testingHelper({
111
- network: Network.PLASMA,
112
- onFork: false,
123
+ network: Network.ARBITRUM,
113
124
  testingRun: testPool
114
125
  });