@defillama/sdk 5.0.149 → 5.0.150

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,30 +1,32 @@
1
1
  import { Balances as BalancesV1 } from "./types";
2
2
  import { Chain } from "./general";
3
+ type BalancesOptions = {
4
+ skipChain?: boolean;
5
+ label?: string;
6
+ };
7
+ type BalancesOptionsWithLabel = BalancesOptions | string;
3
8
  export declare class Balances {
4
9
  chain: Chain | string;
5
10
  timestamp?: number;
6
11
  _balances: BalancesV1;
12
+ _breakdownBalances: {
13
+ [key: string]: Balances;
14
+ };
7
15
  constructor(params: {
8
16
  chain?: Chain | string;
9
17
  timestamp?: number;
10
18
  });
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
- addCGToken(token: string, balance: any): void;
22
- addTokenVannila(token: string, balance: any): void;
23
- addUSDValue(balance: any): void;
24
- addTokens(tokens: string[], balances: any[], { skipChain }?: {
25
- skipChain?: boolean | undefined;
26
- }): void;
27
- addBalances(balances: BalancesV1 | Balances): void;
19
+ _add(token: string, balance: any, optionsOrLabel?: BalancesOptionsWithLabel, options?: BalancesOptions): void;
20
+ add(token: string | string[] | Balances, balance?: any, optionsOrLabel?: BalancesOptionsWithLabel, options?: BalancesOptions): void;
21
+ addToken(token: string, balance: any, optionsOrLabel?: BalancesOptionsWithLabel, options?: BalancesOptions): void;
22
+ addGasToken(balance: any, optionsOrLabel?: BalancesOptionsWithLabel, options?: BalancesOptions): void;
23
+ addCGToken(token: string, balance: any, optionsOrLabel?: BalancesOptionsWithLabel, options?: BalancesOptions): void;
24
+ addTokenVannila(token: string, balance: any, optionsOrLabel?: BalancesOptionsWithLabel, options?: BalancesOptions): void;
25
+ addUSDValue(balance: any, optionsOrLabel?: BalancesOptionsWithLabel, options?: BalancesOptions): void;
26
+ addTokens(tokens: string[], balances: any[], optionsOrLabel?: BalancesOptionsWithLabel, options?: BalancesOptions): void;
27
+ addBalances(balances: BalancesV1 | Balances, optionsOrLabel?: BalancesOptionsWithLabel, options?: (BalancesOptions & {
28
+ skipBreakdown?: boolean;
29
+ })): void;
28
30
  getBalances(): BalancesV1;
29
31
  removeTokenBalance(token: string): void;
30
32
  getUSDValue(): Promise<number>;
@@ -52,5 +54,12 @@ export declare class Balances {
52
54
  skipChain?: boolean | undefined;
53
55
  }): void;
54
56
  removeNegativeBalances(): void;
57
+ _breakdownBalancesAction(action: string, args?: any[]): Promise<{
58
+ [key: string]: any;
59
+ }>;
60
+ hasBreakdownBalances(): boolean;
61
+ getBreakdownBalances(): {
62
+ [key: string]: Balances;
63
+ };
55
64
  }
56
65
  export default Balances;
package/build/Balances.js CHANGED
@@ -7,14 +7,36 @@ exports.Balances = void 0;
7
7
  const generalUtil_1 = require("./generalUtil");
8
8
  const computeTVL_1 = __importDefault(require("./util/computeTVL"));
9
9
  const nullAddress = '0x0000000000000000000000000000000000000000';
10
+ // the last parameter of add function can be either a string or an object with options (or both as last two parameters)
11
+ function getOptions({ optionsOrLabel = {}, options = {} } = {}) {
12
+ var _a;
13
+ let label;
14
+ if (optionsOrLabel) {
15
+ if (typeof optionsOrLabel === 'string') {
16
+ label = optionsOrLabel;
17
+ }
18
+ else {
19
+ options = optionsOrLabel;
20
+ label = options.label;
21
+ }
22
+ }
23
+ return {
24
+ ...options,
25
+ skipChain: (_a = options === null || options === void 0 ? void 0 : options.skipChain) !== null && _a !== void 0 ? _a : false,
26
+ label,
27
+ };
28
+ }
10
29
  class Balances {
11
30
  constructor(params) {
12
31
  var _a;
13
32
  this.chain = (_a = params.chain) !== null && _a !== void 0 ? _a : 'ethereum';
14
33
  this.timestamp = params.timestamp;
15
34
  this._balances = {};
35
+ this._breakdownBalances = {};
16
36
  }
17
- _add(token, balance, { skipChain = false } = {}) {
37
+ _add(token, balance, optionsOrLabel, options) {
38
+ var _a;
39
+ const { label, skipChain } = getOptions({ optionsOrLabel, options });
18
40
  token = token.replace(/\//g, ':');
19
41
  const isIBCToken = token.startsWith('ibc:');
20
42
  let chain = this.chain;
@@ -30,59 +52,88 @@ class Balances {
30
52
  token = token.replace('0x', '0x0');
31
53
  }
32
54
  (0, generalUtil_1.sumSingleBalance)(this._balances, token, balance, chain);
55
+ if (label) {
56
+ this._breakdownBalances[label] = (_a = this._breakdownBalances[label]) !== null && _a !== void 0 ? _a : new Balances({ chain: this.chain, timestamp: this.timestamp });
57
+ this._breakdownBalances[label]._add(token, balance, { skipChain });
58
+ }
33
59
  }
34
- add(token, balance, { skipChain = false } = {}) {
60
+ add(token, balance, optionsOrLabel, options) {
61
+ if (token instanceof Balances) {
62
+ if (typeof optionsOrLabel === 'string')
63
+ throw new Error('When adding a Balances instance, optionsOrLabel must be an object with options, not a string');
64
+ this.addBalances(token, balance, optionsOrLabel);
65
+ return;
66
+ }
67
+ options = getOptions({ optionsOrLabel, options });
35
68
  if (!token)
36
69
  throw new Error('token is required');
37
70
  if (!balance)
38
71
  return;
39
72
  if (Array.isArray(balance)) {
40
73
  if (Array.isArray(token)) {
41
- this.addTokens(token, balance, { skipChain });
74
+ this.addTokens(token, balance, options);
42
75
  return;
43
76
  }
44
77
  else if (typeof token === 'string') {
45
- balance.forEach((v) => this._add(token, v, { skipChain }));
78
+ balance.forEach((v) => this._add(token, v, options));
46
79
  return;
47
80
  }
48
81
  }
49
82
  if (typeof token !== 'string')
50
83
  throw new Error('token must be a string');
51
- this._add(token, balance, { skipChain });
84
+ this._add(token, balance, options);
52
85
  }
53
- addToken(token, balance, { skipChain = false } = {}) {
54
- this._add(token, balance, { skipChain });
86
+ addToken(token, balance, optionsOrLabel, options) {
87
+ options = getOptions({ optionsOrLabel, options });
88
+ this._add(token, balance, options);
55
89
  }
56
- addGasToken(balance) {
57
- this._add(nullAddress, balance);
90
+ addGasToken(balance, optionsOrLabel, options) {
91
+ options = getOptions({ optionsOrLabel, options });
92
+ this._add(nullAddress, balance, options);
58
93
  }
59
- addCGToken(token, balance) {
60
- this.addTokenVannila('coingecko:' + token, balance);
94
+ addCGToken(token, balance, optionsOrLabel, options) {
95
+ options = getOptions({ optionsOrLabel, options });
96
+ this.addTokenVannila('coingecko:' + token, balance, options);
61
97
  }
62
- addTokenVannila(token, balance) {
63
- this._add(token, balance, { skipChain: true });
98
+ addTokenVannila(token, balance, optionsOrLabel, options) {
99
+ options = getOptions({ optionsOrLabel, options });
100
+ this._add(token, balance, { ...options, skipChain: true });
64
101
  }
65
- addUSDValue(balance) {
66
- this.addCGToken('tether', balance);
102
+ addUSDValue(balance, optionsOrLabel, options) {
103
+ options = getOptions({ optionsOrLabel, options });
104
+ this.addCGToken('tether', balance, { ...options, skipChain: true });
67
105
  }
68
- addTokens(tokens, balances, { skipChain = false } = {}) {
106
+ addTokens(tokens, balances, optionsOrLabel, options) {
107
+ options = getOptions({ optionsOrLabel, options });
69
108
  if (!Array.isArray(tokens))
70
109
  throw new Error('tokens must be an array');
71
110
  if (!Array.isArray(balances))
72
111
  throw new Error('balances must be an array');
73
112
  if (tokens.length !== balances.length)
74
113
  throw new Error('token and balance must have the same length');
75
- tokens.forEach((v, i) => this._add(v, balances[i], { skipChain }));
114
+ tokens.forEach((v, i) => this._add(v, balances[i], options));
76
115
  }
77
- addBalances(balances) {
116
+ addBalances(balances, optionsOrLabel, options) {
117
+ var _a;
118
+ options = getOptions({ optionsOrLabel, options });
78
119
  if (balances instanceof Balances) {
79
120
  if (balances === this)
80
121
  return;
122
+ // if balances object has breakdown by label, and overall label is not set, we copy existing breakdown balances into this instance
123
+ if (balances.hasBreakdownBalances() && !options.skipBreakdown && !options.label) {
124
+ this._breakdownBalances = (_a = this._breakdownBalances) !== null && _a !== void 0 ? _a : {};
125
+ const { label, ...restOptions } = options;
126
+ Object.entries(balances.getBreakdownBalances()).forEach(([label, breakdown]) => {
127
+ var _a;
128
+ this._breakdownBalances[label] = (_a = this._breakdownBalances[label]) !== null && _a !== void 0 ? _a : new Balances({ chain: this.chain, timestamp: this.timestamp });
129
+ this._breakdownBalances[label].addBalances(breakdown, restOptions);
130
+ });
131
+ }
81
132
  balances = balances.getBalances();
82
133
  }
83
134
  if (balances === this._balances)
84
135
  return;
85
- Object.entries(balances).forEach(([token, balance]) => this._add(token, balance, { skipChain: true }));
136
+ Object.entries(balances).forEach(([token, balance]) => this._add(token, balance, { ...options, skipChain: true }));
86
137
  }
87
138
  getBalances() {
88
139
  return this._balances;
@@ -93,6 +144,7 @@ class Balances {
93
144
  if (regex.test(i))
94
145
  delete this._balances[i];
95
146
  });
147
+ this._breakdownBalancesAction('removeTokenBalance', [token]);
96
148
  }
97
149
  async getUSDValue() {
98
150
  const { usdTvl } = await (0, computeTVL_1.default)(this.getBalances(), this.timestamp);
@@ -109,6 +161,7 @@ class Balances {
109
161
  Object.keys(this._balances).forEach((token) => {
110
162
  this._balances[token] = Number(this._balances[token]) * ratio;
111
163
  });
164
+ this._breakdownBalancesAction('resizeBy', [ratio]);
112
165
  return this;
113
166
  }
114
167
  static async getUSDValue(balances, timestamp) {
@@ -149,6 +202,30 @@ class Balances {
149
202
  if (Number(this._balances[token]) <= 0)
150
203
  delete this._balances[token];
151
204
  });
205
+ if (this._breakdownBalances) {
206
+ Object.values(this._breakdownBalances).forEach((breakdown) => breakdown.removeNegativeBalances());
207
+ }
208
+ }
209
+ async _breakdownBalancesAction(action, args = []) {
210
+ if (!this._breakdownBalances)
211
+ return {};
212
+ const response = {};
213
+ const entries = Object.entries(this._breakdownBalances);
214
+ for (const [label, breakdown] of entries) {
215
+ if (typeof breakdown[action] === 'function') {
216
+ response[label] = breakdown[action](...args);
217
+ if (response[label] instanceof Promise) {
218
+ response[label] = await response[label];
219
+ }
220
+ }
221
+ }
222
+ return response;
223
+ }
224
+ hasBreakdownBalances() {
225
+ return Object.keys(this._breakdownBalances).length > 0;
226
+ }
227
+ getBreakdownBalances() {
228
+ return this._breakdownBalances;
152
229
  }
153
230
  }
154
231
  exports.Balances = Balances;
@@ -201,6 +201,11 @@ const DEPLOYMENT_BLOCK = {
201
201
  lens: 1,
202
202
  basecamp: 1,
203
203
  platon: 1,
204
+ xrplevm: 1,
205
+ katana: 1,
206
+ mint: 1,
207
+ btnx: 1,
208
+ vechain: 22103870,
204
209
  };
205
210
  async function makeMultiCall(functionABI, calls, chain, block) {
206
211
  if (chain === 'tron')
@@ -344,6 +349,12 @@ function getMulticallAddress(chain, block) {
344
349
  case 'hedera':
345
350
  multicallAddress = '0x18fa376d92511dd04090566ab6144847c03557d8';
346
351
  break;
352
+ case 'xrplevm':
353
+ multicallAddress = '0xaB3a12b26e72F4D6D9B579aCb5027a0918eF1114';
354
+ break;
355
+ case 'vechain':
356
+ multicallAddress = '0x8B2fF167683c5e1DFD6717d934B560F20cf9F2a3';
357
+ break;
347
358
  }
348
359
  return multicallAddress;
349
360
  }