@dhedge/v2-sdk 2.0.0 → 2.0.2

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.
@@ -0,0 +1,70 @@
1
+ import axios from "axios";
2
+ import BigNumber from "bignumber.js";
3
+ import { odosBaseUrl } from "../odos";
4
+
5
+ export const SWAPPER_ADDERSS = "0x4F754e0F0924afD74980886b0B479Fa1D7C58D0D";
6
+
7
+ export interface SwapParams {
8
+ srcAsset: string; // Source asset address
9
+ srcAmount: string; // Amount to swap (use string for big numbers)
10
+ dstAsset: string; // Destination asset address
11
+ chainId: number; // Chain ID
12
+ from: string; // Sender address
13
+ receiver: string; // Receiver address
14
+ slippage: number; // Slippage tolerance in basis points (100 = 1%)
15
+ }
16
+
17
+ export const getSwapDataViaOdos = async ({
18
+ srcAsset,
19
+ srcAmount,
20
+ dstAsset,
21
+ chainId,
22
+ from,
23
+ slippage
24
+ }: SwapParams): Promise<string> => {
25
+ let referralCode = 0; // Defaults to 0 for unregistered activity.
26
+ if (
27
+ process.env.ODOS_REFERAL_CODE &&
28
+ Number(process.env.ODOS_REFERAL_CODE) > 0
29
+ ) {
30
+ referralCode = Number(process.env.ODOS_REFERAL_CODE);
31
+ }
32
+ const quoteParams = {
33
+ chainId: chainId,
34
+ inputTokens: [
35
+ {
36
+ tokenAddress: srcAsset,
37
+ amount: srcAmount
38
+ }
39
+ ],
40
+ outputTokens: [
41
+ {
42
+ tokenAddress: dstAsset,
43
+ proportion: 1
44
+ }
45
+ ],
46
+ slippageLimitPercent: new BigNumber(slippage).div(100).toString(), // Convert basis points to percentage
47
+ userAddr: from,
48
+ referralCode
49
+ };
50
+ try {
51
+ const quoteResult = await axios.post(
52
+ `${odosBaseUrl}/quote/v2`,
53
+ quoteParams
54
+ );
55
+
56
+ const assembleParams = {
57
+ pathId: quoteResult.data.pathId,
58
+ userAddr: from
59
+ };
60
+
61
+ const assembleResult = await axios.post(
62
+ `${odosBaseUrl}/assemble`,
63
+ assembleParams
64
+ );
65
+ return assembleResult.data.transaction.data;
66
+ } catch (e) {
67
+ console.error("Error in Odos API request:", e);
68
+ throw new Error("Swap api request of Odos failed");
69
+ }
70
+ };
@@ -89,7 +89,7 @@ export const CONTRACT_ADDRESS = {
89
89
  VELO: "0x9560e827aF36c94D2Ac33a39bCE1Fe78631088Db",
90
90
  COMPOUNDV3_WETH: "",
91
91
  FLUID_WETH: "",
92
- TOROS: "0x49bf093277bf4dde49c48c6aa55a3bda3eedef68" //USDmny
92
+ TOROS: "0xcacb5a722a36cff6baeb359e21c098a4acbffdfa" //ETHBEAR1X
93
93
  },
94
94
  [Network.ARBITRUM]: {
95
95
  USDC: "0xaf88d065e77c8cC2239327C5EDb3A432268e5831",
@@ -1,14 +1,8 @@
1
- import { BigNumber } from "ethers";
2
- import { Dhedge, ethers, Pool } from "..";
3
- import { AssetEnabled, Network } from "../types";
4
- import { CONTRACT_ADDRESS, TEST_POOL } from "./constants";
5
-
6
- import {
7
- beforeAfterReset,
8
- testingHelper,
9
- TestingRunParams
10
- } from "./utils/testingHelper";
11
- import { allowanceDelta, balanceDelta } from "./utils/token";
1
+ import { Dhedge, Pool } from "..";
2
+ import { Network } from "../types";
3
+ import { TEST_POOL } from "./constants";
4
+
5
+ import { testingHelper, TestingRunParams } from "./utils/testingHelper";
12
6
 
13
7
  const testPool = ({ wallet, network, provider }: TestingRunParams) => {
14
8
  let dhedge: Dhedge;
@@ -20,85 +14,94 @@ const testPool = ({ wallet, network, provider }: TestingRunParams) => {
20
14
  beforeAll(async () => {
21
15
  dhedge = new Dhedge(wallet, network);
22
16
  pool = await dhedge.loadPool(TEST_POOL[network]);
17
+
18
+ await provider.send("hardhat_setBalance", [
19
+ wallet.address,
20
+ "0x10000000000000000"
21
+ ]);
23
22
  });
24
- beforeAfterReset({ beforeAll, afterAll, provider });
25
23
 
26
24
  it("checks fund composition", async () => {
27
25
  const result = await pool.getComposition();
28
26
  expect(result.length).toBeGreaterThan(0);
29
27
  });
30
28
 
31
- it("approves USDC balance of User for Deposit", async () => {
32
- await pool.approveDeposit(
33
- CONTRACT_ADDRESS[network].USDC,
34
- ethers.constants.MaxUint256
35
- );
36
- const UsdcAllowanceDelta = await allowanceDelta(
37
- pool.signer.address,
38
- CONTRACT_ADDRESS[network].USDC,
39
- pool.address,
40
- pool.signer
41
- );
42
- expect(UsdcAllowanceDelta.gt(0));
29
+ it("sets pool private", async () => {
30
+ const result = await pool.setPrivate(true);
31
+ expect(result).not.toBeNull();
43
32
  });
44
33
 
45
- it("deposits 1 USDC into Pool", async () => {
46
- await pool.deposit(CONTRACT_ADDRESS[network].USDC, (1e6).toString());
47
- const poolTokenDelta = await balanceDelta(
48
- pool.signer.address,
49
- pool.address,
50
- pool.signer
51
- );
52
- expect(poolTokenDelta.gt(0));
53
- });
34
+ // it("approves USDC balance of User for Deposit", async () => {
35
+ // await pool.approveDeposit(
36
+ // CONTRACT_ADDRESS[network].USDC,
37
+ // ethers.constants.MaxUint256
38
+ // );
39
+ // const UsdcAllowanceDelta = await allowanceDelta(
40
+ // pool.signer.address,
41
+ // CONTRACT_ADDRESS[network].USDC,
42
+ // pool.address,
43
+ // pool.signer
44
+ // );
45
+ // expect(UsdcAllowanceDelta.gt(0));
46
+ // });
54
47
 
55
- it("adds WBTC to enabled assets", async () => {
56
- const assetsBefore = await pool.getComposition();
57
-
58
- const newAssets: AssetEnabled[] = [
59
- { asset: CONTRACT_ADDRESS[network].USDC, isDeposit: true },
60
- { asset: CONTRACT_ADDRESS[network].WETH, isDeposit: false },
61
- { asset: CONTRACT_ADDRESS[network].WBTC, isDeposit: false }
62
- ];
63
- await pool.changeAssets(newAssets);
64
- const assetsAfter = await pool.getComposition();
65
- if (assetsBefore.length < newAssets.length) {
66
- expect(assetsAfter.length).toBeGreaterThan(assetsBefore.length);
67
- } else {
68
- expect(assetsAfter.length).toBeLessThanOrEqual(assetsBefore.length);
69
- }
70
- });
48
+ // it("deposits 1 USDC into Pool", async () => {
49
+ // await pool.deposit(CONTRACT_ADDRESS[network].USDC, (1e6).toString());
50
+ // const poolTokenDelta = await balanceDelta(
51
+ // pool.signer.address,
52
+ // pool.address,
53
+ // pool.signer
54
+ // );
55
+ // expect(poolTokenDelta.gt(0));
56
+ // });
71
57
 
72
- it("get available Manager Fee", async () => {
73
- const result = await pool.getAvailableManagerFee();
74
- expect(result).toBeInstanceOf(BigNumber);
75
- });
58
+ // it("adds WBTC to enabled assets", async () => {
59
+ // const assetsBefore = await pool.getComposition();
76
60
 
77
- it("mintManagerFee; should not revert", async () => {
78
- const tx = await pool.mintManagerFee();
79
- expect(tx).toHaveProperty("wait");
80
- });
61
+ // const newAssets: AssetEnabled[] = [
62
+ // { asset: CONTRACT_ADDRESS[network].USDC, isDeposit: true },
63
+ // { asset: CONTRACT_ADDRESS[network].WETH, isDeposit: false },
64
+ // { asset: CONTRACT_ADDRESS[network].WBTC, isDeposit: false }
65
+ // ];
66
+ // await pool.changeAssets(newAssets);
67
+ // const assetsAfter = await pool.getComposition();
68
+ // if (assetsBefore.length < newAssets.length) {
69
+ // expect(assetsAfter.length).toBeGreaterThan(assetsBefore.length);
70
+ // } else {
71
+ // expect(assetsAfter.length).toBeLessThanOrEqual(assetsBefore.length);
72
+ // }
73
+ // });
81
74
 
82
- it("withdraw 0.1 pool token into Pool", async () => {
83
- await provider.send("evm_increaseTime", [24 * 60 * 60]);
84
- await provider.send("evm_mine", []);
85
- await pool.withdraw((0.1 * 1e18).toString());
86
- const poolTokenDelta = await balanceDelta(
87
- pool.signer.address,
88
- pool.address,
89
- pool.signer
90
- );
91
- expect(poolTokenDelta.lt(0));
92
- });
75
+ // it("get available Manager Fee", async () => {
76
+ // const result = await pool.getAvailableManagerFee();
77
+ // expect(result).toBeInstanceOf(BigNumber);
78
+ // });
79
+
80
+ // it("mintManagerFee; should not revert", async () => {
81
+ // const tx = await pool.mintManagerFee();
82
+ // expect(tx).toHaveProperty("wait");
83
+ // });
84
+
85
+ // it("withdraw 0.1 pool token into Pool", async () => {
86
+ // await provider.send("evm_increaseTime", [24 * 60 * 60]);
87
+ // await provider.send("evm_mine", []);
88
+ // await pool.withdraw((0.1 * 1e18).toString());
89
+ // const poolTokenDelta = await balanceDelta(
90
+ // pool.signer.address,
91
+ // pool.address,
92
+ // pool.signer
93
+ // );
94
+ // expect(poolTokenDelta.lt(0));
95
+ // });
93
96
  });
94
97
  };
95
98
 
96
- testingHelper({
97
- network: Network.POLYGON,
98
- testingRun: testPool
99
- });
100
-
101
99
  // testingHelper({
102
- // network: Network.OPTIMISM,
100
+ // network: Network.POLYGON,
103
101
  // testingRun: testPool
104
102
  // });
103
+
104
+ testingHelper({
105
+ network: Network.OPTIMISM,
106
+ testingRun: testPool
107
+ });
@@ -1,18 +1,19 @@
1
1
  /* eslint-disable @typescript-eslint/no-non-null-assertion */
2
2
 
3
+ import BigNumber from "bignumber.js";
3
4
  import { Dhedge, Pool } from "..";
4
5
  import { routerAddress } from "../config";
5
6
 
6
7
  import { Dapp, Network } from "../types";
7
8
  import { CONTRACT_ADDRESS, MAX_AMOUNT, TEST_POOL } from "./constants";
8
9
  import {
9
- TestingRunParams,
10
10
  setChainlinkTimeout,
11
11
  setUSDCAmount,
12
- testingHelper
12
+ testingHelper,
13
+ TestingRunParams
13
14
  } from "./utils/testingHelper";
15
+
14
16
  import { allowanceDelta, balanceDelta } from "./utils/token";
15
- import BigNumber from "bignumber.js";
16
17
 
17
18
  const testToros = ({ wallet, network, provider }: TestingRunParams) => {
18
19
  const USDC = CONTRACT_ADDRESS[network].USDC;
@@ -34,7 +35,7 @@ const testToros = ({ wallet, network, provider }: TestingRunParams) => {
34
35
  ]);
35
36
  await provider.send("evm_mine", []);
36
37
  // top up USDC
37
- const amount = new BigNumber(1000).times(1e6).toFixed(0);
38
+ const amount = new BigNumber(100).times(1e6).toFixed(0);
38
39
  await setUSDCAmount({
39
40
  amount,
40
41
  userAddress: pool.address,
@@ -56,7 +57,7 @@ const testToros = ({ wallet, network, provider }: TestingRunParams) => {
56
57
 
57
58
  it("trades USDC balance into Toros Token", async () => {
58
59
  const usdcBalance = await pool.utils.getBalance(USDC, pool.address);
59
- await pool.trade(Dapp.TOROS, USDC, TOROS, usdcBalance, 1);
60
+ await pool.trade(Dapp.TOROS, USDC, TOROS, usdcBalance, 1.5);
60
61
  const torosBalanceDelta = await balanceDelta(
61
62
  pool.address,
62
63
  TOROS,
@@ -70,7 +71,7 @@ const testToros = ({ wallet, network, provider }: TestingRunParams) => {
70
71
  await provider.send("evm_mine", []);
71
72
  const torosBalance = await pool.utils.getBalance(TOROS, pool.address);
72
73
  await pool.approve(Dapp.TOROS, TOROS, MAX_AMOUNT);
73
- await pool.trade(Dapp.TOROS, TOROS, USDC, torosBalance, 1);
74
+ await pool.trade(Dapp.TOROS, TOROS, USDC, torosBalance, 1.5);
74
75
  const torosBalanceDelta = await balanceDelta(
75
76
  pool.address,
76
77
  TOROS,
@@ -80,7 +81,7 @@ const testToros = ({ wallet, network, provider }: TestingRunParams) => {
80
81
  });
81
82
 
82
83
  it("complete withdrawal from Toros asset", async () => {
83
- await pool.completeTorosWithdrawal(USDC, 1);
84
+ await pool.completeTorosWithdrawal(USDC, 1.5);
84
85
  const usdcBalanceDelta = await balanceDelta(
85
86
  pool.address,
86
87
  USDC,
@@ -93,5 +94,6 @@ const testToros = ({ wallet, network, provider }: TestingRunParams) => {
93
94
 
94
95
  testingHelper({
95
96
  network: Network.OPTIMISM,
96
- testingRun: testToros
97
+ testingRun: testToros,
98
+ onFork: true
97
99
  });
package/src/types.ts CHANGED
@@ -101,3 +101,11 @@ export type LyraPosition = {
101
101
  collateral: BigNumber;
102
102
  state: number;
103
103
  };
104
+
105
+ export type SDKOptions =
106
+ | {
107
+ estimateGas: boolean;
108
+ onlyGetTxData?: boolean;
109
+ useTraderAddressAsFrom?: boolean;
110
+ }
111
+ | boolean; // shorthand for { estimateGas: true/false }; for backward compatibility
@@ -3,7 +3,7 @@
3
3
  import set from "lodash/set";
4
4
  import { Interface } from "@ethersproject/abi";
5
5
  import { multiCallAddress } from "../config";
6
- import { ethers, Network, Pool } from "..";
6
+ import { ethers, Network, Pool, SDKOptions } from "..";
7
7
 
8
8
  export async function call(
9
9
  provider: ethers.Signer,
@@ -94,56 +94,84 @@ export class Multicaller {
94
94
  }
95
95
  }
96
96
 
97
+ const getGasEstimateData = async (
98
+ estimateFunc: ethers.ContractFunction<ethers.BigNumber>,
99
+ txInfoData: { to: string; txData: string; minAmountOut: any },
100
+ txOptions: any = {}
101
+ ) => {
102
+ let gas = null;
103
+ let gasEstimationError = null;
104
+ try {
105
+ gas = await estimateFunc(txInfoData.to, txInfoData.txData, txOptions);
106
+ } catch (e) {
107
+ gasEstimationError = e;
108
+ }
109
+ return {
110
+ gas,
111
+ gasEstimationError,
112
+ ...txInfoData
113
+ };
114
+ };
115
+
116
+ export const isSdkOptionsBoolean = (
117
+ sdkOptions: SDKOptions
118
+ ): sdkOptions is boolean => {
119
+ return typeof sdkOptions === "boolean";
120
+ };
121
+
97
122
  export const getPoolTxOrGasEstimate = async (
98
123
  pool: Pool,
99
124
  args: any[],
100
- estimateGas: boolean
125
+ sdkOptions: SDKOptions
101
126
  ): Promise<any> => {
102
- if (pool.isDhedge) {
103
- if (estimateGas) {
104
- let gas = null;
105
- let gasEstimationError = null;
106
- try {
107
- gas = await pool.poolLogic.estimateGas.execTransaction(
108
- args[0],
109
- args[1],
110
- args[2]
111
- );
112
- } catch (e) {
113
- gasEstimationError = e;
114
- }
115
- return {
116
- gas,
117
- txData: args[1],
118
- to: args[0],
119
- gasEstimationError,
120
- minAmountOut: args[3] || null
121
- };
127
+ const txInfoData = {
128
+ txData: args[1],
129
+ to: args[0],
130
+ minAmountOut: args[3] || null
131
+ };
132
+
133
+ if (!isSdkOptionsBoolean(sdkOptions) && sdkOptions.onlyGetTxData) {
134
+ return txInfoData;
135
+ }
136
+
137
+ const txOptions = args[2];
138
+
139
+ if (
140
+ !pool.isDhedge ||
141
+ (!isSdkOptionsBoolean(sdkOptions) && sdkOptions.useTraderAddressAsFrom)
142
+ ) {
143
+ if (
144
+ sdkOptions === true ||
145
+ (!isSdkOptionsBoolean(sdkOptions) && sdkOptions.estimateGas)
146
+ ) {
147
+ return await getGasEstimateData(
148
+ pool.signer.estimateGas,
149
+ txInfoData,
150
+ txOptions
151
+ );
122
152
  } else {
123
- return await pool.poolLogic.execTransaction(args[0], args[1], args[2]);
153
+ return await pool.signer.sendTransaction({
154
+ to: txInfoData.to,
155
+ data: txInfoData.txData,
156
+ ...txOptions
157
+ });
124
158
  }
125
159
  } else {
126
- if (estimateGas) {
127
- let gas = null;
128
- let gasEstimationError = null;
129
- try {
130
- gas = await pool.signer.estimateGas({ to: args[0], data: args[1] });
131
- } catch (e) {
132
- gasEstimationError = e;
133
- }
134
- return {
135
- gas,
136
- txData: args[1],
137
- to: args[0],
138
- gasEstimationError,
139
- minAmountOut: args[3] || null
140
- };
160
+ if (
161
+ sdkOptions === true ||
162
+ (!isSdkOptionsBoolean(sdkOptions) && sdkOptions.estimateGas)
163
+ ) {
164
+ return await getGasEstimateData(
165
+ pool.poolLogic.estimateGas.execTransaction,
166
+ txInfoData,
167
+ txOptions
168
+ );
141
169
  } else {
142
- return await pool.signer.sendTransaction({
143
- to: args[0],
144
- data: args[1],
145
- ...args[2]
146
- });
170
+ return await pool.poolLogic.execTransaction(
171
+ txInfoData.to,
172
+ txInfoData.txData,
173
+ txOptions
174
+ );
147
175
  }
148
176
  }
149
177
  };