stxer 0.3.3 → 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 { serializeCV, deserializeCV, 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
 
@@ -457,62 +457,6 @@ function _batchRead() {
457
457
  }));
458
458
  return _batchRead.apply(this, arguments);
459
459
  }
460
- function batchReadonly(_x3, _x4) {
461
- return _batchReadonly.apply(this, arguments);
462
- }
463
- function _batchReadonly() {
464
- _batchReadonly = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee2(req, options) {
465
- var _options$stxerApi2;
466
- var payload, ibh, url, data, text, results, rs, _iterator5, _step5, result;
467
- return _regeneratorRuntime().wrap(function _callee2$(_context2) {
468
- while (1) switch (_context2.prev = _context2.next) {
469
- case 0:
470
- if (options === void 0) {
471
- options = {};
472
- }
473
- payload = req.readonly.map(function (r) {
474
- return [serializeCV(r.contract), r.functionName].concat(r.functionArgs.map(function (arg) {
475
- return serializeCV(arg);
476
- }));
477
- });
478
- ibh = req.index_block_hash == null ? null : req.index_block_hash.startsWith('0x') ? req.index_block_hash.substring(2) : req.index_block_hash;
479
- url = ((_options$stxerApi2 = options.stxerApi) != null ? _options$stxerApi2 : DEFAULT_STXER_API) + "/sidecar/v2/batch-readonly" + (ibh == null ? '' : "?tip=" + ibh);
480
- _context2.next = 6;
481
- return fetch(url, {
482
- method: 'POST',
483
- body: JSON.stringify(payload)
484
- });
485
- case 6:
486
- data = _context2.sent;
487
- _context2.next = 9;
488
- return data.text();
489
- case 9:
490
- text = _context2.sent;
491
- if (!(!text.includes('Ok') && !text.includes('Err'))) {
492
- _context2.next = 12;
493
- break;
494
- }
495
- throw new Error("Requesting batch readonly failed: " + text + ", url: " + url + ", payload: " + JSON.stringify(payload));
496
- case 12:
497
- results = JSON.parse(text);
498
- rs = [];
499
- for (_iterator5 = _createForOfIteratorHelperLoose(results); !(_step5 = _iterator5()).done;) {
500
- result = _step5.value;
501
- if ('Ok' in result) {
502
- rs.push(deserializeCV(result.Ok));
503
- } else {
504
- rs.push(new Error(result.Err));
505
- }
506
- }
507
- return _context2.abrupt("return", rs);
508
- case 16:
509
- case "end":
510
- return _context2.stop();
511
- }
512
- }, _callee2);
513
- }));
514
- return _batchReadonly.apply(this, arguments);
515
- }
516
460
 
517
461
  function runTx(tx) {
518
462
  // type 0: run transaction
@@ -959,5 +903,318 @@ var SimulationBuilder = /*#__PURE__*/function () {
959
903
  return SimulationBuilder;
960
904
  }();
961
905
 
962
- 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 };
963
1220
  //# sourceMappingURL=stxer.esm.js.map