@defillama/sdk 4.0.70 → 4.0.71

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.
@@ -0,0 +1,29 @@
1
+ import { Balances as BalancesV1 } from "./types";
2
+ import { Chain } from "./general";
3
+ export declare class Balances {
4
+ chain: Chain | string;
5
+ timestamp?: number;
6
+ _balances: BalancesV1;
7
+ constructor(params: {
8
+ chain?: Chain | string;
9
+ timestamp?: number;
10
+ });
11
+ _add(token: string, balance: any, { skipChain }?: {
12
+ skipChain?: boolean | undefined;
13
+ }): void;
14
+ add(token: string | string[], balance: any, { skipChain }?: {
15
+ skipChain?: boolean | undefined;
16
+ }): void;
17
+ addToken(token: string, balance: any, { skipChain }?: {
18
+ skipChain?: boolean | undefined;
19
+ }): void;
20
+ addGasToken(balance: any): void;
21
+ addTokens(tokens: string[], balances: any[], { skipChain }?: {
22
+ skipChain?: boolean | undefined;
23
+ }): void;
24
+ addBalances(balances: BalancesV1 | Balances): void;
25
+ getBalances(): BalancesV1;
26
+ removeTokenBalance(token: string): void;
27
+ getUSDValue(): Promise<number>;
28
+ }
29
+ export default Balances;
@@ -0,0 +1,147 @@
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
+ var __importDefault = (this && this.__importDefault) || function (mod) {
39
+ return (mod && mod.__esModule) ? mod : { "default": mod };
40
+ };
41
+ Object.defineProperty(exports, "__esModule", { value: true });
42
+ 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) {
48
+ var _a;
49
+ this.chain = (_a = params.chain) !== null && _a !== void 0 ? _a : 'ethereum';
50
+ this.timestamp = params.timestamp;
51
+ this._balances = {};
52
+ }
53
+ Balances.prototype._add = function (token, balance, _a) {
54
+ var _b = _a === void 0 ? {} : _a, _c = _b.skipChain, skipChain = _c === void 0 ? false : _c;
55
+ token = token.replace(/\//g, ':');
56
+ var isIBCToken = token.startsWith('ibc:');
57
+ var chain = this.chain;
58
+ if (skipChain || isIBCToken) {
59
+ chain = undefined;
60
+ }
61
+ if (chain === 'injective' && token.startsWith('peggy0x')) {
62
+ chain = 'ethereum';
63
+ token = token.replace('peggy', '');
64
+ }
65
+ (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;
70
+ if (!token)
71
+ throw new Error('token is required');
72
+ if (!balance)
73
+ throw new Error('balance is required');
74
+ if (Array.isArray(balance)) {
75
+ if (Array.isArray(token)) {
76
+ this.addTokens(token, balance, { skipChain: skipChain });
77
+ return;
78
+ }
79
+ else if (typeof token === 'string') {
80
+ balance.forEach(function (v) { return _this._add(token, v, { skipChain: skipChain }); });
81
+ return;
82
+ }
83
+ }
84
+ if (typeof token !== 'string')
85
+ 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) {
93
+ 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;
98
+ if (!Array.isArray(tokens))
99
+ throw new Error('tokens must be an array');
100
+ if (!Array.isArray(balances))
101
+ throw new Error('balances must be an array');
102
+ if (tokens.length !== balances.length)
103
+ 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;
108
+ if (balances instanceof Balances) {
109
+ if (balances === this)
110
+ return;
111
+ balances = balances.getBalances();
112
+ }
113
+ if (balances === this._balances)
114
+ 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 () {
121
+ 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) {
127
+ 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
+ });
142
+ });
143
+ };
144
+ return Balances;
145
+ }());
146
+ exports.Balances = Balances;
147
+ exports.default = Balances;
@@ -1,4 +1,5 @@
1
- import { Block, CallOptions, MulticallOptions, FetchListOptions, Balances, ByteCodeCallOptions } from "./types";
1
+ import { Block, CallOptions, MulticallOptions, FetchListOptions, ByteCodeCallOptions, Balances as BalancesV1 } from "./types";
2
+ import Balances from "./Balances";
2
3
  import { Chain } from "./general";
3
4
  import { ethers } from "ethers";
4
5
  export declare class ChainApi {
@@ -21,17 +22,18 @@ export declare class ChainApi {
21
22
  getBlock(): Promise<number>;
22
23
  log(...args: any): void;
23
24
  logTable(...args: any): void;
24
- add(token: string, balance: any, { skipChain }?: {
25
+ add(token: any, balance: any, { skipChain }?: {
25
26
  skipChain?: boolean | undefined;
26
27
  }): void;
27
28
  addToken(token: string, balance: any, { skipChain }?: {
28
29
  skipChain?: boolean | undefined;
29
30
  }): void;
31
+ addGasToken(balance: any): void;
30
32
  addTokens(tokens: string[], balances: any[], { skipChain }?: {
31
33
  skipChain?: boolean | undefined;
32
34
  }): void;
33
- addBalances(balances: Balances): void;
34
- getBalances(): Balances;
35
+ addBalances(balances: Balances | BalancesV1): void;
36
+ getBalances(): BalancesV1;
35
37
  getChainId(): number | undefined;
36
38
  removeTokenBalance(token: string): void;
37
39
  sumTokens({ tokens, owners, owner, tokensAndOwners, tokensAndOwners2, blacklistedTokens, blacklistedOwners, ownerTokens, }: {
@@ -43,6 +45,6 @@ export declare class ChainApi {
43
45
  ownerTokens?: any[];
44
46
  blacklistedTokens?: string[];
45
47
  blacklistedOwners?: string[];
46
- }): Promise<Balances>;
48
+ }): Promise<BalancesV1>;
47
49
  }
48
50
  export default ChainApi;
package/build/ChainApi.js CHANGED
@@ -51,6 +51,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
51
51
  };
52
52
  Object.defineProperty(exports, "__esModule", { value: true });
53
53
  exports.ChainApi = void 0;
54
+ var Balances_1 = __importDefault(require("./Balances"));
54
55
  var general_1 = require("./general");
55
56
  var abi2_1 = require("./abi/abi2");
56
57
  var blocks_1 = require("./computeTVL/blocks");
@@ -67,7 +68,7 @@ var ChainApi = /** @class */ (function () {
67
68
  this.chain = (_a = params.chain) !== null && _a !== void 0 ? _a : 'ethereum';
68
69
  this.timestamp = params.timestamp;
69
70
  this.provider = (0, general_1.getProvider)(this.chain);
70
- this._balances = {};
71
+ this._balances = new Balances_1.default({ chain: this.chain, timestamp: this.timestamp });
71
72
  // @ts-ignore
72
73
  this.chainId = (_b = providers_json_1.default[this.chain]) === null || _b === void 0 ? void 0 : _b.chainId;
73
74
  }
@@ -120,49 +121,30 @@ var ChainApi = /** @class */ (function () {
120
121
  };
121
122
  ChainApi.prototype.add = function (token, balance, _a) {
122
123
  var _b = _a === void 0 ? {} : _a, _c = _b.skipChain, skipChain = _c === void 0 ? false : _c;
123
- token = token.replace(/\//g, ':');
124
- var isIBCToken = token.startsWith('ibc:');
125
- var chain = this.chain;
126
- if (skipChain || isIBCToken) {
127
- chain = undefined;
128
- }
129
- if (chain === 'injective' && token.startsWith('peggy0x')) {
130
- chain = 'ethereum';
131
- token = token.replace('peggy', '');
132
- }
133
- (0, generalUtil_1.sumSingleBalance)(this._balances, token, balance, chain);
124
+ this._balances.add(token, balance, { skipChain: skipChain });
134
125
  };
135
126
  ChainApi.prototype.addToken = function (token, balance, _a) {
136
127
  var _b = _a === void 0 ? {} : _a, _c = _b.skipChain, skipChain = _c === void 0 ? false : _c;
137
128
  this.add(token, balance, { skipChain: skipChain });
138
129
  };
130
+ ChainApi.prototype.addGasToken = function (balance) {
131
+ this.add(nullAddress, balance);
132
+ };
139
133
  ChainApi.prototype.addTokens = function (tokens, balances, _a) {
140
- var _this = this;
141
134
  var _b = _a === void 0 ? {} : _a, _c = _b.skipChain, skipChain = _c === void 0 ? false : _c;
142
- tokens.forEach(function (v, i) { return _this.add(v, balances[i], { skipChain: skipChain }); });
135
+ this.add(tokens, balances, { skipChain: skipChain });
143
136
  };
144
137
  ChainApi.prototype.addBalances = function (balances) {
145
- var _this = this;
146
- if (balances === this._balances)
147
- return;
148
- Object.entries(balances).forEach(function (_a) {
149
- var token = _a[0], balance = _a[1];
150
- return (0, generalUtil_1.sumSingleBalance)(_this._balances, token, balance);
151
- });
138
+ this._balances.addBalances(balances);
152
139
  };
153
140
  ChainApi.prototype.getBalances = function () {
154
- return this._balances;
141
+ return this._balances.getBalances();
155
142
  };
156
143
  ChainApi.prototype.getChainId = function () {
157
144
  return this.chainId;
158
145
  };
159
146
  ChainApi.prototype.removeTokenBalance = function (token) {
160
- var _this = this;
161
- var regex = new RegExp(token, 'i');
162
- Object.keys(this._balances).forEach(function (i) {
163
- if (regex.test(i))
164
- delete _this._balances[i];
165
- });
147
+ this._balances.removeTokenBalance(token);
166
148
  };
167
149
  ChainApi.prototype.sumTokens = function (_a) {
168
150
  var _b = _a.tokens, tokens = _b === void 0 ? [] : _b, _c = _a.owners, owners = _c === void 0 ? [] : _c, owner = _a.owner, _d = _a.tokensAndOwners, tokensAndOwners = _d === void 0 ? [] : _d, _e = _a.tokensAndOwners2, tokensAndOwners2 = _e === void 0 ? [] : _e, _f = _a.blacklistedTokens, blacklistedTokens = _f === void 0 ? [] : _f, _g = _a.blacklistedOwners, blacklistedOwners = _g === void 0 ? [] : _g, _h = _a.ownerTokens, ownerTokens = _h === void 0 ? [] : _h;
package/build/index.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import { debugLog, debugTable } from "./util/debugLog";
2
2
  export { ChainApi } from './ChainApi';
3
+ export { Balances } from './Balances';
3
4
  export { getProvider, setProvider } from './general';
4
5
  export * as util from "./generalUtil";
5
6
  export * as cache from "./util/cache";
package/build/index.js CHANGED
@@ -23,10 +23,12 @@ var __importStar = (this && this.__importStar) || function (mod) {
23
23
  return result;
24
24
  };
25
25
  Object.defineProperty(exports, "__esModule", { value: true });
26
- exports.humanizeNumber = exports.logTable = exports.log = exports.blocks = exports.api2 = exports.api = exports.sdkCache = exports.cache = exports.util = exports.setProvider = exports.getProvider = exports.ChainApi = void 0;
26
+ exports.humanizeNumber = exports.logTable = exports.log = exports.blocks = exports.api2 = exports.api = exports.sdkCache = exports.cache = exports.util = exports.setProvider = exports.getProvider = exports.Balances = exports.ChainApi = void 0;
27
27
  var debugLog_1 = require("./util/debugLog");
28
28
  var ChainApi_1 = require("./ChainApi");
29
29
  Object.defineProperty(exports, "ChainApi", { enumerable: true, get: function () { return ChainApi_1.ChainApi; } });
30
+ var Balances_1 = require("./Balances");
31
+ Object.defineProperty(exports, "Balances", { enumerable: true, get: function () { return Balances_1.Balances; } });
30
32
  var general_1 = require("./general");
31
33
  Object.defineProperty(exports, "getProvider", { enumerable: true, get: function () { return general_1.getProvider; } });
32
34
  Object.defineProperty(exports, "setProvider", { enumerable: true, get: function () { return general_1.setProvider; } });