@clarigen/core 0.3.2 → 1.0.0-next.6

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/core.esm.js CHANGED
@@ -1,4 +1,8 @@
1
- import { contractPrincipalCV, ClarityType, isClarityAbiTuple, tupleCV, isClarityAbiList, listCV, isClarityAbiOptional, noneCV, someCV, isClarityAbiStringAscii, stringAsciiCV, isClarityAbiStringUtf8, stringUtf8CV, parseToCV as parseToCV$1, uintCV, intCV, addressToString } from '@stacks/transactions';
1
+ import { contractPrincipalCV, ClarityType, hexToCV, tupleCV, listCV, noneCV, someCV, stringAsciiCV, stringUtf8CV, uintCV, intCV, bufferCV, addressToString } from 'micro-stacks/clarity';
2
+ import { isClarityAbiTuple, isClarityAbiList, isClarityAbiOptional, isClarityAbiStringAscii, isClarityAbiStringUtf8, parseToCV as parseToCV$1, isClarityAbiBuffer, broadcastTransaction } from 'micro-stacks/transactions';
3
+ import { bytesToAscii, bytesToHex } from 'micro-stacks/common';
4
+ import { ok, err } from 'neverthrow';
5
+ import { callReadOnlyFunction } from 'micro-stacks/api';
2
6
 
3
7
  var TESTNET_BURN_ADDRESS = 'ST000000000000000000002AMW42H';
4
8
  var MAINNET_BURN_ADDRESS = 'SP000000000000000000002Q6VF78';
@@ -54,6 +58,27 @@ var CoreNodeEventType;
54
58
  CoreNodeEventType["FtBurnEvent"] = "ft_burn_event";
55
59
  })(CoreNodeEventType || (CoreNodeEventType = {}));
56
60
 
61
+ function makeContracts(contracts, options) {
62
+ if (options === void 0) {
63
+ options = {};
64
+ }
65
+
66
+ var instances = {};
67
+
68
+ for (var k in contracts) {
69
+ var contract = contracts[k];
70
+ var address = options.deployerAddress || contract.address;
71
+ var identifier = address + "." + contract.name;
72
+ var instance = contract.contract(address, contract.name);
73
+ instances[k] = {
74
+ identifier: identifier,
75
+ contract: instance
76
+ };
77
+ }
78
+
79
+ return instances;
80
+ }
81
+
57
82
  function principalToString(principal) {
58
83
  if (principal.type === ClarityType.PrincipalStandard) {
59
84
  return addressToString(principal.address);
@@ -64,8 +89,18 @@ function principalToString(principal) {
64
89
  throw new Error("Unexpected principal data: " + JSON.stringify(principal));
65
90
  }
66
91
  }
92
+ /**
93
+ * @param val - ClarityValue
94
+ * @param returnResponse - if true, this will return a "response" object from the `neverthrow`
95
+ * library. Otherwise, it returns the inner value of the response (whether ok or err)
96
+ */
97
+
98
+
99
+ function cvToValue(val, returnResponse) {
100
+ if (returnResponse === void 0) {
101
+ returnResponse = false;
102
+ }
67
103
 
68
- function cvToValue(val) {
69
104
  switch (val.type) {
70
105
  case ClarityType.BoolTrue:
71
106
  return true;
@@ -74,8 +109,6 @@ function cvToValue(val) {
74
109
  return false;
75
110
 
76
111
  case ClarityType.Int:
77
- return val.value;
78
-
79
112
  case ClarityType.UInt:
80
113
  return val.value;
81
114
 
@@ -89,9 +122,11 @@ function cvToValue(val) {
89
122
  return cvToValue(val.value);
90
123
 
91
124
  case ClarityType.ResponseErr:
125
+ if (returnResponse) return err(cvToValue(val.value));
92
126
  return cvToValue(val.value);
93
127
 
94
128
  case ClarityType.ResponseOk:
129
+ if (returnResponse) return ok(cvToValue(val.value));
95
130
  return cvToValue(val.value);
96
131
 
97
132
  case ClarityType.PrincipalStandard:
@@ -105,8 +140,13 @@ function cvToValue(val) {
105
140
 
106
141
  case ClarityType.Tuple:
107
142
  var result = {};
108
- Object.keys(val.data).forEach(function (key) {
109
- result[key] = cvToValue(val.data[key]);
143
+ var arr = Object.keys(val.data).map(function (key) {
144
+ return [key, cvToValue(val.data[key])];
145
+ });
146
+ arr.forEach(function (_ref) {
147
+ var key = _ref[0],
148
+ value = _ref[1];
149
+ result[key] = value;
110
150
  });
111
151
  return result;
112
152
 
@@ -117,6 +157,19 @@ function cvToValue(val) {
117
157
  return val.data;
118
158
  }
119
159
  }
160
+ /**
161
+ * Converts a hex encoded string to the javascript clarity value object {type: string; value: any}
162
+ * @param hex - the hex encoded string with or without `0x` prefix
163
+ * @param jsonCompat - enable to serialize bigints to strings
164
+ */
165
+
166
+ function hexToCvValue(hex, jsonCompat) {
167
+ if (jsonCompat === void 0) {
168
+ jsonCompat = false;
169
+ }
170
+
171
+ return cvToValue(hexToCV(hex), jsonCompat);
172
+ }
120
173
 
121
174
  function inputToBigInt(input) {
122
175
  var isBigInt = typeof input === 'bigint';
@@ -182,6 +235,8 @@ function parseToCV(input, type) {
182
235
  name = _input$split[1];
183
236
 
184
237
  return contractPrincipalCV(addr, name);
238
+ } else if (isClarityAbiBuffer(type)) {
239
+ return bufferCV(input);
185
240
  }
186
241
 
187
242
  return parseToCV$1(input, type);
@@ -206,14 +261,14 @@ function cvToString(val, encoding) {
206
261
 
207
262
  case ClarityType.Buffer:
208
263
  if (encoding === 'tryAscii') {
209
- var str = val.buffer.toString('ascii');
264
+ var str = bytesToAscii(val.buffer);
210
265
 
211
266
  if (/[ -~]/.test(str)) {
212
267
  return JSON.stringify(str);
213
268
  }
214
269
  }
215
270
 
216
- return "0x" + val.buffer.toString('hex');
271
+ return "0x" + bytesToHex(val.buffer);
217
272
 
218
273
  case ClarityType.OptionalNone:
219
274
  return 'none';
@@ -248,6 +303,80 @@ function cvToString(val, encoding) {
248
303
  return "u\"" + val.data + "\"";
249
304
  }
250
305
  }
306
+ function transformArgsToCV(func, args) {
307
+ return args.map(function (arg, index) {
308
+ return parseToCV(arg, func.args[index].type);
309
+ });
310
+ }
311
+ function expectOk(response) {
312
+ return response.match(function (ok) {
313
+ return ok;
314
+ }, function (err) {
315
+ throw new Error("Expected OK, received error: " + err);
316
+ });
317
+ }
318
+ function expectErr(response) {
319
+ return response.match(function (ok) {
320
+ throw new Error("Expected Err, received Ok: " + ok);
321
+ }, function (err) {
322
+ return err;
323
+ });
324
+ }
325
+
326
+ function transformArguments(func, args) {
327
+ return transformArgsToCV(func, args);
328
+ }
329
+
330
+ function getter(contract, property) {
331
+ var foundFunction = contract.abi.functions.find(function (func) {
332
+ return toCamelCase(func.name) === property;
333
+ });
334
+
335
+ if (foundFunction) {
336
+ return function () {
337
+ for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
338
+ args[_key] = arguments[_key];
339
+ }
340
+
341
+ var functionArgs = transformArguments(foundFunction, args);
342
+ return {
343
+ functionArgs: functionArgs,
344
+ contractAddress: contract.contractAddress,
345
+ contractName: contract.contractName,
346
+ "function": foundFunction,
347
+ nativeArgs: args
348
+ };
349
+ };
350
+ }
351
+
352
+ var foundMap = contract.abi.maps.find(function (map) {
353
+ return toCamelCase(map.name) === property;
354
+ });
355
+
356
+ if (foundMap) {
357
+ return function (key) {
358
+ var keyCV = parseToCV(key, foundMap.key);
359
+ var mapGet = {
360
+ contractAddress: contract.contractAddress,
361
+ contractName: contract.contractName,
362
+ map: foundMap,
363
+ nativeKey: key,
364
+ key: keyCV
365
+ };
366
+ return mapGet;
367
+ };
368
+ } // TODO: variables, tokens
369
+
370
+
371
+ throw new Error("Invalid function call: no function exists for " + String(property));
372
+ }
373
+
374
+ var proxyHandler = {
375
+ get: getter
376
+ };
377
+ var pureProxy = function pureProxy(target) {
378
+ return new Proxy(target, proxyHandler);
379
+ };
251
380
 
252
381
  function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
253
382
  try {
@@ -1051,6 +1180,137 @@ var BaseProvider = /*#__PURE__*/function () {
1051
1180
  return BaseProvider;
1052
1181
  }();
1053
1182
 
1183
+ function ro(_x, _x2) {
1184
+ return _ro.apply(this, arguments);
1185
+ }
1186
+
1187
+ function _ro() {
1188
+ _ro = _asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee(tx, options) {
1189
+ var result;
1190
+ return runtime_1.wrap(function _callee$(_context) {
1191
+ while (1) {
1192
+ switch (_context.prev = _context.next) {
1193
+ case 0:
1194
+ _context.next = 2;
1195
+ return callReadOnlyFunction({
1196
+ contractAddress: tx.contractAddress,
1197
+ contractName: tx.contractName,
1198
+ functionArgs: tx.functionArgs,
1199
+ functionName: tx["function"].name,
1200
+ network: options.network,
1201
+ tip: 'latest'
1202
+ });
1203
+
1204
+ case 2:
1205
+ result = _context.sent;
1206
+ return _context.abrupt("return", cvToValue(result, true));
1207
+
1208
+ case 4:
1209
+ case "end":
1210
+ return _context.stop();
1211
+ }
1212
+ }
1213
+ }, _callee);
1214
+ }));
1215
+ return _ro.apply(this, arguments);
1216
+ }
1217
+
1218
+ function roOk(_x3, _x4) {
1219
+ return _roOk.apply(this, arguments);
1220
+ }
1221
+
1222
+ function _roOk() {
1223
+ _roOk = _asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee2(tx, options) {
1224
+ var result;
1225
+ return runtime_1.wrap(function _callee2$(_context2) {
1226
+ while (1) {
1227
+ switch (_context2.prev = _context2.next) {
1228
+ case 0:
1229
+ _context2.next = 2;
1230
+ return ro(tx, options);
1231
+
1232
+ case 2:
1233
+ result = _context2.sent;
1234
+ return _context2.abrupt("return", expectOk(result));
1235
+
1236
+ case 4:
1237
+ case "end":
1238
+ return _context2.stop();
1239
+ }
1240
+ }
1241
+ }, _callee2);
1242
+ }));
1243
+ return _roOk.apply(this, arguments);
1244
+ }
1245
+
1246
+ function roErr(_x5, _x6) {
1247
+ return _roErr.apply(this, arguments);
1248
+ }
1249
+
1250
+ function _roErr() {
1251
+ _roErr = _asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee3(tx, options) {
1252
+ var result;
1253
+ return runtime_1.wrap(function _callee3$(_context3) {
1254
+ while (1) {
1255
+ switch (_context3.prev = _context3.next) {
1256
+ case 0:
1257
+ _context3.next = 2;
1258
+ return ro(tx, options);
1259
+
1260
+ case 2:
1261
+ result = _context3.sent;
1262
+ return _context3.abrupt("return", expectErr(result));
1263
+
1264
+ case 4:
1265
+ case "end":
1266
+ return _context3.stop();
1267
+ }
1268
+ }
1269
+ }, _callee3);
1270
+ }));
1271
+ return _roErr.apply(this, arguments);
1272
+ }
1273
+
1274
+ function broadcast(_x7, _x8) {
1275
+ return _broadcast.apply(this, arguments);
1276
+ }
1277
+
1278
+ function _broadcast() {
1279
+ _broadcast = _asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee4(transaction, options) {
1280
+ var result;
1281
+ return runtime_1.wrap(function _callee4$(_context4) {
1282
+ while (1) {
1283
+ switch (_context4.prev = _context4.next) {
1284
+ case 0:
1285
+ _context4.next = 2;
1286
+ return broadcastTransaction(transaction, options.network);
1287
+
1288
+ case 2:
1289
+ result = _context4.sent;
1290
+
1291
+ if (!('error' in result)) {
1292
+ _context4.next = 7;
1293
+ break;
1294
+ }
1295
+
1296
+ throw new Error("Error broadcasting tx: " + result.error + " - " + result.reason + " - " + result.reason_data);
1297
+
1298
+ case 7:
1299
+ return _context4.abrupt("return", {
1300
+ txId: result.txid,
1301
+ stacksTransaction: transaction
1302
+ });
1303
+
1304
+ case 8:
1305
+ case "end":
1306
+ return _context4.stop();
1307
+ }
1308
+ }
1309
+ }, _callee4);
1310
+ }));
1311
+ return _broadcast.apply(this, arguments);
1312
+ }
1313
+
1054
1314
  var makeHandler = function makeHandler(provider) {
1055
1315
  var handler = {
1056
1316
  get: function get(contract, property) {
@@ -1088,5 +1348,5 @@ var proxy = function proxy(target, provider) {
1088
1348
  return new Proxy(target, makeHandler(provider));
1089
1349
  };
1090
1350
 
1091
- export { BaseProvider, CoreNodeEventType, MAINNET_BURN_ADDRESS, TESTNET_BURN_ADDRESS, bootContractIdentifier, cvToString, cvToValue, getContractIdentifier, getContractNameFromPath, getContractPrincipalCV, parseToCV, proxy, toCamelCase };
1351
+ export { BaseProvider, CoreNodeEventType, MAINNET_BURN_ADDRESS, TESTNET_BURN_ADDRESS, bootContractIdentifier, broadcast, cvToString, cvToValue, expectErr, expectOk, getContractIdentifier, getContractNameFromPath, getContractPrincipalCV, hexToCvValue, makeContracts, parseToCV, proxy, pureProxy, ro, roErr, roOk, toCamelCase };
1092
1352
  //# sourceMappingURL=core.esm.js.map