@clarigen/test 1.0.4 → 1.0.16-alpha.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/index.d.ts +86 -88
- package/dist/index.js +2 -443
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +2 -443
- package/dist/index.mjs.map +1 -0
- package/package.json +17 -15
- package/README.md +0 -178
- package/contracts/test-utils.clar +0 -11
package/dist/index.mjs
CHANGED
|
@@ -1,443 +1,2 @@
|
|
|
1
|
-
var
|
|
2
|
-
|
|
3
|
-
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
4
|
-
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
7
|
-
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
8
|
-
var __spreadValues = (a, b) => {
|
|
9
|
-
for (var prop in b || (b = {}))
|
|
10
|
-
if (__hasOwnProp.call(b, prop))
|
|
11
|
-
__defNormalProp(a, prop, b[prop]);
|
|
12
|
-
if (__getOwnPropSymbols)
|
|
13
|
-
for (var prop of __getOwnPropSymbols(b)) {
|
|
14
|
-
if (__propIsEnum.call(b, prop))
|
|
15
|
-
__defNormalProp(a, prop, b[prop]);
|
|
16
|
-
}
|
|
17
|
-
return a;
|
|
18
|
-
};
|
|
19
|
-
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
20
|
-
|
|
21
|
-
// src/index.ts
|
|
22
|
-
import {
|
|
23
|
-
deployContract as deployContract2,
|
|
24
|
-
createClarityBin
|
|
25
|
-
} from "@clarigen/native-bin";
|
|
26
|
-
import {
|
|
27
|
-
getContractIdentifier,
|
|
28
|
-
expectOk,
|
|
29
|
-
expectErr
|
|
30
|
-
} from "@clarigen/core";
|
|
31
|
-
|
|
32
|
-
// src/utils/index.ts
|
|
33
|
-
import { publicKeyToStxAddress, StacksNetworkVersion } from "micro-stacks/crypto";
|
|
34
|
-
import { getPublicKeyFromStacksPrivateKey, makeRandomPrivKey } from "micro-stacks/transactions";
|
|
35
|
-
|
|
36
|
-
// src/utils/util-contract.ts
|
|
37
|
-
import { hexToCV } from "micro-stacks/clarity";
|
|
38
|
-
import { join } from "path";
|
|
39
|
-
import { cvToValue } from "@clarigen/core";
|
|
40
|
-
import { deployContract, evalJson, executeJson } from "@clarigen/native-bin";
|
|
41
|
-
import { intToBigInt } from "micro-stacks/common";
|
|
42
|
-
var UTIL_CONTRACT_ID = "ST000000000000000000002AMW42H.clarigen-test-utils";
|
|
43
|
-
async function deployUtilContract(clarityBin) {
|
|
44
|
-
let contractFilePath = join(__dirname, "..", "..", "contracts", "test-utils.clar");
|
|
45
|
-
if (__dirname.includes("dist")) {
|
|
46
|
-
contractFilePath = join(__dirname, "..", "contracts", "test-utils.clar");
|
|
47
|
-
}
|
|
48
|
-
await deployContract({
|
|
49
|
-
contractIdentifier: UTIL_CONTRACT_ID,
|
|
50
|
-
provider: clarityBin,
|
|
51
|
-
contractFilePath
|
|
52
|
-
});
|
|
53
|
-
}
|
|
54
|
-
function getBin(provider) {
|
|
55
|
-
return "clarityBin" in provider ? provider.clarityBin : provider;
|
|
56
|
-
}
|
|
57
|
-
async function getBlockHeight(provider) {
|
|
58
|
-
const bin = getBin(provider);
|
|
59
|
-
const { output_serialized } = await evalJson({
|
|
60
|
-
contractAddress: UTIL_CONTRACT_ID,
|
|
61
|
-
functionName: "get-block-height",
|
|
62
|
-
args: [],
|
|
63
|
-
provider: bin
|
|
64
|
-
});
|
|
65
|
-
const outputCV = hexToCV(output_serialized);
|
|
66
|
-
const blockHeight = cvToValue(outputCV);
|
|
67
|
-
return blockHeight;
|
|
68
|
-
}
|
|
69
|
-
async function mineBlock(provider) {
|
|
70
|
-
const bin = getBin(provider);
|
|
71
|
-
await executeJson({
|
|
72
|
-
contractAddress: UTIL_CONTRACT_ID,
|
|
73
|
-
functionName: "mine-block",
|
|
74
|
-
args: [],
|
|
75
|
-
provider: bin,
|
|
76
|
-
senderAddress: "ST000000000000000000002AMW42H"
|
|
77
|
-
});
|
|
78
|
-
}
|
|
79
|
-
async function mineBlocks(_blocks, provider) {
|
|
80
|
-
const blocks = intToBigInt(_blocks);
|
|
81
|
-
for (let index = 0; index < blocks; index++) {
|
|
82
|
-
await mineBlock(provider);
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
async function getStxBalance(provider, account) {
|
|
86
|
-
const bin = getBin(provider);
|
|
87
|
-
const { output_serialized } = await evalJson({
|
|
88
|
-
contractAddress: UTIL_CONTRACT_ID,
|
|
89
|
-
functionName: "get-stx-balance",
|
|
90
|
-
args: [`'${account}`],
|
|
91
|
-
provider: bin
|
|
92
|
-
});
|
|
93
|
-
const outputCV = hexToCV(output_serialized);
|
|
94
|
-
const balance = cvToValue(outputCV);
|
|
95
|
-
return balance;
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
// src/utils/coverage.ts
|
|
99
|
-
import { mkdir, readdir, unlink } from "fs/promises";
|
|
100
|
-
import { join as join2, resolve } from "path";
|
|
101
|
-
async function setupCoverage(dir) {
|
|
102
|
-
const dirName = resolve(process.cwd(), "coverage");
|
|
103
|
-
try {
|
|
104
|
-
await mkdir(dirName);
|
|
105
|
-
} catch (error) {
|
|
106
|
-
}
|
|
107
|
-
const files = await readdir(dirName);
|
|
108
|
-
const removals = files.map((file) => {
|
|
109
|
-
return unlink(join2(dirName, file));
|
|
110
|
-
});
|
|
111
|
-
await Promise.all(removals);
|
|
112
|
-
return dirName;
|
|
113
|
-
}
|
|
114
|
-
async function finishCoverage(provider, dir) {
|
|
115
|
-
const lcovFile = join2(dir, "clarigen.lcov");
|
|
116
|
-
await provider.runCommand(["make_lcov", dir, lcovFile]);
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
// src/utils/index.ts
|
|
120
|
-
function makeRandomAddress(version = StacksNetworkVersion.testnetP2PKH) {
|
|
121
|
-
const privateKey = makeRandomPrivKey();
|
|
122
|
-
const publicKey = getPublicKeyFromStacksPrivateKey(privateKey);
|
|
123
|
-
const address = publicKeyToStxAddress(publicKey.data, version);
|
|
124
|
-
return address;
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
// src/utils/pure.ts
|
|
128
|
-
import {
|
|
129
|
-
cvToString,
|
|
130
|
-
cvToValue as cvToValue2,
|
|
131
|
-
filterEvents,
|
|
132
|
-
CoreNodeEventType,
|
|
133
|
-
ok,
|
|
134
|
-
err
|
|
135
|
-
} from "@clarigen/core";
|
|
136
|
-
import {
|
|
137
|
-
evalRaw,
|
|
138
|
-
evalJson as evalJson2,
|
|
139
|
-
executeJson as executeJson2
|
|
140
|
-
} from "@clarigen/native-bin";
|
|
141
|
-
import {
|
|
142
|
-
hexToCV as hexToCV2,
|
|
143
|
-
responseErrorCV,
|
|
144
|
-
responseOkCV
|
|
145
|
-
} from "micro-stacks/clarity";
|
|
146
|
-
function getPrints(events) {
|
|
147
|
-
return filterEvents(events, CoreNodeEventType.ContractEvent).map((e) => {
|
|
148
|
-
const hex = e.contract_event.raw_value;
|
|
149
|
-
const cv = hexToCV2(hex);
|
|
150
|
-
return cvToValue2(cv);
|
|
151
|
-
});
|
|
152
|
-
}
|
|
153
|
-
function formatArguments(args) {
|
|
154
|
-
return args.map((arg) => cvToString(arg));
|
|
155
|
-
}
|
|
156
|
-
function getIdentifier(tx2) {
|
|
157
|
-
return `${tx2.contractAddress}.${tx2.contractName}`;
|
|
158
|
-
}
|
|
159
|
-
function getLogs(stderr) {
|
|
160
|
-
if (stderr === "")
|
|
161
|
-
return [];
|
|
162
|
-
return stderr.split("\n").map((line) => line.slice(62));
|
|
163
|
-
}
|
|
164
|
-
function makeEvalResult(result) {
|
|
165
|
-
const resultCV = hexToCV2(result.output_serialized);
|
|
166
|
-
const value = cvToValue2(resultCV, true);
|
|
167
|
-
return {
|
|
168
|
-
value,
|
|
169
|
-
clarityValue: resultCV,
|
|
170
|
-
logs: getLogs(result.stderr),
|
|
171
|
-
costs: result.costs
|
|
172
|
-
};
|
|
173
|
-
}
|
|
174
|
-
async function ro({
|
|
175
|
-
tx: tx2,
|
|
176
|
-
bin,
|
|
177
|
-
coverageFolder
|
|
178
|
-
}) {
|
|
179
|
-
const result = await evalJson2({
|
|
180
|
-
functionName: tx2.function.name,
|
|
181
|
-
args: formatArguments(tx2.functionArgs),
|
|
182
|
-
contractAddress: getIdentifier(tx2),
|
|
183
|
-
provider: bin,
|
|
184
|
-
coverageFolder
|
|
185
|
-
});
|
|
186
|
-
return makeEvalResult(result);
|
|
187
|
-
}
|
|
188
|
-
async function evalCode({
|
|
189
|
-
contractAddress,
|
|
190
|
-
code,
|
|
191
|
-
bin,
|
|
192
|
-
coverageFolder
|
|
193
|
-
}) {
|
|
194
|
-
const result = await evalRaw({
|
|
195
|
-
contractAddress,
|
|
196
|
-
code,
|
|
197
|
-
provider: bin,
|
|
198
|
-
coverageFolder
|
|
199
|
-
});
|
|
200
|
-
return makeEvalResult(result);
|
|
201
|
-
}
|
|
202
|
-
async function tx({
|
|
203
|
-
tx: tx2,
|
|
204
|
-
senderAddress,
|
|
205
|
-
bin,
|
|
206
|
-
coverageFolder
|
|
207
|
-
}) {
|
|
208
|
-
const result = await executeJson2({
|
|
209
|
-
contractAddress: getIdentifier(tx2),
|
|
210
|
-
senderAddress,
|
|
211
|
-
provider: bin,
|
|
212
|
-
functionName: tx2.function.name,
|
|
213
|
-
args: formatArguments(tx2.functionArgs),
|
|
214
|
-
coverageFolder
|
|
215
|
-
});
|
|
216
|
-
const resultCV = hexToCV2(result.output_serialized);
|
|
217
|
-
const value = cvToValue2(resultCV);
|
|
218
|
-
const resp = result.success ? ok(value) : err(value);
|
|
219
|
-
const baseReturn = {
|
|
220
|
-
logs: getLogs(result.stderr),
|
|
221
|
-
costs: result.costs,
|
|
222
|
-
value: resp
|
|
223
|
-
};
|
|
224
|
-
if (result.success) {
|
|
225
|
-
return __spreadProps(__spreadValues({}, baseReturn), {
|
|
226
|
-
isOk: true,
|
|
227
|
-
response: responseOkCV(resultCV),
|
|
228
|
-
assets: result.assets,
|
|
229
|
-
events: result.events,
|
|
230
|
-
prints: getPrints(result.events)
|
|
231
|
-
});
|
|
232
|
-
}
|
|
233
|
-
return __spreadProps(__spreadValues({}, baseReturn), {
|
|
234
|
-
isOk: false,
|
|
235
|
-
response: responseErrorCV(resultCV)
|
|
236
|
-
});
|
|
237
|
-
}
|
|
238
|
-
async function mapGet({
|
|
239
|
-
map,
|
|
240
|
-
bin
|
|
241
|
-
}) {
|
|
242
|
-
const code = `(map-get? ${map.map.name} ${cvToString(map.key)})`;
|
|
243
|
-
const result = await evalCode({
|
|
244
|
-
contractAddress: getIdentifier(map),
|
|
245
|
-
code,
|
|
246
|
-
bin
|
|
247
|
-
});
|
|
248
|
-
return result.value;
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
// src/index.ts
|
|
252
|
-
import { resolve as resolve2 } from "path";
|
|
253
|
-
|
|
254
|
-
// src/utils/test-factory.ts
|
|
255
|
-
import {
|
|
256
|
-
projectFactory
|
|
257
|
-
} from "@clarigen/core";
|
|
258
|
-
function testFactory(project) {
|
|
259
|
-
return projectFactory(project, "simnet");
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
// src/index.ts
|
|
263
|
-
import { Allocation, createClarityBin as createClarityBin2, executeJson as executeJson3, evalJson as evalJson3 } from "@clarigen/native-bin";
|
|
264
|
-
var TestProvider = class {
|
|
265
|
-
constructor(clarityBin) {
|
|
266
|
-
this.clarityBin = clarityBin;
|
|
267
|
-
}
|
|
268
|
-
static async fromContracts(contracts, options = {}) {
|
|
269
|
-
const clarityBin = options.clarityBin || await createClarityBin({
|
|
270
|
-
allocations: options.accounts
|
|
271
|
-
});
|
|
272
|
-
const instances = {};
|
|
273
|
-
let coverageFolder = options.coverageFolder;
|
|
274
|
-
if (process.env.CLARIGEN_COVERAGE) {
|
|
275
|
-
coverageFolder = resolve2(process.cwd(), "coverage");
|
|
276
|
-
}
|
|
277
|
-
await deployUtilContract(clarityBin);
|
|
278
|
-
for (const k in contracts) {
|
|
279
|
-
if (Object.prototype.hasOwnProperty.call(contracts, k)) {
|
|
280
|
-
const contract = contracts[k];
|
|
281
|
-
const identifier = getContractIdentifier(contract);
|
|
282
|
-
await deployContract2({
|
|
283
|
-
contractIdentifier: identifier,
|
|
284
|
-
contractFilePath: contract.contractFile,
|
|
285
|
-
provider: clarityBin,
|
|
286
|
-
coverageFolder
|
|
287
|
-
});
|
|
288
|
-
const instance = contract.contract(contract.address, contract.name);
|
|
289
|
-
instances[k] = {
|
|
290
|
-
identifier: getContractIdentifier(contract),
|
|
291
|
-
contract: instance
|
|
292
|
-
};
|
|
293
|
-
}
|
|
294
|
-
}
|
|
295
|
-
const provider = new this(clarityBin);
|
|
296
|
-
provider.coverageFolder = coverageFolder;
|
|
297
|
-
return {
|
|
298
|
-
deployed: instances,
|
|
299
|
-
provider
|
|
300
|
-
};
|
|
301
|
-
}
|
|
302
|
-
static async fromProject(simnet, options = {}) {
|
|
303
|
-
const allocations = Object.fromEntries(Object.entries(simnet.accounts).map(([name, account]) => {
|
|
304
|
-
return [name, __spreadProps(__spreadValues({}, account), { balance: BigInt(account.balance) })];
|
|
305
|
-
}));
|
|
306
|
-
const clarityBin = options.clarityBin || await createClarityBin({
|
|
307
|
-
allocations
|
|
308
|
-
});
|
|
309
|
-
let coverageFolder = options.coverageFolder;
|
|
310
|
-
if (process.env.CLARIGEN_COVERAGE) {
|
|
311
|
-
coverageFolder = resolve2(process.cwd(), "coverage");
|
|
312
|
-
}
|
|
313
|
-
await deployUtilContract(clarityBin);
|
|
314
|
-
for (const contract of simnet.deployment) {
|
|
315
|
-
let contractFilePath = contract.file;
|
|
316
|
-
if (options.clarinetPath) {
|
|
317
|
-
contractFilePath = resolve2(process.cwd(), options.clarinetPath, contractFilePath);
|
|
318
|
-
}
|
|
319
|
-
await deployContract2({
|
|
320
|
-
contractIdentifier: contract.identifier,
|
|
321
|
-
contractFilePath,
|
|
322
|
-
provider: clarityBin,
|
|
323
|
-
coverageFolder
|
|
324
|
-
});
|
|
325
|
-
}
|
|
326
|
-
const provider = new this(clarityBin);
|
|
327
|
-
provider.coverageFolder = coverageFolder;
|
|
328
|
-
return provider;
|
|
329
|
-
}
|
|
330
|
-
static async fromFactory(contracts, options = {}) {
|
|
331
|
-
const clarityBin = options.clarityBin || await createClarityBin({
|
|
332
|
-
allocations: options.accounts
|
|
333
|
-
});
|
|
334
|
-
let coverageFolder = options.coverageFolder;
|
|
335
|
-
if (process.env.CLARIGEN_COVERAGE) {
|
|
336
|
-
coverageFolder = resolve2(process.cwd(), "coverage");
|
|
337
|
-
}
|
|
338
|
-
await deployUtilContract(clarityBin);
|
|
339
|
-
for (const k in contracts) {
|
|
340
|
-
if (Object.prototype.hasOwnProperty.call(contracts, k)) {
|
|
341
|
-
const contract = contracts[k];
|
|
342
|
-
let contractFilePath = contract.contractFile;
|
|
343
|
-
if (options.clarinetPath) {
|
|
344
|
-
contractFilePath = resolve2(process.cwd(), options.clarinetPath, contractFilePath);
|
|
345
|
-
}
|
|
346
|
-
if (typeof contractFilePath === "undefined") {
|
|
347
|
-
throw new Error("Cannot setup @clarigen/test - missing contract file.");
|
|
348
|
-
}
|
|
349
|
-
await deployContract2({
|
|
350
|
-
contractIdentifier: contract.identifier,
|
|
351
|
-
contractFilePath,
|
|
352
|
-
provider: clarityBin,
|
|
353
|
-
coverageFolder
|
|
354
|
-
});
|
|
355
|
-
}
|
|
356
|
-
}
|
|
357
|
-
const provider = new this(clarityBin);
|
|
358
|
-
provider.coverageFolder = coverageFolder;
|
|
359
|
-
return provider;
|
|
360
|
-
}
|
|
361
|
-
ro(tx2) {
|
|
362
|
-
return ro({ tx: tx2, bin: this.clarityBin, coverageFolder: this.coverageFolder });
|
|
363
|
-
}
|
|
364
|
-
async rov(tx2) {
|
|
365
|
-
const result = await this.ro(tx2);
|
|
366
|
-
return result.value;
|
|
367
|
-
}
|
|
368
|
-
async roOk(tx2) {
|
|
369
|
-
const result = await this.ro(tx2);
|
|
370
|
-
const value = expectOk(result.value);
|
|
371
|
-
return __spreadProps(__spreadValues({}, result), {
|
|
372
|
-
value
|
|
373
|
-
});
|
|
374
|
-
}
|
|
375
|
-
async roErr(tx2) {
|
|
376
|
-
const result = await this.ro(tx2);
|
|
377
|
-
const value = expectErr(result.value);
|
|
378
|
-
return __spreadProps(__spreadValues({}, result), {
|
|
379
|
-
value
|
|
380
|
-
});
|
|
381
|
-
}
|
|
382
|
-
async rovOk(tx2) {
|
|
383
|
-
return (await this.roOk(tx2)).value;
|
|
384
|
-
}
|
|
385
|
-
async rovErr(tx2) {
|
|
386
|
-
return (await this.roErr(tx2)).value;
|
|
387
|
-
}
|
|
388
|
-
tx(tx2, senderAddress) {
|
|
389
|
-
return tx({
|
|
390
|
-
tx: tx2,
|
|
391
|
-
senderAddress,
|
|
392
|
-
bin: this.clarityBin,
|
|
393
|
-
coverageFolder: this.coverageFolder
|
|
394
|
-
});
|
|
395
|
-
}
|
|
396
|
-
async txOk(tx2, senderAddress) {
|
|
397
|
-
const result = await this.tx(tx2, senderAddress);
|
|
398
|
-
const value = expectOk(result.value);
|
|
399
|
-
if (!result.isOk)
|
|
400
|
-
throw new Error(`Expected OK, received error: ${String(result.value)}`);
|
|
401
|
-
return __spreadProps(__spreadValues({}, result), {
|
|
402
|
-
value
|
|
403
|
-
});
|
|
404
|
-
}
|
|
405
|
-
async txErr(tx2, senderAddress) {
|
|
406
|
-
const result = await this.tx(tx2, senderAddress);
|
|
407
|
-
const value = expectErr(result.value);
|
|
408
|
-
if (result.isOk)
|
|
409
|
-
throw new Error(`Expected Err, received ok: ${String(result.value)}`);
|
|
410
|
-
return __spreadProps(__spreadValues({}, result), {
|
|
411
|
-
value
|
|
412
|
-
});
|
|
413
|
-
}
|
|
414
|
-
evalCode(code, contractAddress = UTIL_CONTRACT_ID) {
|
|
415
|
-
return evalCode({
|
|
416
|
-
contractAddress,
|
|
417
|
-
code,
|
|
418
|
-
bin: this.clarityBin,
|
|
419
|
-
coverageFolder: this.coverageFolder
|
|
420
|
-
});
|
|
421
|
-
}
|
|
422
|
-
async eval(code, contractAddress = UTIL_CONTRACT_ID) {
|
|
423
|
-
const result = await this.evalCode(code, contractAddress);
|
|
424
|
-
return result.value;
|
|
425
|
-
}
|
|
426
|
-
mapGet(map) {
|
|
427
|
-
return mapGet({ map, bin: this.clarityBin });
|
|
428
|
-
}
|
|
429
|
-
};
|
|
430
|
-
export {
|
|
431
|
-
Allocation,
|
|
432
|
-
TestProvider,
|
|
433
|
-
createClarityBin2 as createClarityBin,
|
|
434
|
-
evalJson3 as evalJson,
|
|
435
|
-
executeJson3 as executeJson,
|
|
436
|
-
finishCoverage,
|
|
437
|
-
getBlockHeight,
|
|
438
|
-
getStxBalance,
|
|
439
|
-
makeRandomAddress,
|
|
440
|
-
mineBlocks,
|
|
441
|
-
setupCoverage,
|
|
442
|
-
testFactory
|
|
443
|
-
};
|
|
1
|
+
var C=Object.defineProperty,T=Object.defineProperties;var x=Object.getOwnPropertyDescriptors;var E=Object.getOwnPropertySymbols;var m=Object.prototype.hasOwnProperty,N=Object.prototype.propertyIsEnumerable;var u=(t,e,n)=>e in t?C(t,e,{enumerable:!0,configurable:!0,writable:!0,value:n}):t[e]=n,i=(t,e)=>{for(var n in e||(e={}))m.call(e,n)&&u(t,n,e[n]);if(E)for(var n of E(e))N.call(e,n)&&u(t,n,e[n]);return t},v=(t,e)=>T(t,x(e));import{cvToValue as l,mapFactory as k}from"@clarigen/core";import{cvToValue as R}from"@clarigen/core";import{deserializeCV as _,serializeCV as S}from"@stacks/transactions";import{serializeCV as A,deserializeCV as g}from"micro-stacks/clarity";function s(t){return _(A(t))}function p(t){return g(S(t))}function c(t,e){let n=R(p(t),!0);if(!n.hasOwnProperty("isOk")&&typeof e<"u")throw new Error("Expected response when calling function, but not a response. Try using just `tx` or `rov`");if(typeof e<"u"&&"isOk"in n){let o=n,r=o.value;if(e&&!o.isOk)throw new Error(`Tx result failed. Expected OK, received ERR ${r}.`);if(e===!1&&o.isOk)throw new Error(`Tx result failed. Expected ERR, received OK ${r}.`);return r}return n}function q(t,e){return t.filter(n=>n.event===e)}function M(t,e){let n=t.functionArgs.map(s),o=`${t.contractAddress}.${t.contractName}`,r=simnet.callPublicFn(o,t.function.name,n,e),a=c(r.result,!0);return v(i({},r),{events:r.events,value:a})}function F(t,e){let n=t.functionArgs.map(s),o=`${t.contractAddress}.${t.contractName}`,r=simnet.callPublicFn(o,t.function.name,n,e),a=c(r.result,!1);return v(i({},r),{events:r.events,value:a})}function V(t,e){let n=t.functionArgs.map(s),o=`${t.contractAddress}.${t.contractName}`,r=simnet.callPublicFn(o,t.function.name,n,e),a=c(r.result);return v(i({},r),{events:r.events,value:a})}function f(t,e){let n=t.functionArgs.map(s),o=`${t.contractAddress}.${t.contractName}`,r=simnet.callReadOnlyFn(o,t.function.name,n,e??t.contractAddress),a=c(r.result);return v(i({},r),{events:r.events,value:a})}function w(t,e){return f(t,e).value}function d(t,e){let n=t.functionArgs.map(s),o=`${t.contractAddress}.${t.contractName}`,r=simnet.callReadOnlyFn(o,t.function.name,n,e??t.contractAddress),a=c(r.result,!0);return v(i({},r),{events:r.events,value:a})}function B(t,e){return d(t,e).value}function y(t,e){let n=t.functionArgs.map(s),o=`${t.contractAddress}.${t.contractName}`,r=simnet.callReadOnlyFn(o,t.function.name,n,e??t.contractAddress),a=c(r.result,!1);return v(i({},r),{events:r.events,value:a})}function O(t,e){return y(t,e).value}function ot(t,e,n){let o=k(e,n),r=simnet.getMapEntry(t,o.map.name,s(o.keyCV));return l(p(r))}function at(t,e){let n=simnet.getDataVar(t,e.name);return l(p(n))}var st={txOk:M,txErr:F,tx:V,ro:f,roOk:d,roErr:y,rov:w,rovOk:B,rovErr:O};async function it(t,e){await simnet.initSession(t??process.cwd(),e??"./Clarinet.toml")}export{st as chain,q as filterEvents,it as makeNewSession,ot as mapGet,f as ro,y as roErr,d as roOk,w as rov,O as rovErr,B as rovOk,V as tx,F as txErr,M as txOk,at as varGet};
|
|
2
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/utils.ts","../src/events.ts"],"sourcesContent":["import {\n ContractCallTyped,\n ErrType,\n OkType,\n TypedAbiMap,\n TypedAbiVariable,\n UnknownArgs,\n cvToValue,\n mapFactory,\n rawClarityToValue,\n} from '@clarigen/core';\nimport { Simnet, ParsedTransactionResult } from '@hirosystems/clarinet-sdk';\nimport { cvConvertHiro, cvConvertMS, validateResponse } from './utils';\nimport { CoreNodeEvent } from './events';\nimport { ClarityType } from '@stacks/transactions';\nexport * from './events';\n\ndeclare global {\n let simnet: Simnet;\n}\n\nexport type TransactionResult<R> = Omit<ParsedTransactionResult, 'events'> & {\n value: R;\n events: CoreNodeEvent[];\n};\n\nexport function txOk<A extends UnknownArgs, R>(\n tx: ContractCallTyped<A, R>,\n sender: string\n): TransactionResult<OkType<R>> {\n const args = tx.functionArgs.map(cvConvertMS);\n const contractId = `${tx.contractAddress}.${tx.contractName}`;\n const receipt = simnet.callPublicFn(contractId, tx.function.name, args, sender);\n const value = validateResponse<OkType<R>>(receipt.result, true);\n\n return {\n ...receipt,\n events: receipt.events as unknown as CoreNodeEvent[],\n value,\n };\n}\n\nexport function txErr<A extends UnknownArgs, R>(\n tx: ContractCallTyped<A, R>,\n sender: string\n): TransactionResult<ErrType<R>> {\n const args = tx.functionArgs.map(cvConvertMS);\n const contractId = `${tx.contractAddress}.${tx.contractName}`;\n const receipt = simnet.callPublicFn(contractId, tx.function.name, args, sender);\n const value = validateResponse<ErrType<R>>(receipt.result, false);\n\n return {\n ...receipt,\n events: receipt.events as unknown as CoreNodeEvent[],\n value,\n };\n}\n\nexport function tx<A extends UnknownArgs, R>(\n tx: ContractCallTyped<A, R>,\n sender: string\n): TransactionResult<R> {\n const args = tx.functionArgs.map(cvConvertMS);\n const contractId = `${tx.contractAddress}.${tx.contractName}`;\n const receipt = simnet.callPublicFn(contractId, tx.function.name, args, sender);\n const value = validateResponse<R>(receipt.result);\n\n return {\n ...receipt,\n events: receipt.events as unknown as CoreNodeEvent[],\n value,\n };\n}\n\nexport function ro<A extends UnknownArgs, R>(\n tx: ContractCallTyped<A, R>,\n sender?: string\n): TransactionResult<R> {\n const args = tx.functionArgs.map(cvConvertMS);\n const contractId = `${tx.contractAddress}.${tx.contractName}`;\n const receipt = simnet.callReadOnlyFn(\n contractId,\n tx.function.name,\n args,\n sender ?? tx.contractAddress\n );\n const value = validateResponse<R>(receipt.result);\n return {\n ...receipt,\n events: receipt.events as unknown as CoreNodeEvent[],\n value,\n };\n\n // return value;\n}\n\nexport function rov<A extends UnknownArgs, R>(tx: ContractCallTyped<A, R>, sender?: string): R {\n return ro(tx, sender).value;\n}\n\nexport function roOk<A extends UnknownArgs, R>(\n tx: ContractCallTyped<A, R>,\n sender?: string\n): TransactionResult<OkType<R>> {\n const args = tx.functionArgs.map(cvConvertMS);\n const contractId = `${tx.contractAddress}.${tx.contractName}`;\n const receipt = simnet.callReadOnlyFn(\n contractId,\n tx.function.name,\n args,\n sender ?? tx.contractAddress\n );\n const value = validateResponse<OkType<R>>(receipt.result, true);\n return {\n ...receipt,\n events: receipt.events as unknown as CoreNodeEvent[],\n value,\n };\n\n // return value;\n}\n\nexport function rovOk<A extends UnknownArgs, R>(\n tx: ContractCallTyped<A, R>,\n sender?: string\n): OkType<R> {\n return roOk(tx, sender).value;\n}\n\nexport function roErr<A extends UnknownArgs, R>(\n tx: ContractCallTyped<A, R>,\n sender?: string\n): TransactionResult<ErrType<R>> {\n const args = tx.functionArgs.map(cvConvertMS);\n const contractId = `${tx.contractAddress}.${tx.contractName}`;\n const receipt = simnet.callReadOnlyFn(\n contractId,\n tx.function.name,\n args,\n sender ?? tx.contractAddress\n );\n const value = validateResponse<ErrType<R>>(receipt.result, false);\n\n return {\n ...receipt,\n events: receipt.events as unknown as CoreNodeEvent[],\n value,\n };\n\n // return value;\n}\n\nexport function rovErr<A extends UnknownArgs, R>(\n tx: ContractCallTyped<A, R>,\n sender?: string\n): ErrType<R> {\n return roErr(tx, sender).value;\n}\n\nexport function mapGet<Key, Val>(contractId: string, map: TypedAbiMap<Key, Val>, key: Key) {\n const payload = mapFactory(map, key);\n const result = simnet.getMapEntry(contractId, payload.map.name, cvConvertMS(payload.keyCV));\n return cvToValue<Val | null>(cvConvertHiro(result));\n}\n\nexport function varGet<T>(contractId: string, variable: TypedAbiVariable<T>) {\n const result = simnet.getDataVar(contractId, variable.name);\n return cvToValue<T>(cvConvertHiro(result));\n}\n\n// Helper export for previous Deno-based tests\nexport const chain = {\n txOk,\n txErr,\n tx,\n ro,\n roOk,\n roErr,\n rov,\n rovOk,\n rovErr,\n};\n\nexport async function makeNewSession(cwd?: string, manifestPath?: string) {\n await simnet.initSession(cwd ?? process.cwd(), manifestPath ?? './Clarinet.toml');\n}\n","import { Response, cvToValue } from '@clarigen/core';\nimport {\n ClarityValue as HiroClarityValue,\n deserializeCV as deserializeCVHiro,\n serializeCV as serializeCVHiro,\n StandardPrincipalCV as HiroPrincipalCV,\n} from '@stacks/transactions';\nimport {\n ClarityValue as MSClarityValue,\n StandardPrincipalCV as MSPrincipalCV,\n serializeCV as serializeCVMS,\n deserializeCV as deserializeCVMS,\n ClarityAbiFunction,\n Address,\n ClarityType,\n} from 'micro-stacks/clarity';\n\nexport function cvConvertMS(value: MSClarityValue): HiroClarityValue {\n return deserializeCVHiro(serializeCVMS(value));\n}\n\nexport function cvConvertHiro(value: HiroClarityValue): MSClarityValue {\n return deserializeCVMS(serializeCVHiro(value));\n}\n\nexport function validateResponse<T>(result: HiroClarityValue, expectOk?: boolean): T {\n const value = cvToValue(cvConvertHiro(result), true);\n // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call\n if (!value.hasOwnProperty('isOk') && typeof expectOk !== 'undefined') {\n throw new Error(\n `Expected response when calling function, but not a response. Try using just \\`tx\\` or \\`rov\\``\n );\n }\n if (typeof expectOk !== 'undefined' && 'isOk' in value) {\n const response = value as Response<unknown, unknown>;\n const inner = response.value;\n if (expectOk && !response.isOk) {\n // eslint-disable-next-line @typescript-eslint/restrict-template-expressions\n throw new Error(`Tx result failed. Expected OK, received ERR ${inner}.`);\n }\n if (expectOk === false && response.isOk) {\n // eslint-disable-next-line @typescript-eslint/restrict-template-expressions\n throw new Error(`Tx result failed. Expected ERR, received OK ${inner}.`);\n }\n return inner as T;\n }\n return value as T;\n}\n","import {\n CoreNodeEventType,\n SmartContractEvent as _SmartContractEvent,\n StxTransferEvent as _StxTransferEvent,\n StxBurnEvent as _StxBurnEvent,\n StxMintEvent as _StxMintEvent,\n StxLockEvent as _StxLockEvent,\n FtBurnEvent as _FtBurnEvent,\n FtMintEvent as _FtMintEvent,\n FtTransferEvent as _FtTransferEvent,\n NftBurnEvent as _NftBurnEvent,\n NftMintEvent as _NftMintEvent,\n NftTransferEvent as _NftTransferEvent,\n} from '@clarigen/core';\n\nexport type SmartContractEvent = {\n event: _SmartContractEvent['type'];\n data: _SmartContractEvent['contract_event'];\n};\n\nexport type StxTransferEvent = {\n event: _StxTransferEvent['type'];\n data: _StxTransferEvent['stx_transfer_event'];\n};\n\nexport type StxBurnEvent = {\n event: _StxBurnEvent['type'];\n data: _StxBurnEvent['stx_burn_event'];\n};\n\nexport type StxLockEvent = {\n event: _StxLockEvent['type'];\n data: _StxLockEvent['stx_lock_event'];\n};\n\nexport type StxMintEvent = {\n event: _StxMintEvent['type'];\n data: _StxMintEvent['stx_mint_event'];\n};\n\nexport type FtBurnEvent = {\n event: _FtBurnEvent['type'];\n data: _FtBurnEvent['ft_burn_event'];\n};\n\nexport type FtMintEvent = {\n event: _FtMintEvent['type'];\n data: _FtMintEvent['ft_mint_event'];\n};\n\nexport type FtTransferEvent = {\n event: _FtTransferEvent['type'];\n data: _FtTransferEvent['ft_transfer_event'];\n};\n\nexport type NftBurnEvent = {\n event: _NftBurnEvent['type'];\n data: _NftBurnEvent['nft_burn_event'];\n};\n\nexport type NftMintEvent = {\n event: _NftMintEvent['type'];\n data: _NftMintEvent['nft_mint_event'];\n};\n\nexport type NftTransferEvent = {\n event: _NftTransferEvent['type'];\n data: _NftTransferEvent['nft_transfer_event'];\n};\n\nexport type CoreNodeEvent =\n | SmartContractEvent\n | StxTransferEvent\n | StxMintEvent\n | StxBurnEvent\n | StxLockEvent\n | FtTransferEvent\n | FtMintEvent\n | FtBurnEvent\n | NftTransferEvent\n | NftMintEvent\n | NftBurnEvent;\n\nexport function filterEvents(\n events: CoreNodeEvent[],\n type: CoreNodeEventType.ContractEvent\n): SmartContractEvent[];\nexport function filterEvents(\n events: CoreNodeEvent[],\n type: CoreNodeEventType.StxTransferEvent\n): StxTransferEvent[];\nexport function filterEvents(\n events: CoreNodeEvent[],\n type: CoreNodeEventType.StxMintEvent\n): StxMintEvent[];\nexport function filterEvents(\n events: CoreNodeEvent[],\n type: CoreNodeEventType.StxBurnEvent\n): StxBurnEvent[];\nexport function filterEvents(\n events: CoreNodeEvent[],\n type: CoreNodeEventType.StxLockEvent\n): StxLockEvent[];\nexport function filterEvents(\n events: CoreNodeEvent[],\n type: CoreNodeEventType.NftTransferEvent\n): NftTransferEvent[];\nexport function filterEvents(\n events: CoreNodeEvent[],\n type: CoreNodeEventType.NftMintEvent\n): NftMintEvent[];\nexport function filterEvents(\n events: CoreNodeEvent[],\n type: CoreNodeEventType.NftBurnEvent\n): NftBurnEvent[];\nexport function filterEvents(\n events: CoreNodeEvent[],\n type: CoreNodeEventType.FtTransferEvent\n): FtTransferEvent[];\nexport function filterEvents(\n events: CoreNodeEvent[],\n type: CoreNodeEventType.FtMintEvent\n): FtMintEvent[];\nexport function filterEvents(\n events: CoreNodeEvent[],\n type: CoreNodeEventType.FtBurnEvent\n): FtBurnEvent[];\nexport function filterEvents(events: CoreNodeEvent[], type: CoreNod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ACZ,MAAO,GAAM,EAAI,CAAM,EAAE,KAC3B,CAEO,YAA0B,EAAoB,EAA4B,EAAU,CACzF,GAAM,GAAU,EAAW,EAAK,CAAG,EAC7B,EAAS,OAAO,YAAY,EAAY,EAAQ,IAAI,KAAM,EAAY,EAAQ,KAAK,CAAC,EAC1F,MAAO,GAAsB,EAAc,CAAM,CAAC,CACpD,CAEO,YAAmB,EAAoB,EAA+B,CAC3E,GAAM,GAAS,OAAO,WAAW,EAAY,EAAS,IAAI,EAC1D,MAAO,GAAa,EAAc,CAAM,CAAC,CAC3C,CAGO,GAAM,IAAQ,CACnB,OACA,QACA,KACA,KACA,OACA,QACA,MACA,QACA,QACF,EAEA,kBAAqC,EAAc,EAAuB,CACxE,KAAM,QAAO,YAAY,GAAO,QAAQ,IAAI,EAAG,GAAgB,iBAAiB,CAClF","names":[]}
|
package/package.json
CHANGED
|
@@ -1,13 +1,12 @@
|
|
|
1
1
|
{
|
|
2
|
-
"version": "1.0.
|
|
2
|
+
"version": "1.0.16-alpha.1",
|
|
3
3
|
"license": "MIT",
|
|
4
4
|
"types": "./dist/index.d.ts",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"module": "./dist/index.mjs",
|
|
7
7
|
"import": "./dist/index.mjs",
|
|
8
8
|
"files": [
|
|
9
|
-
"dist"
|
|
10
|
-
"contracts"
|
|
9
|
+
"dist"
|
|
11
10
|
],
|
|
12
11
|
"engines": {
|
|
13
12
|
"node": ">=10"
|
|
@@ -16,24 +15,27 @@
|
|
|
16
15
|
"name": "@clarigen/test",
|
|
17
16
|
"author": "Hank Stoever",
|
|
18
17
|
"devDependencies": {
|
|
19
|
-
"
|
|
18
|
+
"@types/node": "20.10.4",
|
|
19
|
+
"vitest": "1.0.4",
|
|
20
|
+
"vitest-environment-clarinet": "^1.1.0",
|
|
21
|
+
"demo-project": "1.0.16-alpha.1"
|
|
20
22
|
},
|
|
21
23
|
"dependencies": {
|
|
22
|
-
"
|
|
23
|
-
"@
|
|
24
|
-
"
|
|
24
|
+
"micro-stacks": "^1.1.4",
|
|
25
|
+
"@stacks/transactions": "6.10.0",
|
|
26
|
+
"@hirosystems/clarinet-sdk": "1.2.0",
|
|
27
|
+
"@hirosystems/clarinet-sdk-wasm": "1.2.0",
|
|
28
|
+
"@clarigen/core": "1.0.16-alpha.1"
|
|
25
29
|
},
|
|
26
30
|
"publishConfig": {
|
|
27
31
|
"access": "public"
|
|
28
32
|
},
|
|
29
33
|
"scripts": {
|
|
30
|
-
"start": "tsup
|
|
31
|
-
"dev": "tsup
|
|
32
|
-
"build": "shx rm -rf
|
|
33
|
-
"test": "
|
|
34
|
-
"lint": "eslint \"src/**/*.{ts,tsx}\" && prettier
|
|
35
|
-
"typecheck": "tsc --noEmit"
|
|
36
|
-
"build-test-types": "sh build-types.sh",
|
|
37
|
-
"publish:dev": "pnpm build && yalc publish --push"
|
|
34
|
+
"start": "tsup --watch",
|
|
35
|
+
"dev": "tsup --watch",
|
|
36
|
+
"build": "shx rm -rf dist && tsup",
|
|
37
|
+
"test": "vitest run",
|
|
38
|
+
"lint": "eslint \"src/**/*.{ts,tsx}\" && prettier --check \"./**/*.{ts,js}\"",
|
|
39
|
+
"typecheck": "tsc --noEmit"
|
|
38
40
|
}
|
|
39
41
|
}
|