@defillama/sdk 5.0.240 → 5.0.242

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.
Files changed (131) hide show
  1. package/build/Balances.d.ts +139 -0
  2. package/build/Balances.js +363 -0
  3. package/build/ChainApi.d.ts +142 -0
  4. package/build/ChainApi.js +310 -0
  5. package/build/abi/abi2.d.ts +5 -0
  6. package/build/abi/abi2.js +215 -0
  7. package/build/abi/cachedABIs.d.ts +4 -0
  8. package/build/abi/cachedABIs.js +8 -0
  9. package/build/abi/convertResults.d.ts +5 -0
  10. package/build/abi/convertResults.js +60 -0
  11. package/build/abi/index.d.ts +31 -0
  12. package/build/abi/index.js +327 -0
  13. package/build/abi/multicall.d.ts +23 -0
  14. package/build/abi/multicall.js +383 -0
  15. package/build/abi/multicall3.d.ts +7 -0
  16. package/build/abi/multicall3.js +403 -0
  17. package/build/abi/rpcCall.d.ts +4 -0
  18. package/build/abi/rpcCall.js +62 -0
  19. package/build/abi/tron.d.ts +33 -0
  20. package/build/abi/tron.js +116 -0
  21. package/build/api.d.ts +10 -0
  22. package/build/api.js +45 -0
  23. package/build/api2.d.ts +5 -0
  24. package/build/api2.js +41 -0
  25. package/build/chains/algorand.d.ts +277 -0
  26. package/build/chains/algorand.js +618 -0
  27. package/build/chains/aptos.d.ts +269 -0
  28. package/build/chains/aptos.js +700 -0
  29. package/build/chains/cardano.d.ts +242 -0
  30. package/build/chains/cardano.js +407 -0
  31. package/build/chains/cosmos.d.ts +252 -0
  32. package/build/chains/cosmos.js +618 -0
  33. package/build/chains/icp.d.ts +235 -0
  34. package/build/chains/icp.js +1040 -0
  35. package/build/chains/index.d.ts +23 -0
  36. package/build/chains/index.js +59 -0
  37. package/build/chains/near.d.ts +192 -0
  38. package/build/chains/near.js +446 -0
  39. package/build/chains/rpc.d.ts +125 -0
  40. package/build/chains/rpc.js +326 -0
  41. package/build/chains/starknet.d.ts +205 -0
  42. package/build/chains/starknet.js +884 -0
  43. package/build/chains/stellar.d.ts +410 -0
  44. package/build/chains/stellar.js +1082 -0
  45. package/build/chains/substrate.d.ts +333 -0
  46. package/build/chains/substrate.js +1042 -0
  47. package/build/chains/sui.d.ts +365 -0
  48. package/build/chains/sui.js +1135 -0
  49. package/build/chains/svm.d.ts +340 -0
  50. package/build/chains/svm.js +760 -0
  51. package/build/chains/tezos.d.ts +225 -0
  52. package/build/chains/tezos.js +412 -0
  53. package/build/chains/ton.d.ts +353 -0
  54. package/build/chains/ton.js +788 -0
  55. package/build/chains/tron.d.ts +45 -0
  56. package/build/chains/tron.js +110 -0
  57. package/build/chains/utxo.d.ts +210 -0
  58. package/build/chains/utxo.js +678 -0
  59. package/build/chains/xrpl.d.ts +179 -0
  60. package/build/chains/xrpl.js +467 -0
  61. package/build/computeTVL/blocks.d.ts +1 -0
  62. package/build/computeTVL/blocks.js +12 -0
  63. package/build/computeTVL/humanizeNumber.d.ts +1 -0
  64. package/build/computeTVL/humanizeNumber.js +19 -0
  65. package/build/erc20/index.d.ts +31 -0
  66. package/build/erc20/index.js +56 -0
  67. package/build/eth/index.d.ts +22 -0
  68. package/build/eth/index.js +118 -0
  69. package/build/general.d.ts +5 -0
  70. package/build/general.js +20 -0
  71. package/build/generalUtil.d.ts +8 -0
  72. package/build/generalUtil.js +73 -0
  73. package/build/index.d.ts +32 -0
  74. package/build/index.js +74 -0
  75. package/build/providers.json +1 -0
  76. package/build/types.d.ts +76 -0
  77. package/build/types.js +2 -0
  78. package/build/util/LlamaProvider.d.ts +46 -0
  79. package/build/util/LlamaProvider.js +367 -0
  80. package/build/util/blocks.d.ts +36 -0
  81. package/build/util/blocks.js +500 -0
  82. package/build/util/cache.d.ts +45 -0
  83. package/build/util/cache.js +309 -0
  84. package/build/util/chainUtils/data.json +1437 -0
  85. package/build/util/chainUtils/index.d.ts +11 -0
  86. package/build/util/chainUtils/index.js +136 -0
  87. package/build/util/coins.d.ts +19 -0
  88. package/build/util/coins.js +124 -0
  89. package/build/util/common.d.ts +34 -0
  90. package/build/util/common.js +376 -0
  91. package/build/util/computeTVL.d.ts +19 -0
  92. package/build/util/computeTVL.js +102 -0
  93. package/build/util/cosmos.d.ts +1 -0
  94. package/build/util/cosmos.js +8 -0
  95. package/build/util/debugLog.d.ts +4 -0
  96. package/build/util/debugLog.js +18 -0
  97. package/build/util/discord.d.ts +1 -0
  98. package/build/util/discord.js +33 -0
  99. package/build/util/elastic.d.ts +43 -0
  100. package/build/util/elastic.js +113 -0
  101. package/build/util/env.d.ts +20 -0
  102. package/build/util/env.js +176 -0
  103. package/build/util/graph.d.ts +8 -0
  104. package/build/util/graph.js +50 -0
  105. package/build/util/index.d.ts +32 -0
  106. package/build/util/index.js +227 -0
  107. package/build/util/indexer.compatibility.d.ts +6 -0
  108. package/build/util/indexer.compatibility.js +64 -0
  109. package/build/util/indexer.d.ts +92 -0
  110. package/build/util/indexer.js +850 -0
  111. package/build/util/indexerSql.d.ts +9 -0
  112. package/build/util/indexerSql.js +102 -0
  113. package/build/util/internal-cache.d.ts +11 -0
  114. package/build/util/internal-cache.js +77 -0
  115. package/build/util/logs.d.ts +58 -0
  116. package/build/util/logs.decode.shared.d.ts +8 -0
  117. package/build/util/logs.decode.shared.js +289 -0
  118. package/build/util/logs.js +432 -0
  119. package/build/util/promisePool.d.ts +8 -0
  120. package/build/util/promisePool.js +31 -0
  121. package/build/util/r2.d.ts +4 -0
  122. package/build/util/r2.js +52 -0
  123. package/build/util/removeRedundantRPCs.d.ts +1 -0
  124. package/build/util/removeRedundantRPCs.js +49 -0
  125. package/build/util/transactions.d.ts +35 -0
  126. package/build/util/transactions.js +22 -0
  127. package/build/util/updateChainLabels.d.ts +1 -0
  128. package/build/util/updateChainLabels.js +5 -0
  129. package/build/util/updateProviderList.d.ts +1 -0
  130. package/build/util/updateProviderList.js +275 -0
  131. package/package.json +3 -2
@@ -0,0 +1,139 @@
1
+ import { Balances as BalancesV1 } from "./types";
2
+ import { Chain } from "./general";
3
+ type BalancesOptions = {
4
+ skipChain?: boolean;
5
+ label?: string;
6
+ tags?: string[];
7
+ tag?: string;
8
+ id?: string;
9
+ isUSDValue?: boolean;
10
+ };
11
+ type BalancesOptionsWithLabel = BalancesOptions | string;
12
+ export declare class Balances {
13
+ chain: Chain | string;
14
+ timestamp?: number;
15
+ _balances: BalancesV1;
16
+ _llamaBalancesObject: boolean;
17
+ _breakdownBalances: {
18
+ [key: string]: Balances;
19
+ };
20
+ _taggedBalances: {
21
+ [tag: string]: Balances;
22
+ };
23
+ _usdBalances: BalancesV1;
24
+ constructor(params: {
25
+ chain?: Chain | string;
26
+ timestamp?: number;
27
+ });
28
+ _add(token: string, balance: any, optionsOrLabel?: BalancesOptionsWithLabel, options?: BalancesOptions): void;
29
+ add(token: string | string[] | Balances, balance?: any, optionsOrLabel?: BalancesOptionsWithLabel, options?: BalancesOptions): void;
30
+ addToken(token: string, balance: any, optionsOrLabel?: BalancesOptionsWithLabel, options?: BalancesOptions): void;
31
+ addGasToken(balance: any, optionsOrLabel?: BalancesOptionsWithLabel, options?: BalancesOptions): void;
32
+ addCGToken(token: string, balance: any, optionsOrLabel?: BalancesOptionsWithLabel, options?: BalancesOptions): void;
33
+ addTokenVannila(token: string, balance: any, optionsOrLabel?: BalancesOptionsWithLabel, options?: BalancesOptions): void;
34
+ addUSDValue(balance: any, optionsOrLabel?: BalancesOptionsWithLabel, options?: BalancesOptions): void;
35
+ addTokens(tokens: string[], balances: any[], optionsOrLabel?: BalancesOptionsWithLabel, options?: BalancesOptions): void;
36
+ addBalances(balances: BalancesV1 | Balances, optionsOrLabel?: BalancesOptionsWithLabel, options?: (BalancesOptions & {
37
+ skipBreakdown?: boolean;
38
+ })): void;
39
+ getBalances(): BalancesV1;
40
+ removeTokenBalance(token: string): void;
41
+ getUSDValue(): Promise<number>;
42
+ getUSDString(): Promise<string>;
43
+ getUSDJSONs({ debug, debugOptions: { printTokenTable, minTokenUSDValue } }?: {
44
+ debug?: boolean;
45
+ debugOptions?: {
46
+ printTokenTable?: boolean;
47
+ minTokenUSDValue?: number;
48
+ };
49
+ }): Promise<{
50
+ usdTvl: number;
51
+ usdTokenBalances: BalancesV1;
52
+ rawTokenBalances: BalancesV1;
53
+ labelBreakdown?: {
54
+ [key: string]: number;
55
+ };
56
+ tagBreakdown?: {
57
+ [key: string]: number;
58
+ };
59
+ debugData?: {
60
+ tokenData: {
61
+ balance: string | number;
62
+ price: number;
63
+ decimals: number;
64
+ value: number;
65
+ confidence: number;
66
+ token: string;
67
+ symbol: string;
68
+ }[];
69
+ };
70
+ }>;
71
+ debug({ printTokenTable, minTokenUSDValue }?: {
72
+ printTokenTable?: boolean;
73
+ minTokenUSDValue?: number;
74
+ }): Promise<{
75
+ usdTvl: number;
76
+ usdTokenBalances: BalancesV1;
77
+ rawTokenBalances: BalancesV1;
78
+ labelBreakdown?: {
79
+ [key: string]: number;
80
+ };
81
+ tagBreakdown?: {
82
+ [key: string]: number;
83
+ };
84
+ debugData?: {
85
+ tokenData: {
86
+ balance: string | number;
87
+ price: number;
88
+ decimals: number;
89
+ value: number;
90
+ confidence: number;
91
+ token: string;
92
+ symbol: string;
93
+ }[];
94
+ };
95
+ }>;
96
+ resizeBy(ratio: number): this;
97
+ static getUSDValue(balances: BalancesV1, timestamp?: number): Promise<number>;
98
+ static getUSDString(balances: BalancesV1, timestamp?: number): Promise<string>;
99
+ static getBalanceObjects(balances: BalancesV1, timestamp?: number): Promise<{
100
+ usdTvl: number;
101
+ usdTokenBalances: BalancesV1;
102
+ debugData: {
103
+ tokenData: {
104
+ balance: string | number;
105
+ price: number;
106
+ decimals: number;
107
+ value: number;
108
+ confidence: number;
109
+ token: string;
110
+ symbol: string;
111
+ }[];
112
+ };
113
+ }>;
114
+ static getUSDJSONs(balances: BalancesV1, timestamp?: number): Promise<{
115
+ usdTvl: number;
116
+ usdTokenBalances: BalancesV1;
117
+ rawTokenBalances: BalancesV1;
118
+ }>;
119
+ clone(ratio?: number, optionsOrLabel?: BalancesOptionsWithLabel, options?: BalancesOptions): Balances;
120
+ subtract(balances: BalancesV1 | Balances, optionsOrLabel?: BalancesOptionsWithLabel, options?: BalancesOptions): void;
121
+ subtractToken(token: string, balance: any, optionsOrLabel?: BalancesOptionsWithLabel, options?: BalancesOptions): void;
122
+ removeNegativeBalances(): void;
123
+ _breakdownBalancesAction(action: string, args?: any[]): Promise<{
124
+ [key: string]: any;
125
+ }>;
126
+ _taggedBalancesAction(action: string, args?: any[]): Promise<{
127
+ [key: string]: any;
128
+ }>;
129
+ hasBreakdownBalances(): boolean;
130
+ hasTaggedBalances(): boolean;
131
+ isEmpty(): boolean;
132
+ getBreakdownBalances(): {
133
+ [key: string]: Balances;
134
+ };
135
+ getTaggedBalances(): {
136
+ [tag: string]: Balances;
137
+ };
138
+ }
139
+ export default Balances;
@@ -0,0 +1,363 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.Balances = void 0;
7
+ const generalUtil_1 = require("./generalUtil");
8
+ const computeTVL_1 = __importDefault(require("./util/computeTVL"));
9
+ const humanizeNumber_1 = require("./computeTVL/humanizeNumber");
10
+ const nullAddress = '0x0000000000000000000000000000000000000000';
11
+ // the last parameter of add function can be either a string or an object with options (or both as last two parameters)
12
+ function getOptions({ optionsOrLabel = {}, options = {} } = {}) {
13
+ var _a;
14
+ let label;
15
+ if (optionsOrLabel) {
16
+ if (typeof optionsOrLabel === 'string') {
17
+ label = optionsOrLabel;
18
+ }
19
+ else {
20
+ options = optionsOrLabel;
21
+ label = options.label;
22
+ }
23
+ }
24
+ return {
25
+ ...options,
26
+ skipChain: (_a = options === null || options === void 0 ? void 0 : options.skipChain) !== null && _a !== void 0 ? _a : false,
27
+ label,
28
+ };
29
+ }
30
+ // Duck-type check that also works across module boundaries (when the same package
31
+ // is loaded from two different node_modules copies, `instanceof Balances` returns false).
32
+ function isLlamaBalancesObject(o) {
33
+ return typeof o === 'object' && o !== null && o._llamaBalancesObject === true;
34
+ }
35
+ class Balances {
36
+ constructor(params) {
37
+ var _a;
38
+ this.chain = (_a = params.chain) !== null && _a !== void 0 ? _a : 'ethereum';
39
+ this.timestamp = params.timestamp;
40
+ this._balances = {};
41
+ this._breakdownBalances = {};
42
+ this._taggedBalances = {};
43
+ this._usdBalances = {};
44
+ this._llamaBalancesObject = true;
45
+ }
46
+ _add(token, balance, optionsOrLabel, options) {
47
+ var _a;
48
+ const { label, skipChain, tag, tags = [], isUSDValue } = getOptions({ optionsOrLabel, options });
49
+ if (tag && typeof tag === 'string' && !tags.includes(tag))
50
+ tags.push(tag);
51
+ token = token.replace(/\//g, ':');
52
+ const isIBCToken = token.startsWith('ibc:');
53
+ let chain = this.chain;
54
+ if (skipChain || isIBCToken) {
55
+ chain = undefined;
56
+ }
57
+ if (chain === 'injective' && token.startsWith('peggy0x')) {
58
+ chain = 'ethereum';
59
+ token = token.replace('peggy', '');
60
+ }
61
+ // pad starknet token addresses back to the canonical 32 byte form
62
+ if (!skipChain && this.chain === 'starknet' && token.startsWith('0x') && token.length < 66) {
63
+ token = '0x' + token.slice(2).padStart(64, '0');
64
+ }
65
+ if (isUSDValue) {
66
+ (0, generalUtil_1.sumSingleBalance)(this._usdBalances, token, balance, chain);
67
+ }
68
+ else {
69
+ (0, generalUtil_1.sumSingleBalance)(this._balances, token, balance, chain);
70
+ }
71
+ if (label) {
72
+ this._breakdownBalances[label] = (_a = this._breakdownBalances[label]) !== null && _a !== void 0 ? _a : new Balances({ chain: this.chain, timestamp: this.timestamp });
73
+ this._breakdownBalances[label]._add(token, balance, { skipChain, isUSDValue });
74
+ }
75
+ if (tags.length > 0) {
76
+ tags.forEach((tag) => {
77
+ var _a;
78
+ this._taggedBalances[tag] = (_a = this._taggedBalances[tag]) !== null && _a !== void 0 ? _a : new Balances({ chain: this.chain, timestamp: this.timestamp });
79
+ this._taggedBalances[tag]._add(token, balance, { skipChain, isUSDValue });
80
+ });
81
+ }
82
+ }
83
+ add(token, balance, optionsOrLabel, options) {
84
+ if (isLlamaBalancesObject(token)) {
85
+ if (typeof optionsOrLabel === 'string')
86
+ throw new Error('When adding a Balances instance, optionsOrLabel must be an object with options, not a string');
87
+ this.addBalances(token, balance, optionsOrLabel);
88
+ return;
89
+ }
90
+ options = getOptions({ optionsOrLabel, options });
91
+ if (!token)
92
+ throw new Error('token is required');
93
+ if (!balance)
94
+ return;
95
+ if (Array.isArray(balance)) {
96
+ if (Array.isArray(token)) {
97
+ this.addTokens(token, balance, options);
98
+ return;
99
+ }
100
+ else if (typeof token === 'string') {
101
+ balance.forEach((v) => this._add(token, v, options));
102
+ return;
103
+ }
104
+ }
105
+ if (typeof token !== 'string')
106
+ throw new Error('token must be a string');
107
+ this._add(token, balance, options);
108
+ }
109
+ addToken(token, balance, optionsOrLabel, options) {
110
+ options = getOptions({ optionsOrLabel, options });
111
+ this._add(token, balance, options);
112
+ }
113
+ addGasToken(balance, optionsOrLabel, options) {
114
+ options = getOptions({ optionsOrLabel, options });
115
+ this._add(nullAddress, balance, options);
116
+ }
117
+ addCGToken(token, balance, optionsOrLabel, options) {
118
+ options = getOptions({ optionsOrLabel, options });
119
+ this.addTokenVannila('coingecko:' + token, balance, options);
120
+ }
121
+ addTokenVannila(token, balance, optionsOrLabel, options) {
122
+ options = getOptions({ optionsOrLabel, options });
123
+ this._add(token, balance, { ...options, skipChain: true });
124
+ }
125
+ addUSDValue(balance, optionsOrLabel, options) {
126
+ options = getOptions({ optionsOrLabel, options });
127
+ if (options.id) {
128
+ this.add(options.id, balance, { ...options, isUSDValue: true, skipChain: true });
129
+ return;
130
+ }
131
+ this.addCGToken('tether', balance, { ...options, skipChain: true });
132
+ }
133
+ addTokens(tokens, balances, optionsOrLabel, options) {
134
+ options = getOptions({ optionsOrLabel, options });
135
+ if (!Array.isArray(tokens))
136
+ throw new Error('tokens must be an array');
137
+ if (!Array.isArray(balances))
138
+ throw new Error('balances must be an array');
139
+ if (tokens.length !== balances.length)
140
+ throw new Error('token and balance must have the same length');
141
+ tokens.forEach((v, i) => this._add(v, balances[i], options));
142
+ }
143
+ addBalances(balances, optionsOrLabel, options) {
144
+ var _a, _b;
145
+ options = getOptions({ optionsOrLabel, options });
146
+ if (isLlamaBalancesObject(balances)) {
147
+ const balancesInstance = balances;
148
+ if (balancesInstance === this)
149
+ return;
150
+ // if balances object has breakdown by label, and overall label is not set, we copy existing breakdown balances into this instance
151
+ if (balancesInstance.hasBreakdownBalances() && !options.skipBreakdown && !options.label) {
152
+ this._breakdownBalances = (_a = this._breakdownBalances) !== null && _a !== void 0 ? _a : {};
153
+ const { label, ...restOptions } = options;
154
+ Object.entries(balancesInstance.getBreakdownBalances()).forEach(([label, breakdown]) => {
155
+ var _a;
156
+ this._breakdownBalances[label] = (_a = this._breakdownBalances[label]) !== null && _a !== void 0 ? _a : new Balances({ chain: this.chain, timestamp: this.timestamp });
157
+ this._breakdownBalances[label].addBalances(breakdown, restOptions);
158
+ });
159
+ }
160
+ if (balancesInstance.hasTaggedBalances()) {
161
+ this._taggedBalances = (_b = this._taggedBalances) !== null && _b !== void 0 ? _b : {};
162
+ const { label, ...restOptions } = options;
163
+ Object.entries(balancesInstance.getTaggedBalances()).forEach(([tag, taggedBalance]) => {
164
+ var _a;
165
+ this._taggedBalances[tag] = (_a = this._taggedBalances[tag]) !== null && _a !== void 0 ? _a : new Balances({ chain: this.chain, timestamp: this.timestamp });
166
+ this._taggedBalances[tag].addBalances(taggedBalance, restOptions);
167
+ });
168
+ }
169
+ if (Object.keys(balancesInstance._usdBalances).length > 0) {
170
+ Object.entries(balancesInstance._usdBalances).forEach(([token, balance]) => {
171
+ this._add(token, balance, { ...options, skipChain: true, isUSDValue: true });
172
+ });
173
+ }
174
+ balances = balancesInstance.getBalances();
175
+ }
176
+ if (balances === this._balances)
177
+ return;
178
+ Object.entries(balances).forEach(([token, balance]) => this._add(token, balance, { ...options, skipChain: true }));
179
+ }
180
+ getBalances() {
181
+ return this._balances;
182
+ }
183
+ removeTokenBalance(token) {
184
+ const regex = new RegExp(token, 'i');
185
+ Object.keys(this._balances).forEach((i) => {
186
+ if (regex.test(i))
187
+ delete this._balances[i];
188
+ });
189
+ Object.keys(this._usdBalances).forEach((i) => {
190
+ if (regex.test(i))
191
+ delete this._usdBalances[i];
192
+ });
193
+ this._breakdownBalancesAction('removeTokenBalance', [token]);
194
+ this._taggedBalancesAction('removeTokenBalance', [token]);
195
+ }
196
+ async getUSDValue() {
197
+ let { usdTvl } = await (0, computeTVL_1.default)(this.getBalances(), this.timestamp);
198
+ usdTvl = Object.values(this._usdBalances).reduce((a, b) => a + Number(b), usdTvl);
199
+ return usdTvl;
200
+ }
201
+ async getUSDString() {
202
+ return Number(await this.getUSDValue()).toFixed(0);
203
+ }
204
+ async getUSDJSONs({ debug = false, debugOptions: { printTokenTable = true, minTokenUSDValue } = {} } = {}) {
205
+ let { usdTvl, usdTokenBalances, debugData } = await (0, computeTVL_1.default)(this.getBalances(), this.timestamp, { debug });
206
+ const response = { usdTvl, usdTokenBalances, rawTokenBalances: this.getBalances(), labelBreakdown: {} };
207
+ // add the USD value of tokens that were directly added as USD value (instead of raw token balances that need to be converted to USD value using price cache)
208
+ Object.entries(this._usdBalances).forEach(([symbol, balance]) => {
209
+ usdTvl += Number(balance);
210
+ (0, generalUtil_1.sumSingleBalance)(usdTokenBalances, symbol, balance);
211
+ // skipping it from debug table, unlikely that we would mix raw token balances and direct USD value in the same Balances instance, but if we do, it's cleaner to skip these from debug table since they are already in USD value and don't have price/confidence/decimals data
212
+ // if (debug && !isNaN(Number(balance))) debugData.tokenData.push({ symbol, value: Number(balance), token: symbol, price: 1, confidence: 1, balance, decimals: 0 })
213
+ });
214
+ response.usdTvl = usdTvl;
215
+ if (debug) {
216
+ response.debugData = debugData;
217
+ if (!minTokenUSDValue && minTokenUSDValue !== 0) {
218
+ minTokenUSDValue = usdTvl / 100; // default to 1% of total TVL
219
+ }
220
+ let tokenData = debugData.tokenData;
221
+ // filter out tokens with value less than minTokenUSDValue
222
+ if (minTokenUSDValue)
223
+ tokenData = tokenData.filter((i) => +i.value >= minTokenUSDValue);
224
+ tokenData.sort((a, b) => b.value - a.value); // sort descending by value
225
+ if (printTokenTable) {
226
+ const dataClone = tokenData.map((i) => ({ ...i, value: (0, humanizeNumber_1.humanizeNumber)(i.value), }));
227
+ console.log((0, generalUtil_1.tableToString)(dataClone));
228
+ }
229
+ // add a humanized value field so it's easier to read big numbers
230
+ tokenData.forEach((i) => {
231
+ i.valueHN = (0, humanizeNumber_1.humanizeNumber)(i.value);
232
+ });
233
+ debugData.tokenData = tokenData;
234
+ }
235
+ if (this.hasBreakdownBalances()) {
236
+ for (const [label, breakdown] of Object.entries(this.getBreakdownBalances())) {
237
+ const breakdownUSD = await breakdown.getUSDValue();
238
+ response.labelBreakdown[label] = breakdownUSD;
239
+ }
240
+ }
241
+ if (this.hasTaggedBalances()) {
242
+ response.tagBreakdown = {};
243
+ for (const [tag, taggedBalance] of Object.entries(this.getTaggedBalances())) {
244
+ const taggedUSD = await taggedBalance.getUSDValue();
245
+ response.tagBreakdown[tag] = taggedUSD;
246
+ }
247
+ }
248
+ return response;
249
+ }
250
+ async debug({ printTokenTable = true, minTokenUSDValue } = {}) {
251
+ return this.getUSDJSONs({ debug: true, debugOptions: { printTokenTable, minTokenUSDValue } });
252
+ }
253
+ resizeBy(ratio) {
254
+ Object.keys(this._balances).forEach((token) => {
255
+ this._balances[token] = Number(this._balances[token]) * ratio;
256
+ });
257
+ Object.keys(this._usdBalances).forEach((token) => {
258
+ this._usdBalances[token] = Number(this._usdBalances[token]) * ratio;
259
+ });
260
+ this._breakdownBalancesAction('resizeBy', [ratio]);
261
+ this._taggedBalancesAction('resizeBy', [ratio]);
262
+ return this;
263
+ }
264
+ static async getUSDValue(balances, timestamp) {
265
+ return (await (0, computeTVL_1.default)(balances, timestamp)).usdTvl;
266
+ }
267
+ static async getUSDString(balances, timestamp) {
268
+ return Number(await Balances.getUSDValue(balances, timestamp)).toFixed(0);
269
+ }
270
+ static async getBalanceObjects(balances, timestamp) {
271
+ return (0, computeTVL_1.default)(balances, timestamp);
272
+ }
273
+ static async getUSDJSONs(balances, timestamp) {
274
+ const { usdTvl, usdTokenBalances } = await (0, computeTVL_1.default)(balances, timestamp);
275
+ return { usdTvl, usdTokenBalances, rawTokenBalances: balances };
276
+ }
277
+ clone(ratio = 1, optionsOrLabel, options) {
278
+ options = getOptions({ optionsOrLabel, options });
279
+ const newBalances = new Balances({ chain: this.chain, timestamp: this.timestamp });
280
+ newBalances.addBalances(this, options);
281
+ if (ratio !== 1)
282
+ newBalances.resizeBy(ratio);
283
+ return newBalances;
284
+ }
285
+ subtract(balances, optionsOrLabel, options) {
286
+ options = getOptions({ optionsOrLabel, options });
287
+ if (isLlamaBalancesObject(balances)) {
288
+ if (balances === this)
289
+ return;
290
+ const balancesInstance = balances;
291
+ Object.entries(balancesInstance._usdBalances).forEach(([token, balance]) => {
292
+ this._add(token, Number(balance) * -1, { skipChain: true, label: options.label, isUSDValue: true });
293
+ });
294
+ balances = balancesInstance.getBalances();
295
+ }
296
+ Object.entries(balances).forEach(([token, balance]) => {
297
+ this._add(token, Number(balance) * -1, { skipChain: true, label: options.label });
298
+ });
299
+ }
300
+ subtractToken(token, balance, optionsOrLabel, options) {
301
+ options = getOptions({ optionsOrLabel, options });
302
+ this._add(token, Number(balance) * -1, options);
303
+ }
304
+ removeNegativeBalances() {
305
+ Object.keys(this._balances).forEach((token) => {
306
+ if (Number(this._balances[token]) <= 0)
307
+ delete this._balances[token];
308
+ });
309
+ Object.keys(this._usdBalances).forEach((token) => {
310
+ if (Number(this._usdBalances[token]) <= 0)
311
+ delete this._usdBalances[token];
312
+ });
313
+ this._breakdownBalancesAction('removeNegativeBalances');
314
+ this._taggedBalancesAction('removeNegativeBalances');
315
+ }
316
+ async _breakdownBalancesAction(action, args = []) {
317
+ if (!this._breakdownBalances)
318
+ return {};
319
+ const response = {};
320
+ const entries = Object.entries(this._breakdownBalances);
321
+ for (const [label, breakdown] of entries) {
322
+ if (typeof breakdown[action] === 'function') {
323
+ response[label] = breakdown[action](...args);
324
+ if (response[label] instanceof Promise) {
325
+ response[label] = await response[label];
326
+ }
327
+ }
328
+ }
329
+ return response;
330
+ }
331
+ async _taggedBalancesAction(action, args = []) {
332
+ if (!this._taggedBalances)
333
+ return {};
334
+ const response = {};
335
+ const entries = Object.entries(this._taggedBalances);
336
+ for (const [label, breakdown] of entries) {
337
+ if (typeof breakdown[action] === 'function') {
338
+ response[label] = breakdown[action](...args);
339
+ if (response[label] instanceof Promise) {
340
+ response[label] = await response[label];
341
+ }
342
+ }
343
+ }
344
+ return response;
345
+ }
346
+ hasBreakdownBalances() {
347
+ return Object.keys(this._breakdownBalances).length > 0 && Object.values(this._breakdownBalances).some(b => !b.isEmpty());
348
+ }
349
+ hasTaggedBalances() {
350
+ return Object.keys(this._taggedBalances).length > 0 && Object.values(this._taggedBalances).some(b => !b.isEmpty());
351
+ }
352
+ isEmpty() {
353
+ return Object.keys(this._balances).length === 0 && Object.keys(this._usdBalances).length === 0;
354
+ }
355
+ getBreakdownBalances() {
356
+ return this._breakdownBalances;
357
+ }
358
+ getTaggedBalances() {
359
+ return this._taggedBalances;
360
+ }
361
+ }
362
+ exports.Balances = Balances;
363
+ exports.default = Balances;
@@ -0,0 +1,142 @@
1
+ import Balances from "./Balances";
2
+ import { Chain } from "./general";
3
+ import { Balances as BalancesV1, Block, ByteCodeCallOptions, CallOptions, FetchListOptions, MulticallOptions } from "./types";
4
+ import { Provider } from "ethers";
5
+ import { GetLogsOptions } from "./util/logs";
6
+ import { GetTransactionOptions } from "./util/transactions";
7
+ type Erc4626SumOptions = {
8
+ calls: string[];
9
+ tokenAbi?: string;
10
+ balanceAbi?: string;
11
+ balanceCalls?: any[];
12
+ permitFailure?: boolean;
13
+ isOG4626?: boolean;
14
+ };
15
+ type GetTokenBalancesOptions = {
16
+ token?: string;
17
+ tokens?: string[];
18
+ owner?: string;
19
+ owners?: string[];
20
+ tokensAndOwners?: [string, string][];
21
+ tokensAndOwners2?: [string[], string[]];
22
+ ownerTokens?: [string[], string][];
23
+ blacklistedTokens?: string[];
24
+ blacklistedOwners?: string[];
25
+ skipDuplicates?: boolean;
26
+ permitFailure?: boolean;
27
+ withTokenData?: boolean;
28
+ };
29
+ type SumTokensOptions = GetTokenBalancesOptions & {
30
+ balancesV2Options?: BalancesV2Options;
31
+ };
32
+ type DebugStats = {
33
+ total: number;
34
+ [key: string]: any;
35
+ };
36
+ type BalancesV2Options = {
37
+ skipChain?: boolean;
38
+ label?: string;
39
+ };
40
+ export declare class ChainApi {
41
+ block?: Block;
42
+ chain: Chain | string;
43
+ chainId?: number;
44
+ timestamp?: number;
45
+ provider: Provider;
46
+ _balances: Balances;
47
+ storedKey?: string;
48
+ stats: DebugStats;
49
+ api: ChainApi;
50
+ constructor(params: {
51
+ block?: Block;
52
+ chain?: Chain | string;
53
+ storedKey?: string;
54
+ timestamp?: number;
55
+ protocol?: string;
56
+ });
57
+ addStat(method: string, value?: any, skipTotal?: boolean): void;
58
+ _setBlockFromTimestamp(): Promise<void>;
59
+ call(params: CallOptions): Promise<any>;
60
+ batchCall(params: CallOptions[]): Promise<any[]>;
61
+ multiCall(params: MulticallOptions): Promise<any[]>;
62
+ fetchList(params: FetchListOptions): Promise<any>;
63
+ bytecodeCall(params: ByteCodeCallOptions): Promise<any>;
64
+ getBlock(): Promise<number>;
65
+ log(...args: any): void;
66
+ logTable(...args: any): void;
67
+ add(token: any, balance: any, options?: BalancesV2Options): void;
68
+ addToken(token: string, balance: any, options?: BalancesV2Options): void;
69
+ addGasToken(balance: any, options?: BalancesV2Options): void;
70
+ addUSDValue(balance: any, options?: BalancesV2Options): void;
71
+ addTokens(tokens: string[], balances: any[], options?: BalancesV2Options): void;
72
+ addBalances(balances: Balances | BalancesV1, options?: BalancesV2Options): void;
73
+ addCGToken(token: string, balance: any, options?: BalancesV2Options): void;
74
+ addTokenVannila(token: string, balance: any, options?: BalancesV2Options): void;
75
+ getBalances(): BalancesV1;
76
+ getChainId(): number | undefined;
77
+ removeTokenBalance(token: string): void;
78
+ deleteToken(token: string): void;
79
+ deleteTokens(tokens: string[]): void;
80
+ erc4626Sum2(options: Erc4626SumOptions): Promise<BalancesV1>;
81
+ erc4626Sum({ calls, tokenAbi, balanceAbi, balanceCalls, permitFailure, isOG4626 }: Erc4626SumOptions): Promise<BalancesV1>;
82
+ getTokenBalances({ token, tokens, owners, owner, tokensAndOwners, tokensAndOwners2, blacklistedTokens, blacklistedOwners, ownerTokens, skipDuplicates, permitFailure, withTokenData, }: GetTokenBalancesOptions): Promise<string[] | [token: string, balance: string][]>;
83
+ sumTokens({ balancesV2Options, ...tokenBalanceOptions }: SumTokensOptions): Promise<BalancesV1>;
84
+ getERC20TokenBalances(options: GetTokenBalancesOptions): Promise<string[] | [token: string, balance: string][]>;
85
+ getGasTokenBalance(owner: string, options?: GetTokenBalancesOptions): Promise<string | [token: string, balance: string]>;
86
+ getGasTokenBalances(options: GetTokenBalancesOptions): Promise<string[] | [token: string, balance: string][]>;
87
+ getEthBalance(owner: string, options?: GetTokenBalancesOptions): Promise<string | [token: string, balance: string]>;
88
+ getEthBalances(options: GetTokenBalancesOptions): Promise<string[] | [token: string, balance: string][]>;
89
+ getUSDValue(): Promise<number>;
90
+ getUSDString(): Promise<string>;
91
+ getUSDJSONs(): Promise<{
92
+ usdTvl: number;
93
+ usdTokenBalances: BalancesV1;
94
+ rawTokenBalances: BalancesV1;
95
+ labelBreakdown?: {
96
+ [key: string]: number;
97
+ };
98
+ tagBreakdown?: {
99
+ [key: string]: number;
100
+ };
101
+ debugData?: {
102
+ tokenData: {
103
+ balance: string | number;
104
+ price: number;
105
+ decimals: number;
106
+ value: number;
107
+ confidence: number;
108
+ token: string;
109
+ symbol: string;
110
+ }[];
111
+ };
112
+ }>;
113
+ getLogs(params: GetLogsOptions): Promise<any[] | import("ethers").EventLog[] | import("ethers").EventLog[][]>;
114
+ getBalancesV2(): Balances;
115
+ getTransactions(params: GetTransactionOptions): Promise<{
116
+ hash: any;
117
+ blockNumber: number;
118
+ transactionIndex: number;
119
+ from: any;
120
+ to: any;
121
+ value: any;
122
+ gasPrice: any;
123
+ gas: any;
124
+ input: any;
125
+ nonce: number;
126
+ data: any;
127
+ type: any;
128
+ maxFeePerGas: any;
129
+ maxPriorityFeePerGas: any;
130
+ baseFeePerGas: any;
131
+ effectiveGasPrice: any;
132
+ gasUsed: any;
133
+ cumulativeGasUsed: any;
134
+ status: number;
135
+ contractCreated: any;
136
+ timestamp: any;
137
+ }[] | null>;
138
+ getTransactionReceipt(tx: string): Promise<import("ethers").TransactionReceipt | null>;
139
+ getStats(): DebugStats;
140
+ }
141
+ export default ChainApi;
142
+ export declare function getDebugStats(): Record<string, DebugStats>;