@clarigen/core 0.2.3 → 1.0.0-next.3

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, 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';
@@ -38,6 +42,22 @@ function bootContractIdentifier(name, mainnet) {
38
42
  return addr + "." + name;
39
43
  }
40
44
 
45
+ var CoreNodeEventType;
46
+
47
+ (function (CoreNodeEventType) {
48
+ CoreNodeEventType["ContractEvent"] = "contract_event";
49
+ CoreNodeEventType["StxTransferEvent"] = "stx_transfer_event";
50
+ CoreNodeEventType["StxMintEvent"] = "stx_mint_event";
51
+ CoreNodeEventType["StxBurnEvent"] = "stx_burn_event";
52
+ CoreNodeEventType["StxLockEvent"] = "stx_lock_event";
53
+ CoreNodeEventType["NftTransferEvent"] = "nft_transfer_event";
54
+ CoreNodeEventType["NftMintEvent"] = "nft_mint_event";
55
+ CoreNodeEventType["NftBurnEvent"] = "nft_burn_event";
56
+ CoreNodeEventType["FtTransferEvent"] = "ft_transfer_event";
57
+ CoreNodeEventType["FtMintEvent"] = "ft_mint_event";
58
+ CoreNodeEventType["FtBurnEvent"] = "ft_burn_event";
59
+ })(CoreNodeEventType || (CoreNodeEventType = {}));
60
+
41
61
  function principalToString(principal) {
42
62
  if (principal.type === ClarityType.PrincipalStandard) {
43
63
  return addressToString(principal.address);
@@ -48,8 +68,18 @@ function principalToString(principal) {
48
68
  throw new Error("Unexpected principal data: " + JSON.stringify(principal));
49
69
  }
50
70
  }
71
+ /**
72
+ * @param val - ClarityValue
73
+ * @param returnResponse - if true, this will return a "response" object from the `neverthrow`
74
+ * library. Otherwise, it returns the inner value of the response (whether ok or err)
75
+ */
76
+
77
+
78
+ function cvToValue(val, returnResponse) {
79
+ if (returnResponse === void 0) {
80
+ returnResponse = false;
81
+ }
51
82
 
52
- function cvToValue(val) {
53
83
  switch (val.type) {
54
84
  case ClarityType.BoolTrue:
55
85
  return true;
@@ -58,10 +88,8 @@ function cvToValue(val) {
58
88
  return false;
59
89
 
60
90
  case ClarityType.Int:
61
- return BigInt("0x" + val.value.toString('hex'));
62
-
63
91
  case ClarityType.UInt:
64
- return BigInt("0x" + val.value.toString('hex'));
92
+ return val.value;
65
93
 
66
94
  case ClarityType.Buffer:
67
95
  return val.buffer;
@@ -73,9 +101,11 @@ function cvToValue(val) {
73
101
  return cvToValue(val.value);
74
102
 
75
103
  case ClarityType.ResponseErr:
104
+ if (returnResponse) return err(cvToValue(val.value));
76
105
  return cvToValue(val.value);
77
106
 
78
107
  case ClarityType.ResponseOk:
108
+ if (returnResponse) return ok(cvToValue(val.value));
79
109
  return cvToValue(val.value);
80
110
 
81
111
  case ClarityType.PrincipalStandard:
@@ -89,8 +119,13 @@ function cvToValue(val) {
89
119
 
90
120
  case ClarityType.Tuple:
91
121
  var result = {};
92
- Object.keys(val.data).forEach(function (key) {
93
- result[key] = cvToValue(val.data[key]);
122
+ var arr = Object.keys(val.data).map(function (key) {
123
+ return [key, cvToValue(val.data[key])];
124
+ });
125
+ arr.forEach(function (_ref) {
126
+ var key = _ref[0],
127
+ value = _ref[1];
128
+ result[key] = value;
94
129
  });
95
130
  return result;
96
131
 
@@ -101,6 +136,19 @@ function cvToValue(val) {
101
136
  return val.data;
102
137
  }
103
138
  }
139
+ /**
140
+ * Converts a hex encoded string to the javascript clarity value object {type: string; value: any}
141
+ * @param hex - the hex encoded string with or without `0x` prefix
142
+ * @param jsonCompat - enable to serialize bigints to strings
143
+ */
144
+
145
+ function hexToCvValue(hex, jsonCompat) {
146
+ if (jsonCompat === void 0) {
147
+ jsonCompat = false;
148
+ }
149
+
150
+ return cvToValue(hexToCV(hex), jsonCompat);
151
+ }
104
152
 
105
153
  function inputToBigInt(input) {
106
154
  var isBigInt = typeof input === 'bigint';
@@ -157,12 +205,21 @@ function parseToCV(input, type) {
157
205
  } else if (type === 'int128') {
158
206
  var _bigi = inputToBigInt(input);
159
207
 
160
- return uintCV(_bigi.toString());
208
+ return intCV(_bigi.toString());
209
+ } else if (type === 'trait_reference') {
210
+ if (typeof input !== 'string') throw new Error('Invalid input for trait_reference');
211
+
212
+ var _input$split = input.split('.'),
213
+ addr = _input$split[0],
214
+ name = _input$split[1];
215
+
216
+ return contractPrincipalCV(addr, name);
217
+ } else if (isClarityAbiBuffer(type)) {
218
+ return bufferCV(input);
161
219
  }
162
220
 
163
221
  return parseToCV$1(input, type);
164
222
  }
165
- var CLARITY_INT_SIZE = 128;
166
223
  function cvToString(val, encoding) {
167
224
  if (encoding === void 0) {
168
225
  encoding = 'hex';
@@ -176,21 +233,21 @@ function cvToString(val, encoding) {
176
233
  return 'false';
177
234
 
178
235
  case ClarityType.Int:
179
- return val.value.fromTwos(CLARITY_INT_SIZE).toString();
236
+ return val.value.toString();
180
237
 
181
238
  case ClarityType.UInt:
182
239
  return "u" + val.value.toString();
183
240
 
184
241
  case ClarityType.Buffer:
185
242
  if (encoding === 'tryAscii') {
186
- var str = val.buffer.toString('ascii');
243
+ var str = bytesToAscii(val.buffer);
187
244
 
188
245
  if (/[ -~]/.test(str)) {
189
246
  return JSON.stringify(str);
190
247
  }
191
248
  }
192
249
 
193
- return "0x" + val.buffer.toString('hex');
250
+ return "0x" + bytesToHex(val.buffer);
194
251
 
195
252
  case ClarityType.OptionalNone:
196
253
  return 'none';
@@ -225,6 +282,80 @@ function cvToString(val, encoding) {
225
282
  return "u\"" + val.data + "\"";
226
283
  }
227
284
  }
285
+ function transformArgsToCV(func, args) {
286
+ return args.map(function (arg, index) {
287
+ return parseToCV(arg, func.args[index].type);
288
+ });
289
+ }
290
+ function expectOk(response) {
291
+ return response.match(function (ok) {
292
+ return ok;
293
+ }, function (err) {
294
+ throw new Error("Expected OK, received error: " + err);
295
+ });
296
+ }
297
+ function expectErr(response) {
298
+ return response.match(function (ok) {
299
+ throw new Error("Expected Err, received Ok: " + ok);
300
+ }, function (err) {
301
+ return err;
302
+ });
303
+ }
304
+
305
+ function transformArguments(func, args) {
306
+ return transformArgsToCV(func, args);
307
+ }
308
+
309
+ function getter(contract, property) {
310
+ var foundFunction = contract.abi.functions.find(function (func) {
311
+ return toCamelCase(func.name) === property;
312
+ });
313
+
314
+ if (foundFunction) {
315
+ return function () {
316
+ for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
317
+ args[_key] = arguments[_key];
318
+ }
319
+
320
+ var functionArgs = transformArguments(foundFunction, args);
321
+ return {
322
+ functionArgs: functionArgs,
323
+ contractAddress: contract.contractAddress,
324
+ contractName: contract.contractName,
325
+ "function": foundFunction,
326
+ nativeArgs: args
327
+ };
328
+ };
329
+ }
330
+
331
+ var foundMap = contract.abi.maps.find(function (map) {
332
+ return toCamelCase(map.name) === property;
333
+ });
334
+
335
+ if (foundMap) {
336
+ return function (key) {
337
+ var keyCV = parseToCV(key, foundMap.key);
338
+ var mapGet = {
339
+ contractAddress: contract.contractAddress,
340
+ contractName: contract.contractName,
341
+ map: foundMap,
342
+ nativeKey: key,
343
+ key: keyCV
344
+ };
345
+ return mapGet;
346
+ };
347
+ } // TODO: variables, tokens
348
+
349
+
350
+ throw new Error("Invalid function call: no function exists for " + String(property));
351
+ }
352
+
353
+ var proxyHandler = {
354
+ get: getter
355
+ };
356
+ var pureProxy = function pureProxy(target) {
357
+ return new Proxy(target, proxyHandler);
358
+ };
228
359
 
229
360
  function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
230
361
  try {
@@ -1028,6 +1159,136 @@ var BaseProvider = /*#__PURE__*/function () {
1028
1159
  return BaseProvider;
1029
1160
  }();
1030
1161
 
1162
+ function ro(_x, _x2) {
1163
+ return _ro.apply(this, arguments);
1164
+ }
1165
+
1166
+ function _ro() {
1167
+ _ro = _asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee(tx, options) {
1168
+ var result;
1169
+ return runtime_1.wrap(function _callee$(_context) {
1170
+ while (1) {
1171
+ switch (_context.prev = _context.next) {
1172
+ case 0:
1173
+ _context.next = 2;
1174
+ return callReadOnlyFunction({
1175
+ contractAddress: tx.contractAddress,
1176
+ contractName: tx.contractName,
1177
+ functionArgs: tx.functionArgs,
1178
+ functionName: tx["function"].name,
1179
+ network: options.network
1180
+ });
1181
+
1182
+ case 2:
1183
+ result = _context.sent;
1184
+ return _context.abrupt("return", cvToValue(result, true));
1185
+
1186
+ case 4:
1187
+ case "end":
1188
+ return _context.stop();
1189
+ }
1190
+ }
1191
+ }, _callee);
1192
+ }));
1193
+ return _ro.apply(this, arguments);
1194
+ }
1195
+
1196
+ function roOk(_x3, _x4) {
1197
+ return _roOk.apply(this, arguments);
1198
+ }
1199
+
1200
+ function _roOk() {
1201
+ _roOk = _asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee2(tx, options) {
1202
+ var result;
1203
+ return runtime_1.wrap(function _callee2$(_context2) {
1204
+ while (1) {
1205
+ switch (_context2.prev = _context2.next) {
1206
+ case 0:
1207
+ _context2.next = 2;
1208
+ return ro(tx, options);
1209
+
1210
+ case 2:
1211
+ result = _context2.sent;
1212
+ return _context2.abrupt("return", expectOk(result));
1213
+
1214
+ case 4:
1215
+ case "end":
1216
+ return _context2.stop();
1217
+ }
1218
+ }
1219
+ }, _callee2);
1220
+ }));
1221
+ return _roOk.apply(this, arguments);
1222
+ }
1223
+
1224
+ function roErr(_x5, _x6) {
1225
+ return _roErr.apply(this, arguments);
1226
+ }
1227
+
1228
+ function _roErr() {
1229
+ _roErr = _asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee3(tx, options) {
1230
+ var result;
1231
+ return runtime_1.wrap(function _callee3$(_context3) {
1232
+ while (1) {
1233
+ switch (_context3.prev = _context3.next) {
1234
+ case 0:
1235
+ _context3.next = 2;
1236
+ return ro(tx, options);
1237
+
1238
+ case 2:
1239
+ result = _context3.sent;
1240
+ return _context3.abrupt("return", expectErr(result));
1241
+
1242
+ case 4:
1243
+ case "end":
1244
+ return _context3.stop();
1245
+ }
1246
+ }
1247
+ }, _callee3);
1248
+ }));
1249
+ return _roErr.apply(this, arguments);
1250
+ }
1251
+
1252
+ function broadcast(_x7, _x8) {
1253
+ return _broadcast.apply(this, arguments);
1254
+ }
1255
+
1256
+ function _broadcast() {
1257
+ _broadcast = _asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee4(transaction, options) {
1258
+ var result;
1259
+ return runtime_1.wrap(function _callee4$(_context4) {
1260
+ while (1) {
1261
+ switch (_context4.prev = _context4.next) {
1262
+ case 0:
1263
+ _context4.next = 2;
1264
+ return broadcastTransaction(transaction, options.network);
1265
+
1266
+ case 2:
1267
+ result = _context4.sent;
1268
+
1269
+ if (!('error' in result)) {
1270
+ _context4.next = 7;
1271
+ break;
1272
+ }
1273
+
1274
+ throw new Error("Error broadcasting tx: " + result.error + " - " + result.reason + " - " + result.reason_data);
1275
+
1276
+ case 7:
1277
+ return _context4.abrupt("return", {
1278
+ txId: result.txid,
1279
+ stacksTransaction: transaction
1280
+ });
1281
+
1282
+ case 8:
1283
+ case "end":
1284
+ return _context4.stop();
1285
+ }
1286
+ }
1287
+ }, _callee4);
1288
+ }));
1289
+ return _broadcast.apply(this, arguments);
1290
+ }
1291
+
1031
1292
  var makeHandler = function makeHandler(provider) {
1032
1293
  var handler = {
1033
1294
  get: function get(contract, property) {
@@ -1065,5 +1326,5 @@ var proxy = function proxy(target, provider) {
1065
1326
  return new Proxy(target, makeHandler(provider));
1066
1327
  };
1067
1328
 
1068
- export { BaseProvider, MAINNET_BURN_ADDRESS, TESTNET_BURN_ADDRESS, bootContractIdentifier, cvToString, cvToValue, getContractIdentifier, getContractNameFromPath, getContractPrincipalCV, parseToCV, proxy, toCamelCase };
1329
+ export { BaseProvider, CoreNodeEventType, MAINNET_BURN_ADDRESS, TESTNET_BURN_ADDRESS, bootContractIdentifier, broadcast, cvToString, cvToValue, expectErr, expectOk, getContractIdentifier, getContractNameFromPath, getContractPrincipalCV, hexToCvValue, parseToCV, proxy, pureProxy, ro, roErr, roOk, toCamelCase };
1069
1330
  //# sourceMappingURL=core.esm.js.map