@defillama/sdk 5.0.150 → 5.0.152

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.
@@ -48,11 +48,9 @@ export declare class Balances {
48
48
  usdTokenBalances: BalancesV1;
49
49
  rawTokenBalances: BalancesV1;
50
50
  }>;
51
- clone(ratio?: number): Balances;
52
- subtract(balances: BalancesV1 | Balances): void;
53
- subtractToken(token: string, balance: any, { skipChain }?: {
54
- skipChain?: boolean | undefined;
55
- }): void;
51
+ clone(ratio?: number, optionsOrLabel?: BalancesOptionsWithLabel, options?: BalancesOptions): Balances;
52
+ subtract(balances: BalancesV1 | Balances, optionsOrLabel?: BalancesOptionsWithLabel, options?: BalancesOptions): void;
53
+ subtractToken(token: string, balance: any, optionsOrLabel?: BalancesOptionsWithLabel, options?: BalancesOptions): void;
56
54
  removeNegativeBalances(): void;
57
55
  _breakdownBalancesAction(action: string, args?: any[]): Promise<{
58
56
  [key: string]: any;
package/build/Balances.js CHANGED
@@ -177,25 +177,28 @@ class Balances {
177
177
  const { usdTvl, usdTokenBalances } = await (0, computeTVL_1.default)(balances, timestamp);
178
178
  return { usdTvl, usdTokenBalances, rawTokenBalances: balances };
179
179
  }
180
- clone(ratio = 1) {
180
+ clone(ratio = 1, optionsOrLabel, options) {
181
+ options = getOptions({ optionsOrLabel, options });
181
182
  const newBalances = new Balances({ chain: this.chain, timestamp: this.timestamp });
182
- newBalances.addBalances(this);
183
+ newBalances.addBalances(this, options);
183
184
  if (ratio !== 1)
184
185
  newBalances.resizeBy(ratio);
185
186
  return newBalances;
186
187
  }
187
- subtract(balances) {
188
+ subtract(balances, optionsOrLabel, options) {
189
+ options = getOptions({ optionsOrLabel, options });
188
190
  if (balances instanceof Balances) {
189
191
  if (balances === this)
190
192
  return;
191
193
  balances = balances.getBalances();
192
194
  }
193
195
  Object.entries(balances).forEach(([token, balance]) => {
194
- this._add(token, Number(balance) * -1, { skipChain: true });
196
+ this._add(token, Number(balance) * -1, { skipChain: true, label: options.label });
195
197
  });
196
198
  }
197
- subtractToken(token, balance, { skipChain = false } = {}) {
198
- this._add(token, Number(balance) * -1, { skipChain });
199
+ subtractToken(token, balance, optionsOrLabel, options) {
200
+ options = getOptions({ optionsOrLabel, options });
201
+ this._add(token, Number(balance) * -1, options);
199
202
  }
200
203
  removeNegativeBalances() {
201
204
  Object.keys(this._balances).forEach((token) => {
@@ -12,6 +12,14 @@ type Erc4626SumOptions = {
12
12
  permitFailure?: boolean;
13
13
  isOG4626?: boolean;
14
14
  };
15
+ type DebugStats = {
16
+ total: number;
17
+ [key: string]: any;
18
+ };
19
+ type BalancesV2Options = {
20
+ skipChain?: boolean;
21
+ label?: string;
22
+ };
15
23
  export declare class ChainApi {
16
24
  block?: Block;
17
25
  chain: Chain | string;
@@ -20,13 +28,16 @@ export declare class ChainApi {
20
28
  provider: Provider;
21
29
  _balances: Balances;
22
30
  storedKey?: string;
31
+ stats: DebugStats;
23
32
  api: ChainApi;
24
33
  constructor(params: {
25
34
  block?: Block;
26
35
  chain?: Chain | string;
27
36
  storedKey?: string;
28
37
  timestamp?: number;
38
+ protocol?: string;
29
39
  });
40
+ addStat(method: string, value?: any, skipTotal?: boolean): void;
30
41
  call(params: CallOptions): Promise<any>;
31
42
  batchCall(params: CallOptions[]): Promise<any[]>;
32
43
  multiCall(params: MulticallOptions): Promise<any[]>;
@@ -35,20 +46,14 @@ export declare class ChainApi {
35
46
  getBlock(): Promise<number>;
36
47
  log(...args: any): void;
37
48
  logTable(...args: any): void;
38
- add(token: any, balance: any, { skipChain }?: {
39
- skipChain?: boolean | undefined;
40
- }): void;
41
- addToken(token: string, balance: any, { skipChain }?: {
42
- skipChain?: boolean | undefined;
43
- }): void;
44
- addGasToken(balance: any): void;
45
- addUSDValue(balance: any): void;
46
- addTokens(tokens: string[], balances: any[], { skipChain }?: {
47
- skipChain?: boolean | undefined;
48
- }): void;
49
- addBalances(balances: Balances | BalancesV1): void;
50
- addCGToken(token: string, balance: any): void;
51
- addTokenVannila(token: string, balance: any): void;
49
+ add(token: any, balance: any, options?: BalancesV2Options): void;
50
+ addToken(token: string, balance: any, options?: BalancesV2Options): void;
51
+ addGasToken(balance: any, options?: BalancesV2Options): void;
52
+ addUSDValue(balance: any, options?: BalancesV2Options): void;
53
+ addTokens(tokens: string[], balances: any[], options?: BalancesV2Options): void;
54
+ addBalances(balances: Balances | BalancesV1, options?: BalancesV2Options): void;
55
+ addCGToken(token: string, balance: any, options?: BalancesV2Options): void;
56
+ addTokenVannila(token: string, balance: any, options?: BalancesV2Options): void;
52
57
  getBalances(): BalancesV1;
53
58
  getChainId(): number | undefined;
54
59
  removeTokenBalance(token: string): void;
@@ -56,7 +61,7 @@ export declare class ChainApi {
56
61
  deleteTokens(tokens: string[]): void;
57
62
  erc4626Sum2(options: Erc4626SumOptions): Promise<BalancesV1>;
58
63
  erc4626Sum({ calls, tokenAbi, balanceAbi, balanceCalls, permitFailure, isOG4626 }: Erc4626SumOptions): Promise<BalancesV1>;
59
- sumTokens({ token, tokens, owners, owner, tokensAndOwners, tokensAndOwners2, blacklistedTokens, blacklistedOwners, ownerTokens, }: {
64
+ sumTokens({ token, tokens, owners, owner, tokensAndOwners, tokensAndOwners2, blacklistedTokens, blacklistedOwners, ownerTokens, balancesV2Options, }: {
60
65
  token?: string;
61
66
  tokens?: string[];
62
67
  owner?: string;
@@ -66,6 +71,7 @@ export declare class ChainApi {
66
71
  ownerTokens?: any[];
67
72
  blacklistedTokens?: string[];
68
73
  blacklistedOwners?: string[];
74
+ balancesV2Options?: BalancesV2Options;
69
75
  }): Promise<BalancesV1>;
70
76
  getUSDValue(): Promise<number>;
71
77
  getUSDString(): Promise<string>;
@@ -80,3 +86,4 @@ export declare class ChainApi {
80
86
  getTransactionReceipt(tx: string): Promise<import("ethers").TransactionReceipt | null>;
81
87
  }
82
88
  export default ChainApi;
89
+ export declare function getDebugStats(): Record<string, DebugStats>;
package/build/ChainApi.js CHANGED
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.ChainApi = void 0;
6
+ exports.getDebugStats = exports.ChainApi = void 0;
7
7
  const abi2_1 = require("./abi/abi2");
8
8
  const Balances_1 = __importDefault(require("./Balances"));
9
9
  const blocks_1 = require("./computeTVL/blocks");
@@ -15,6 +15,7 @@ const generalUtil_1 = require("./generalUtil");
15
15
  const debugLog_1 = require("./util/debugLog");
16
16
  const logs_1 = __importDefault(require("./util/logs"));
17
17
  const transactions_1 = require("./util/transactions");
18
+ const callStatsByProject = {};
18
19
  const nullAddress = '0x0000000000000000000000000000000000000000';
19
20
  class ChainApi {
20
21
  constructor(params) {
@@ -28,8 +29,28 @@ class ChainApi {
28
29
  this.chainId = (_b = providers_json_1.default[this.chain]) === null || _b === void 0 ? void 0 : _b.chainId;
29
30
  this.storedKey = params.storedKey;
30
31
  this.api = this;
32
+ const uKey = `${params.protocol}:${this.storedKey}`;
33
+ const stats = {
34
+ meta: {
35
+ chain: this.chain,
36
+ storedKey: this.storedKey,
37
+ protocol: params.protocol,
38
+ },
39
+ label: uKey,
40
+ total: 0,
41
+ };
42
+ callStatsByProject[uKey] = stats;
43
+ this.stats = stats;
44
+ }
45
+ addStat(method, value = 1, skipTotal = false) {
46
+ if (!skipTotal)
47
+ this.stats.total += value;
48
+ if (!this.stats[method])
49
+ this.stats[method] = 0;
50
+ this.stats[method] += value;
31
51
  }
32
52
  call(params) {
53
+ this.addStat('call');
33
54
  return (0, abi2_1.call)({
34
55
  ...params,
35
56
  block: this.block,
@@ -40,6 +61,8 @@ class ChainApi {
40
61
  return Promise.all(params.map(i => this.call(i)));
41
62
  }
42
63
  multiCall(params) {
64
+ this.addStat('multiCall', params.calls.length, true);
65
+ this.addStat('call');
43
66
  return (0, abi2_1.multiCall)({
44
67
  ...params,
45
68
  block: this.block,
@@ -47,6 +70,7 @@ class ChainApi {
47
70
  });
48
71
  }
49
72
  fetchList(params) {
73
+ this.addStat('fetchList');
50
74
  return (0, abi2_1.fetchList)({
51
75
  ...params,
52
76
  block: this.block,
@@ -61,6 +85,7 @@ class ChainApi {
61
85
  });
62
86
  }
63
87
  async getBlock() {
88
+ this.addStat('getBlock');
64
89
  if (!this.block)
65
90
  this.block = await (0, blocks_1.getBlockNumber)(this.chain, this.timestamp);
66
91
  return this.block;
@@ -71,29 +96,29 @@ class ChainApi {
71
96
  logTable(...args) {
72
97
  (0, debugLog_1.debugTable)(...args);
73
98
  }
74
- add(token, balance, { skipChain = false } = {}) {
75
- this._balances.add(token, balance, { skipChain });
99
+ add(token, balance, options = {}) {
100
+ this._balances.add(token, balance, options);
76
101
  }
77
- addToken(token, balance, { skipChain = false } = {}) {
78
- this.add(token, balance, { skipChain });
102
+ addToken(token, balance, options = {}) {
103
+ this.add(token, balance, options);
79
104
  }
80
- addGasToken(balance) {
81
- this.add(nullAddress, balance);
105
+ addGasToken(balance, options = {}) {
106
+ this.add(nullAddress, balance, options);
82
107
  }
83
- addUSDValue(balance) {
84
- this._balances.addUSDValue(balance);
108
+ addUSDValue(balance, options = {}) {
109
+ this._balances.addUSDValue(balance, options);
85
110
  }
86
- addTokens(tokens, balances, { skipChain = false } = {}) {
87
- this.add(tokens, balances, { skipChain });
111
+ addTokens(tokens, balances, options = {}) {
112
+ this.add(tokens, balances, options);
88
113
  }
89
- addBalances(balances) {
90
- this._balances.addBalances(balances);
114
+ addBalances(balances, options = {}) {
115
+ this._balances.addBalances(balances, options);
91
116
  }
92
- addCGToken(token, balance) {
93
- this._balances.addCGToken(token, balance);
117
+ addCGToken(token, balance, options = {}) {
118
+ this._balances.addCGToken(token, balance, options);
94
119
  }
95
- addTokenVannila(token, balance) {
96
- this._balances.addTokenVannila(token, balance);
120
+ addTokenVannila(token, balance, options = {}) {
121
+ this._balances.addTokenVannila(token, balance, options);
97
122
  }
98
123
  getBalances() {
99
124
  return this._balances.getBalances();
@@ -139,7 +164,7 @@ class ChainApi {
139
164
  });
140
165
  return this.getBalances();
141
166
  }
142
- async sumTokens({ token, tokens = [], owners = [], owner, tokensAndOwners = [], tokensAndOwners2 = [], blacklistedTokens = [], blacklistedOwners = [], ownerTokens = [], }) {
167
+ async sumTokens({ token, tokens = [], owners = [], owner, tokensAndOwners = [], tokensAndOwners2 = [], blacklistedTokens = [], blacklistedOwners = [], ownerTokens = [], balancesV2Options = {}, }) {
143
168
  if (tokensAndOwners2.length)
144
169
  tokensAndOwners.push(...tokensAndOwners2[0].map((i, j) => [i, tokensAndOwners2[1][j]]));
145
170
  if (token)
@@ -157,11 +182,12 @@ class ChainApi {
157
182
  tokensAndOwners = tokensAndOwners.filter(i => !blacklistedTokens.includes(i[0]) && !blacklistedOwners.includes(i[1]));
158
183
  const ethBalOwners = tokensAndOwners.filter(i => i[0] === nullAddress).map(i => i[1]);
159
184
  tokensAndOwners = tokensAndOwners.filter(i => i[0] !== nullAddress);
185
+ this.addStat('sumTokens', tokensAndOwners.length + ethBalOwners.length, true);
160
186
  const bals = await this.multiCall({
161
187
  calls: tokensAndOwners.map(i => ({ target: i[0], params: [i[1]] })),
162
188
  abi: 'erc20:balanceOf',
163
189
  });
164
- this.addTokens(tokensAndOwners.map(i => i[0]), bals);
190
+ this.addTokens(tokensAndOwners.map(i => i[0]), bals, balancesV2Options);
165
191
  if (ethBalOwners.length) {
166
192
  let ethBals = [];
167
193
  const multicallAddress = (0, multicall3_1.getMulticallAddress)(this.chain, this.block);
@@ -176,7 +202,7 @@ class ChainApi {
176
202
  const res = await (0, eth_1.getBalances)({ chain: this.chain, targets: ethBalOwners, block: this.block });
177
203
  ethBals = res.output.map((i) => i.balance);
178
204
  }
179
- ethBals.map(i => this.addToken(nullAddress, i));
205
+ ethBals.map(i => this.addToken(nullAddress, i, balancesV2Options));
180
206
  }
181
207
  return this.getBalances();
182
208
  }
@@ -190,6 +216,8 @@ class ChainApi {
190
216
  return this._balances.getUSDJSONs();
191
217
  }
192
218
  async getLogs(params) {
219
+ var _a;
220
+ this.addStat('getLogs', ((_a = params.targets) === null || _a === void 0 ? void 0 : _a.length) || 1);
193
221
  params.chain = this.chain;
194
222
  return (0, logs_1.default)(params);
195
223
  }
@@ -197,9 +225,11 @@ class ChainApi {
197
225
  return this._balances;
198
226
  }
199
227
  async getTransactions(params) {
228
+ this.addStat('getTransactions');
200
229
  return (0, transactions_1.getTransactions)({ ...params, chain: this.chain });
201
230
  }
202
231
  async getTransactionReceipt(tx) {
232
+ this.addStat('getTransactionReceipt');
203
233
  return this.provider.getTransactionReceipt(tx);
204
234
  }
205
235
  }
@@ -212,3 +242,7 @@ function getUniqueTokensAndOwners(toa, chain) {
212
242
  const uniqueMerged = (0, generalUtil_1.getUniqueAddresses)(mergedToa, chain);
213
243
  return uniqueMerged.map(i => i.split('~'));
214
244
  }
245
+ function getDebugStats() {
246
+ return callStatsByProject;
247
+ }
248
+ exports.getDebugStats = getDebugStats;
@@ -206,6 +206,7 @@ const DEPLOYMENT_BLOCK = {
206
206
  mint: 1,
207
207
  btnx: 1,
208
208
  vechain: 22103870,
209
+ tac: 1,
209
210
  };
210
211
  async function makeMultiCall(functionABI, calls, chain, block) {
211
212
  if (chain === 'tron')