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.
@@ -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
- SimulationBuilder.new({
43
- apiEndpoint: 'https://testnet-api.stxer.xyz',
44
- stacksNodeAPI: 'https://api.testnet.hiro.so',
45
- network: 'testnet',
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
+ }
@@ -121,5 +121,7 @@ import {
121
121
  await batchReadonlyExample();
122
122
  }
123
123
 
124
- main().catch(console.error);
124
+ if (require.main === module) {
125
+ main().catch(console.error);
126
+ }
125
127
 
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
- // contract call
143
- contract_id: string;
144
- function_name: string;
145
- function_args?: ClarityValue[];
146
- sender: string;
147
- fee: number;
148
- }
143
+ // inline simulation
144
+ simulationId: string;
145
+ }
149
146
  | {
150
- // contract deploy
151
- contract_name: string;
152
- source_code: string;
153
- deployer: string;
154
- fee: number;
155
- clarity_version: ClarityVersion;
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
- // STX transfer
159
- recipient: string;
160
- amount: number;
161
- sender: string;
162
- fee: number;
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
- this.stacksNodeAPI
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
- let network = this.network === 'mainnet' ? STACKS_MAINNET : STACKS_TESTNET;
315
- if (this.stacksNodeAPI) {
316
- network = {
317
- ...network,
318
- client: {
319
- ...network.client,
320
- baseUrl: this.stacksNodeAPI,
321
- },
322
- };
323
- }
324
- if ('sender' in step && 'function_name' in step) {
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({