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