@dhedge/v2-sdk 1.1.0 → 1.2.1

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 +37 -3
  4. package/dist/entities/utils.d.ts +4 -0
  5. package/dist/services/claim-balancer/claim.service.d.ts +17 -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 +3497 -690
  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 +3497 -690
  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 +21 -4
  25. package/src/entities/pool.ts +148 -5
  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 +262 -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 -244
  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;
@@ -109,11 +109,12 @@ export declare class Pool {
109
109
  * Lend asset to a lending pool
110
110
  * @param {Dapp} dapp Platform like Aave
111
111
  * @param {string} asset Asset
112
- * @param {BigNumber | string} amount Amount of asset to lend
112
+ * @param {BigNumber | string} amount Amount of asset to lend
113
+ * @param {number} referrralCode Code from Aave referral program
113
114
  * @param {any} options Transaction options
114
115
  * @returns {Promise<any>} Transaction
115
116
  */
116
- lend(dapp: Dapp, asset: string, amount: BigNumber | string, options?: any): Promise<any>;
117
+ lend(dapp: Dapp, asset: string, amount: BigNumber | string, referrralCode?: number, options?: any): Promise<any>;
117
118
  /**
118
119
  * Witdraw asset from a lending pool
119
120
  * @param {Dapp} dapp Platform like Aave
@@ -128,10 +129,11 @@ export declare class Pool {
128
129
  * @param {Dapp} dapp Platform like Aave
129
130
  * @param {string} asset Asset
130
131
  * @param {BigNumber | string} amount Amount of asset to lend
132
+ * @param {number} referrralCode Code from Aave referral program
131
133
  * @param {any} options Transaction options
132
134
  * @returns {Promise<any>} Transaction
133
135
  */
134
- borrow(dapp: Dapp, asset: string, amount: BigNumber | string, options?: any): Promise<any>;
136
+ borrow(dapp: Dapp, asset: string, amount: BigNumber | string, referrralCode?: number, options?: any): Promise<any>;
135
137
  /**
136
138
  * Repays borrowed asset to a lending pool
137
139
  * @param {Dapp} dapp Platform like Aave
@@ -163,4 +165,36 @@ export declare class Pool {
163
165
  * @returns {Promise<any>} Transaction
164
166
  */
165
167
  setTrader(trader: string, options?: any): Promise<any>;
168
+ /**
169
+ * Invest into a Balancer pool
170
+ * @param {string} poolId Balancer pool id
171
+ * @param {string[] | } assetsIn Array of balancer pool assets
172
+ * @param {BigNumber[] | string[]} amountsIn Array of maximum amounts to provide to pool
173
+ * @param {any} options Transaction options
174
+ * @returns {Promise<any>} Transaction
175
+ */
176
+ joinBalancerPool(poolId: string, assets: string[], amountsIn: string[] | BigNumber[], options?: any): Promise<any>;
177
+ /**
178
+ * Invest into a Balancer pool
179
+ * @param {string} poolId Balancer pool id
180
+ * @param {string[] | } assets Array of balancer pool assets
181
+ * @param {BigNumber | string } amount Amount of pool tokens to withdraw
182
+ * @param {any} options Transaction options
183
+ * @returns {Promise<any>} Transaction
184
+ */
185
+ exitBalancerPool(poolId: string, assets: string[], amount: string | BigNumber, options?: any): Promise<any>;
186
+ /**
187
+ * Claim rewards from Balancer pools
188
+ * @param {string[]} assets Array of tokens being claimed
189
+ * @param {any} options Transaction options
190
+ * @returns {Promise<any>} Transaction
191
+ */
192
+ harvestBalancerRewards(options?: any): Promise<any>;
193
+ /**
194
+ * Claim rewards from Aave platform
195
+ * @param {string[]} assets Aave tokens (deposit/debt) hold by pool
196
+ * @param {any} options Transaction options
197
+ * @returns {Promise<any>} Transaction
198
+ */
199
+ harvestAaveRewards(assets: string[], options?: any): Promise<any>;
166
200
  }
@@ -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,17 @@
1
+ import { ethers, Wallet } from "ethers";
2
+ import { 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
+ multiTokenClaimRewards(account: string, multiTokenPendingClaims: MultiTokenPendingClaims[]): Promise<any>;
11
+ private computeClaimProofs;
12
+ private computeClaimProof;
13
+ private getTokenClaimsInfo;
14
+ private getSnapshot;
15
+ private getClaimStatus;
16
+ private getReports;
17
+ }
@@ -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;