@clarigen/core 0.3.1 → 1.0.0-next.4

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';
@@ -174,6 +227,16 @@ function parseToCV(input, type) {
174
227
  var _bigi = inputToBigInt(input);
175
228
 
176
229
  return intCV(_bigi.toString());
230
+ } else if (type === 'trait_reference') {
231
+ if (typeof input !== 'string') throw new Error('Invalid input for trait_reference');
232
+
233
+ var _input$split = input.split('.'),
234
+ addr = _input$split[0],
235
+ name = _input$split[1];
236
+
237
+ return contractPrincipalCV(addr, name);
238
+ } else if (isClarityAbiBuffer(type)) {
239
+ return bufferCV(input);
177
240
  }
178
241
 
179
242
  return parseToCV$1(input, type);
@@ -198,14 +261,14 @@ function cvToString(val, encoding) {
198
261
 
199
262
  case ClarityType.Buffer:
200
263
  if (encoding === 'tryAscii') {
201
- var str = val.buffer.toString('ascii');
264
+ var str = bytesToAscii(val.buffer);
202
265
 
203
266
  if (/[ -~]/.test(str)) {
204
267
  return JSON.stringify(str);
205
268
  }
206
269
  }
207
270
 
208
- return "0x" + val.buffer.toString('hex');
271
+ return "0x" + bytesToHex(val.buffer);
209
272
 
210
273
  case ClarityType.OptionalNone:
211
274
  return 'none';
@@ -240,6 +303,80 @@ function cvToString(val, encoding) {
240
303
  return "u\"" + val.data + "\"";
241
304
  }
242
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
+ };
243
380
 
244
381
  function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
245
382
  try {
@@ -1043,6 +1180,136 @@ var BaseProvider = /*#__PURE__*/function () {
1043
1180
  return BaseProvider;
1044
1181
  }();
1045
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
+ });
1202
+
1203
+ case 2:
1204
+ result = _context.sent;
1205
+ return _context.abrupt("return", cvToValue(result, true));
1206
+
1207
+ case 4:
1208
+ case "end":
1209
+ return _context.stop();
1210
+ }
1211
+ }
1212
+ }, _callee);
1213
+ }));
1214
+ return _ro.apply(this, arguments);
1215
+ }
1216
+
1217
+ function roOk(_x3, _x4) {
1218
+ return _roOk.apply(this, arguments);
1219
+ }
1220
+
1221
+ function _roOk() {
1222
+ _roOk = _asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee2(tx, options) {
1223
+ var result;
1224
+ return runtime_1.wrap(function _callee2$(_context2) {
1225
+ while (1) {
1226
+ switch (_context2.prev = _context2.next) {
1227
+ case 0:
1228
+ _context2.next = 2;
1229
+ return ro(tx, options);
1230
+
1231
+ case 2:
1232
+ result = _context2.sent;
1233
+ return _context2.abrupt("return", expectOk(result));
1234
+
1235
+ case 4:
1236
+ case "end":
1237
+ return _context2.stop();
1238
+ }
1239
+ }
1240
+ }, _callee2);
1241
+ }));
1242
+ return _roOk.apply(this, arguments);
1243
+ }
1244
+
1245
+ function roErr(_x5, _x6) {
1246
+ return _roErr.apply(this, arguments);
1247
+ }
1248
+
1249
+ function _roErr() {
1250
+ _roErr = _asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee3(tx, options) {
1251
+ var result;
1252
+ return runtime_1.wrap(function _callee3$(_context3) {
1253
+ while (1) {
1254
+ switch (_context3.prev = _context3.next) {
1255
+ case 0:
1256
+ _context3.next = 2;
1257
+ return ro(tx, options);
1258
+
1259
+ case 2:
1260
+ result = _context3.sent;
1261
+ return _context3.abrupt("return", expectErr(result));
1262
+
1263
+ case 4:
1264
+ case "end":
1265
+ return _context3.stop();
1266
+ }
1267
+ }
1268
+ }, _callee3);
1269
+ }));
1270
+ return _roErr.apply(this, arguments);
1271
+ }
1272
+
1273
+ function broadcast(_x7, _x8) {
1274
+ return _broadcast.apply(this, arguments);
1275
+ }
1276
+
1277
+ function _broadcast() {
1278
+ _broadcast = _asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee4(transaction, options) {
1279
+ var result;
1280
+ return runtime_1.wrap(function _callee4$(_context4) {
1281
+ while (1) {
1282
+ switch (_context4.prev = _context4.next) {
1283
+ case 0:
1284
+ _context4.next = 2;
1285
+ return broadcastTransaction(transaction, options.network);
1286
+
1287
+ case 2:
1288
+ result = _context4.sent;
1289
+
1290
+ if (!('error' in result)) {
1291
+ _context4.next = 7;
1292
+ break;
1293
+ }
1294
+
1295
+ throw new Error("Error broadcasting tx: " + result.error + " - " + result.reason + " - " + result.reason_data);
1296
+
1297
+ case 7:
1298
+ return _context4.abrupt("return", {
1299
+ txId: result.txid,
1300
+ stacksTransaction: transaction
1301
+ });
1302
+
1303
+ case 8:
1304
+ case "end":
1305
+ return _context4.stop();
1306
+ }
1307
+ }
1308
+ }, _callee4);
1309
+ }));
1310
+ return _broadcast.apply(this, arguments);
1311
+ }
1312
+
1046
1313
  var makeHandler = function makeHandler(provider) {
1047
1314
  var handler = {
1048
1315
  get: function get(contract, property) {
@@ -1080,5 +1347,5 @@ var proxy = function proxy(target, provider) {
1080
1347
  return new Proxy(target, makeHandler(provider));
1081
1348
  };
1082
1349
 
1083
- export { BaseProvider, CoreNodeEventType, MAINNET_BURN_ADDRESS, TESTNET_BURN_ADDRESS, bootContractIdentifier, cvToString, cvToValue, getContractIdentifier, getContractNameFromPath, getContractPrincipalCV, parseToCV, proxy, toCamelCase };
1350
+ 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 };
1084
1351
  //# sourceMappingURL=core.esm.js.map