@dhedge/v2-sdk 1.1.1 → 1.2.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.
Files changed (42) hide show
  1. package/README.md +60 -3
  2. package/dist/config.d.ts +2 -0
  3. package/dist/entities/pool.d.ts +32 -0
  4. package/dist/entities/utils.d.ts +4 -0
  5. package/dist/services/claim-balancer/claim.service.d.ts +21 -0
  6. package/dist/services/claim-balancer/claim.worker.d.ts +4 -0
  7. package/dist/services/claim-balancer/ipfs.service.d.ts +4 -0
  8. package/dist/services/claim-balancer/types.d.ts +54 -0
  9. package/dist/test/constants.d.ts +12 -0
  10. package/dist/types.d.ts +5 -2
  11. package/dist/utils/contract.d.ts +14 -0
  12. package/dist/utils/index.d.ts +7 -0
  13. package/dist/utils/merkle.d.ts +22 -0
  14. package/dist/v2-sdk.cjs.development.js +3623 -672
  15. package/dist/v2-sdk.cjs.development.js.map +1 -1
  16. package/dist/v2-sdk.cjs.production.min.js +1 -1
  17. package/dist/v2-sdk.cjs.production.min.js.map +1 -1
  18. package/dist/v2-sdk.esm.js +3623 -672
  19. package/dist/v2-sdk.esm.js.map +1 -1
  20. package/package.json +9 -2
  21. package/src/abi/IAaveIncentivesController.json +50 -0
  22. package/src/abi/IBalancerMerkleOrchard.json +353 -0
  23. package/src/abi/IBalancertV2Vault.json +938 -0
  24. package/src/config.ts +16 -3
  25. package/src/entities/pool.ts +140 -1
  26. package/src/entities/utils.ts +135 -0
  27. package/src/services/claim-balancer/MultiTokenClaim.json +115 -0
  28. package/src/services/claim-balancer/claim.service.ts +324 -0
  29. package/src/services/claim-balancer/claim.worker.ts +32 -0
  30. package/src/services/claim-balancer/ipfs.service.ts +12 -0
  31. package/src/services/claim-balancer/types.ts +66 -0
  32. package/src/test/aave.test.ts +73 -0
  33. package/src/test/balancer.test.ts +109 -0
  34. package/src/test/constants.ts +13 -0
  35. package/src/test/oneInch.test.ts +56 -0
  36. package/src/test/pool.test.ts +5 -249
  37. package/src/test/sushi.test.ts +173 -0
  38. package/src/test/utils.test.ts +41 -26
  39. package/src/types.ts +6 -3
  40. package/src/utils/contract.ts +95 -0
  41. package/src/utils/index.ts +38 -0
  42. package/src/utils/merkle.ts +172 -0
package/README.md CHANGED
@@ -142,17 +142,17 @@ const tx = await pool.approve(
142
142
 
143
143
  ### Trade pool assets
144
144
 
145
- Trade 1 USDC into DAI on Sushiswap
145
+ Trade 1 USDC into DAI on Sushiswap (other options: QUICKSWAP, BALANCER, or ONEINCH)
146
146
 
147
147
  ```
148
148
  const amountIn = "1000000"
149
- const minAmountOut = "997085"
149
+ const slippage = 0.5
150
150
  const tx = await pool.trade(
151
151
  Dapp.SUSHISWAP,
152
152
  "USDC_TOKEN_ADDRESS",
153
153
  "DAI_TOKEN_ADDRESS",
154
154
  amountIn,
155
- minAmountOut
155
+ slippage
156
156
  )
157
157
  ```
158
158
 
@@ -184,6 +184,28 @@ const tx = await pool.removeLiquidity(
184
184
  )
185
185
  ```
186
186
 
187
+ Add 0.00002 WBTC, 1 USDC and 0.0002 WETH to a WBTC/USDC/WETH Balancer pool
188
+
189
+ ```
190
+ const balancerPoolId = "0x03cd191f589d12b0582a99808cf19851e468e6b500010000000000000000000a"
191
+ const assets = [WBTC_TOKEN_ADDRESS, USDC_TOKEN_ADDRESS, WETH_TOKEN_ADDRESS];
192
+ const amounts = ["2000", "1000000", "200000000000000"];
193
+ const tx = await pool.joinBalancerPool(balancerPoolId, assets, amounts)
194
+ ```
195
+
196
+ Remove all tokens from WBTC/USDC/WETH Balancer pool
197
+
198
+ ```
199
+ const amount = await dhedge.utils.getBalance(BALANCER_LP_TOKEN_ADDRESS, pool.address)
200
+ const tx = await pool.exitBalancerPool(balancerPoolId, assets, amount)
201
+ ```
202
+
203
+ Harvest rewards from Balancer
204
+
205
+ ```
206
+ const tx = await pool.harvestBalancerRewards()
207
+ ```
208
+
187
209
  ### Staking
188
210
 
189
211
  Approve unlimited amound of SLP USDC-DAI token for staking on Sushiswap
@@ -226,3 +248,38 @@ const tx = await pool.harvestRewards(
226
248
  "SLP_USDC_DAI_TOKEN_ADDRESS"
227
249
  )
228
250
  ```
251
+
252
+ ### Lending/Borrowing Aave
253
+
254
+ Deposit 1 USDC into Aave lending pool
255
+
256
+ ```
257
+ const tx = await pool.lend(Dapp.AAVE, USDC_TOKEN_ADDRESS, "1000000")
258
+ ```
259
+
260
+ Withdraw 1 USDC from Aave lending pool
261
+
262
+ ```
263
+ const tx = await pool.withdrawDeposit(Dapp.AAVE, USDC_TOKEN_ADDRESS, "1000000")
264
+ ```
265
+
266
+ Borrow 0.0001 WETH from Aave lending pool
267
+
268
+ ```
269
+ const tx = await pool.borrow(Dapp.AAVE, WETH_TOKEN_ADDRESS, "100000000000000");
270
+ ```
271
+
272
+ Repay 0.0001 WETH to Aave lending pool
273
+
274
+ ```
275
+ const tx = await pool.repay(Dapp.AAVE, WETH_TOKEN_ADDRESS, "100000000000000");
276
+ ```
277
+
278
+ Harvest rewards from Aave
279
+
280
+ ```
281
+ const tx = await pool.harvestAaveRewards([
282
+ AAVE_USDC_ADDRESS,
283
+ AAVE_WETH_DEBT_ADDRESS
284
+ ]);
285
+ ```
package/dist/config.d.ts CHANGED
@@ -5,3 +5,5 @@ export declare const routerAddress: AddressDappNetworkMap;
5
5
  export declare const dappFactoryAddress: AddressDappNetworkMap;
6
6
  export declare const stakingAddress: AddressDappNetworkMap;
7
7
  export declare const networkChainIdMap: NetworkChainIdMap;
8
+ export declare const balancerSubgraph: AddressNetworkMap;
9
+ export declare const multiCallAddress: AddressNetworkMap;
@@ -163,4 +163,36 @@ export declare class Pool {
163
163
  * @returns {Promise<any>} Transaction
164
164
  */
165
165
  setTrader(trader: string, options?: any): Promise<any>;
166
+ /**
167
+ * Invest into a Balancer pool
168
+ * @param {string} poolId Balancer pool id
169
+ * @param {string[] | } assetsIn Array of balancer pool assets
170
+ * @param {BigNumber[] | string[]} amountsIn Array of maximum amounts to provide to pool
171
+ * @param {any} options Transaction options
172
+ * @returns {Promise<any>} Transaction
173
+ */
174
+ joinBalancerPool(poolId: string, assets: string[], amountsIn: string[] | BigNumber[], options?: any): Promise<any>;
175
+ /**
176
+ * Invest into a Balancer pool
177
+ * @param {string} poolId Balancer pool id
178
+ * @param {string[] | } assets Array of balancer pool assets
179
+ * @param {BigNumber | string } amount Amount of pool tokens to withdraw
180
+ * @param {any} options Transaction options
181
+ * @returns {Promise<any>} Transaction
182
+ */
183
+ exitBalancerPool(poolId: string, assets: string[], amount: string | BigNumber, options?: any): Promise<any>;
184
+ /**
185
+ * Claim rewards from Balancer pools
186
+ * @param {string[]} assets Array of tokens being claimed
187
+ * @param {any} options Transaction options
188
+ * @returns {Promise<any>} Transaction
189
+ */
190
+ harvestBalancerRewards(options?: any): Promise<any>;
191
+ /**
192
+ * Claim rewards from Aave platform
193
+ * @param {string[]} assets Aave tokens (deposit/debt) hold by pool
194
+ * @param {any} options Transaction options
195
+ * @returns {Promise<any>} Transaction
196
+ */
197
+ harvestAaveRewards(assets: string[], options?: any): Promise<any>;
166
198
  }
@@ -1,5 +1,6 @@
1
1
  import { ethers, Wallet } from "ethers";
2
2
  import { Dapp, Network, Reserves } from "../types";
3
+ import { Pool } from ".";
3
4
  export declare class Utils {
4
5
  network: Network;
5
6
  signer: Wallet;
@@ -40,4 +41,7 @@ export declare class Utils {
40
41
  * @returns {Promise<ethers.BigNumber>} Reserves of the assets in BigNumber
41
42
  */
42
43
  getMinAmountOut(dapp: Dapp, assetFrom: string, assetTo: string, amountIn: string | ethers.BigNumber, slippage: number): Promise<ethers.BigNumber>;
44
+ getBalancerSwapTx(pool: Pool, assetFrom: string, assetTo: string, amountIn: ethers.BigNumber | string, slippage: number): Promise<any>;
45
+ getBalancerJoinPoolTx(pool: Pool, balancerPoolId: string, assets: string[], amountsIn: string[] | ethers.BigNumber[]): Promise<any>;
46
+ getBalancerExitPoolTx(pool: Pool, balancerPoolId: string, assets: string[], amount: string | ethers.BigNumber): Promise<any>;
43
47
  }
@@ -0,0 +1,21 @@
1
+ import { ethers, Wallet } from "ethers";
2
+ import { MultiTokenCurrentRewardsEstimate, MultiTokenPendingClaims, TokenClaimInfo } from "./types";
3
+ import { Network } from "../../types";
4
+ export declare class ClaimService {
5
+ network: Network;
6
+ signer: ethers.Wallet;
7
+ constructor(network: Network, signer: Wallet);
8
+ getMultiTokensPendingClaims(account: string): Promise<MultiTokenPendingClaims[]>;
9
+ getTokenPendingClaims(tokenClaimInfo: TokenClaimInfo, account: string): Promise<MultiTokenPendingClaims>;
10
+ getMultiTokensCurrentRewardsEstimate(account: string): Promise<{
11
+ data: MultiTokenCurrentRewardsEstimate[];
12
+ timestamp: string | null;
13
+ }>;
14
+ multiTokenClaimRewards(account: string, multiTokenPendingClaims: MultiTokenPendingClaims[]): Promise<any>;
15
+ private computeClaimProofs;
16
+ private computeClaimProof;
17
+ private getTokenClaimsInfo;
18
+ private getSnapshot;
19
+ private getClaimStatus;
20
+ private getReports;
21
+ }
@@ -0,0 +1,4 @@
1
+ import { ComputeClaimProofPayload } from "./types";
2
+ export declare class ClaimWorker {
3
+ calcClaimProof(payload: ComputeClaimProofPayload): any;
4
+ }
@@ -0,0 +1,4 @@
1
+ export default class IpfsService {
2
+ get<T>(hash: string, protocol?: string): Promise<T>;
3
+ }
4
+ export declare const ipfsService: IpfsService;
@@ -0,0 +1,54 @@
1
+ export interface Claim {
2
+ id: string;
3
+ amount: string;
4
+ }
5
+ export declare type Snapshot = Record<number, string>;
6
+ export declare type TokenClaimInfo = {
7
+ label: string;
8
+ distributor: string;
9
+ token: string;
10
+ decimals: number;
11
+ manifest: string;
12
+ weekStart: number;
13
+ };
14
+ export declare type MultiTokenPendingClaims = {
15
+ claims: Claim[];
16
+ reports: Report;
17
+ tokenClaimInfo: TokenClaimInfo;
18
+ availableToClaim: string;
19
+ };
20
+ export declare type ClaimStatus = boolean;
21
+ export declare type Report = Record<string, any>;
22
+ export declare type MultiTokenCurrentRewardsEstimateResponse = {
23
+ success: boolean;
24
+ result: {
25
+ current_timestamp: string;
26
+ "liquidity-providers": Array<{
27
+ snapshot_timestamp: string;
28
+ address: string;
29
+ token_address: string;
30
+ chain_id: number;
31
+ current_estimate: string;
32
+ velocity: string;
33
+ week: number;
34
+ }>;
35
+ };
36
+ };
37
+ export declare type MultiTokenCurrentRewardsEstimate = {
38
+ rewards: string;
39
+ velocity: string;
40
+ token: string;
41
+ };
42
+ export declare type ClaimProofTuple = [number, string, string, number, string[]];
43
+ export declare type ComputeClaimProofPayload = {
44
+ report: Report;
45
+ account: string;
46
+ claim: Claim;
47
+ distributor: string;
48
+ tokenIndex: number;
49
+ decimals: number;
50
+ };
51
+ export declare type ClaimWorkerMessage<P = any> = {
52
+ type: "computeClaimProof";
53
+ payload: P;
54
+ };
@@ -0,0 +1,12 @@
1
+ export declare const USDC = "0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174";
2
+ export declare const USDT = "0xc2132D05D31c914a87C6611C10748AEb04B58e8F";
3
+ export declare const DAI = "0x8f3Cf7ad23Cd3CaDbD9735AFf958023239c6A063";
4
+ export declare const TUSD = "0x2e1ad108ff1d8c782fcbbb89aad783ac49586756";
5
+ export declare const WETH = "0x7ceB23fD6bC0adD59E62ac25578270cFf1b9f619";
6
+ export declare const WBTC = "0x1BFD67037B42Cf73acF2047067bd4F2C47D9BfD6";
7
+ export declare const SUSHI = "0x0b3f868e0be5597d5db7feb59e1cadbb0fdda50a";
8
+ export declare const WMATIC = "0x0d500b1d8e8ef31e21c99d1db9a6444d3adf1270";
9
+ export declare const BAL = "0x9a71012B13CA4d3D0Cdc72A177DF3ef03b0E76A3";
10
+ export declare const AMUSDC = "0x1a13f4ca1d028320a707d99520abfefca3998b7f";
11
+ export declare const VDEBTWETH = "0xede17e9d79fc6f9ff9250d9eefbdb88cc18038b5";
12
+ export declare const TEST_POOL = "0x3deeba9ca29e2dd98d32eed8dd559dac55014615";
package/dist/types.d.ts CHANGED
@@ -7,14 +7,16 @@ export declare enum Dapp {
7
7
  SUSHISWAP = "sushiswap",
8
8
  AAVE = "aave",
9
9
  ONEINCH = "1inch",
10
- QUICKSWAP = "quickswap"
10
+ QUICKSWAP = "quickswap",
11
+ BALANCER = "balancer"
11
12
  }
12
13
  export declare enum Transaction {
13
14
  SWAP = "swapExactTokensForTokens",
14
15
  ADD_LIQUIDITY = "addLiquidity",
15
16
  DEPOSIT = "deposit",
16
17
  HARVEST = "harvest",
17
- UNSTAKE = "withdrawAndHarvest",
18
+ CLAIM_DISTRIBIUTIONS = "claimDistributions",
19
+ CLAIM_REWARDS = "claimRewards",
18
20
  REMOVE_LIQUIDITY = "removeLiquidity",
19
21
  BORROW = "borrow",
20
22
  REPAY = "repay",
@@ -26,6 +28,7 @@ export declare type AddressDappMap = {
26
28
  [Dapp.AAVE]?: string;
27
29
  [Dapp.ONEINCH]?: string;
28
30
  [Dapp.QUICKSWAP]?: string;
31
+ [Dapp.BALANCER]?: string;
29
32
  };
30
33
  export declare type AddressDappNetworkMap = Readonly<Record<Network, AddressDappMap>>;
31
34
  export declare type SupportedAsset = [string, boolean];
@@ -0,0 +1,14 @@
1
+ import { ethers, Network } from "..";
2
+ export declare function call(provider: ethers.Signer, abi: any[], call: any[], options?: any): Promise<any>;
3
+ export declare function multicall<T>(network: Network, provider: ethers.Signer, abi: any[], calls: any[], options?: any, requireSuccess?: boolean): Promise<(T | null)[]>;
4
+ export declare class Multicaller {
5
+ network: Network;
6
+ provider: ethers.Signer;
7
+ abi: any[];
8
+ options: any;
9
+ calls: any[];
10
+ paths: any[];
11
+ constructor(network: Network, provider: ethers.Signer, abi: any[]);
12
+ call(path: any, address: any, fn: any, params?: any): Multicaller;
13
+ execute(from?: any): Promise<any>;
14
+ }
@@ -0,0 +1,7 @@
1
+ import BigNumber from "bignumber.js";
2
+ import { MerkleTree } from "./merkle";
3
+ export declare function scale(input: BigNumber | string, decimalPlaces: number): BigNumber;
4
+ export declare function loadTree(balances: {
5
+ [x: string]: string | BigNumber;
6
+ }, decimals?: number): MerkleTree;
7
+ export declare function bnum(val: string | number | BigNumber): BigNumber;
@@ -0,0 +1,22 @@
1
+ /// <reference types="node" />
2
+ import BigNumber from "bignumber.js";
3
+ export declare class MerkleTree {
4
+ elements: any;
5
+ layers: any;
6
+ constructor(elements: any[]);
7
+ getLayers(elements: string | any[]): (string | any[])[];
8
+ getNextLayer(elements: any[]): any[];
9
+ combinedHash(first: any, second: any): any;
10
+ getRoot(): any;
11
+ getHexRoot(): string;
12
+ getProof(el: any): any;
13
+ getHexProof(_el: any): string[];
14
+ getPairElement(idx: number, layer: string | any[]): any;
15
+ bufIndexOf(el: string | any[], arr: string | any[]): number;
16
+ bufDedup(elements: any[]): any[];
17
+ bufArrToHexArr(arr: any[]): string[];
18
+ sortAndConcat(...args: any[]): Buffer;
19
+ }
20
+ export declare function loadTree(balances: {
21
+ [x: string]: string | BigNumber;
22
+ }, decimals?: number): MerkleTree;