@defisaver/positions-sdk 0.0.32 → 0.0.33
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/cjs/config/contracts.d.ts +908 -41
- package/cjs/config/contracts.js +171 -44
- package/cjs/contracts.js +4 -10
- package/cjs/curveUsd/index.js +1 -1
- package/cjs/helpers/curveUsdHelpers/index.d.ts +2 -1
- package/cjs/helpers/curveUsdHelpers/index.js +25 -17
- package/cjs/moneymarket/moneymarketCommonService.js +1 -1
- package/cjs/services/utils.d.ts +4 -0
- package/cjs/services/utils.js +13 -1
- package/cjs/types/contracts/generated/CrvUSDView.d.ts +30 -4
- package/cjs/types/curveUsd.d.ts +6 -0
- package/esm/config/contracts.d.ts +908 -41
- package/esm/config/contracts.js +171 -44
- package/esm/contracts.js +4 -10
- package/esm/curveUsd/index.js +1 -1
- package/esm/helpers/curveUsdHelpers/index.d.ts +2 -1
- package/esm/helpers/curveUsdHelpers/index.js +26 -18
- package/esm/moneymarket/moneymarketCommonService.js +1 -1
- package/esm/services/utils.d.ts +4 -0
- package/esm/services/utils.js +11 -0
- package/esm/types/contracts/generated/CrvUSDView.d.ts +30 -4
- package/esm/types/curveUsd.d.ts +6 -0
- package/package.json +1 -1
- package/src/config/contracts.js +171 -44
- package/src/contracts.ts +5 -11
- package/src/curveUsd/index.ts +1 -1
- package/src/helpers/curveUsdHelpers/index.ts +28 -20
- package/src/moneymarket/moneymarketCommonService.ts +1 -1
- package/src/services/utils.ts +14 -1
- package/src/types/contracts/generated/CrvUSDView.ts +43 -8
- package/src/types/curveUsd.ts +6 -0
- package/yarn-error.log +64 -0
package/src/curveUsd/index.ts
CHANGED
|
@@ -216,7 +216,7 @@ export const getCurveUsdUserData = async (web3: Web3, network: NetworkNumber, ad
|
|
|
216
216
|
usedAssets,
|
|
217
217
|
status,
|
|
218
218
|
...getCrvUsdAggregatedData({
|
|
219
|
-
loanExists: data.loanExists, usedAssets, network: NetworkNumber.Eth, selectedMarket,
|
|
219
|
+
loanExists: data.loanExists, usedAssets, network: NetworkNumber.Eth, selectedMarket, numOfBands: data.N,
|
|
220
220
|
}),
|
|
221
221
|
userBands,
|
|
222
222
|
};
|
|
@@ -1,33 +1,41 @@
|
|
|
1
1
|
import Dec from 'decimal.js';
|
|
2
2
|
import { CrvUSDAggregatedPositionData, CrvUSDMarketData, CrvUSDUsedAssets } from '../../types';
|
|
3
|
-
import { NetworkNumber } from '../../types/common';
|
|
4
|
-
import { getAssetsTotal } from '../../moneymarket';
|
|
3
|
+
import { MMUsedAssets, NetworkNumber } from '../../types/common';
|
|
4
|
+
import { calcLeverageLiqPrice, getAssetsTotal, isLeveragedPos } from '../../moneymarket';
|
|
5
|
+
import { mapRange } from '../../services/utils';
|
|
5
6
|
|
|
6
7
|
export const getCrvUsdAggregatedData = ({
|
|
7
|
-
loanExists, usedAssets, network, selectedMarket, ...rest
|
|
8
|
+
loanExists, usedAssets, network, selectedMarket, numOfBands, ...rest
|
|
8
9
|
}:{
|
|
9
|
-
loanExists: boolean, usedAssets: CrvUSDUsedAssets, network: NetworkNumber, selectedMarket: CrvUSDMarketData,
|
|
10
|
+
loanExists: boolean, usedAssets: CrvUSDUsedAssets, network: NetworkNumber, selectedMarket: CrvUSDMarketData, numOfBands: number | string
|
|
10
11
|
}): CrvUSDAggregatedPositionData => {
|
|
11
|
-
const
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
12
|
+
const payload = {} as CrvUSDAggregatedPositionData;
|
|
13
|
+
payload.supplied = getAssetsTotal(usedAssets, ({ isSupplied }: { isSupplied: boolean }) => isSupplied, ({ supplied }: { supplied: string }) => supplied); // this is wrong if we are in soft-liquidations
|
|
14
|
+
payload.borrowed = getAssetsTotal(usedAssets, ({ isBorrowed }: { isBorrowed: boolean }) => isBorrowed, ({ borrowed }: { borrowed: string }) => borrowed);
|
|
15
|
+
payload.suppliedUsd = getAssetsTotal(usedAssets, ({ isSupplied }: { isSupplied: boolean }) => isSupplied, ({ suppliedUsd }: { suppliedUsd: string }) => suppliedUsd);
|
|
16
|
+
payload.borrowedUsd = getAssetsTotal(usedAssets, ({ isBorrowed }: { isBorrowed: boolean }) => isBorrowed, ({ borrowedUsd }: { borrowedUsd: string }) => borrowedUsd);
|
|
15
17
|
|
|
16
|
-
|
|
17
|
-
? new Dec(
|
|
18
|
-
.dividedBy(
|
|
18
|
+
payload.ratio = loanExists
|
|
19
|
+
? new Dec(payload.suppliedUsd)
|
|
20
|
+
.dividedBy(payload.borrowedUsd)
|
|
19
21
|
.times(100)
|
|
20
22
|
.toString()
|
|
21
23
|
: '0';
|
|
22
24
|
|
|
23
|
-
//
|
|
25
|
+
// this is all approximation
|
|
26
|
+
payload.minAllowedRatio = mapRange(numOfBands, 4, 50, 115, 140); // collateral ratio
|
|
27
|
+
payload.collFactor = new Dec(1).div(payload.minAllowedRatio).mul(100).toString(); // collateral factor = 1 / collateral ratio
|
|
28
|
+
// only take in consideration collAsset
|
|
29
|
+
payload.borrowLimitUsd = usedAssets?.[selectedMarket.collAsset]?.isSupplied
|
|
30
|
+
? new Dec(usedAssets[selectedMarket.collAsset].suppliedUsd).mul(payload.collFactor).toString()
|
|
31
|
+
: '0';
|
|
32
|
+
|
|
33
|
+
const { leveragedType, leveragedAsset } = isLeveragedPos(usedAssets as unknown as MMUsedAssets);
|
|
34
|
+
payload.leveragedType = leveragedType;
|
|
35
|
+
if (leveragedType !== '') {
|
|
36
|
+
payload.leveragedAsset = leveragedAsset;
|
|
37
|
+
payload.liquidationPrice = calcLeverageLiqPrice(leveragedType, usedAssets[selectedMarket.collAsset].price, payload.borrowedUsd, payload.borrowLimitUsd);
|
|
38
|
+
}
|
|
24
39
|
|
|
25
|
-
return
|
|
26
|
-
ratio,
|
|
27
|
-
supplied: _supplied,
|
|
28
|
-
suppliedUsd: _suppliedUsd,
|
|
29
|
-
borrowedUsd: _borrowedUsd,
|
|
30
|
-
borrowed: _borrowed,
|
|
31
|
-
safetyRatio: ratio,
|
|
32
|
-
};
|
|
40
|
+
return payload;
|
|
33
41
|
};
|
|
@@ -20,7 +20,7 @@ export const calcLeverageLiqPrice = (leverageType: string, assetPrice: string, b
|
|
|
20
20
|
|
|
21
21
|
export const calculateBorrowingAssetLimit = (assetBorrowedUsd: string, borrowLimitUsd: string) => new Dec(assetBorrowedUsd).div(borrowLimitUsd).times(100).toString();
|
|
22
22
|
|
|
23
|
-
export const STABLE_ASSETS = ['DAI', 'USDC', 'USDT', 'TUSD', 'USDP', 'GUSD', 'BUSD', 'SUSD', 'FRAX', 'LUSD', 'USDC.e', 'GHO', 'sDAI'];
|
|
23
|
+
export const STABLE_ASSETS = ['DAI', 'USDC', 'USDT', 'TUSD', 'USDP', 'GUSD', 'BUSD', 'SUSD', 'FRAX', 'LUSD', 'USDC.e', 'GHO', 'sDAI', 'crvUSD'];
|
|
24
24
|
|
|
25
25
|
export const isLeveragedPos = (usedAssets: MMUsedAssets, dustLimit = 5) => {
|
|
26
26
|
let borrowUnstable = 0;
|
package/src/services/utils.ts
CHANGED
|
@@ -32,4 +32,17 @@ export const ethToWethByAddress = (maybeEthAddr: string, chainId = NetworkNumber
|
|
|
32
32
|
export const bytesToString = (hex: string) => Buffer.from(hex.replace(/^0x/, ''), 'hex')
|
|
33
33
|
.toString()
|
|
34
34
|
// eslint-disable-next-line no-control-regex
|
|
35
|
-
.replace(/\x00/g, '');
|
|
35
|
+
.replace(/\x00/g, '');
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Map an input value from one range (minInput, maxInput) to a value in another range (minOutput, maxOutput)
|
|
39
|
+
*/
|
|
40
|
+
export const mapRange = (input: number | string, minInput: number | string, maxInput: number | string, minOutput:number | string, maxOutput: number | string) => {
|
|
41
|
+
// slope = 1.0 * (output_end - output_start) / (input_end - input_start)
|
|
42
|
+
const inputDiff = new Dec(maxInput).minus(minInput);
|
|
43
|
+
const outputDiff = new Dec(maxOutput).minus(minOutput);
|
|
44
|
+
const slope = new Dec(outputDiff).div(inputDiff);
|
|
45
|
+
|
|
46
|
+
// output = output_start + slope * (input - input_start)
|
|
47
|
+
return new Dec(minOutput).plus(new Dec(slope).mul(new Dec(input).minus(minInput))).toDP(2).toNumber();
|
|
48
|
+
};
|
|
@@ -159,7 +159,9 @@ export declare namespace CurveUsdView {
|
|
|
159
159
|
number | string | BN,
|
|
160
160
|
number | string | BN,
|
|
161
161
|
[number | string | BN, number | string | BN],
|
|
162
|
-
[number | string | BN[], number | string | BN[]]
|
|
162
|
+
[number | string | BN[], number | string | BN[]],
|
|
163
|
+
number | string | BN,
|
|
164
|
+
boolean
|
|
163
165
|
]
|
|
164
166
|
| {
|
|
165
167
|
loanExists: boolean;
|
|
@@ -174,6 +176,8 @@ export declare namespace CurveUsdView {
|
|
|
174
176
|
health: number | string | BN;
|
|
175
177
|
bandRange: [number | string | BN, number | string | BN];
|
|
176
178
|
usersBands: [number | string | BN[], number | string | BN[]];
|
|
179
|
+
collRatio: number | string | BN;
|
|
180
|
+
isInSoftLiquidation: boolean;
|
|
177
181
|
};
|
|
178
182
|
|
|
179
183
|
export type UserDataStructOutputArray = [
|
|
@@ -188,7 +192,9 @@ export declare namespace CurveUsdView {
|
|
|
188
192
|
string,
|
|
189
193
|
string,
|
|
190
194
|
[string, string],
|
|
191
|
-
[string[], string[]]
|
|
195
|
+
[string[], string[]],
|
|
196
|
+
string,
|
|
197
|
+
boolean
|
|
192
198
|
];
|
|
193
199
|
export type UserDataStructOutputStruct = {
|
|
194
200
|
loanExists: boolean;
|
|
@@ -203,6 +209,8 @@ export declare namespace CurveUsdView {
|
|
|
203
209
|
health: string;
|
|
204
210
|
bandRange: [string, string];
|
|
205
211
|
usersBands: [string[], string[]];
|
|
212
|
+
collRatio: string;
|
|
213
|
+
isInSoftLiquidation: boolean;
|
|
206
214
|
};
|
|
207
215
|
export type UserDataStructOutput = UserDataStructOutputArray &
|
|
208
216
|
UserDataStructOutputStruct;
|
|
@@ -216,6 +224,10 @@ export interface CrvUSDView extends BaseContract {
|
|
|
216
224
|
): CrvUSDView;
|
|
217
225
|
clone(): CrvUSDView;
|
|
218
226
|
methods: {
|
|
227
|
+
WBTC_HEALTH_ZAP(): NonPayableTransactionObject<string>;
|
|
228
|
+
|
|
229
|
+
WBTC_MARKET(): NonPayableTransactionObject<string>;
|
|
230
|
+
|
|
219
231
|
createLoanData(
|
|
220
232
|
market: string,
|
|
221
233
|
collateral: number | string | BN,
|
|
@@ -228,18 +240,32 @@ export interface CrvUSDView extends BaseContract {
|
|
|
228
240
|
n: number | string | BN
|
|
229
241
|
): NonPayableTransactionObject<CurveUsdView.BandStructOutput>;
|
|
230
242
|
|
|
231
|
-
|
|
243
|
+
getBandsData(
|
|
244
|
+
market: string,
|
|
245
|
+
from: number | string | BN,
|
|
246
|
+
to: number | string | BN
|
|
247
|
+
): NonPayableTransactionObject<CurveUsdView.BandStructOutput[]>;
|
|
248
|
+
|
|
249
|
+
getBandsDataForPosition(
|
|
232
250
|
market: string,
|
|
233
251
|
collateral: number | string | BN,
|
|
234
252
|
debt: number | string | BN,
|
|
235
253
|
N: number | string | BN
|
|
236
254
|
): NonPayableTransactionObject<CurveUsdView.BandStructOutput[]>;
|
|
237
255
|
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
256
|
+
getCollAmountsFromAMM(
|
|
257
|
+
_controllerAddress: string,
|
|
258
|
+
_user: string
|
|
259
|
+
): NonPayableTransactionObject<
|
|
260
|
+
[string, string] & { crvUsdAmount: string; collAmount: string }
|
|
261
|
+
>;
|
|
262
|
+
|
|
263
|
+
getCollateralRatio(
|
|
264
|
+
_user: string,
|
|
265
|
+
_controllerAddr: string
|
|
266
|
+
): NonPayableTransactionObject<
|
|
267
|
+
[string, boolean] & { collRatio: string; isInSoftLiquidation: boolean }
|
|
268
|
+
>;
|
|
243
269
|
|
|
244
270
|
globalData(
|
|
245
271
|
market: string
|
|
@@ -254,6 +280,10 @@ export interface CrvUSDView extends BaseContract {
|
|
|
254
280
|
numBands: number | string | BN
|
|
255
281
|
): NonPayableTransactionObject<string>;
|
|
256
282
|
|
|
283
|
+
isControllerValid(
|
|
284
|
+
_controllerAddr: string
|
|
285
|
+
): NonPayableTransactionObject<boolean>;
|
|
286
|
+
|
|
257
287
|
maxBorrow(
|
|
258
288
|
market: string,
|
|
259
289
|
collateral: number | string | BN,
|
|
@@ -270,6 +300,11 @@ export interface CrvUSDView extends BaseContract {
|
|
|
270
300
|
market: string,
|
|
271
301
|
user: string
|
|
272
302
|
): NonPayableTransactionObject<CurveUsdView.UserDataStructOutput>;
|
|
303
|
+
|
|
304
|
+
userMaxWithdraw(
|
|
305
|
+
_controllerAddress: string,
|
|
306
|
+
_user: string
|
|
307
|
+
): NonPayableTransactionObject<string>;
|
|
273
308
|
};
|
|
274
309
|
events: {
|
|
275
310
|
allEvents(options?: EventOptions, cb?: Callback<EventLog>): EventEmitter;
|
package/src/types/curveUsd.ts
CHANGED
|
@@ -74,6 +74,12 @@ export interface CrvUSDAggregatedPositionData {
|
|
|
74
74
|
borrowedUsd: string,
|
|
75
75
|
borrowed: string,
|
|
76
76
|
safetyRatio: string,
|
|
77
|
+
borrowLimitUsd: string,
|
|
78
|
+
minAllowedRatio: number,
|
|
79
|
+
collFactor: string,
|
|
80
|
+
leveragedType: string,
|
|
81
|
+
leveragedAsset?: string,
|
|
82
|
+
liquidationPrice?: string,
|
|
77
83
|
}
|
|
78
84
|
|
|
79
85
|
export interface CrvUSDUsedAsset {
|
package/yarn-error.log
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
Arguments:
|
|
2
|
+
/Users/stefan/.nvm/versions/node/v14.19.0/bin/node /usr/local/Cellar/yarn/1.22.19/libexec/bin/yarn.js
|
|
3
|
+
|
|
4
|
+
PATH:
|
|
5
|
+
/Users/stefan/.nvm/versions/node/v14.19.0/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Applications/VMware Fusion.app/Contents/Public:/usr/local/go/bin:/Users/stefan/WebstormProjects/defisaver-positions-sdk/node_modules/.bin:/usr/local/go
|
|
6
|
+
|
|
7
|
+
Yarn version:
|
|
8
|
+
1.22.19
|
|
9
|
+
|
|
10
|
+
Node version:
|
|
11
|
+
14.19.0
|
|
12
|
+
|
|
13
|
+
Platform:
|
|
14
|
+
darwin x64
|
|
15
|
+
|
|
16
|
+
Trace:
|
|
17
|
+
Error: incorrect data check
|
|
18
|
+
at Zlib.zlibOnError [as onerror] (zlib.js:187:17)
|
|
19
|
+
|
|
20
|
+
npm manifest:
|
|
21
|
+
{
|
|
22
|
+
"name": "defisaver-positions-sdk",
|
|
23
|
+
"version": "0.0.1",
|
|
24
|
+
"description": "",
|
|
25
|
+
"main": "./cjs/index.js",
|
|
26
|
+
"module": "./esm/src/index.js",
|
|
27
|
+
"types": "./esm/src/index.d.ts",
|
|
28
|
+
"scripts": {
|
|
29
|
+
"build:esm": "rm -rf esm && tsc -p tsconfig.json",
|
|
30
|
+
"build:cjs": "rm -rf cjs && tsc -p tsconfig.cjs.json",
|
|
31
|
+
"build": "npm run build:cjs && npm run build:esm",
|
|
32
|
+
"dev": "tsc -p tsconfig.cjs.json --watch",
|
|
33
|
+
"lint": "eslint src/ --fix",
|
|
34
|
+
"generate-contracts": "node scripts/generateContracts.js",
|
|
35
|
+
"test": "mocha tests/*",
|
|
36
|
+
"build-test": "npm run build && mocha tests/*"
|
|
37
|
+
},
|
|
38
|
+
"keywords": [],
|
|
39
|
+
"author": "",
|
|
40
|
+
"license": "ISC",
|
|
41
|
+
"dependencies": {
|
|
42
|
+
"@defisaver/tokens": "^1.4.56",
|
|
43
|
+
"decimal.js": "^10.4.3"
|
|
44
|
+
},
|
|
45
|
+
"devDependencies": {
|
|
46
|
+
"@defisaver/eslint-config": "^1.0.1",
|
|
47
|
+
"chai": "^4.3.8",
|
|
48
|
+
"dotenv": "^16.3.1",
|
|
49
|
+
"eslint": "^8.49.0",
|
|
50
|
+
"mocha": "^10.2.0",
|
|
51
|
+
"typechain": "^8.3.1",
|
|
52
|
+
"typechain-target-web3-v1-3mihai3": "^6.0.2",
|
|
53
|
+
"typescript": "^5.2.2"
|
|
54
|
+
},
|
|
55
|
+
"peerDependencies": {
|
|
56
|
+
"web3": "^1.10.2"
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
yarn manifest:
|
|
61
|
+
No manifest
|
|
62
|
+
|
|
63
|
+
Lockfile:
|
|
64
|
+
No lockfile
|