stxer 0.3.2 → 0.4.0

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.
@@ -373,80 +373,87 @@ function _unsupportedIterableToArray(r, a) {
373
373
  }
374
374
 
375
375
  var DEFAULT_STXER_API = 'https://api.stxer.xyz';
376
+ function convertResults(rs) {
377
+ var results = [];
378
+ for (var _iterator = _createForOfIteratorHelperLoose(rs), _step; !(_step = _iterator()).done;) {
379
+ var v = _step.value;
380
+ if ('Ok' in v) {
381
+ results.push(transactions.deserializeCV(v.Ok));
382
+ } else {
383
+ results.push(new Error(v.Err));
384
+ }
385
+ }
386
+ return results;
387
+ }
376
388
  function batchRead(_x, _x2) {
377
389
  return _batchRead.apply(this, arguments);
378
390
  }
379
391
  function _batchRead() {
380
392
  _batchRead = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee(reads, options) {
381
393
  var _options$stxerApi;
382
- var payload, _iterator, _step, variable, _iterator2, _step2, map, ibh, url, data, text, results, rs, variablesLength, i, result, _i, _result;
394
+ var ibh, payload, _iterator2, _step2, variable, _iterator3, _step3, map, _iterator4, _step4, ro, url, data, text, rs;
383
395
  return _regeneratorRuntime().wrap(function _callee$(_context) {
384
396
  while (1) switch (_context.prev = _context.next) {
385
397
  case 0:
386
398
  if (options === void 0) {
387
399
  options = {};
388
400
  }
389
- payload = [];
401
+ ibh = reads.index_block_hash == null ? undefined : reads.index_block_hash.startsWith('0x') ? reads.index_block_hash.substring(2) : reads.index_block_hash;
402
+ payload = {
403
+ vars: [],
404
+ maps: [],
405
+ readonly: [],
406
+ tip: ibh
407
+ };
390
408
  if (reads.variables != null) {
391
- for (_iterator = _createForOfIteratorHelperLoose(reads.variables); !(_step = _iterator()).done;) {
392
- variable = _step.value;
393
- payload.push([transactions.serializeCV(variable.contract), variable.variableName]);
409
+ for (_iterator2 = _createForOfIteratorHelperLoose(reads.variables); !(_step2 = _iterator2()).done;) {
410
+ variable = _step2.value;
411
+ payload.vars.push([transactions.serializeCV(variable.contract), variable.variableName]);
394
412
  }
395
413
  }
396
414
  if (reads.maps != null) {
397
- for (_iterator2 = _createForOfIteratorHelperLoose(reads.maps); !(_step2 = _iterator2()).done;) {
398
- map = _step2.value;
399
- payload.push([transactions.serializeCV(map.contract), map.mapName, transactions.serializeCV(map.mapKey)]);
415
+ for (_iterator3 = _createForOfIteratorHelperLoose(reads.maps); !(_step3 = _iterator3()).done;) {
416
+ map = _step3.value;
417
+ payload.maps.push([transactions.serializeCV(map.contract), map.mapName, transactions.serializeCV(map.mapKey)]);
400
418
  }
401
419
  }
402
- ibh = reads.index_block_hash == null ? null : reads.index_block_hash.startsWith('0x') ? reads.index_block_hash.substring(2) : reads.index_block_hash;
403
- url = ((_options$stxerApi = options.stxerApi) != null ? _options$stxerApi : DEFAULT_STXER_API) + "/sidecar/v2/batch-reads" + (ibh == null ? '' : "?tip=" + ibh);
404
- _context.next = 8;
420
+ if (reads.readonly != null) {
421
+ for (_iterator4 = _createForOfIteratorHelperLoose(reads.readonly); !(_step4 = _iterator4()).done;) {
422
+ ro = _step4.value;
423
+ payload.readonly.push([transactions.serializeCV(ro.contract), ro.functionName].concat(ro.functionArgs.map(function (v) {
424
+ return transactions.serializeCV(v);
425
+ })));
426
+ }
427
+ }
428
+ url = ((_options$stxerApi = options.stxerApi) != null ? _options$stxerApi : DEFAULT_STXER_API) + "/sidecar/v2/batch";
429
+ _context.next = 9;
405
430
  return fetch(url, {
406
431
  method: 'POST',
407
- body: JSON.stringify(payload)
432
+ body: JSON.stringify(payload),
433
+ headers: {
434
+ 'Content-Type': 'application/json'
435
+ }
408
436
  });
409
- case 8:
437
+ case 9:
410
438
  data = _context.sent;
411
- _context.next = 11;
439
+ _context.next = 12;
412
440
  return data.text();
413
- case 11:
441
+ case 12:
414
442
  text = _context.sent;
415
443
  if (!(!text.includes('Ok') && !text.includes('Err'))) {
416
- _context.next = 14;
444
+ _context.next = 15;
417
445
  break;
418
446
  }
419
447
  throw new Error("Requesting batch reads failed: " + text + ", url: " + url + ", payload: " + JSON.stringify(payload));
420
- case 14:
421
- results = JSON.parse(text);
422
- rs = {
423
- variables: [],
424
- maps: []
425
- };
426
- variablesLength = 0;
427
- if (reads.variables != null) {
428
- variablesLength = reads.variables.length;
429
- for (i = 0; i < variablesLength; i++) {
430
- result = results[i];
431
- if ('Ok' in result) {
432
- rs.variables.push(transactions.deserializeCV(result.Ok));
433
- } else {
434
- rs.variables.push(new Error(result.Err));
435
- }
436
- }
437
- }
438
- if (reads.maps != null) {
439
- for (_i = 0; _i < reads.maps.length; _i++) {
440
- _result = results[_i + variablesLength];
441
- if ('Ok' in _result) {
442
- rs.maps.push(transactions.deserializeCV(_result.Ok));
443
- } else {
444
- rs.maps.push(new Error(_result.Err));
445
- }
446
- }
447
- }
448
- return _context.abrupt("return", rs);
449
- case 20:
448
+ case 15:
449
+ rs = JSON.parse(text);
450
+ return _context.abrupt("return", {
451
+ tip: rs.tip,
452
+ vars: convertResults(rs.vars),
453
+ maps: convertResults(rs.maps),
454
+ readonly: convertResults(rs.readonly)
455
+ });
456
+ case 17:
450
457
  case "end":
451
458
  return _context.stop();
452
459
  }
@@ -454,62 +461,6 @@ function _batchRead() {
454
461
  }));
455
462
  return _batchRead.apply(this, arguments);
456
463
  }
457
- function batchReadonly(_x3, _x4) {
458
- return _batchReadonly.apply(this, arguments);
459
- }
460
- function _batchReadonly() {
461
- _batchReadonly = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee2(req, options) {
462
- var _options$stxerApi2;
463
- var payload, ibh, url, data, text, results, rs, _iterator3, _step3, result;
464
- return _regeneratorRuntime().wrap(function _callee2$(_context2) {
465
- while (1) switch (_context2.prev = _context2.next) {
466
- case 0:
467
- if (options === void 0) {
468
- options = {};
469
- }
470
- payload = req.readonly.map(function (r) {
471
- return [transactions.serializeCV(r.contract), r.functionName].concat(r.functionArgs.map(function (arg) {
472
- return transactions.serializeCV(arg);
473
- }));
474
- });
475
- ibh = req.index_block_hash == null ? null : req.index_block_hash.startsWith('0x') ? req.index_block_hash.substring(2) : req.index_block_hash;
476
- url = ((_options$stxerApi2 = options.stxerApi) != null ? _options$stxerApi2 : DEFAULT_STXER_API) + "/sidecar/v2/batch-readonly" + (ibh == null ? '' : "?tip=" + ibh);
477
- _context2.next = 6;
478
- return fetch(url, {
479
- method: 'POST',
480
- body: JSON.stringify(payload)
481
- });
482
- case 6:
483
- data = _context2.sent;
484
- _context2.next = 9;
485
- return data.text();
486
- case 9:
487
- text = _context2.sent;
488
- if (!(!text.includes('Ok') && !text.includes('Err'))) {
489
- _context2.next = 12;
490
- break;
491
- }
492
- throw new Error("Requesting batch readonly failed: " + text + ", url: " + url + ", payload: " + JSON.stringify(payload));
493
- case 12:
494
- results = JSON.parse(text);
495
- rs = [];
496
- for (_iterator3 = _createForOfIteratorHelperLoose(results); !(_step3 = _iterator3()).done;) {
497
- result = _step3.value;
498
- if ('Ok' in result) {
499
- rs.push(transactions.deserializeCV(result.Ok));
500
- } else {
501
- rs.push(new Error(result.Err));
502
- }
503
- }
504
- return _context2.abrupt("return", rs);
505
- case 16:
506
- case "end":
507
- return _context2.stop();
508
- }
509
- }, _callee2);
510
- }));
511
- return _batchReadonly.apply(this, arguments);
512
- }
513
464
 
514
465
  function runTx(tx) {
515
466
  // type 0: run transaction
@@ -956,9 +907,324 @@ var SimulationBuilder = /*#__PURE__*/function () {
956
907
  return SimulationBuilder;
957
908
  }();
958
909
 
910
+ var BatchProcessor = /*#__PURE__*/function () {
911
+ function BatchProcessor(options) {
912
+ var _options$stxerAPIEndp;
913
+ this.queues = new Map();
914
+ this.timeoutIds = new Map();
915
+ this.stxerAPIEndpoint = void 0;
916
+ this.batchDelayMs = void 0;
917
+ this.stxerAPIEndpoint = (_options$stxerAPIEndp = options.stxerAPIEndpoint) != null ? _options$stxerAPIEndp : 'https://api.stxer.xyz';
918
+ this.batchDelayMs = options.batchDelayMs;
919
+ }
920
+ var _proto = BatchProcessor.prototype;
921
+ _proto.getQueueKey = function getQueueKey(tip) {
922
+ return tip != null ? tip : '_undefined';
923
+ };
924
+ _proto.enqueue = function enqueue(request) {
925
+ var _this$queues$get,
926
+ _this = this;
927
+ var queueKey = this.getQueueKey(request.tip);
928
+ var queue = (_this$queues$get = this.queues.get(queueKey)) != null ? _this$queues$get : [];
929
+ if (!this.queues.has(queueKey)) {
930
+ this.queues.set(queueKey, queue);
931
+ }
932
+ queue.push(request);
933
+ if (!this.timeoutIds.has(queueKey)) {
934
+ var timeoutId = setTimeout(function () {
935
+ return _this.processBatch(queueKey);
936
+ }, this.batchDelayMs);
937
+ this.timeoutIds.set(queueKey, timeoutId);
938
+ }
939
+ };
940
+ _proto.processBatch = /*#__PURE__*/function () {
941
+ var _processBatch = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee(queueKey) {
942
+ var _this$queues$get2;
943
+ var currentQueue, timeoutId, readonlyRequests, mapRequests, variableRequests, tip, batchRequest, results, _iterator, _step, _step$value, index, result, _iterator2, _step2, _step2$value, _index, _result, _iterator3, _step3, _step3$value, _index2, _result2, _iterator4, _step4, item;
944
+ return _regeneratorRuntime().wrap(function _callee$(_context) {
945
+ while (1) switch (_context.prev = _context.next) {
946
+ case 0:
947
+ currentQueue = (_this$queues$get2 = this.queues.get(queueKey)) != null ? _this$queues$get2 : [];
948
+ this.queues["delete"](queueKey);
949
+ timeoutId = this.timeoutIds.get(queueKey);
950
+ if (timeoutId) {
951
+ clearTimeout(timeoutId);
952
+ this.timeoutIds["delete"](queueKey);
953
+ }
954
+ if (!(currentQueue.length === 0)) {
955
+ _context.next = 6;
956
+ break;
957
+ }
958
+ return _context.abrupt("return");
959
+ case 6:
960
+ _context.prev = 6;
961
+ readonlyRequests = currentQueue.filter(function (q) {
962
+ return q.request.mode === 'readonly';
963
+ });
964
+ mapRequests = currentQueue.filter(function (q) {
965
+ return q.request.mode === 'mapEntry';
966
+ });
967
+ variableRequests = currentQueue.filter(function (q) {
968
+ return q.request.mode === 'variable';
969
+ });
970
+ tip = queueKey === '_undefined' ? undefined : queueKey;
971
+ batchRequest = {
972
+ readonly: readonlyRequests.map(function (_ref) {
973
+ var request = _ref.request;
974
+ return {
975
+ contract: transactions.contractPrincipalCV(request.contractAddress, request.contractName),
976
+ functionName: request.functionName,
977
+ functionArgs: request.functionArgs
978
+ };
979
+ }),
980
+ maps: mapRequests.map(function (_ref2) {
981
+ var request = _ref2.request;
982
+ return {
983
+ contract: transactions.contractPrincipalCV(request.contractAddress, request.contractName),
984
+ mapName: request.mapName,
985
+ mapKey: request.mapKey
986
+ };
987
+ }),
988
+ variables: variableRequests.map(function (_ref3) {
989
+ var request = _ref3.request;
990
+ return {
991
+ contract: transactions.contractPrincipalCV(request.contractAddress, request.contractName),
992
+ variableName: request.variableName
993
+ };
994
+ }),
995
+ index_block_hash: tip
996
+ };
997
+ _context.next = 14;
998
+ return batchRead(batchRequest, {
999
+ stxerApi: this.stxerAPIEndpoint
1000
+ });
1001
+ case 14:
1002
+ results = _context.sent;
1003
+ // Handle readonly results
1004
+ for (_iterator = _createForOfIteratorHelperLoose(results.readonly.entries()); !(_step = _iterator()).done;) {
1005
+ _step$value = _step.value, index = _step$value[0], result = _step$value[1];
1006
+ if (result instanceof Error) {
1007
+ readonlyRequests[index].reject(result);
1008
+ } else {
1009
+ readonlyRequests[index].resolve(result);
1010
+ }
1011
+ }
1012
+ // Handle variable results
1013
+ for (_iterator2 = _createForOfIteratorHelperLoose(results.vars.entries()); !(_step2 = _iterator2()).done;) {
1014
+ _step2$value = _step2.value, _index = _step2$value[0], _result = _step2$value[1];
1015
+ if (_result instanceof Error) {
1016
+ variableRequests[_index].reject(_result);
1017
+ } else {
1018
+ variableRequests[_index].resolve(_result);
1019
+ }
1020
+ }
1021
+ // Handle map results
1022
+ for (_iterator3 = _createForOfIteratorHelperLoose(results.maps.entries()); !(_step3 = _iterator3()).done;) {
1023
+ _step3$value = _step3.value, _index2 = _step3$value[0], _result2 = _step3$value[1];
1024
+ if (_result2 instanceof Error) {
1025
+ mapRequests[_index2].reject(_result2);
1026
+ } else {
1027
+ mapRequests[_index2].resolve(_result2);
1028
+ }
1029
+ }
1030
+ _context.next = 23;
1031
+ break;
1032
+ case 20:
1033
+ _context.prev = 20;
1034
+ _context.t0 = _context["catch"](6);
1035
+ for (_iterator4 = _createForOfIteratorHelperLoose(currentQueue); !(_step4 = _iterator4()).done;) {
1036
+ item = _step4.value;
1037
+ item.reject(_context.t0);
1038
+ }
1039
+ case 23:
1040
+ case "end":
1041
+ return _context.stop();
1042
+ }
1043
+ }, _callee, this, [[6, 20]]);
1044
+ }));
1045
+ function processBatch(_x) {
1046
+ return _processBatch.apply(this, arguments);
1047
+ }
1048
+ return processBatch;
1049
+ }();
1050
+ return BatchProcessor;
1051
+ }();
1052
+
1053
+ // Shared processor instance with default settings
1054
+ var defaultProcessor = /*#__PURE__*/new BatchProcessor({
1055
+ batchDelayMs: 100
1056
+ });
1057
+ function callReadonly(_x) {
1058
+ return _callReadonly.apply(this, arguments);
1059
+ }
1060
+ function _callReadonly() {
1061
+ _callReadonly = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee(params) {
1062
+ var _params$batchProcesso;
1063
+ var processor, _params$contract$spli, deployer, contractName, fn, functionDef, argsKV, args, _iterator, _step, argDef;
1064
+ return _regeneratorRuntime().wrap(function _callee$(_context) {
1065
+ while (1) switch (_context.prev = _context.next) {
1066
+ case 0:
1067
+ processor = (_params$batchProcesso = params.batchProcessor) != null ? _params$batchProcesso : defaultProcessor;
1068
+ _params$contract$spli = params.contract.split('.', 2), deployer = _params$contract$spli[0], contractName = _params$contract$spli[1];
1069
+ fn = String(params.functionName);
1070
+ functionDef = params.abi.find(function (def) {
1071
+ return def.name === params.functionName;
1072
+ });
1073
+ if (functionDef) {
1074
+ _context.next = 6;
1075
+ break;
1076
+ }
1077
+ throw new Error("failed to find function definition for " + params.functionName);
1078
+ case 6:
1079
+ argsKV = params.args;
1080
+ args = [];
1081
+ for (_iterator = _createForOfIteratorHelperLoose(functionDef.args); !(_step = _iterator()).done;) {
1082
+ argDef = _step.value;
1083
+ args.push(tsClarity.encodeAbi(argDef.type, argsKV[argDef.name]));
1084
+ }
1085
+ return _context.abrupt("return", new Promise(function (_resolve, reject) {
1086
+ processor.enqueue({
1087
+ request: {
1088
+ mode: 'readonly',
1089
+ contractAddress: deployer,
1090
+ contractName: contractName,
1091
+ functionName: fn,
1092
+ functionArgs: args
1093
+ },
1094
+ tip: params.indexBlockHash,
1095
+ resolve: function resolve(result) {
1096
+ try {
1097
+ var decoded = tsClarity.decodeAbi(functionDef.outputs.type, result);
1098
+ _resolve(decoded);
1099
+ } catch (error) {
1100
+ reject(error);
1101
+ }
1102
+ },
1103
+ reject: reject
1104
+ });
1105
+ }));
1106
+ case 10:
1107
+ case "end":
1108
+ return _context.stop();
1109
+ }
1110
+ }, _callee);
1111
+ }));
1112
+ return _callReadonly.apply(this, arguments);
1113
+ }
1114
+ function readMap(_x2) {
1115
+ return _readMap.apply(this, arguments);
1116
+ }
1117
+ function _readMap() {
1118
+ _readMap = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee2(params) {
1119
+ var _params$batchProcesso2;
1120
+ var processor, _params$contract$spli2, deployer, contractName, mapDef, key;
1121
+ return _regeneratorRuntime().wrap(function _callee2$(_context2) {
1122
+ while (1) switch (_context2.prev = _context2.next) {
1123
+ case 0:
1124
+ processor = (_params$batchProcesso2 = params.batchProcessor) != null ? _params$batchProcesso2 : defaultProcessor;
1125
+ _params$contract$spli2 = params.contract.split('.', 2), deployer = _params$contract$spli2[0], contractName = _params$contract$spli2[1];
1126
+ mapDef = params.abi.find(function (m) {
1127
+ return m.name === params.mapName;
1128
+ });
1129
+ if (mapDef) {
1130
+ _context2.next = 5;
1131
+ break;
1132
+ }
1133
+ throw new Error("failed to find map definition for " + params.mapName);
1134
+ case 5:
1135
+ key = tsClarity.encodeAbi(mapDef.key, params.key);
1136
+ return _context2.abrupt("return", new Promise(function (_resolve2, reject) {
1137
+ processor.enqueue({
1138
+ request: {
1139
+ mode: 'mapEntry',
1140
+ contractAddress: deployer,
1141
+ contractName: contractName,
1142
+ mapName: params.mapName,
1143
+ mapKey: key
1144
+ },
1145
+ tip: params.indexBlockHash,
1146
+ resolve: function resolve(result) {
1147
+ try {
1148
+ if (result.type === transactions.ClarityType.OptionalNone) {
1149
+ _resolve2(null);
1150
+ return;
1151
+ }
1152
+ if (result.type !== transactions.ClarityType.OptionalSome) {
1153
+ throw new Error("unexpected map value: " + result);
1154
+ }
1155
+ var someCV = result;
1156
+ var decoded = tsClarity.decodeAbi(mapDef.value, someCV.value);
1157
+ _resolve2(decoded);
1158
+ } catch (error) {
1159
+ reject(error);
1160
+ }
1161
+ },
1162
+ reject: reject
1163
+ });
1164
+ }));
1165
+ case 7:
1166
+ case "end":
1167
+ return _context2.stop();
1168
+ }
1169
+ }, _callee2);
1170
+ }));
1171
+ return _readMap.apply(this, arguments);
1172
+ }
1173
+ function readVariable(_x3) {
1174
+ return _readVariable.apply(this, arguments);
1175
+ }
1176
+ function _readVariable() {
1177
+ _readVariable = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee3(params) {
1178
+ var _params$batchProcesso3;
1179
+ var processor, _params$contract$spli3, deployer, contractName, varDef;
1180
+ return _regeneratorRuntime().wrap(function _callee3$(_context3) {
1181
+ while (1) switch (_context3.prev = _context3.next) {
1182
+ case 0:
1183
+ processor = (_params$batchProcesso3 = params.batchProcessor) != null ? _params$batchProcesso3 : defaultProcessor;
1184
+ _params$contract$spli3 = params.contract.split('.', 2), deployer = _params$contract$spli3[0], contractName = _params$contract$spli3[1];
1185
+ varDef = params.abi.find(function (def) {
1186
+ return def.name === params.variableName;
1187
+ });
1188
+ if (varDef) {
1189
+ _context3.next = 5;
1190
+ break;
1191
+ }
1192
+ throw new Error("failed to find variable definition for " + params.variableName);
1193
+ case 5:
1194
+ return _context3.abrupt("return", new Promise(function (_resolve3, reject) {
1195
+ processor.enqueue({
1196
+ request: {
1197
+ mode: 'variable',
1198
+ contractAddress: deployer,
1199
+ contractName: contractName,
1200
+ variableName: params.variableName
1201
+ },
1202
+ tip: params.indexBlockHash,
1203
+ resolve: function resolve(result) {
1204
+ try {
1205
+ var decoded = tsClarity.decodeAbi(varDef.type, result);
1206
+ _resolve3(decoded);
1207
+ } catch (error) {
1208
+ reject(error);
1209
+ }
1210
+ },
1211
+ reject: reject
1212
+ });
1213
+ }));
1214
+ case 6:
1215
+ case "end":
1216
+ return _context3.stop();
1217
+ }
1218
+ }, _callee3);
1219
+ }));
1220
+ return _readVariable.apply(this, arguments);
1221
+ }
1222
+
959
1223
  exports.SimulationBuilder = SimulationBuilder;
960
1224
  exports.batchRead = batchRead;
961
- exports.batchReadonly = batchReadonly;
1225
+ exports.callReadonly = callReadonly;
1226
+ exports.readMap = readMap;
1227
+ exports.readVariable = readVariable;
962
1228
  exports.runEval = runEval;
963
1229
  exports.runSimulation = runSimulation;
964
1230
  //# sourceMappingURL=stxer.cjs.development.js.map