@defillama/sdk 5.0.9 → 5.0.11

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.
@@ -30,5 +30,10 @@ export declare class Balances {
30
30
  usdTokenBalances: BalancesV1;
31
31
  rawTokenBalances: BalancesV1;
32
32
  }>;
33
+ static getUSDValue(balances: BalancesV1, timestamp?: number): Promise<number>;
34
+ static getBalanceObjects(balances: BalancesV1, timestamp?: number): Promise<{
35
+ usdTvl: number;
36
+ usdTokenBalances: BalancesV1;
37
+ }>;
33
38
  }
34
39
  export default Balances;
package/build/Balances.js CHANGED
@@ -1,60 +1,23 @@
1
1
  "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- var __generator = (this && this.__generator) || function (thisArg, body) {
12
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
- function verb(n) { return function (v) { return step([n, v]); }; }
15
- function step(op) {
16
- if (f) throw new TypeError("Generator is already executing.");
17
- while (g && (g = 0, op[0] && (_ = 0)), _) try {
18
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
- if (y = 0, t) op = [op[0] & 2, t.value];
20
- switch (op[0]) {
21
- case 0: case 1: t = op; break;
22
- case 4: _.label++; return { value: op[1], done: false };
23
- case 5: _.label++; y = op[1]; op = [0]; continue;
24
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
- default:
26
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
- if (t[2]) _.ops.pop();
31
- _.trys.pop(); continue;
32
- }
33
- op = body.call(thisArg, _);
34
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
- }
37
- };
38
2
  var __importDefault = (this && this.__importDefault) || function (mod) {
39
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
40
4
  };
41
5
  Object.defineProperty(exports, "__esModule", { value: true });
42
6
  exports.Balances = void 0;
43
- var generalUtil_1 = require("./generalUtil");
44
- var computeTVL_1 = __importDefault(require("./util/computeTVL"));
45
- var nullAddress = '0x0000000000000000000000000000000000000000';
46
- var Balances = /** @class */ (function () {
47
- function Balances(params) {
7
+ const generalUtil_1 = require("./generalUtil");
8
+ const computeTVL_1 = __importDefault(require("./util/computeTVL"));
9
+ const nullAddress = '0x0000000000000000000000000000000000000000';
10
+ class Balances {
11
+ constructor(params) {
48
12
  var _a;
49
13
  this.chain = (_a = params.chain) !== null && _a !== void 0 ? _a : 'ethereum';
50
14
  this.timestamp = params.timestamp;
51
15
  this._balances = {};
52
16
  }
53
- Balances.prototype._add = function (token, balance, _a) {
54
- var _b = _a === void 0 ? {} : _a, _c = _b.skipChain, skipChain = _c === void 0 ? false : _c;
17
+ _add(token, balance, { skipChain = false } = {}) {
55
18
  token = token.replace(/\//g, ':');
56
- var isIBCToken = token.startsWith('ibc:');
57
- var chain = this.chain;
19
+ const isIBCToken = token.startsWith('ibc:');
20
+ let chain = this.chain;
58
21
  if (skipChain || isIBCToken) {
59
22
  chain = undefined;
60
23
  }
@@ -63,48 +26,42 @@ var Balances = /** @class */ (function () {
63
26
  token = token.replace('peggy', '');
64
27
  }
65
28
  (0, generalUtil_1.sumSingleBalance)(this._balances, token, balance, chain);
66
- };
67
- Balances.prototype.add = function (token, balance, _a) {
68
- var _this = this;
69
- var _b = _a === void 0 ? {} : _a, _c = _b.skipChain, skipChain = _c === void 0 ? false : _c;
29
+ }
30
+ add(token, balance, { skipChain = false } = {}) {
70
31
  if (!token)
71
32
  throw new Error('token is required');
72
33
  if (!balance)
73
34
  return;
74
35
  if (Array.isArray(balance)) {
75
36
  if (Array.isArray(token)) {
76
- this.addTokens(token, balance, { skipChain: skipChain });
37
+ this.addTokens(token, balance, { skipChain });
77
38
  return;
78
39
  }
79
40
  else if (typeof token === 'string') {
80
- balance.forEach(function (v) { return _this._add(token, v, { skipChain: skipChain }); });
41
+ balance.forEach((v) => this._add(token, v, { skipChain }));
81
42
  return;
82
43
  }
83
44
  }
84
45
  if (typeof token !== 'string')
85
46
  throw new Error('token must be a string');
86
- this._add(token, balance, { skipChain: skipChain });
87
- };
88
- Balances.prototype.addToken = function (token, balance, _a) {
89
- var _b = _a === void 0 ? {} : _a, _c = _b.skipChain, skipChain = _c === void 0 ? false : _c;
90
- this._add(token, balance, { skipChain: skipChain });
91
- };
92
- Balances.prototype.addGasToken = function (balance) {
47
+ this._add(token, balance, { skipChain });
48
+ }
49
+ addToken(token, balance, { skipChain = false } = {}) {
50
+ this._add(token, balance, { skipChain });
51
+ }
52
+ addGasToken(balance) {
93
53
  this._add(nullAddress, balance);
94
- };
95
- Balances.prototype.addTokens = function (tokens, balances, _a) {
96
- var _this = this;
97
- var _b = _a === void 0 ? {} : _a, _c = _b.skipChain, skipChain = _c === void 0 ? false : _c;
54
+ }
55
+ addTokens(tokens, balances, { skipChain = false } = {}) {
98
56
  if (!Array.isArray(tokens))
99
57
  throw new Error('tokens must be an array');
100
58
  if (!Array.isArray(balances))
101
59
  throw new Error('balances must be an array');
102
60
  if (tokens.length !== balances.length)
103
61
  throw new Error('token and balance must have the same length');
104
- tokens.forEach(function (v, i) { return _this._add(v, balances[i], { skipChain: skipChain }); });
105
- };
106
- Balances.prototype.addBalances = function (balances) {
107
- var _this = this;
62
+ tokens.forEach((v, i) => this._add(v, balances[i], { skipChain }));
63
+ }
64
+ addBalances(balances) {
108
65
  if (balances instanceof Balances) {
109
66
  if (balances === this)
110
67
  return;
@@ -112,49 +69,32 @@ var Balances = /** @class */ (function () {
112
69
  }
113
70
  if (balances === this._balances)
114
71
  return;
115
- Object.entries(balances).forEach(function (_a) {
116
- var token = _a[0], balance = _a[1];
117
- return _this._add(token, balance, { skipChain: true });
118
- });
119
- };
120
- Balances.prototype.getBalances = function () {
72
+ Object.entries(balances).forEach(([token, balance]) => this._add(token, balance, { skipChain: true }));
73
+ }
74
+ getBalances() {
121
75
  return this._balances;
122
- };
123
- Balances.prototype.removeTokenBalance = function (token) {
124
- var _this = this;
125
- var regex = new RegExp(token, 'i');
126
- Object.keys(this._balances).forEach(function (i) {
76
+ }
77
+ removeTokenBalance(token) {
78
+ const regex = new RegExp(token, 'i');
79
+ Object.keys(this._balances).forEach((i) => {
127
80
  if (regex.test(i))
128
- delete _this._balances[i];
129
- });
130
- };
131
- Balances.prototype.getUSDValue = function () {
132
- return __awaiter(this, void 0, void 0, function () {
133
- var usdTvl;
134
- return __generator(this, function (_a) {
135
- switch (_a.label) {
136
- case 0: return [4 /*yield*/, (0, computeTVL_1.default)(this.getBalances(), this.timestamp)];
137
- case 1:
138
- usdTvl = (_a.sent()).usdTvl;
139
- return [2 /*return*/, usdTvl];
140
- }
141
- });
81
+ delete this._balances[i];
142
82
  });
143
- };
144
- Balances.prototype.getUSDJSONs = function () {
145
- return __awaiter(this, void 0, void 0, function () {
146
- var _a, usdTvl, usdTokenBalances;
147
- return __generator(this, function (_b) {
148
- switch (_b.label) {
149
- case 0: return [4 /*yield*/, (0, computeTVL_1.default)(this.getBalances(), this.timestamp)];
150
- case 1:
151
- _a = _b.sent(), usdTvl = _a.usdTvl, usdTokenBalances = _a.usdTokenBalances;
152
- return [2 /*return*/, { usdTvl: usdTvl, usdTokenBalances: usdTokenBalances, rawTokenBalances: this.getBalances() }];
153
- }
154
- });
155
- });
156
- };
157
- return Balances;
158
- }());
83
+ }
84
+ async getUSDValue() {
85
+ const { usdTvl } = await (0, computeTVL_1.default)(this.getBalances(), this.timestamp);
86
+ return usdTvl;
87
+ }
88
+ async getUSDJSONs() {
89
+ const { usdTvl, usdTokenBalances } = await (0, computeTVL_1.default)(this.getBalances(), this.timestamp);
90
+ return { usdTvl, usdTokenBalances, rawTokenBalances: this.getBalances() };
91
+ }
92
+ static async getUSDValue(balances, timestamp) {
93
+ return (await (0, computeTVL_1.default)(balances, timestamp)).usdTvl;
94
+ }
95
+ static async getBalanceObjects(balances, timestamp) {
96
+ return (0, computeTVL_1.default)(balances, timestamp);
97
+ }
98
+ }
159
99
  exports.Balances = Balances;
160
100
  exports.default = Balances;
@@ -64,5 +64,6 @@ export declare class ChainApi {
64
64
  rawTokenBalances: BalancesV1;
65
65
  }>;
66
66
  getLogs(params: GetLogsOptions): Promise<any[] | import("ethers").EventLog[] | import("ethers").EventLog[][]>;
67
+ getBalancesV2(): Balances;
67
68
  }
68
69
  export default ChainApi;