@defillama/sdk 5.0.251 → 5.0.253

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.
@@ -196,11 +196,19 @@ async function multiCall(params) {
196
196
  return cachedMultiCall(params);
197
197
  const abi = resolveABI(params.abi);
198
198
  const chunkedCalls = (0, util_1.sliceIntoChunks)(contractCalls, chunkSize);
199
+ // Tron only: its JSON-RPC rejects a whole multicall batch when the calls in it
200
+ // add up past the node's CPU limit ("CPU timeout for ... operation executing").
201
+ // That is an RPC error, not a failing contract, so it must not become null
202
+ // results under permitFailure: the batch is split and retried, and an RPC
203
+ // error that survives down to a single call throws.
204
+ const isTron = chain === 'tron';
199
205
  const results = await (0, util_1.runInPromisePool)({
200
206
  items: chunkedCalls,
201
207
  concurrency: 10,
202
- processor: (calls) => (0, multicall3_1.default)(abi, calls, chain, params.block),
203
- permitFailure: params.permitFailure,
208
+ processor: (calls) => isTron
209
+ ? makeTronMultiCall(abi, calls, params.block, !!params.permitFailure)
210
+ : (0, multicall3_1.default)(abi, calls, chain, params.block),
211
+ permitFailure: isTron ? false : params.permitFailure,
204
212
  });
205
213
  // When an entire chunk's processor call throws (e.g. a chunk-level RPC
206
214
  // failure, as opposed to an individual call within it), runInPromisePool
@@ -243,6 +251,33 @@ async function multiCall(params) {
243
251
  output: flatResults, // flatten array
244
252
  };
245
253
  }
254
+ // Tron multicall batch. Calls with an unusable target are failed up front under
255
+ // permitFailure (they are a caller problem and never reach the RPC). If the RPC
256
+ // cannot execute the batch, it is halved and retried - halves run one after the
257
+ // other, so a dead RPC fails after ~log2(n) attempts - and the error is rethrown
258
+ // once a single call still cannot be executed.
259
+ async function makeTronMultiCall(abi, calls, block, permitFailure) {
260
+ if (permitFailure && calls.some((call) => !isValidTarget(call.contract, 'tron'))) {
261
+ const validCalls = calls.filter((call) => isValidTarget(call.contract, 'tron'));
262
+ const validResults = validCalls.length ? await makeTronMultiCall(abi, validCalls, block, false) : [];
263
+ let validIndex = 0;
264
+ return calls.map((call) => isValidTarget(call.contract, 'tron')
265
+ ? validResults[validIndex++]
266
+ : { input: { params: call.params, target: call.contract }, success: false, output: null });
267
+ }
268
+ try {
269
+ return await (0, multicall3_1.default)(abi, calls, 'tron', block);
270
+ }
271
+ catch (e) {
272
+ if (calls.length <= 1)
273
+ throw e;
274
+ const mid = Math.ceil(calls.length / 2);
275
+ (0, debugLog_1.debugLog)(`[chain: tron] multicall batch of ${calls.length} failed, retrying as two batches of ${mid}`);
276
+ const firstHalf = await makeTronMultiCall(abi, calls.slice(0, mid), block, false);
277
+ const secondHalf = await makeTronMultiCall(abi, calls.slice(mid), block, false);
278
+ return [...firstHalf, ...secondHalf];
279
+ }
280
+ }
246
281
  async function cachedCall(params) {
247
282
  params.skipCache = true;
248
283
  if (!isCachableAbi(params.abi))
@@ -188,6 +188,7 @@ const DEPLOYMENT_BLOCK = {
188
188
  tac: 1,
189
189
  eventum: 1,
190
190
  somnia: 1,
191
+ somnia_tokyo: 1,
191
192
  camp: 1,
192
193
  bitci: 1,
193
194
  plasma: 1,
package/build/abi/tron.js CHANGED
@@ -47,7 +47,7 @@ const env_1 = require("../util/env");
47
47
  const common_1 = require("../util/common");
48
48
  const evmAbi = __importStar(require("./index"));
49
49
  const callStats_1 = require("../util/callStats");
50
- const limitRPCCalls = (0, p_limit_1.default)(+(0, env_1.getEnvValue)('TRON_RPC_CONCURRENCY_LIMIT', '5'));
50
+ const limitRPCCalls = (0, p_limit_1.default)(+(0, env_1.getEnvValue)('TRON_RPC_CONCURRENCY_LIMIT'));
51
51
  const getEndpoint = () => (0, env_1.getEnvValue)('TRON_WALLET_RPC');
52
52
  const balanceCache = {};
53
53
  async function call(options) {
@@ -95,7 +95,7 @@ async function getBalances(params) {
95
95
  exports.unhexifyTarget = common_1.evmToTronAddress;
96
96
  exports.hexifyTarget = common_1.tronToEvmAddress;
97
97
  async function post(body = {}, endpoint = '/wallet/triggerconstantcontract') {
98
- await (0, common_1.sleepRandom)(3000); // sleep random time to avoid rate limit
98
+ // await sleepRandom(3000) // sleep random time to avoid rate limit
99
99
  const hosts = (getEndpoint()).split(',');
100
100
  (0, callStats_1.registerEndpoints)('tron', hosts);
101
101
  hosts.sort(() => Math.random() - 0.5); // shuffle hosts
@@ -13,7 +13,9 @@ export declare const SHELLEY_SLOT_OFFSET: number;
13
13
  export declare function getEndpointList(): string[];
14
14
  /** First configured base url. */
15
15
  export declare function getEndpoint(): string;
16
- /** Blockfrost project id from `BLOCKFROST_PROJECT_ID`; throws when unset. */
16
+ /** All Blockfrost project ids from `BLOCKFROST_PROJECT_ID` (comma separated); throws when unset. */
17
+ export declare function getProjectIds(): string[];
18
+ /** A random Blockfrost project id from `BLOCKFROST_PROJECT_ID`; throws when unset. */
17
19
  export declare function getProjectId(): string;
18
20
  export interface AssetAmount {
19
21
  /** `lovelace` or `<policyId><assetNameHex>` */
@@ -6,6 +6,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.SHELLEY_SLOT_OFFSET = exports.SHELLEY_START_SLOT = exports.SHELLEY_START_TIME = exports.MAX_PAGE_SIZE = exports.DEFAULT_ENDPOINT = void 0;
7
7
  exports.getEndpointList = getEndpointList;
8
8
  exports.getEndpoint = getEndpoint;
9
+ exports.getProjectIds = getProjectIds;
9
10
  exports.getProjectId = getProjectId;
10
11
  exports.blockfrost = blockfrost;
11
12
  exports.blockfrostAll = blockfrostAll;
@@ -53,8 +54,8 @@ exports.lovelaceToAda = lovelaceToAda;
53
54
  * every function returns raw chain values (lovelace / asset quantities as strings).
54
55
  *
55
56
  * Configuration:
56
- * - `BLOCKFROST_PROJECT_ID` (required, also `SDK_` / `LLAMA_SDK_` prefixed): Blockfrost api key.
57
- * No key is embedded in the sdk.
57
+ * - `BLOCKFROST_PROJECT_ID` (required, also `SDK_` / `LLAMA_SDK_` prefixed): Blockfrost api key(s),
58
+ * comma separated. A random key is picked per request and reused by its retries. No key is embedded in the sdk.
58
59
  * - `CARDANO_BLOCKFROST` (optional, comma separated): api base url(s), default
59
60
  * `https://cardano-mainnet.blockfrost.io/api/v0`.
60
61
  * - `CARDANO_RPC_CONCURRENCY` (optional): max parallel requests, default 5.
@@ -92,12 +93,18 @@ function getEndpointList() {
92
93
  function getEndpoint() {
93
94
  return getEndpointList()[0];
94
95
  }
95
- /** Blockfrost project id from `BLOCKFROST_PROJECT_ID`; throws when unset. */
96
- function getProjectId() {
97
- const value = (0, env_1.getEnvValue)(PROJECT_ID_ENV_KEY);
98
- if (!value)
96
+ /** All Blockfrost project ids from `BLOCKFROST_PROJECT_ID` (comma separated); throws when unset. */
97
+ function getProjectIds() {
98
+ var _a;
99
+ const ids = ((_a = (0, env_1.getEnvValue)(PROJECT_ID_ENV_KEY)) !== null && _a !== void 0 ? _a : '').split(',').map(i => i.trim()).filter(Boolean);
100
+ if (!ids.length)
99
101
  throw new Error(`[chains.cardano] missing Blockfrost api key: set the ${PROJECT_ID_ENV_KEY} env var (https://blockfrost.io)`);
100
- return value;
102
+ return ids;
103
+ }
104
+ /** A random Blockfrost project id from `BLOCKFROST_PROJECT_ID`; throws when unset. */
105
+ function getProjectId() {
106
+ const ids = getProjectIds();
107
+ return ids[Math.floor(Math.random() * ids.length)];
101
108
  }
102
109
  // ---------------------------------------------------------------------------
103
110
  // client
@@ -114,7 +121,7 @@ function isNotFound(e) {
114
121
  */
115
122
  async function blockfrost({ path, params, allowNotFound = false, timeout = DEFAULT_TIMEOUT, retries }) {
116
123
  const endpoints = getEndpointList();
117
- const headers = { project_id: getProjectId(), 'Content-Type': 'application/json' };
124
+ const headers = { project_id: getProjectId(), 'Content-Type': 'application/json' }; // one key per call, reused by retries
118
125
  const config = { timeout, headers, params };
119
126
  const limiter = (0, rpc_1.getLimiter)(LIMITER_KEY, CONCURRENCY);
120
127
  const res = await (0, rpc_1.withRetry)(async (attempt) => {