@gearbox-protocol/sdk 13.0.0-beta.1 → 13.0.0-beta.3

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.
@@ -1,7 +1,9 @@
1
1
  "use strict";
2
+ var __create = Object.create;
2
3
  var __defProp = Object.defineProperty;
3
4
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
5
  var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
5
7
  var __hasOwnProp = Object.prototype.hasOwnProperty;
6
8
  var __export = (target, all) => {
7
9
  for (var name in all)
@@ -15,6 +17,14 @@ var __copyProps = (to, from, except, desc) => {
15
17
  }
16
18
  return to;
17
19
  };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
18
28
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
29
  var formatter_exports = {};
20
30
  __export(formatter_exports, {
@@ -22,14 +32,23 @@ __export(formatter_exports, {
22
32
  formatBN: () => formatBN,
23
33
  formatBNvalue: () => formatBNvalue,
24
34
  formatDuration: () => formatDuration,
35
+ formatLeverage: () => formatLeverage,
25
36
  formatNumberToString_: () => formatNumberToString_,
37
+ formatPercentage: () => formatPercentage,
26
38
  formatTimestamp: () => formatTimestamp,
27
39
  numberWithCommas: () => numberWithCommas,
28
40
  percentFmt: () => percentFmt,
29
- toBigInt: () => toBigInt
41
+ rayToNumber: () => rayToNumber,
42
+ shortAddress: () => shortAddress,
43
+ shortHash: () => shortHash,
44
+ toBN: () => toBN,
45
+ toBigInt: () => toBigInt,
46
+ toSignificant: () => toSignificant
30
47
  });
31
48
  module.exports = __toCommonJS(formatter_exports);
32
49
  var import_date_fns = require("date-fns");
50
+ var import_decimal = __toESM(require("decimal.js-light"));
51
+ var import__ = require("../index.js");
33
52
  const toBigInt = (v) => {
34
53
  const value = typeof v === "object" && v.type === "BigNumber" ? v.hex : v.toString();
35
54
  return BigInt(value);
@@ -129,15 +148,49 @@ function formatTimestamp(timestamp, raw = true) {
129
148
  });
130
149
  return raw ? `${result} [${timestamp}]` : result;
131
150
  }
151
+ function rayToNumber(num) {
152
+ return Number(toBigInt(num) / 10n ** 21n) / 1e6;
153
+ }
154
+ function toSignificant(num, decimals, precision = 6) {
155
+ if (num === 1n) return "0";
156
+ const divider = new import_decimal.default(10).toPower(decimals);
157
+ const number = new import_decimal.default(num.toString()).div(divider);
158
+ return number.toSignificantDigits(precision, 4).toString();
159
+ }
160
+ function toBN(num, decimals) {
161
+ if (num === "") return 0n;
162
+ const multiplier = new import_decimal.default(10).toPower(decimals);
163
+ const number = new import_decimal.default(num).mul(multiplier);
164
+ return BigInt(number.toFixed(0));
165
+ }
166
+ function shortAddress(address) {
167
+ return address === void 0 ? "" : `${address.slice(0, 6)}...${address.slice(address.length - 4)}`;
168
+ }
169
+ function shortHash(address) {
170
+ return address === void 0 ? "" : `${address.slice(0, 5)}...`;
171
+ }
172
+ function formatPercentage(healthFactor, decimals = 2) {
173
+ return (healthFactor / Number(import__.PERCENTAGE_FACTOR)).toFixed(decimals);
174
+ }
175
+ function formatLeverage(leverage, decimals = 2) {
176
+ return (leverage / Number(import__.LEVERAGE_DECIMALS)).toFixed(decimals);
177
+ }
132
178
  // Annotate the CommonJS export names for ESM import in node:
133
179
  0 && (module.exports = {
134
180
  fmtBinaryMask,
135
181
  formatBN,
136
182
  formatBNvalue,
137
183
  formatDuration,
184
+ formatLeverage,
138
185
  formatNumberToString_,
186
+ formatPercentage,
139
187
  formatTimestamp,
140
188
  numberWithCommas,
141
189
  percentFmt,
142
- toBigInt
190
+ rayToNumber,
191
+ shortAddress,
192
+ shortHash,
193
+ toBN,
194
+ toBigInt,
195
+ toSignificant
143
196
  });
@@ -18,9 +18,13 @@ module.exports = __toCommonJS(utils_exports);
18
18
  __reExport(utils_exports, require("./AddressMap.js"), module.exports);
19
19
  __reExport(utils_exports, require("./AddressSet.js"), module.exports);
20
20
  __reExport(utils_exports, require("./abi-decode.js"), module.exports);
21
+ __reExport(utils_exports, require("./assetsMath.js"), module.exports);
22
+ __reExport(utils_exports, require("./bigintMath.js"), module.exports);
21
23
  __reExport(utils_exports, require("./bytes32ToString.js"), module.exports);
22
24
  __reExport(utils_exports, require("./childLogger.js"), module.exports);
23
25
  __reExport(utils_exports, require("./createRawTx.js"), module.exports);
26
+ __reExport(utils_exports, require("./creditAccount.js"), module.exports);
27
+ __reExport(utils_exports, require("./endpoints.js"), module.exports);
24
28
  __reExport(utils_exports, require("./etherscan.js"), module.exports);
25
29
  __reExport(utils_exports, require("./filterDust.js"), module.exports);
26
30
  __reExport(utils_exports, require("./formatter.js"), module.exports);
@@ -28,6 +32,7 @@ __reExport(utils_exports, require("./hex.js"), module.exports);
28
32
  __reExport(utils_exports, require("./isDust.js"), module.exports);
29
33
  __reExport(utils_exports, require("./json.js"), module.exports);
30
34
  __reExport(utils_exports, require("./mappers.js"), module.exports);
35
+ __reExport(utils_exports, require("./priceMath.js"), module.exports);
31
36
  __reExport(utils_exports, require("./retry.js"), module.exports);
32
37
  __reExport(utils_exports, require("./toAddress.js"), module.exports);
33
38
  __reExport(utils_exports, require("./type-utils.js"), module.exports);
@@ -37,9 +42,13 @@ __reExport(utils_exports, require("./zod.js"), module.exports);
37
42
  ...require("./AddressMap.js"),
38
43
  ...require("./AddressSet.js"),
39
44
  ...require("./abi-decode.js"),
45
+ ...require("./assetsMath.js"),
46
+ ...require("./bigintMath.js"),
40
47
  ...require("./bytes32ToString.js"),
41
48
  ...require("./childLogger.js"),
42
49
  ...require("./createRawTx.js"),
50
+ ...require("./creditAccount.js"),
51
+ ...require("./endpoints.js"),
43
52
  ...require("./etherscan.js"),
44
53
  ...require("./filterDust.js"),
45
54
  ...require("./formatter.js"),
@@ -47,6 +56,7 @@ __reExport(utils_exports, require("./zod.js"), module.exports);
47
56
  ...require("./isDust.js"),
48
57
  ...require("./json.js"),
49
58
  ...require("./mappers.js"),
59
+ ...require("./priceMath.js"),
50
60
  ...require("./retry.js"),
51
61
  ...require("./toAddress.js"),
52
62
  ...require("./type-utils.js"),
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var priceMath_exports = {};
20
+ __export(priceMath_exports, {
21
+ PriceUtils: () => PriceUtils
22
+ });
23
+ module.exports = __toCommonJS(priceMath_exports);
24
+ var import__ = require("../index.js");
25
+ class PriceUtils {
26
+ static calcTotalPrice = (price, amount, decimals = 18) => amount * import__.WAD * price / 10n ** BigInt(decimals) / import__.PRICE_DECIMALS;
27
+ static convertByPrice(totalMoney, { price: targetPrice, decimals: targetDecimals = 18 }) {
28
+ if (targetPrice <= 0n) return 0n;
29
+ return totalMoney * 10n ** BigInt(targetDecimals) * import__.PRICE_DECIMALS / targetPrice / import__.WAD;
30
+ }
31
+ }
32
+ // Annotate the CommonJS export names for ESM import in node:
33
+ 0 && (module.exports = {
34
+ PriceUtils
35
+ });
@@ -0,0 +1,125 @@
1
+ import { BigIntMath, PriceUtils } from "../index.js";
2
+ import { CreditAccountDataUtils } from "./creditAccount.js";
3
+ class AssetUtils {
4
+ static nextAsset({
5
+ allowedTokens,
6
+ selectedAssets,
7
+ balances,
8
+ tokensList,
9
+ prices = {}
10
+ }) {
11
+ const selectedRecord = selectedAssets.reduce(
12
+ (acc, { token }) => {
13
+ acc[token.toLowerCase()] = true;
14
+ return acc;
15
+ },
16
+ {}
17
+ );
18
+ const notSelected = allowedTokens.filter((allowedToken) => {
19
+ const alreadySelected = selectedRecord[allowedToken.toLowerCase()];
20
+ return !alreadySelected;
21
+ });
22
+ const sorted = CreditAccountDataUtils.sortBalances(
23
+ AssetUtils.getBalances(notSelected, balances),
24
+ prices,
25
+ tokensList
26
+ );
27
+ const [address] = sorted[0] || [];
28
+ return address;
29
+ }
30
+ static getBalances(allowedTokens, externalBalances) {
31
+ return allowedTokens.reduce((acc, address) => {
32
+ const addressLc = address.toLowerCase();
33
+ return {
34
+ ...acc,
35
+ [addressLc]: externalBalances[addressLc] || 0n
36
+ };
37
+ }, {});
38
+ }
39
+ static constructAssetRecord(a) {
40
+ const record = a.reduce((acc, asset) => {
41
+ acc[asset.token] = asset;
42
+ return acc;
43
+ }, {});
44
+ return record;
45
+ }
46
+ static memoWrap = (unwrappedAddress, wrappedAddress, prices, tokensList) => function wrap(assets) {
47
+ const assetsRecord = AssetUtils.constructAssetRecord(assets);
48
+ const unwrapped = assetsRecord[unwrappedAddress];
49
+ const wrapped = assetsRecord[wrappedAddress];
50
+ const { balance: wrappedAmount = 0n } = wrapped || {};
51
+ if (unwrapped) {
52
+ const { balance: unwrappedAmount = 0n } = unwrapped || {};
53
+ const unwrappedToken = tokensList[unwrappedAddress];
54
+ const unwrappedPrice = prices[unwrappedAddress] || 0n;
55
+ const wrappedToken = tokensList[wrappedAddress];
56
+ const wrappedPrice = prices[wrappedAddress] || 0n;
57
+ const unwrappedInWrapped = PriceUtils.convertByPrice(
58
+ PriceUtils.calcTotalPrice(
59
+ unwrappedPrice,
60
+ BigIntMath.max(0n, unwrappedAmount),
61
+ unwrappedToken.decimals
62
+ ),
63
+ {
64
+ price: wrappedPrice,
65
+ decimals: wrappedToken.decimals
66
+ }
67
+ );
68
+ assetsRecord[wrappedAddress] = {
69
+ token: wrappedAddress,
70
+ balance: BigIntMath.max(0n, wrappedAmount) + unwrappedInWrapped
71
+ };
72
+ delete assetsRecord[unwrappedAddress];
73
+ return [Object.values(assetsRecord), unwrappedInWrapped, wrappedAmount];
74
+ }
75
+ return [Object.values(assetsRecord), 0n, wrappedAmount];
76
+ };
77
+ /**
78
+ * Sums the the second assets list into the first assets list
79
+ * Balances cant be negative; creates new assets.
80
+ */
81
+ static sumAssets(a, b) {
82
+ const aRecord = AssetUtils.constructAssetRecord(a);
83
+ const resRecord = b.reduce((acc, bAsset) => {
84
+ const aAsset = acc[bAsset.token];
85
+ const { balance: amount = 0n } = aAsset || {};
86
+ const amountSum = BigIntMath.max(0n, bAsset.balance) + BigIntMath.max(0n, amount);
87
+ const aOrB = aAsset || bAsset;
88
+ acc[bAsset.token] = {
89
+ ...aOrB,
90
+ balance: amountSum
91
+ };
92
+ return acc;
93
+ }, aRecord);
94
+ return Object.values(resRecord);
95
+ }
96
+ /**
97
+ * Sums the the second assets list into the first assets list
98
+ * Balances cant be negative; doesn't create new assets.
99
+ */
100
+ static addBalances(a, b) {
101
+ const bRecord = AssetUtils.constructAssetRecord(b);
102
+ return a.map((asset) => {
103
+ const bAsset = bRecord[asset.token];
104
+ const { balance: bAmount = 0n } = bAsset || {};
105
+ const amountSum = BigIntMath.max(0n, asset.balance) + BigIntMath.max(0n, bAmount);
106
+ return { ...asset, balance: amountSum };
107
+ });
108
+ }
109
+ /**
110
+ * Subtracts the the second assets list from the first assets list
111
+ * Balances cant be negative; doesn't create new assets.
112
+ */
113
+ static subAssets(a, b) {
114
+ const bRecord = AssetUtils.constructAssetRecord(b);
115
+ return a.map((asset) => {
116
+ const bAsset = bRecord[asset.token];
117
+ const { balance: bAmount = 0n } = bAsset || {};
118
+ const amountSub = BigIntMath.max(0n, asset.balance) - BigIntMath.max(0n, bAmount);
119
+ return { ...asset, balance: BigIntMath.max(0n, amountSub) };
120
+ });
121
+ }
122
+ }
123
+ export {
124
+ AssetUtils
125
+ };
@@ -0,0 +1,9 @@
1
+ class BigIntMath {
2
+ static abs = (x) => x < 0n ? -x : x;
3
+ static max = (a, b) => a > b ? a : b;
4
+ static min = (a, b) => a < b ? a : b;
5
+ static neg = (a) => a > 0 ? a * -1n : a;
6
+ }
7
+ export {
8
+ BigIntMath
9
+ };