@dhedge/v2-sdk 1.9.1 → 1.9.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dhedge/v2-sdk",
3
- "version": "1.9.1",
3
+ "version": "1.9.2",
4
4
  "license": "MIT",
5
5
  "description": "🛠 An SDK for building applications on top of dHEDGE V2",
6
6
  "main": "dist/index.js",
@@ -20,9 +20,9 @@
20
20
  "prepare": "tsdx build",
21
21
  "size": "size-limit",
22
22
  "analyze": "size-limit --why",
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)",
25
- "fork:arbitrum": "hardhat node --fork $(grep ARBITRUM_URL .env | cut -d '=' -f2)"
23
+ "fork:polygon": "hardhat node --port 8542 --fork $(grep POLYGON_URL .env | cut -d '=' -f2)",
24
+ "fork:optimism": "hardhat node --port 8544 --fork $(grep OPTIMISM_URL .env | cut -d '=' -f2)",
25
+ "fork:arbitrum": "hardhat node --port 8540 --fork $(grep ARBITRUM_URL .env | cut -d '=' -f2)"
26
26
  },
27
27
  "husky": {
28
28
  "hooks": {
@@ -1,6 +1,5 @@
1
1
  /* eslint-disable @typescript-eslint/explicit-module-boundary-types */
2
2
  /* eslint-disable @typescript-eslint/no-explicit-any */
3
- import axios from "axios";
4
3
  import { Contract, ethers, Wallet, BigNumber } from "ethers";
5
4
 
6
5
  import IERC20 from "../abi/IERC20.json";
@@ -16,7 +15,6 @@ import IBalancerRewardsGauge from "../abi/IBalancerRewardsGauge.json";
16
15
 
17
16
  import {
18
17
  MaxUint128,
19
- networkChainIdMap,
20
18
  nonfungiblePositionManagerAddress,
21
19
  routerAddress,
22
20
  stakingAddress,
@@ -42,7 +40,6 @@ import {
42
40
  import { FeeAmount } from "@uniswap/v3-sdk";
43
41
  import { getUniswapV3SwapTxData } from "../services/uniswap/V3Trade";
44
42
  import { getEasySwapperTxData } from "../services/toros/easySwapper";
45
- import { getOneInchProtocols } from "../services/oneInch/protocols";
46
43
  import { getAaveV3ClaimTxData } from "../services/aave/incentives";
47
44
  import {
48
45
  getVelodromeAddLiquidityTxData,
@@ -61,7 +58,7 @@ import {
61
58
  } from "../services/futures";
62
59
  import { getFuturesCancelOrderTxData } from "../services/futures/trade";
63
60
  import { getZeroExTradeTxData } from "../services/zeroEx/zeroExTrade";
64
- import { ApiError } from "../errors";
61
+ import { getOneInchSwapTxData } from "../services/oneInch";
65
62
 
66
63
  export class Pool {
67
64
  public readonly poolLogic: Contract;
@@ -299,24 +296,13 @@ export class Pool {
299
296
  );
300
297
  break;
301
298
  case Dapp.ONEINCH:
302
- const chainId = networkChainIdMap[this.network];
303
- const protocols = await getOneInchProtocols(chainId);
304
- if (!process.env.ONEINCH_API_URL)
305
- throw new Error("ONEINCH_API_URL not configured in .env file");
306
- const apiUrl = `${
307
- process.env.ONEINCH_API_URL
308
- }/${chainId}/swap?fromTokenAddress=${assetFrom}&toTokenAddress=${assetTo}&amount=${amountIn.toString()}&fromAddress=${
309
- this.address
310
- }&destReceiver=${
311
- this.address
312
- }&slippage=${slippage.toString()}&disableEstimate=true${protocols}`;
313
- try {
314
- const response = await axios.get(apiUrl);
315
- swapTxData = response.data.tx.data;
316
- } catch (e) {
317
- throw new ApiError("Swap api request of 1inch failed");
318
- }
319
-
299
+ swapTxData = await getOneInchSwapTxData(
300
+ this,
301
+ assetFrom,
302
+ assetTo,
303
+ amountIn,
304
+ slippage
305
+ );
320
306
  break;
321
307
  case Dapp.BALANCER:
322
308
  swapTxData = await this.utils.getBalancerSwapTx(
@@ -0,0 +1,33 @@
1
+ /* eslint-disable @typescript-eslint/no-explicit-any */
2
+ import axios from "axios";
3
+ import { ApiError, ethers } from "../..";
4
+ import { networkChainIdMap } from "../../config";
5
+ import { Pool } from "../../entities";
6
+
7
+ const oneInchBaseUrl = "https://api.1inch.dev/swap/v5.2/";
8
+
9
+ export async function getOneInchSwapTxData(
10
+ pool: Pool,
11
+ assetFrom: string,
12
+ assetTo: string,
13
+ amountIn: ethers.BigNumber | string,
14
+ slippage: number
15
+ ): Promise<string> {
16
+ if (!process.env.ONEINCH_API_KEY)
17
+ throw new Error("ONEINCH_API_KEY not configured in .env file");
18
+
19
+ const chainId = networkChainIdMap[pool.network];
20
+ const apiUrl = `${oneInchBaseUrl}${chainId}/swap?src=${assetFrom}&dst=${assetTo}&amount=${amountIn.toString()}&from=${
21
+ pool.address
22
+ }&slippage=${slippage.toString()}&disableEstimate=true`;
23
+ try {
24
+ const response = await axios.get(apiUrl, {
25
+ headers: {
26
+ Authorization: `Bearer ${process.env.ONEINCH_API_KEY}`
27
+ }
28
+ });
29
+ return response.data.tx.data;
30
+ } catch (e) {
31
+ throw new ApiError("Swap api request of 1inch failed");
32
+ }
33
+ }
@@ -3,44 +3,52 @@ import { Dhedge, Pool } from "..";
3
3
  import { routerAddress } from "../config";
4
4
  import { Dapp, Network } from "../types";
5
5
  import { CONTRACT_ADDRESS, MAX_AMOUNT, TEST_POOL } from "./constants";
6
+ import { TestingRunParams, testingHelper } from "./utils/testingHelper";
6
7
  import { allowanceDelta, balanceDelta } from "./utils/token";
7
8
 
8
- import { wallet } from "./wallet";
9
+ const testOneInch = ({ wallet, network }: TestingRunParams) => {
10
+ const USDC = CONTRACT_ADDRESS[network].USDC;
11
+ const WETH = CONTRACT_ADDRESS[network].WETH;
9
12
 
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;
13
+ let dhedge: Dhedge;
14
+ let pool: Pool;
15
+ jest.setTimeout(100000);
15
16
 
16
- let dhedge: Dhedge;
17
- let pool: Pool;
18
- jest.setTimeout(100000);
17
+ describe(`pool on ${network}`, () => {
18
+ beforeAll(async () => {
19
+ dhedge = new Dhedge(wallet, network);
20
+ pool = await dhedge.loadPool(TEST_POOL[network]);
21
+ });
19
22
 
20
- describe("pool", () => {
21
- beforeAll(async () => {
22
- dhedge = new Dhedge(wallet, network);
23
- pool = await dhedge.loadPool(TEST_POOL[network]);
24
- });
23
+ it("approves unlimited USDC on 1Inch", async () => {
24
+ await pool.approve(Dapp.ONEINCH, USDC, MAX_AMOUNT);
25
+ const usdcAllowanceDelta = await allowanceDelta(
26
+ pool.address,
27
+ USDC,
28
+ routerAddress[network]["1inch"]!,
29
+ pool.signer
30
+ );
31
+ await expect(usdcAllowanceDelta.gt(0));
32
+ });
25
33
 
26
- it("approves unlimited USDC on 1Inch", async () => {
27
- await pool.approve(Dapp.ONEINCH, USDC, MAX_AMOUNT);
28
- const usdcAllowanceDelta = await allowanceDelta(
29
- pool.address,
30
- USDC,
31
- routerAddress[network]["1inch"]!,
32
- pool.signer
33
- );
34
- await expect(usdcAllowanceDelta.gt(0));
34
+ it("trades 2 USDC into WETH on 1Inch", async () => {
35
+ await pool.trade(Dapp.ONEINCH, USDC, WETH, "2000000", 0.5);
36
+ const wethBalanceDelta = await balanceDelta(
37
+ pool.address,
38
+ WETH,
39
+ pool.signer
40
+ );
41
+ expect(wethBalanceDelta.gt(0));
42
+ });
35
43
  });
44
+ };
36
45
 
37
- it("trades 2 USDC into WETH on 1Inch", async () => {
38
- await pool.trade(Dapp.ONEINCH, USDC, WETH, "2000000", 0.5);
39
- const wethBalanceDelta = await balanceDelta(
40
- pool.address,
41
- WETH,
42
- pool.signer
43
- );
44
- expect(wethBalanceDelta.gt(0));
45
- });
46
+ testingHelper({
47
+ network: Network.OPTIMISM,
48
+ testingRun: testOneInch
49
+ });
50
+
51
+ testingHelper({
52
+ network: Network.POLYGON,
53
+ testingRun: testOneInch
46
54
  });
@@ -0,0 +1,20 @@
1
+ import { ethers } from "ethers";
2
+ import { Network } from "../../types";
3
+ import { getWallet } from "../wallet";
4
+
5
+ export type TestingRunParams = {
6
+ network: Network;
7
+ wallet: ethers.Wallet;
8
+ };
9
+
10
+ type TestHelperParams = {
11
+ testingRun: (testingRunParams: TestingRunParams) => void;
12
+ } & { network: Network };
13
+
14
+ export const testingHelper = ({
15
+ network,
16
+ testingRun
17
+ }: TestHelperParams): void => {
18
+ const wallet = getWallet(network);
19
+ testingRun({ network, wallet });
20
+ };
@@ -1,5 +1,5 @@
1
1
  import { ethers } from "ethers";
2
-
2
+ import { Network } from "../types";
3
3
  // eslint-disable-next-line @typescript-eslint/no-var-requires
4
4
  require("dotenv").config();
5
5
 
@@ -25,3 +25,16 @@ export const wallet = new ethers.Wallet(
25
25
  process.env.PRIVATE_KEY as string,
26
26
  provider
27
27
  );
28
+
29
+ export const networkPortMap = {
30
+ [Network.POLYGON]: 8542,
31
+ [Network.OPTIMISM]: 8544,
32
+ [Network.ARBITRUM]: 8540
33
+ };
34
+
35
+ export const getWallet = (network: Network): ethers.Wallet => {
36
+ const provider = new ethers.providers.JsonRpcProvider(
37
+ `http://127.0.0.1:${networkPortMap[network]}/`
38
+ );
39
+ return new ethers.Wallet(process.env.PRIVATE_KEY as string, provider);
40
+ };
@@ -3,44 +3,52 @@ import { Dhedge, Pool } from "..";
3
3
  import { routerAddress } from "../config";
4
4
  import { Dapp, Network } from "../types";
5
5
  import { CONTRACT_ADDRESS, MAX_AMOUNT, TEST_POOL } from "./constants";
6
+ import { TestingRunParams, testingHelper } from "./utils/testingHelper";
6
7
  import { allowanceDelta, balanceDelta } from "./utils/token";
7
8
 
8
- import { wallet } from "./wallet";
9
+ const testZeroEx = ({ wallet, network }: TestingRunParams) => {
10
+ const USDC = CONTRACT_ADDRESS[network].USDC;
11
+ const WETH = CONTRACT_ADDRESS[network].WETH;
9
12
 
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;
13
+ let dhedge: Dhedge;
14
+ let pool: Pool;
15
+ jest.setTimeout(100000);
15
16
 
16
- let dhedge: Dhedge;
17
- let pool: Pool;
18
- jest.setTimeout(100000);
17
+ describe(`[${network}] 0x trade`, () => {
18
+ beforeAll(async () => {
19
+ dhedge = new Dhedge(wallet, network);
20
+ pool = await dhedge.loadPool(TEST_POOL[network]);
21
+ });
19
22
 
20
- describe("pool", () => {
21
- beforeAll(async () => {
22
- dhedge = new Dhedge(wallet, network);
23
- pool = await dhedge.loadPool(TEST_POOL[network]);
24
- });
23
+ it("approves unlimited USDC on 0x", async () => {
24
+ await pool.approve(Dapp.ZEROEX, USDC, MAX_AMOUNT);
25
+ const usdcAllowanceDelta = await allowanceDelta(
26
+ pool.address,
27
+ USDC,
28
+ routerAddress[network]["0x"]!,
29
+ pool.signer
30
+ );
31
+ await expect(usdcAllowanceDelta.gt(0));
32
+ });
25
33
 
26
- it("approves unlimited USDC on 0x", async () => {
27
- await pool.approve(Dapp.ZEROEX, USDC, MAX_AMOUNT);
28
- const usdcAllowanceDelta = await allowanceDelta(
29
- pool.address,
30
- USDC,
31
- routerAddress[network]["0x"]!,
32
- pool.signer
33
- );
34
- await expect(usdcAllowanceDelta.gt(0));
34
+ it("trades 2 USDC into WETH on 0x", async () => {
35
+ await pool.trade(Dapp.ZEROEX, USDC, WETH, "2000000", 0.5);
36
+ const wethBalanceDelta = await balanceDelta(
37
+ pool.address,
38
+ WETH,
39
+ pool.signer
40
+ );
41
+ expect(wethBalanceDelta.gt(0));
42
+ });
35
43
  });
44
+ };
36
45
 
37
- it("trades 2 USDC into WETH on 0x", async () => {
38
- await pool.trade(Dapp.ZEROEX, USDC, WETH, "2000000", 0.5);
39
- const wethBalanceDelta = await balanceDelta(
40
- pool.address,
41
- WETH,
42
- pool.signer
43
- );
44
- expect(wethBalanceDelta.gt(0));
45
- });
46
+ testingHelper({
47
+ network: Network.OPTIMISM,
48
+ testingRun: testZeroEx
49
+ });
50
+
51
+ testingHelper({
52
+ network: Network.POLYGON,
53
+ testingRun: testZeroEx
46
54
  });
@@ -1 +0,0 @@
1
- export declare function getOneInchProtocols(chainId: number): Promise<string>;
@@ -1,17 +0,0 @@
1
- import axios from "axios";
2
-
3
- export async function getOneInchProtocols(chainId: number): Promise<string> {
4
- try {
5
- const response = await axios.get(
6
- `https://api.1inch.io/v5.0/${chainId}/liquidity-sources`
7
- );
8
- const protocols = response.data.protocols.map((e: { id: string }) => e.id);
9
- const filteredProtocols = protocols.filter(
10
- (e: string) => !e.includes("PMM")
11
- );
12
-
13
- return `&protocols=${filteredProtocols.join(",")}`;
14
- } catch {
15
- return "";
16
- }
17
- }