stxer 0.3.3 → 0.4.1

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,328 @@ 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.read = function read(request) {
921
+ var _this = this;
922
+ return new Promise(function (resolve, reject) {
923
+ _this.enqueue({
924
+ request: request,
925
+ resolve: resolve,
926
+ reject: reject
927
+ });
928
+ });
929
+ };
930
+ _proto.enqueue = function enqueue(request) {
931
+ var _this$queues$get,
932
+ _this2 = this;
933
+ var queueKey = this.getQueueKey(request.tip);
934
+ var queue = (_this$queues$get = this.queues.get(queueKey)) != null ? _this$queues$get : [];
935
+ if (!this.queues.has(queueKey)) {
936
+ this.queues.set(queueKey, queue);
937
+ }
938
+ queue.push(request);
939
+ if (!this.timeoutIds.has(queueKey)) {
940
+ var timeoutId = setTimeout(function () {
941
+ return _this2.processBatch(queueKey);
942
+ }, this.batchDelayMs);
943
+ this.timeoutIds.set(queueKey, timeoutId);
944
+ }
945
+ };
946
+ _proto.processBatch = /*#__PURE__*/function () {
947
+ var _processBatch = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee(queueKey) {
948
+ var _this$queues$get2;
949
+ 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;
950
+ return _regeneratorRuntime().wrap(function _callee$(_context) {
951
+ while (1) switch (_context.prev = _context.next) {
952
+ case 0:
953
+ currentQueue = (_this$queues$get2 = this.queues.get(queueKey)) != null ? _this$queues$get2 : [];
954
+ this.queues["delete"](queueKey);
955
+ timeoutId = this.timeoutIds.get(queueKey);
956
+ if (timeoutId) {
957
+ clearTimeout(timeoutId);
958
+ this.timeoutIds["delete"](queueKey);
959
+ }
960
+ if (!(currentQueue.length === 0)) {
961
+ _context.next = 6;
962
+ break;
963
+ }
964
+ return _context.abrupt("return");
965
+ case 6:
966
+ _context.prev = 6;
967
+ readonlyRequests = currentQueue.filter(function (q) {
968
+ return q.request.mode === 'readonly';
969
+ });
970
+ mapRequests = currentQueue.filter(function (q) {
971
+ return q.request.mode === 'mapEntry';
972
+ });
973
+ variableRequests = currentQueue.filter(function (q) {
974
+ return q.request.mode === 'variable';
975
+ });
976
+ tip = queueKey === '_undefined' ? undefined : queueKey;
977
+ batchRequest = {
978
+ readonly: readonlyRequests.map(function (_ref) {
979
+ var request = _ref.request;
980
+ return {
981
+ contract: contractPrincipalCV(request.contractAddress, request.contractName),
982
+ functionName: request.functionName,
983
+ functionArgs: request.functionArgs
984
+ };
985
+ }),
986
+ maps: mapRequests.map(function (_ref2) {
987
+ var request = _ref2.request;
988
+ return {
989
+ contract: contractPrincipalCV(request.contractAddress, request.contractName),
990
+ mapName: request.mapName,
991
+ mapKey: request.mapKey
992
+ };
993
+ }),
994
+ variables: variableRequests.map(function (_ref3) {
995
+ var request = _ref3.request;
996
+ return {
997
+ contract: contractPrincipalCV(request.contractAddress, request.contractName),
998
+ variableName: request.variableName
999
+ };
1000
+ }),
1001
+ index_block_hash: tip
1002
+ };
1003
+ _context.next = 14;
1004
+ return batchRead(batchRequest, {
1005
+ stxerApi: this.stxerAPIEndpoint
1006
+ });
1007
+ case 14:
1008
+ results = _context.sent;
1009
+ // Handle readonly results
1010
+ for (_iterator = _createForOfIteratorHelperLoose(results.readonly.entries()); !(_step = _iterator()).done;) {
1011
+ _step$value = _step.value, index = _step$value[0], result = _step$value[1];
1012
+ if (result instanceof Error) {
1013
+ readonlyRequests[index].reject(result);
1014
+ } else {
1015
+ readonlyRequests[index].resolve(result);
1016
+ }
1017
+ }
1018
+ // Handle variable results
1019
+ for (_iterator2 = _createForOfIteratorHelperLoose(results.vars.entries()); !(_step2 = _iterator2()).done;) {
1020
+ _step2$value = _step2.value, _index = _step2$value[0], _result = _step2$value[1];
1021
+ if (_result instanceof Error) {
1022
+ variableRequests[_index].reject(_result);
1023
+ } else {
1024
+ variableRequests[_index].resolve(_result);
1025
+ }
1026
+ }
1027
+ // Handle map results
1028
+ for (_iterator3 = _createForOfIteratorHelperLoose(results.maps.entries()); !(_step3 = _iterator3()).done;) {
1029
+ _step3$value = _step3.value, _index2 = _step3$value[0], _result2 = _step3$value[1];
1030
+ if (_result2 instanceof Error) {
1031
+ mapRequests[_index2].reject(_result2);
1032
+ } else {
1033
+ mapRequests[_index2].resolve(_result2);
1034
+ }
1035
+ }
1036
+ _context.next = 23;
1037
+ break;
1038
+ case 20:
1039
+ _context.prev = 20;
1040
+ _context.t0 = _context["catch"](6);
1041
+ for (_iterator4 = _createForOfIteratorHelperLoose(currentQueue); !(_step4 = _iterator4()).done;) {
1042
+ item = _step4.value;
1043
+ item.reject(_context.t0);
1044
+ }
1045
+ case 23:
1046
+ case "end":
1047
+ return _context.stop();
1048
+ }
1049
+ }, _callee, this, [[6, 20]]);
1050
+ }));
1051
+ function processBatch(_x) {
1052
+ return _processBatch.apply(this, arguments);
1053
+ }
1054
+ return processBatch;
1055
+ }();
1056
+ return BatchProcessor;
1057
+ }();
1058
+
1059
+ // Shared processor instance with default settings
1060
+ var defaultProcessor = /*#__PURE__*/new BatchProcessor({
1061
+ batchDelayMs: 100
1062
+ });
1063
+ function callReadonly(_x) {
1064
+ return _callReadonly.apply(this, arguments);
1065
+ }
1066
+ function _callReadonly() {
1067
+ _callReadonly = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee(params) {
1068
+ var _params$batchProcesso;
1069
+ var processor, _params$contract$spli, deployer, contractName, fn, functionDef, argsKV, args, _iterator, _step, argDef;
1070
+ return _regeneratorRuntime().wrap(function _callee$(_context) {
1071
+ while (1) switch (_context.prev = _context.next) {
1072
+ case 0:
1073
+ processor = (_params$batchProcesso = params.batchProcessor) != null ? _params$batchProcesso : defaultProcessor;
1074
+ _params$contract$spli = params.contract.split('.', 2), deployer = _params$contract$spli[0], contractName = _params$contract$spli[1];
1075
+ fn = String(params.functionName);
1076
+ functionDef = params.abi.find(function (def) {
1077
+ return def.name === params.functionName;
1078
+ });
1079
+ if (functionDef) {
1080
+ _context.next = 6;
1081
+ break;
1082
+ }
1083
+ throw new Error("failed to find function definition for " + params.functionName);
1084
+ case 6:
1085
+ argsKV = params.args;
1086
+ args = [];
1087
+ for (_iterator = _createForOfIteratorHelperLoose(functionDef.args); !(_step = _iterator()).done;) {
1088
+ argDef = _step.value;
1089
+ args.push(encodeAbi(argDef.type, argsKV[argDef.name]));
1090
+ }
1091
+ return _context.abrupt("return", new Promise(function (_resolve, reject) {
1092
+ processor.enqueue({
1093
+ request: {
1094
+ mode: 'readonly',
1095
+ contractAddress: deployer,
1096
+ contractName: contractName,
1097
+ functionName: fn,
1098
+ functionArgs: args
1099
+ },
1100
+ tip: params.indexBlockHash,
1101
+ resolve: function resolve(result) {
1102
+ try {
1103
+ var decoded = decodeAbi(functionDef.outputs.type, result);
1104
+ _resolve(decoded);
1105
+ } catch (error) {
1106
+ reject(error);
1107
+ }
1108
+ },
1109
+ reject: reject
1110
+ });
1111
+ }));
1112
+ case 10:
1113
+ case "end":
1114
+ return _context.stop();
1115
+ }
1116
+ }, _callee);
1117
+ }));
1118
+ return _callReadonly.apply(this, arguments);
1119
+ }
1120
+ function readMap(_x2) {
1121
+ return _readMap.apply(this, arguments);
1122
+ }
1123
+ function _readMap() {
1124
+ _readMap = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee2(params) {
1125
+ var _params$batchProcesso2;
1126
+ var processor, _params$contract$spli2, deployer, contractName, mapDef, key;
1127
+ return _regeneratorRuntime().wrap(function _callee2$(_context2) {
1128
+ while (1) switch (_context2.prev = _context2.next) {
1129
+ case 0:
1130
+ processor = (_params$batchProcesso2 = params.batchProcessor) != null ? _params$batchProcesso2 : defaultProcessor;
1131
+ _params$contract$spli2 = params.contract.split('.', 2), deployer = _params$contract$spli2[0], contractName = _params$contract$spli2[1];
1132
+ mapDef = params.abi.find(function (m) {
1133
+ return m.name === params.mapName;
1134
+ });
1135
+ if (mapDef) {
1136
+ _context2.next = 5;
1137
+ break;
1138
+ }
1139
+ throw new Error("failed to find map definition for " + params.mapName);
1140
+ case 5:
1141
+ key = encodeAbi(mapDef.key, params.key);
1142
+ return _context2.abrupt("return", new Promise(function (_resolve2, reject) {
1143
+ processor.enqueue({
1144
+ request: {
1145
+ mode: 'mapEntry',
1146
+ contractAddress: deployer,
1147
+ contractName: contractName,
1148
+ mapName: params.mapName,
1149
+ mapKey: key
1150
+ },
1151
+ tip: params.indexBlockHash,
1152
+ resolve: function resolve(result) {
1153
+ try {
1154
+ if (result.type === ClarityType.OptionalNone) {
1155
+ _resolve2(null);
1156
+ return;
1157
+ }
1158
+ if (result.type !== ClarityType.OptionalSome) {
1159
+ throw new Error("unexpected map value: " + result);
1160
+ }
1161
+ var someCV = result;
1162
+ var decoded = decodeAbi(mapDef.value, someCV.value);
1163
+ _resolve2(decoded);
1164
+ } catch (error) {
1165
+ reject(error);
1166
+ }
1167
+ },
1168
+ reject: reject
1169
+ });
1170
+ }));
1171
+ case 7:
1172
+ case "end":
1173
+ return _context2.stop();
1174
+ }
1175
+ }, _callee2);
1176
+ }));
1177
+ return _readMap.apply(this, arguments);
1178
+ }
1179
+ function readVariable(_x3) {
1180
+ return _readVariable.apply(this, arguments);
1181
+ }
1182
+ function _readVariable() {
1183
+ _readVariable = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee3(params) {
1184
+ var _params$batchProcesso3;
1185
+ var processor, _params$contract$spli3, deployer, contractName, varDef;
1186
+ return _regeneratorRuntime().wrap(function _callee3$(_context3) {
1187
+ while (1) switch (_context3.prev = _context3.next) {
1188
+ case 0:
1189
+ processor = (_params$batchProcesso3 = params.batchProcessor) != null ? _params$batchProcesso3 : defaultProcessor;
1190
+ _params$contract$spli3 = params.contract.split('.', 2), deployer = _params$contract$spli3[0], contractName = _params$contract$spli3[1];
1191
+ varDef = params.abi.find(function (def) {
1192
+ return def.name === params.variableName;
1193
+ });
1194
+ if (varDef) {
1195
+ _context3.next = 5;
1196
+ break;
1197
+ }
1198
+ throw new Error("failed to find variable definition for " + params.variableName);
1199
+ case 5:
1200
+ return _context3.abrupt("return", new Promise(function (_resolve3, reject) {
1201
+ processor.enqueue({
1202
+ request: {
1203
+ mode: 'variable',
1204
+ contractAddress: deployer,
1205
+ contractName: contractName,
1206
+ variableName: params.variableName
1207
+ },
1208
+ tip: params.indexBlockHash,
1209
+ resolve: function resolve(result) {
1210
+ try {
1211
+ var decoded = decodeAbi(varDef.type, result);
1212
+ _resolve3(decoded);
1213
+ } catch (error) {
1214
+ reject(error);
1215
+ }
1216
+ },
1217
+ reject: reject
1218
+ });
1219
+ }));
1220
+ case 6:
1221
+ case "end":
1222
+ return _context3.stop();
1223
+ }
1224
+ }, _callee3);
1225
+ }));
1226
+ return _readVariable.apply(this, arguments);
1227
+ }
1228
+
1229
+ export { SimulationBuilder, batchRead, callReadonly, readMap, readVariable, runEval, runSimulation };
963
1230
  //# sourceMappingURL=stxer.esm.js.map