stxer 0.3.0 → 0.3.2
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/README.md +93 -4
- package/dist/simulation.d.ts +1 -0
- package/dist/stxer.cjs.development.js +68 -38
- package/dist/stxer.cjs.development.js.map +1 -1
- package/dist/stxer.cjs.production.min.js +1 -1
- package/dist/stxer.cjs.production.min.js.map +1 -1
- package/dist/stxer.esm.js +69 -39
- package/dist/stxer.esm.js.map +1 -1
- package/package.json +5 -1
- package/src/sample/counter.ts +7 -43
- package/src/sample/read.ts +3 -1
- package/src/simulation.ts +56 -34
package/src/sample/counter.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { uintCV } from '@stacks/transactions';
|
|
2
2
|
import { SimulationBuilder } from '..';
|
|
3
3
|
|
|
4
|
-
SimulationBuilder.new()
|
|
4
|
+
const test = () => SimulationBuilder.new()
|
|
5
5
|
.withSender('SP212Y5JKN59YP3GYG07K3S8W5SSGE4KH6B5STXER')
|
|
6
|
+
.inlineSimulation('1ab04a8d13d72a301b77b6af9d4f612b')
|
|
6
7
|
.addContractDeploy({
|
|
7
8
|
contract_name: 'test-simulation',
|
|
8
9
|
source_code: `
|
|
@@ -37,46 +38,9 @@ SimulationBuilder.new()
|
|
|
37
38
|
'(get-counter)'
|
|
38
39
|
)
|
|
39
40
|
.run()
|
|
40
|
-
.catch(console.error);
|
|
41
41
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
}
|
|
47
|
-
.withSender('ST3MZM9WJ34Y4311XBJDBKQ41SXX5DY68406J26WJ')
|
|
48
|
-
.addContractDeploy({
|
|
49
|
-
contract_name: 'test-simulation',
|
|
50
|
-
source_code: `
|
|
51
|
-
;; counter example
|
|
52
|
-
(define-data-var counter uint u0)
|
|
53
|
-
|
|
54
|
-
(define-public (increment (delta uint))
|
|
55
|
-
(begin
|
|
56
|
-
(var-set counter (+ (var-get counter) delta))
|
|
57
|
-
(ok (var-get counter))))
|
|
58
|
-
|
|
59
|
-
(define-public (decrement)
|
|
60
|
-
(begin
|
|
61
|
-
(var-set counter (- (var-get counter) u1))
|
|
62
|
-
(ok (var-get counter))))
|
|
63
|
-
|
|
64
|
-
(define-read-only (get-counter)
|
|
65
|
-
(ok (var-get counter)))
|
|
66
|
-
`,
|
|
67
|
-
})
|
|
68
|
-
.addEvalCode(
|
|
69
|
-
'ST3MZM9WJ34Y4311XBJDBKQ41SXX5DY68406J26WJ.test-simulation',
|
|
70
|
-
'(get-counter)'
|
|
71
|
-
)
|
|
72
|
-
.addContractCall({
|
|
73
|
-
contract_id: 'ST3MZM9WJ34Y4311XBJDBKQ41SXX5DY68406J26WJ.test-simulation',
|
|
74
|
-
function_name: 'increment',
|
|
75
|
-
function_args: [uintCV(10)],
|
|
76
|
-
})
|
|
77
|
-
.addEvalCode(
|
|
78
|
-
'ST3MZM9WJ34Y4311XBJDBKQ41SXX5DY68406J26WJ.test-simulation',
|
|
79
|
-
'(get-counter)'
|
|
80
|
-
)
|
|
81
|
-
.run()
|
|
82
|
-
.catch(console.error);
|
|
42
|
+
if (require.main === module) {
|
|
43
|
+
; (async () => {
|
|
44
|
+
await test()
|
|
45
|
+
})().catch(console.error);
|
|
46
|
+
}
|
package/src/sample/read.ts
CHANGED
package/src/simulation.ts
CHANGED
|
@@ -12,6 +12,7 @@ import {
|
|
|
12
12
|
type StacksTransactionWire,
|
|
13
13
|
bufferCV,
|
|
14
14
|
contractPrincipalCV,
|
|
15
|
+
deserializeTransaction,
|
|
15
16
|
makeUnsignedContractCall,
|
|
16
17
|
makeUnsignedContractDeploy,
|
|
17
18
|
makeUnsignedSTXTokenTransfer,
|
|
@@ -139,28 +140,32 @@ export class SimulationBuilder {
|
|
|
139
140
|
private sender = '';
|
|
140
141
|
private steps: (
|
|
141
142
|
| {
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
function_args?: ClarityValue[];
|
|
146
|
-
sender: string;
|
|
147
|
-
fee: number;
|
|
148
|
-
}
|
|
143
|
+
// inline simulation
|
|
144
|
+
simulationId: string;
|
|
145
|
+
}
|
|
149
146
|
| {
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
147
|
+
// contract call
|
|
148
|
+
contract_id: string;
|
|
149
|
+
function_name: string;
|
|
150
|
+
function_args?: ClarityValue[];
|
|
151
|
+
sender: string;
|
|
152
|
+
fee: number;
|
|
153
|
+
}
|
|
157
154
|
| {
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
155
|
+
// contract deploy
|
|
156
|
+
contract_name: string;
|
|
157
|
+
source_code: string;
|
|
158
|
+
deployer: string;
|
|
159
|
+
fee: number;
|
|
160
|
+
clarity_version: ClarityVersion;
|
|
161
|
+
}
|
|
162
|
+
| {
|
|
163
|
+
// STX transfer
|
|
164
|
+
recipient: string;
|
|
165
|
+
amount: number;
|
|
166
|
+
sender: string;
|
|
167
|
+
fee: number;
|
|
168
|
+
}
|
|
164
169
|
| SimulationEval
|
|
165
170
|
)[] = [];
|
|
166
171
|
|
|
@@ -172,6 +177,12 @@ export class SimulationBuilder {
|
|
|
172
177
|
this.sender = address;
|
|
173
178
|
return this;
|
|
174
179
|
}
|
|
180
|
+
public inlineSimulation(simulationId: string) {
|
|
181
|
+
this.steps.push({
|
|
182
|
+
simulationId,
|
|
183
|
+
})
|
|
184
|
+
return this;
|
|
185
|
+
}
|
|
175
186
|
public addSTXTransfer(params: {
|
|
176
187
|
recipient: string;
|
|
177
188
|
amount: number;
|
|
@@ -298,9 +309,8 @@ To get in touch: contact@stxer.xyz
|
|
|
298
309
|
const nextNonce = async (sender: string) => {
|
|
299
310
|
const nonce = nonce_by_address.get(sender);
|
|
300
311
|
if (nonce == null) {
|
|
301
|
-
const url = `${
|
|
302
|
-
|
|
303
|
-
}/v2/accounts/${sender}?proof=${false}&tip=${block.index_block_hash}`;
|
|
312
|
+
const url = `${this.stacksNodeAPI
|
|
313
|
+
}/v2/accounts/${sender}?proof=${false}&tip=${block.index_block_hash}`;
|
|
304
314
|
const account: AccountDataResponse = await richFetch(url).then((r) =>
|
|
305
315
|
r.json()
|
|
306
316
|
);
|
|
@@ -310,18 +320,30 @@ To get in touch: contact@stxer.xyz
|
|
|
310
320
|
nonce_by_address.set(sender, nonce + 1);
|
|
311
321
|
return nonce;
|
|
312
322
|
};
|
|
323
|
+
let network = this.network === 'mainnet' ? STACKS_MAINNET : STACKS_TESTNET;
|
|
324
|
+
if (this.stacksNodeAPI) {
|
|
325
|
+
network = {
|
|
326
|
+
...network,
|
|
327
|
+
client: {
|
|
328
|
+
...network.client,
|
|
329
|
+
baseUrl: this.stacksNodeAPI,
|
|
330
|
+
},
|
|
331
|
+
};
|
|
332
|
+
}
|
|
313
333
|
for (const step of this.steps) {
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
334
|
+
if ('simulationId' in step) {
|
|
335
|
+
const previousSimulation: {steps: ({tx: string} | {code: string, contract: string})[]} = await fetch(`https://api.stxer.xyz/simulations/${step.simulationId}/request`).then(x => x.json())
|
|
336
|
+
for (const step of previousSimulation.steps) {
|
|
337
|
+
if ('tx' in step) {
|
|
338
|
+
txs.push(deserializeTransaction(step.tx));
|
|
339
|
+
} else if ('code' in step && 'contract' in step) {
|
|
340
|
+
txs.push({
|
|
341
|
+
contract_id: step.contract,
|
|
342
|
+
code: step.code,
|
|
343
|
+
});
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
} else if ('sender' in step && 'function_name' in step) {
|
|
325
347
|
const nonce = await nextNonce(step.sender);
|
|
326
348
|
const [contractAddress, contractName] = step.contract_id.split('.');
|
|
327
349
|
const tx = await makeUnsignedContractCall({
|