@defillama/sdk 5.0.10 → 5.0.11

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,115 +1,52 @@
1
1
  "use strict";
2
- var __assign = (this && this.__assign) || function () {
3
- __assign = Object.assign || function(t) {
4
- for (var s, i = 1, n = arguments.length; i < n; i++) {
5
- s = arguments[i];
6
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
- t[p] = s[p];
8
- }
9
- return t;
10
- };
11
- return __assign.apply(this, arguments);
12
- };
13
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
14
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
15
- return new (P || (P = Promise))(function (resolve, reject) {
16
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
17
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
18
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
19
- step((generator = generator.apply(thisArg, _arguments || [])).next());
20
- });
21
- };
22
- var __generator = (this && this.__generator) || function (thisArg, body) {
23
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
24
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
25
- function verb(n) { return function (v) { return step([n, v]); }; }
26
- function step(op) {
27
- if (f) throw new TypeError("Generator is already executing.");
28
- while (g && (g = 0, op[0] && (_ = 0)), _) try {
29
- 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;
30
- if (y = 0, t) op = [op[0] & 2, t.value];
31
- switch (op[0]) {
32
- case 0: case 1: t = op; break;
33
- case 4: _.label++; return { value: op[1], done: false };
34
- case 5: _.label++; y = op[1]; op = [0]; continue;
35
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
36
- default:
37
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
38
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
39
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
40
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
41
- if (t[2]) _.ops.pop();
42
- _.trys.pop(); continue;
43
- }
44
- op = body.call(thisArg, _);
45
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
46
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
47
- }
48
- };
49
2
  Object.defineProperty(exports, "__esModule", { value: true });
50
3
  exports.call = void 0;
51
- var events_1 = require("events");
52
- var debugLog_1 = require("../util/debugLog");
53
- var env_1 = require("../util/env");
54
- var COUNTERS = {};
55
- var emitter = new events_1.EventEmitter();
4
+ const events_1 = require("events");
5
+ const debugLog_1 = require("../util/debugLog");
6
+ const env_1 = require("../util/env");
7
+ const COUNTERS = {};
8
+ const emitter = new events_1.EventEmitter();
56
9
  emitter.setMaxListeners(500000);
57
- function call(provider, data, block, chain, options) {
10
+ async function call(provider, data, block, chain, options = { retry: true }) {
58
11
  var _a;
59
- if (options === void 0) { options = { retry: true }; }
60
- return __awaiter(this, void 0, void 0, function () {
61
- function onComplete() {
62
- counter.activeWorkers--;
63
- if (counter.queue.length) {
64
- var nextRequestId = counter.pickFromTop ? counter.queue.shift() : counter.queue.pop();
65
- counter.pickFromTop = !counter.pickFromTop;
66
- emitter.emit(nextRequestId);
67
- }
12
+ const retry = (_a = options.retry) !== null && _a !== void 0 ? _a : true;
13
+ data.blockTag = block;
14
+ if (!chain)
15
+ chain = 'noChain';
16
+ const counter = getChainCounter(chain);
17
+ const currentId = counter.requestCount++;
18
+ const eventId = `${chain}-${currentId}`;
19
+ let chainMaxParallelCalls = (0, env_1.getMaxParallelRequests)(chain);
20
+ if (counter.activeWorkers > chainMaxParallelCalls) {
21
+ counter.queue.push(eventId);
22
+ await (0, events_1.once)(emitter, eventId);
23
+ }
24
+ counter.activeWorkers++;
25
+ if (debugLog_1.DEBUG_ENABLED) {
26
+ const showEveryX = counter.queue.length > 100 ? 50 : 10; // show log fewer times if lot more are queued up
27
+ if (currentId % showEveryX === 0)
28
+ (0, debugLog_1.debugLog)(`chain: ${chain} request #: ${currentId} queue: ${counter.queue.length} active requests: ${counter.activeWorkers}`);
29
+ }
30
+ let response;
31
+ try {
32
+ response = await provider.call(data, block);
33
+ onComplete();
34
+ }
35
+ catch (e) {
36
+ onComplete();
37
+ if (retry)
38
+ return call(provider, data, block, chain, { ...options, retry: false });
39
+ throw e;
40
+ }
41
+ return response;
42
+ function onComplete() {
43
+ counter.activeWorkers--;
44
+ if (counter.queue.length) {
45
+ const nextRequestId = counter.pickFromTop ? counter.queue.shift() : counter.queue.pop();
46
+ counter.pickFromTop = !counter.pickFromTop;
47
+ emitter.emit(nextRequestId);
68
48
  }
69
- var retry, counter, currentId, eventId, chainMaxParallelCalls, showEveryX, response, e_1;
70
- return __generator(this, function (_b) {
71
- switch (_b.label) {
72
- case 0:
73
- retry = (_a = options.retry) !== null && _a !== void 0 ? _a : true;
74
- data.blockTag = block;
75
- if (!chain)
76
- chain = 'noChain';
77
- counter = getChainCounter(chain);
78
- currentId = counter.requestCount++;
79
- eventId = "".concat(chain, "-").concat(currentId);
80
- chainMaxParallelCalls = (0, env_1.getMaxParallelRequests)(chain);
81
- if (!(counter.activeWorkers > chainMaxParallelCalls)) return [3 /*break*/, 2];
82
- counter.queue.push(eventId);
83
- return [4 /*yield*/, (0, events_1.once)(emitter, eventId)];
84
- case 1:
85
- _b.sent();
86
- _b.label = 2;
87
- case 2:
88
- counter.activeWorkers++;
89
- if (debugLog_1.DEBUG_ENABLED) {
90
- showEveryX = counter.queue.length > 100 ? 50 : 10 // show log fewer times if lot more are queued up
91
- ;
92
- if (currentId % showEveryX === 0)
93
- (0, debugLog_1.debugLog)("chain: ".concat(chain, " request #: ").concat(currentId, " queue: ").concat(counter.queue.length, " active requests: ").concat(counter.activeWorkers));
94
- }
95
- _b.label = 3;
96
- case 3:
97
- _b.trys.push([3, 5, , 6]);
98
- return [4 /*yield*/, provider.call(data, block)];
99
- case 4:
100
- response = _b.sent();
101
- onComplete();
102
- return [3 /*break*/, 6];
103
- case 5:
104
- e_1 = _b.sent();
105
- onComplete();
106
- if (retry)
107
- return [2 /*return*/, call(provider, data, block, chain, __assign(__assign({}, options), { retry: false }))];
108
- throw e_1;
109
- case 6: return [2 /*return*/, response];
110
- }
111
- });
112
- });
49
+ }
113
50
  }
114
51
  exports.call = call;
115
52
  function getChainCounter(chain) {
package/build/abi/tron.js CHANGED
@@ -1,283 +1,167 @@
1
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 __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
39
- if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
40
- if (ar || !(i in from)) {
41
- if (!ar) ar = Array.prototype.slice.call(from, 0, i);
42
- ar[i] = from[i];
43
- }
44
- }
45
- return to.concat(ar || Array.prototype.slice.call(from));
46
- };
47
2
  var __importDefault = (this && this.__importDefault) || function (mod) {
48
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
49
4
  };
50
5
  Object.defineProperty(exports, "__esModule", { value: true });
51
6
  exports.getBalances = exports.getBalance = exports.multiCall = exports.call = void 0;
52
- var node_fetch_1 = __importDefault(require("node-fetch"));
53
- var ethers_1 = require("ethers");
54
- var tron_format_address_1 = require("tron-format-address");
55
- var convertResults_1 = __importDefault(require("./convertResults"));
56
- var debugLog_1 = require("../util/debugLog");
57
- var util_1 = require("../util");
58
- var general_1 = require("../general");
59
- var p_limit_1 = __importDefault(require("p-limit"));
60
- var env_1 = require("../util/env");
61
- var limitRPCCalls = (0, p_limit_1.default)(+(0, env_1.getEnvValue)('TRON_RPC_CONCURRENCY_LIMIT', '5'));
62
- var ownerAddress = 'TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t';
63
- var MULTICALL_ADDRESS = 'TGXuuKAb4bnrn137u39EKbYzKNXvdCes98';
64
- var getEndpoint = function () { return (0, env_1.getEnvRPC)('tron'); };
65
- function call(options) {
7
+ const axios_1 = __importDefault(require("axios"));
8
+ const ethers_1 = require("ethers");
9
+ const tron_format_address_1 = require("tron-format-address");
10
+ const convertResults_1 = __importDefault(require("./convertResults"));
11
+ const debugLog_1 = require("../util/debugLog");
12
+ const util_1 = require("../util");
13
+ const general_1 = require("../general");
14
+ const p_limit_1 = __importDefault(require("p-limit"));
15
+ const env_1 = require("../util/env");
16
+ const limitRPCCalls = (0, p_limit_1.default)(+(0, env_1.getEnvValue)('TRON_RPC_CONCURRENCY_LIMIT', '5'));
17
+ const ownerAddress = 'TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t';
18
+ const MULTICALL_ADDRESS = 'TGXuuKAb4bnrn137u39EKbYzKNXvdCes98';
19
+ const getEndpoint = () => (0, env_1.getEnvRPC)('tron');
20
+ async function call(options) {
66
21
  var _a, _b;
67
- return __awaiter(this, void 0, void 0, function () {
68
- var target, abi, params, isMulticall, contractInterface, functionABI, inputTypes, outputTypes, callData, body, data, decodedResult;
69
- return __generator(this, function (_c) {
70
- switch (_c.label) {
71
- case 0:
72
- target = options.target, abi = options.abi, params = options.params, isMulticall = options.isMulticall;
73
- target = unhexifyTarget(target);
74
- contractInterface = new ethers_1.ethers.Interface([abi]);
75
- functionABI = ethers_1.ethers.FunctionFragment.from(abi);
76
- inputTypes = ((_a = abi.inputs) !== null && _a !== void 0 ? _a : []).map(function (i) { return i.type; });
77
- if (isMulticall)
78
- inputTypes = [ethers_1.ethers.ParamType.from({
79
- components: [
80
- { name: "target", type: "address" },
81
- { name: "callData", type: "bytes" },
82
- ],
83
- name: "data",
84
- type: "tuple[]",
85
- })];
86
- outputTypes = ((_b = abi.outputs) !== null && _b !== void 0 ? _b : []).map(function (i) { return i.type; });
87
- params.forEach(function (v, i) {
88
- if (inputTypes[i] === 'address' && !v.startsWith('0x'))
89
- params[i] = (0, tron_format_address_1.toHex)(v);
90
- });
91
- callData = contractInterface._encodeParams(inputTypes, params);
92
- body = {
93
- owner_address: ownerAddress,
94
- contract_address: target,
95
- function_selector: contractInterface.fragments[0].format(),
96
- parameter: callData.slice(2),
97
- visible: true,
98
- };
99
- return [4 /*yield*/, limitRPCCalls(function () { return post(body); })];
100
- case 1:
101
- data = (_c.sent()).constant_result[0];
102
- decodedResult = contractInterface.decodeFunctionResult(functionABI, '0x' + data);
103
- decodedResult = __spreadArray([], decodedResult, true);
104
- decodedResult.forEach(function (v, i) {
105
- if (outputTypes[i] === 'address' && v.startsWith('0x')) {
106
- v = (0, tron_format_address_1.fromHex)(v);
107
- decodedResult[i] = v;
108
- }
109
- var outputName = abi.outputs[i].name;
110
- if (outputName && outputName.length)
111
- decodedResult[outputName] = v;
112
- });
113
- return [2 /*return*/, {
114
- output: (0, convertResults_1.default)(decodedResult, functionABI),
115
- }];
116
- }
117
- });
22
+ let { target, abi, params, isMulticall, } = options;
23
+ target = unhexifyTarget(target);
24
+ const contractInterface = new ethers_1.ethers.Interface([abi]);
25
+ const functionABI = ethers_1.ethers.FunctionFragment.from(abi);
26
+ let inputTypes = ((_a = abi.inputs) !== null && _a !== void 0 ? _a : []).map((i) => i.type);
27
+ if (isMulticall)
28
+ inputTypes = [ethers_1.ethers.ParamType.from({
29
+ components: [
30
+ { name: "target", type: "address" },
31
+ { name: "callData", type: "bytes" },
32
+ ],
33
+ name: "data",
34
+ type: "tuple[]",
35
+ })];
36
+ const outputTypes = ((_b = abi.outputs) !== null && _b !== void 0 ? _b : []).map((i) => i.type);
37
+ params.forEach((v, i) => {
38
+ if (inputTypes[i] === 'address' && !v.startsWith('0x'))
39
+ params[i] = (0, tron_format_address_1.toHex)(v);
40
+ });
41
+ const callData = contractInterface._encodeParams(inputTypes, params);
42
+ const body = {
43
+ owner_address: ownerAddress,
44
+ contract_address: target,
45
+ function_selector: contractInterface.fragments[0].format(),
46
+ parameter: callData.slice(2),
47
+ visible: true,
48
+ };
49
+ const { constant_result: [data] } = await limitRPCCalls(() => post(body));
50
+ let decodedResult = contractInterface.decodeFunctionResult(functionABI, '0x' + data);
51
+ decodedResult = [...decodedResult];
52
+ decodedResult.forEach((v, i) => {
53
+ if (outputTypes[i] === 'address' && v.startsWith('0x')) {
54
+ v = (0, tron_format_address_1.fromHex)(v);
55
+ decodedResult[i] = v;
56
+ }
57
+ let outputName = abi.outputs[i].name;
58
+ if (outputName && outputName.length)
59
+ decodedResult[outputName] = v;
118
60
  });
61
+ return {
62
+ output: (0, convertResults_1.default)(decodedResult, functionABI),
63
+ };
119
64
  }
120
65
  exports.call = call;
121
- function multiCall(functionABI, calls) {
66
+ async function multiCall(functionABI, calls) {
122
67
  var _a;
123
- return __awaiter(this, void 0, void 0, function () {
124
- var inputs_1, returnValues;
125
- return __generator(this, function (_b) {
126
- switch (_b.label) {
127
- case 0:
128
- if ((_a = functionABI.inputs) === null || _a === void 0 ? void 0 : _a.length) {
129
- inputs_1 = functionABI.inputs;
130
- calls.forEach(function (call) {
131
- var _a;
132
- (_a = call.params) === null || _a === void 0 ? void 0 : _a.forEach(function (v, i) {
133
- if (inputs_1[i].type === 'address')
134
- call.params[i] = hexifyTarget(v);
135
- });
136
- });
137
- }
138
- return [4 /*yield*/, executeCalls(functionABI, calls)];
139
- case 1:
140
- returnValues = _b.sent();
141
- return [2 /*return*/, returnValues.map(function (output, index) {
142
- return {
143
- input: {
144
- params: calls[index].params,
145
- target: calls[index].contract,
146
- },
147
- success: output !== null,
148
- output: output,
149
- };
150
- })];
151
- }
68
+ if ((_a = functionABI.inputs) === null || _a === void 0 ? void 0 : _a.length) {
69
+ const inputs = functionABI.inputs;
70
+ calls.forEach((call) => {
71
+ var _a;
72
+ (_a = call.params) === null || _a === void 0 ? void 0 : _a.forEach((v, i) => {
73
+ if (inputs[i].type === 'address')
74
+ call.params[i] = hexifyTarget(v);
75
+ });
152
76
  });
77
+ }
78
+ const returnValues = await executeCalls(functionABI, calls);
79
+ return returnValues.map((output, index) => {
80
+ return {
81
+ input: {
82
+ params: calls[index].params,
83
+ target: calls[index].contract,
84
+ },
85
+ success: output !== null,
86
+ output,
87
+ };
153
88
  });
154
89
  }
155
90
  exports.multiCall = multiCall;
156
- function getBalance(params) {
91
+ async function getBalance(params) {
157
92
  var _a, _b, _c;
158
- return __awaiter(this, void 0, void 0, function () {
159
- var data, balance;
160
- return __generator(this, function (_d) {
161
- switch (_d.label) {
162
- case 0: return [4 /*yield*/, limitRPCCalls(function () { return post({ address: params.target, visible: true, }, '/wallet/getaccount'); })];
163
- case 1:
164
- data = _d.sent();
165
- balance = (((_a = data.balance) !== null && _a !== void 0 ? _a : 0) + ((_c = (_b = data.frozen) === null || _b === void 0 ? void 0 : _b.reduce(function (t, _a) {
166
- var frozen_balance = _a.frozen_balance;
167
- return t + frozen_balance;
168
- }, 0)) !== null && _c !== void 0 ? _c : 0)).toString();
169
- return [2 /*return*/, {
170
- output: (0, general_1.handleDecimals)(balance, params.decimals),
171
- }];
172
- }
173
- });
174
- });
93
+ const data = await limitRPCCalls(() => post({ address: params.target, visible: true, }, '/wallet/getaccount'));
94
+ const balance = (((_a = data.balance) !== null && _a !== void 0 ? _a : 0) + ((_c = (_b = data.frozen) === null || _b === void 0 ? void 0 : _b.reduce((t, { frozen_balance }) => t + frozen_balance, 0)) !== null && _c !== void 0 ? _c : 0)).toString();
95
+ return {
96
+ output: (0, general_1.handleDecimals)(balance, params.decimals),
97
+ };
175
98
  }
176
99
  exports.getBalance = getBalance;
177
- function getBalances(params) {
178
- return __awaiter(this, void 0, void 0, function () {
179
- var targets, decimals, res;
180
- return __generator(this, function (_a) {
181
- switch (_a.label) {
182
- case 0:
183
- targets = params.targets, decimals = params.decimals;
184
- return [4 /*yield*/, Promise.all(targets.map(function (i) { return getBalance({ target: i, decimals: decimals }); }))];
185
- case 1:
186
- res = _a.sent();
187
- return [2 /*return*/, {
188
- output: res.map(function (v, i) { return ({ target: targets[i], balance: v.output }); }),
189
- }];
190
- }
191
- });
192
- });
100
+ async function getBalances(params) {
101
+ const { targets, decimals } = params;
102
+ const res = await Promise.all(targets.map(i => getBalance({ target: i, decimals })));
103
+ return {
104
+ output: res.map((v, i) => ({ target: targets[i], balance: v.output })),
105
+ };
193
106
  }
194
107
  exports.getBalances = getBalances;
195
- function executeCalls(functionABI, calls) {
196
- return __awaiter(this, void 0, void 0, function () {
197
- var contractInterface, fd, contractCalls, _a, _, returnData, e_1, chunkSize, chunks, response;
198
- var _this = this;
199
- return __generator(this, function (_b) {
200
- switch (_b.label) {
201
- case 0:
202
- contractInterface = new ethers_1.ethers.Interface([functionABI]);
203
- fd = contractInterface.fragments[0];
204
- contractCalls = calls.map(function (call) {
205
- var data = contractInterface.encodeFunctionData(fd, call.params);
206
- return {
207
- to: call.contract,
208
- data: data,
209
- };
210
- });
211
- _b.label = 1;
212
- case 1:
213
- _b.trys.push([1, 3, , 6]);
214
- return [4 /*yield*/, call({ target: MULTICALL_ADDRESS, params: [contractCalls.map(function (call) { return ({ target: hexifyTarget(call.to), callData: call.data, isMulticall: true }); })], abi: MULTICALL_ABI, isMulticall: true })];
215
- case 2:
216
- _a = (_b.sent()).output, _ = _a[0], returnData = _a[1];
217
- return [2 /*return*/, returnData.map(function (v) {
218
- var _a;
219
- var decodedResult = contractInterface.decodeFunctionResult(functionABI, v);
220
- var outputTypes = ((_a = functionABI.outputs) !== null && _a !== void 0 ? _a : []).map(function (i) { return i.type; });
221
- var output = __spreadArray([], decodedResult, true);
222
- output.forEach(function (v, i) {
223
- if (outputTypes[i] === 'address' && v.startsWith('0x')) {
224
- v = (0, tron_format_address_1.fromHex)(v);
225
- output[i] = v;
226
- }
227
- var outputName = functionABI.outputs[i].name;
228
- if (outputName && outputName.length)
229
- output[outputName] = v;
230
- });
231
- return (0, convertResults_1.default)(output, fd);
232
- })];
233
- case 3:
234
- e_1 = _b.sent();
235
- console.error(e_1);
236
- if (!(calls.length > 10)) return [3 /*break*/, 5];
237
- chunkSize = Math.ceil(calls.length / 5);
238
- chunks = (0, util_1.sliceIntoChunks)(calls, chunkSize);
239
- (0, debugLog_1.debugLog)("Multicall failed, call size: ".concat(contractCalls.length, ", splitting into smaller chunks and trying again, new call size: ").concat(chunks[0].length));
240
- return [4 /*yield*/, (0, util_1.runInPromisePool)({
241
- items: chunks,
242
- concurrency: 2,
243
- processor: function (calls) { return executeCalls(functionABI, calls); }
244
- })];
245
- case 4:
246
- response = _b.sent();
247
- return [2 /*return*/, response.flat()];
248
- case 5:
249
- (0, debugLog_1.debugLog)("Multicall failed, defaulting to single transactions...");
250
- return [3 /*break*/, 6];
251
- case 6: return [2 /*return*/, (0, util_1.runInPromisePool)({
252
- items: calls,
253
- concurrency: 3,
254
- processor: function (_a) {
255
- var contract = _a.contract, params = _a.params;
256
- return __awaiter(_this, void 0, void 0, function () {
257
- var result, e_2;
258
- return __generator(this, function (_b) {
259
- switch (_b.label) {
260
- case 0:
261
- result = null;
262
- _b.label = 1;
263
- case 1:
264
- _b.trys.push([1, 3, , 4]);
265
- return [4 /*yield*/, call({ target: contract, params: params, abi: functionABI })];
266
- case 2:
267
- result = (_b.sent()).output;
268
- return [3 /*break*/, 4];
269
- case 3:
270
- e_2 = _b.sent();
271
- (0, debugLog_1.debugLog)(e_2);
272
- return [3 /*break*/, 4];
273
- case 4: return [2 /*return*/, result];
274
- }
275
- });
276
- });
277
- }
278
- })];
279
- }
108
+ async function executeCalls(functionABI, calls) {
109
+ const contractInterface = new ethers_1.ethers.Interface([functionABI]);
110
+ let fd = contractInterface.fragments[0];
111
+ const contractCalls = calls.map((call) => {
112
+ const data = contractInterface.encodeFunctionData(fd, call.params);
113
+ return {
114
+ to: call.contract,
115
+ data: data,
116
+ };
117
+ });
118
+ try {
119
+ const { output: [_, returnData] } = await call({ target: MULTICALL_ADDRESS, params: [contractCalls.map((call) => ({ target: hexifyTarget(call.to), callData: call.data, isMulticall: true }))], abi: MULTICALL_ABI, isMulticall: true });
120
+ return returnData.map((v) => {
121
+ var _a;
122
+ let decodedResult = contractInterface.decodeFunctionResult(functionABI, v);
123
+ const outputTypes = ((_a = functionABI.outputs) !== null && _a !== void 0 ? _a : []).map((i) => i.type);
124
+ let output = [...decodedResult];
125
+ output.forEach((v, i) => {
126
+ if (outputTypes[i] === 'address' && v.startsWith('0x')) {
127
+ v = (0, tron_format_address_1.fromHex)(v);
128
+ output[i] = v;
129
+ }
130
+ let outputName = functionABI.outputs[i].name;
131
+ if (outputName && outputName.length)
132
+ output[outputName] = v;
133
+ });
134
+ return (0, convertResults_1.default)(output, fd);
280
135
  });
136
+ }
137
+ catch (e) {
138
+ console.error(e);
139
+ if (calls.length > 10) {
140
+ const chunkSize = Math.ceil(calls.length / 5);
141
+ const chunks = (0, util_1.sliceIntoChunks)(calls, chunkSize);
142
+ (0, debugLog_1.debugLog)(`Multicall failed, call size: ${contractCalls.length}, splitting into smaller chunks and trying again, new call size: ${chunks[0].length}`);
143
+ const response = await (0, util_1.runInPromisePool)({
144
+ items: chunks,
145
+ concurrency: 2,
146
+ processor: (calls) => executeCalls(functionABI, calls)
147
+ });
148
+ return response.flat();
149
+ }
150
+ (0, debugLog_1.debugLog)("Multicall failed, defaulting to single transactions...");
151
+ }
152
+ return (0, util_1.runInPromisePool)({
153
+ items: calls,
154
+ concurrency: 3,
155
+ processor: async ({ contract, params }) => {
156
+ let result = null;
157
+ try {
158
+ result = (await call({ target: contract, params, abi: functionABI })).output;
159
+ }
160
+ catch (e) {
161
+ (0, debugLog_1.debugLog)(e);
162
+ }
163
+ return result;
164
+ }
281
165
  });
282
166
  }
283
167
  function unhexifyTarget(address) {
@@ -290,33 +174,19 @@ function hexifyTarget(address) {
290
174
  return (0, tron_format_address_1.toHex)(address);
291
175
  return address;
292
176
  }
293
- function post(body, endpoint) {
294
- if (body === void 0) { body = {}; }
295
- if (endpoint === void 0) { endpoint = '/wallet/triggerconstantcontract'; }
296
- return __awaiter(this, void 0, void 0, function () {
297
- var host, headers, apiKey, response;
298
- return __generator(this, function (_a) {
299
- switch (_a.label) {
300
- case 0:
301
- host = getEndpoint();
302
- headers = { 'Content-Type': 'application/json' };
303
- apiKey = (0, env_1.getEnvValue)('TRON_PRO_API_KEY');
304
- if (apiKey)
305
- headers['TRON-PRO-API-KEY'] = apiKey;
306
- return [4 /*yield*/, (0, node_fetch_1.default)(host + endpoint, {
307
- method: 'post',
308
- body: JSON.stringify(body),
309
- headers: headers,
310
- })];
311
- case 1:
312
- response = _a.sent();
313
- return [2 /*return*/, response.json()];
314
- }
315
- });
177
+ async function post(body = {}, endpoint = '/wallet/triggerconstantcontract') {
178
+ const host = getEndpoint();
179
+ const headers = { 'Content-Type': 'application/json' };
180
+ const apiKey = (0, env_1.getEnvValue)('TRON_PRO_API_KEY');
181
+ if (apiKey)
182
+ headers['TRON-PRO-API-KEY'] = apiKey;
183
+ const { data } = await axios_1.default.post(host + endpoint, body, {
184
+ headers,
316
185
  });
186
+ return data;
317
187
  }
318
188
  // this is needed else correct function identifier wont be picked up while calling
319
- var MULTICALL_ABI = {
189
+ const MULTICALL_ABI = {
320
190
  "inputs": [
321
191
  {
322
192
  "components": [
package/build/api.js CHANGED
@@ -28,9 +28,8 @@ exports.util = __importStar(require("./util"));
28
28
  exports.eth = __importStar(require("./eth"));
29
29
  exports.erc20 = __importStar(require("./erc20"));
30
30
  exports.abi = __importStar(require("./abi"));
31
- var general_1 = require("./general");
32
- var config = {
33
- setProvider: general_1.setProvider,
34
- getProvider: general_1.getProvider,
31
+ const general_1 = require("./general");
32
+ const config = {
33
+ setProvider: general_1.setProvider, getProvider: general_1.getProvider,
35
34
  };
36
35
  exports.config = config;