stxer 0.2.5 → 0.3.0
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/batch-api.d.ts +30 -0
- package/dist/index.d.ts +2 -51
- package/dist/sample/read.d.ts +1 -0
- package/dist/simulation.d.ts +52 -0
- package/dist/stxer.cjs.development.js +158 -7
- 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 +157 -8
- package/dist/stxer.esm.js.map +1 -1
- package/package.json +3 -2
- package/src/batch-api.ts +157 -0
- package/src/index.ts +2 -380
- package/src/sample/read.ts +125 -0
- package/src/simulation.ts +387 -0
package/src/batch-api.ts
ADDED
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type ClarityValue,
|
|
3
|
+
type ContractPrincipalCV,
|
|
4
|
+
deserializeCV,
|
|
5
|
+
type OptionalCV,
|
|
6
|
+
serializeCV,
|
|
7
|
+
} from '@stacks/transactions';
|
|
8
|
+
|
|
9
|
+
export interface BatchReads {
|
|
10
|
+
variables?: {
|
|
11
|
+
contract: ContractPrincipalCV;
|
|
12
|
+
variableName: string;
|
|
13
|
+
}[];
|
|
14
|
+
maps?: {
|
|
15
|
+
contract: ContractPrincipalCV;
|
|
16
|
+
mapName: string;
|
|
17
|
+
mapKey: ClarityValue;
|
|
18
|
+
}[];
|
|
19
|
+
index_block_hash?: string;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface BatchReadsResult {
|
|
23
|
+
variables: (ClarityValue | Error)[];
|
|
24
|
+
maps: (OptionalCV | Error)[];
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export interface BatchApiOptions {
|
|
28
|
+
stxerApi?: string;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const DEFAULT_STXER_API = 'https://api.stxer.xyz';
|
|
32
|
+
|
|
33
|
+
export async function batchRead(
|
|
34
|
+
reads: BatchReads,
|
|
35
|
+
options: BatchApiOptions = {}
|
|
36
|
+
): Promise<BatchReadsResult> {
|
|
37
|
+
const payload: string[][] = [];
|
|
38
|
+
if (reads.variables != null) {
|
|
39
|
+
for (const variable of reads.variables) {
|
|
40
|
+
payload.push([serializeCV(variable.contract), variable.variableName]);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
if (reads.maps != null) {
|
|
44
|
+
for (const map of reads.maps) {
|
|
45
|
+
payload.push([
|
|
46
|
+
serializeCV(map.contract),
|
|
47
|
+
map.mapName,
|
|
48
|
+
serializeCV(map.mapKey),
|
|
49
|
+
]);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const ibh =
|
|
54
|
+
reads.index_block_hash == null
|
|
55
|
+
? null
|
|
56
|
+
: reads.index_block_hash.startsWith('0x')
|
|
57
|
+
? reads.index_block_hash.substring(2)
|
|
58
|
+
: reads.index_block_hash;
|
|
59
|
+
|
|
60
|
+
const url = `${options.stxerApi ?? DEFAULT_STXER_API}/sidecar/v2/batch-reads${
|
|
61
|
+
ibh == null ? '' : `?tip=${ibh}`
|
|
62
|
+
}`;
|
|
63
|
+
const data = await fetch(url, {
|
|
64
|
+
method: 'POST',
|
|
65
|
+
body: JSON.stringify(payload),
|
|
66
|
+
});
|
|
67
|
+
const text = await data.text();
|
|
68
|
+
if (!text.includes('Ok') && !text.includes('Err')) {
|
|
69
|
+
throw new Error(
|
|
70
|
+
`Requesting batch reads failed: ${text}, url: ${url}, payload: ${JSON.stringify(
|
|
71
|
+
payload
|
|
72
|
+
)}`
|
|
73
|
+
);
|
|
74
|
+
}
|
|
75
|
+
const results = JSON.parse(text) as [{ Ok: string } | { Err: string }];
|
|
76
|
+
const rs: BatchReadsResult = {
|
|
77
|
+
variables: [],
|
|
78
|
+
maps: [],
|
|
79
|
+
};
|
|
80
|
+
let variablesLength = 0;
|
|
81
|
+
if (reads.variables != null) {
|
|
82
|
+
variablesLength = reads.variables.length;
|
|
83
|
+
for (let i = 0; i < variablesLength; i++) {
|
|
84
|
+
const result = results[i];
|
|
85
|
+
if ('Ok' in result) {
|
|
86
|
+
rs.variables.push(deserializeCV(result.Ok));
|
|
87
|
+
} else {
|
|
88
|
+
rs.variables.push(new Error(result.Err));
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
if (reads.maps != null) {
|
|
93
|
+
for (let i = 0; i < reads.maps.length; i++) {
|
|
94
|
+
const result = results[i + variablesLength];
|
|
95
|
+
if ('Ok' in result) {
|
|
96
|
+
rs.maps.push(deserializeCV(result.Ok) as OptionalCV);
|
|
97
|
+
} else {
|
|
98
|
+
rs.maps.push(new Error(result.Err));
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
return rs;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export interface BatchReadonlyRequest {
|
|
106
|
+
readonly: {
|
|
107
|
+
contract: ContractPrincipalCV;
|
|
108
|
+
functionName: string;
|
|
109
|
+
functionArgs: ClarityValue[];
|
|
110
|
+
}[];
|
|
111
|
+
index_block_hash?: string;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
export async function batchReadonly(
|
|
115
|
+
req: BatchReadonlyRequest,
|
|
116
|
+
options: BatchApiOptions = {}
|
|
117
|
+
): Promise<(ClarityValue | Error)[]> {
|
|
118
|
+
const payload = req.readonly.map((r) => [
|
|
119
|
+
serializeCV(r.contract),
|
|
120
|
+
r.functionName,
|
|
121
|
+
...r.functionArgs.map((arg) => serializeCV(arg)),
|
|
122
|
+
]);
|
|
123
|
+
|
|
124
|
+
const ibh =
|
|
125
|
+
req.index_block_hash == null
|
|
126
|
+
? null
|
|
127
|
+
: req.index_block_hash.startsWith('0x')
|
|
128
|
+
? req.index_block_hash.substring(2)
|
|
129
|
+
: req.index_block_hash;
|
|
130
|
+
|
|
131
|
+
const url = `${options.stxerApi ?? DEFAULT_STXER_API}/sidecar/v2/batch-readonly${
|
|
132
|
+
ibh == null ? '' : `?tip=${ibh}`
|
|
133
|
+
}`;
|
|
134
|
+
const data = await fetch(url, {
|
|
135
|
+
method: 'POST',
|
|
136
|
+
body: JSON.stringify(payload),
|
|
137
|
+
});
|
|
138
|
+
const text = await data.text();
|
|
139
|
+
if (!text.includes('Ok') && !text.includes('Err')) {
|
|
140
|
+
throw new Error(
|
|
141
|
+
`Requesting batch readonly failed: ${text}, url: ${url}, payload: ${JSON.stringify(
|
|
142
|
+
payload
|
|
143
|
+
)}`
|
|
144
|
+
);
|
|
145
|
+
}
|
|
146
|
+
const results = JSON.parse(text) as [{ Ok: string } | { Err: string }];
|
|
147
|
+
const rs: (ClarityValue | Error)[] = [];
|
|
148
|
+
for (const result of results) {
|
|
149
|
+
if ('Ok' in result) {
|
|
150
|
+
rs.push(deserializeCV(result.Ok));
|
|
151
|
+
} else {
|
|
152
|
+
rs.push(new Error(result.Err));
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
return rs;
|
|
156
|
+
}
|
|
157
|
+
|
package/src/index.ts
CHANGED
|
@@ -1,380 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
import {
|
|
4
|
-
networkFrom,
|
|
5
|
-
STACKS_MAINNET,
|
|
6
|
-
STACKS_TESTNET,
|
|
7
|
-
type StacksNetworkName,
|
|
8
|
-
} from '@stacks/network';
|
|
9
|
-
import {
|
|
10
|
-
type ClarityValue,
|
|
11
|
-
ClarityVersion,
|
|
12
|
-
PostConditionMode,
|
|
13
|
-
type StacksTransactionWire,
|
|
14
|
-
bufferCV,
|
|
15
|
-
contractPrincipalCV,
|
|
16
|
-
makeUnsignedContractCall,
|
|
17
|
-
makeUnsignedContractDeploy,
|
|
18
|
-
makeUnsignedSTXTokenTransfer,
|
|
19
|
-
serializeCVBytes,
|
|
20
|
-
stringAsciiCV,
|
|
21
|
-
tupleCV,
|
|
22
|
-
uintCV,
|
|
23
|
-
} from '@stacks/transactions';
|
|
24
|
-
import { c32addressDecode } from 'c32check';
|
|
25
|
-
|
|
26
|
-
function runTx(tx: StacksTransactionWire) {
|
|
27
|
-
// type 0: run transaction
|
|
28
|
-
return tupleCV({ type: uintCV(0), data: bufferCV(tx.serializeBytes()) });
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
export interface SimulationEval {
|
|
32
|
-
contract_id: string;
|
|
33
|
-
code: string;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
export function runEval({ contract_id, code }: SimulationEval) {
|
|
37
|
-
const [contract_address, contract_name] = contract_id.split('.');
|
|
38
|
-
// type 1: eval arbitrary code inside a contract
|
|
39
|
-
return tupleCV({
|
|
40
|
-
type: uintCV(1),
|
|
41
|
-
data: bufferCV(
|
|
42
|
-
serializeCVBytes(
|
|
43
|
-
tupleCV({
|
|
44
|
-
contract: contractPrincipalCV(contract_address, contract_name),
|
|
45
|
-
code: stringAsciiCV(code),
|
|
46
|
-
})
|
|
47
|
-
)
|
|
48
|
-
),
|
|
49
|
-
});
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
export async function runSimulation(
|
|
53
|
-
apiEndpoint: string,
|
|
54
|
-
block_hash: string,
|
|
55
|
-
block_height: number,
|
|
56
|
-
txs: (StacksTransactionWire | SimulationEval)[]
|
|
57
|
-
) {
|
|
58
|
-
// Convert 'sim-v1' to Uint8Array
|
|
59
|
-
const header = new TextEncoder().encode('sim-v1');
|
|
60
|
-
// Create 8 bytes for block height
|
|
61
|
-
const heightBytes = new Uint8Array(8);
|
|
62
|
-
// Convert block height to bytes
|
|
63
|
-
const view = new DataView(heightBytes.buffer);
|
|
64
|
-
view.setBigUint64(0, BigInt(block_height), false); // false for big-endian
|
|
65
|
-
|
|
66
|
-
// Convert block hash to bytes
|
|
67
|
-
const hashHex = block_hash.startsWith('0x')
|
|
68
|
-
? block_hash.substring(2)
|
|
69
|
-
: block_hash;
|
|
70
|
-
// biome-ignore lint/style/noNonNullAssertion: <explanation>
|
|
71
|
-
const hashBytes = new Uint8Array(
|
|
72
|
-
hashHex.match(/.{1,2}/g)!.map((byte) => Number.parseInt(byte, 16))
|
|
73
|
-
);
|
|
74
|
-
|
|
75
|
-
// Convert transactions to bytes
|
|
76
|
-
const txBytes = txs
|
|
77
|
-
.map((t) => ('contract_id' in t && 'code' in t ? runEval(t) : runTx(t)))
|
|
78
|
-
.map((t) => serializeCVBytes(t));
|
|
79
|
-
|
|
80
|
-
// Combine all byte arrays
|
|
81
|
-
const totalLength =
|
|
82
|
-
header.length +
|
|
83
|
-
heightBytes.length +
|
|
84
|
-
hashBytes.length +
|
|
85
|
-
txBytes.reduce((acc, curr) => acc + curr.length, 0);
|
|
86
|
-
const body = new Uint8Array(totalLength);
|
|
87
|
-
|
|
88
|
-
let offset = 0;
|
|
89
|
-
body.set(header, offset);
|
|
90
|
-
offset += header.length;
|
|
91
|
-
body.set(heightBytes, offset);
|
|
92
|
-
offset += heightBytes.length;
|
|
93
|
-
body.set(hashBytes, offset);
|
|
94
|
-
offset += hashBytes.length;
|
|
95
|
-
for (const tx of txBytes) {
|
|
96
|
-
body.set(tx, offset);
|
|
97
|
-
offset += tx.length;
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
const rs = await fetch(apiEndpoint, {
|
|
101
|
-
method: 'POST',
|
|
102
|
-
body,
|
|
103
|
-
}).then(async (rs) => {
|
|
104
|
-
const response = await rs.text();
|
|
105
|
-
if (!response.startsWith('{')) {
|
|
106
|
-
throw new Error(`failed to submit simulation: ${response}`);
|
|
107
|
-
}
|
|
108
|
-
return JSON.parse(response) as { id: string };
|
|
109
|
-
});
|
|
110
|
-
return rs.id;
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
interface SimulationBuilderOptions {
|
|
114
|
-
apiEndpoint?: string;
|
|
115
|
-
stacksNodeAPI?: string;
|
|
116
|
-
network?: StacksNetworkName | string;
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
export class SimulationBuilder {
|
|
120
|
-
private apiEndpoint: string;
|
|
121
|
-
private stacksNodeAPI: string;
|
|
122
|
-
private network: StacksNetworkName | string;
|
|
123
|
-
|
|
124
|
-
private constructor(options: SimulationBuilderOptions = {}) {
|
|
125
|
-
this.apiEndpoint = options.apiEndpoint ?? 'https://api.stxer.xyz';
|
|
126
|
-
this.stacksNodeAPI = options.stacksNodeAPI ?? 'https://api.hiro.so';
|
|
127
|
-
this.network = options.network ?? 'mainnet';
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
public static new(options?: SimulationBuilderOptions) {
|
|
131
|
-
return new SimulationBuilder(options);
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
// biome-ignore lint/style/useNumberNamespace: <explanation>
|
|
135
|
-
private block = NaN;
|
|
136
|
-
private sender = '';
|
|
137
|
-
private steps: (
|
|
138
|
-
| {
|
|
139
|
-
// contract call
|
|
140
|
-
contract_id: string;
|
|
141
|
-
function_name: string;
|
|
142
|
-
function_args?: ClarityValue[];
|
|
143
|
-
sender: string;
|
|
144
|
-
fee: number;
|
|
145
|
-
}
|
|
146
|
-
| {
|
|
147
|
-
// contract deploy
|
|
148
|
-
contract_name: string;
|
|
149
|
-
source_code: string;
|
|
150
|
-
deployer: string;
|
|
151
|
-
fee: number;
|
|
152
|
-
clarity_version: ClarityVersion;
|
|
153
|
-
}
|
|
154
|
-
| {
|
|
155
|
-
// STX transfer
|
|
156
|
-
recipient: string;
|
|
157
|
-
amount: number;
|
|
158
|
-
sender: string;
|
|
159
|
-
fee: number;
|
|
160
|
-
}
|
|
161
|
-
| SimulationEval
|
|
162
|
-
)[] = [];
|
|
163
|
-
|
|
164
|
-
public useBlockHeight(block: number) {
|
|
165
|
-
this.block = block;
|
|
166
|
-
return this;
|
|
167
|
-
}
|
|
168
|
-
public withSender(address: string) {
|
|
169
|
-
this.sender = address;
|
|
170
|
-
return this;
|
|
171
|
-
}
|
|
172
|
-
public addSTXTransfer(params: {
|
|
173
|
-
recipient: string;
|
|
174
|
-
amount: number;
|
|
175
|
-
sender?: string;
|
|
176
|
-
fee?: number;
|
|
177
|
-
}) {
|
|
178
|
-
if (params.sender == null && this.sender === '') {
|
|
179
|
-
throw new Error(
|
|
180
|
-
'Please specify a sender with useSender or adding a sender paramenter'
|
|
181
|
-
);
|
|
182
|
-
}
|
|
183
|
-
this.steps.push({
|
|
184
|
-
...params,
|
|
185
|
-
sender: params.sender ?? this.sender,
|
|
186
|
-
fee: params.fee ?? 0,
|
|
187
|
-
});
|
|
188
|
-
return this;
|
|
189
|
-
}
|
|
190
|
-
public addContractCall(params: {
|
|
191
|
-
contract_id: string;
|
|
192
|
-
function_name: string;
|
|
193
|
-
function_args?: ClarityValue[];
|
|
194
|
-
sender?: string;
|
|
195
|
-
fee?: number;
|
|
196
|
-
}) {
|
|
197
|
-
if (params.sender == null && this.sender === '') {
|
|
198
|
-
throw new Error(
|
|
199
|
-
'Please specify a sender with useSender or adding a sender paramenter'
|
|
200
|
-
);
|
|
201
|
-
}
|
|
202
|
-
this.steps.push({
|
|
203
|
-
...params,
|
|
204
|
-
sender: params.sender ?? this.sender,
|
|
205
|
-
fee: params.fee ?? 0,
|
|
206
|
-
});
|
|
207
|
-
return this;
|
|
208
|
-
}
|
|
209
|
-
public addContractDeploy(params: {
|
|
210
|
-
contract_name: string;
|
|
211
|
-
source_code: string;
|
|
212
|
-
deployer?: string;
|
|
213
|
-
fee?: number;
|
|
214
|
-
clarity_version?: ClarityVersion;
|
|
215
|
-
}) {
|
|
216
|
-
if (params.deployer == null && this.sender === '') {
|
|
217
|
-
throw new Error(
|
|
218
|
-
'Please specify a deployer with useSender or adding a deployer paramenter'
|
|
219
|
-
);
|
|
220
|
-
}
|
|
221
|
-
this.steps.push({
|
|
222
|
-
...params,
|
|
223
|
-
deployer: params.deployer ?? this.sender,
|
|
224
|
-
fee: params.fee ?? 0,
|
|
225
|
-
clarity_version: params.clarity_version ?? ClarityVersion.Clarity3,
|
|
226
|
-
});
|
|
227
|
-
return this;
|
|
228
|
-
}
|
|
229
|
-
public addEvalCode(inside_contract_id: string, code: string) {
|
|
230
|
-
this.steps.push({
|
|
231
|
-
contract_id: inside_contract_id,
|
|
232
|
-
code,
|
|
233
|
-
});
|
|
234
|
-
return this;
|
|
235
|
-
}
|
|
236
|
-
public addMapRead(contract_id: string, map: string, key: string) {
|
|
237
|
-
this.steps.push({
|
|
238
|
-
contract_id,
|
|
239
|
-
code: `(map-get ${map} ${key})`,
|
|
240
|
-
});
|
|
241
|
-
return this;
|
|
242
|
-
}
|
|
243
|
-
public addVarRead(contract_id: string, variable: string) {
|
|
244
|
-
this.steps.push({
|
|
245
|
-
contract_id,
|
|
246
|
-
code: `(var-get ${variable})`,
|
|
247
|
-
});
|
|
248
|
-
return this;
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
private async getBlockInfo() {
|
|
252
|
-
if (Number.isNaN(this.block)) {
|
|
253
|
-
const { stacks_tip_height } = await getNodeInfo({
|
|
254
|
-
stacksEndpoint: this.stacksNodeAPI,
|
|
255
|
-
});
|
|
256
|
-
this.block = stacks_tip_height;
|
|
257
|
-
}
|
|
258
|
-
const info: Block = await richFetch(
|
|
259
|
-
`${this.stacksNodeAPI}/extended/v1/block/by_height/${this.block}?unanchored=true`
|
|
260
|
-
).then((r) => r.json());
|
|
261
|
-
if (
|
|
262
|
-
info.height !== this.block ||
|
|
263
|
-
typeof info.hash !== 'string' ||
|
|
264
|
-
!info.hash.startsWith('0x')
|
|
265
|
-
) {
|
|
266
|
-
throw new Error(
|
|
267
|
-
`failed to get block info for block height ${this.block}`
|
|
268
|
-
);
|
|
269
|
-
}
|
|
270
|
-
return {
|
|
271
|
-
block_height: this.block,
|
|
272
|
-
block_hash: info.hash.substring(2),
|
|
273
|
-
index_block_hash: info.index_block_hash.substring(2),
|
|
274
|
-
};
|
|
275
|
-
}
|
|
276
|
-
|
|
277
|
-
public async run() {
|
|
278
|
-
console.log(
|
|
279
|
-
`--------------------------------
|
|
280
|
-
This product can never exist without your support!
|
|
281
|
-
|
|
282
|
-
We receive sponsorship funds with:
|
|
283
|
-
SP212Y5JKN59YP3GYG07K3S8W5SSGE4KH6B5STXER
|
|
284
|
-
|
|
285
|
-
Feedbacks and feature requests are welcome.
|
|
286
|
-
To get in touch: contact@stxer.xyz
|
|
287
|
-
--------------------------------`
|
|
288
|
-
);
|
|
289
|
-
const block = await this.getBlockInfo();
|
|
290
|
-
console.log(
|
|
291
|
-
`Using block height ${block.block_height} hash 0x${block.block_hash} to run simulation.`
|
|
292
|
-
);
|
|
293
|
-
const txs: (StacksTransactionWire | SimulationEval)[] = [];
|
|
294
|
-
const nonce_by_address = new Map<string, number>();
|
|
295
|
-
const nextNonce = async (sender: string) => {
|
|
296
|
-
const nonce = nonce_by_address.get(sender);
|
|
297
|
-
if (nonce == null) {
|
|
298
|
-
const url = `${
|
|
299
|
-
this.stacksNodeAPI
|
|
300
|
-
}/v2/accounts/${sender}?proof=${false}&tip=${block.index_block_hash}`;
|
|
301
|
-
const account: AccountDataResponse = await richFetch(url).then((r) =>
|
|
302
|
-
r.json()
|
|
303
|
-
);
|
|
304
|
-
nonce_by_address.set(sender, account.nonce + 1);
|
|
305
|
-
return account.nonce;
|
|
306
|
-
}
|
|
307
|
-
nonce_by_address.set(sender, nonce + 1);
|
|
308
|
-
return nonce;
|
|
309
|
-
};
|
|
310
|
-
for (const step of this.steps) {
|
|
311
|
-
let network = this.network === 'mainnet' ? STACKS_MAINNET : STACKS_TESTNET;
|
|
312
|
-
if (this.stacksNodeAPI) {
|
|
313
|
-
network = {
|
|
314
|
-
...network,
|
|
315
|
-
client: {
|
|
316
|
-
...network.client,
|
|
317
|
-
baseUrl: this.stacksNodeAPI,
|
|
318
|
-
},
|
|
319
|
-
};
|
|
320
|
-
}
|
|
321
|
-
if ('sender' in step && 'function_name' in step) {
|
|
322
|
-
const nonce = await nextNonce(step.sender);
|
|
323
|
-
const [contractAddress, contractName] = step.contract_id.split('.');
|
|
324
|
-
const tx = await makeUnsignedContractCall({
|
|
325
|
-
contractAddress,
|
|
326
|
-
contractName,
|
|
327
|
-
functionName: step.function_name,
|
|
328
|
-
functionArgs: step.function_args ?? [],
|
|
329
|
-
nonce,
|
|
330
|
-
network,
|
|
331
|
-
publicKey: '',
|
|
332
|
-
postConditionMode: PostConditionMode.Allow,
|
|
333
|
-
fee: step.fee,
|
|
334
|
-
});
|
|
335
|
-
tx.auth.spendingCondition.signer = c32addressDecode(step.sender)[1];
|
|
336
|
-
txs.push(tx);
|
|
337
|
-
} else if ('sender' in step && 'recipient' in step) {
|
|
338
|
-
const nonce = await nextNonce(step.sender);
|
|
339
|
-
const tx = await makeUnsignedSTXTokenTransfer({
|
|
340
|
-
recipient: step.recipient,
|
|
341
|
-
amount: step.amount,
|
|
342
|
-
nonce,
|
|
343
|
-
network,
|
|
344
|
-
publicKey: '',
|
|
345
|
-
fee: step.fee,
|
|
346
|
-
});
|
|
347
|
-
tx.auth.spendingCondition.signer = c32addressDecode(step.sender)[1];
|
|
348
|
-
txs.push(tx);
|
|
349
|
-
} else if ('deployer' in step) {
|
|
350
|
-
const nonce = await nextNonce(step.deployer);
|
|
351
|
-
const tx = await makeUnsignedContractDeploy({
|
|
352
|
-
contractName: step.contract_name,
|
|
353
|
-
codeBody: step.source_code,
|
|
354
|
-
nonce,
|
|
355
|
-
network,
|
|
356
|
-
publicKey: '',
|
|
357
|
-
postConditionMode: PostConditionMode.Allow,
|
|
358
|
-
fee: step.fee,
|
|
359
|
-
});
|
|
360
|
-
tx.auth.spendingCondition.signer = c32addressDecode(step.deployer)[1];
|
|
361
|
-
txs.push(tx);
|
|
362
|
-
} else if ('code' in step) {
|
|
363
|
-
txs.push(step);
|
|
364
|
-
} else {
|
|
365
|
-
// biome-ignore lint/style/noUnusedTemplateLiteral: <explanation>
|
|
366
|
-
console.log(`Invalid simulation step:`, step);
|
|
367
|
-
}
|
|
368
|
-
}
|
|
369
|
-
const id = await runSimulation(
|
|
370
|
-
`${this.apiEndpoint}/simulations`,
|
|
371
|
-
block.block_hash,
|
|
372
|
-
block.block_height,
|
|
373
|
-
txs
|
|
374
|
-
);
|
|
375
|
-
console.log(
|
|
376
|
-
`Simulation will be available at: https://stxer.xyz/simulations/${this.network}/${id}`
|
|
377
|
-
);
|
|
378
|
-
return id;
|
|
379
|
-
}
|
|
380
|
-
}
|
|
1
|
+
export * from './batch-api';
|
|
2
|
+
export * from './simulation';
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import {
|
|
2
|
+
contractPrincipalCV,
|
|
3
|
+
principalCV,
|
|
4
|
+
stringAsciiCV,
|
|
5
|
+
tupleCV,
|
|
6
|
+
uintCV,
|
|
7
|
+
} from '@stacks/transactions';
|
|
8
|
+
import { batchRead, batchReadonly } from '../batch-api';
|
|
9
|
+
|
|
10
|
+
async function batchReadsExample() {
|
|
11
|
+
const rs = await batchRead({
|
|
12
|
+
// index_block_hash:
|
|
13
|
+
// 'ce04817b9c6d90814ff9c06228d3a07d64335b1d9b01a233456fc304e34f7c0e', // block 373499
|
|
14
|
+
variables: [
|
|
15
|
+
{
|
|
16
|
+
contract: contractPrincipalCV(
|
|
17
|
+
'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275',
|
|
18
|
+
'liquidity-token-v5kbe3oqvac'
|
|
19
|
+
),
|
|
20
|
+
variableName: 'balance-x',
|
|
21
|
+
},
|
|
22
|
+
{
|
|
23
|
+
contract: contractPrincipalCV(
|
|
24
|
+
'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275',
|
|
25
|
+
'liquidity-token-v5kbe3oqvac'
|
|
26
|
+
),
|
|
27
|
+
variableName: 'balance-y',
|
|
28
|
+
},
|
|
29
|
+
{
|
|
30
|
+
contract: contractPrincipalCV(
|
|
31
|
+
'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275',
|
|
32
|
+
'liquidity-token-v5kbe3oqvac'
|
|
33
|
+
),
|
|
34
|
+
variableName: 'something-not-exists',
|
|
35
|
+
},
|
|
36
|
+
],
|
|
37
|
+
maps: [
|
|
38
|
+
{
|
|
39
|
+
contract: contractPrincipalCV(
|
|
40
|
+
'SP102V8P0F7JX67ARQ77WEA3D3CFB5XW39REDT0AM',
|
|
41
|
+
'amm-registry-v2-01'
|
|
42
|
+
),
|
|
43
|
+
mapName: 'pools-data-map',
|
|
44
|
+
mapKey: tupleCV({
|
|
45
|
+
'token-x': principalCV(
|
|
46
|
+
'SP102V8P0F7JX67ARQ77WEA3D3CFB5XW39REDT0AM.token-wstx-v2'
|
|
47
|
+
),
|
|
48
|
+
'token-y': principalCV(
|
|
49
|
+
'SP102V8P0F7JX67ARQ77WEA3D3CFB5XW39REDT0AM.token-alex'
|
|
50
|
+
),
|
|
51
|
+
factor: uintCV(1e8),
|
|
52
|
+
}),
|
|
53
|
+
},
|
|
54
|
+
{
|
|
55
|
+
contract: contractPrincipalCV(
|
|
56
|
+
'SP1Y5YSTAHZ88XYK1VPDH24GY0HPX5J4JECTMY4A1',
|
|
57
|
+
'univ2-core'
|
|
58
|
+
),
|
|
59
|
+
mapName: 'pools',
|
|
60
|
+
mapKey: uintCV(1),
|
|
61
|
+
},
|
|
62
|
+
{
|
|
63
|
+
contract: contractPrincipalCV(
|
|
64
|
+
'SP1Y5YSTAHZ88XYK1VPDH24GY0HPX5J4JECTMY4A1',
|
|
65
|
+
'contract-not-exists'
|
|
66
|
+
),
|
|
67
|
+
mapName: 'pools',
|
|
68
|
+
mapKey: uintCV(1),
|
|
69
|
+
},
|
|
70
|
+
],
|
|
71
|
+
});
|
|
72
|
+
console.log(rs);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
async function batchReadonlyExample() {
|
|
76
|
+
const rs = await batchReadonly({
|
|
77
|
+
index_block_hash:
|
|
78
|
+
'ce04817b9c6d90814ff9c06228d3a07d64335b1d9b01a233456fc304e34f7c0e',
|
|
79
|
+
readonly: [
|
|
80
|
+
{
|
|
81
|
+
contract: contractPrincipalCV(
|
|
82
|
+
'SM3VDXK3WZZSA84XXFKAFAF15NNZX32CTSG82JFQ4',
|
|
83
|
+
'sbtc-token'
|
|
84
|
+
),
|
|
85
|
+
functionName: 'get-total-supply',
|
|
86
|
+
functionArgs: [],
|
|
87
|
+
},
|
|
88
|
+
{
|
|
89
|
+
contract: contractPrincipalCV(
|
|
90
|
+
'SM3VDXK3WZZSA84XXFKAFAF15NNZX32CTSG82JFQ4',
|
|
91
|
+
'sbtc-token'
|
|
92
|
+
),
|
|
93
|
+
functionName: 'get-balance',
|
|
94
|
+
functionArgs: [
|
|
95
|
+
principalCV('SP1CT7J2RWBZD62QAX36A2PQ3HKH5NFDGVHB8J34V'),
|
|
96
|
+
],
|
|
97
|
+
},
|
|
98
|
+
{
|
|
99
|
+
contract: contractPrincipalCV(
|
|
100
|
+
'SM3VDXK3WZZSA84XXFKAFAF15NNZX32CTSG82JFQ4',
|
|
101
|
+
'sbtc-token'
|
|
102
|
+
),
|
|
103
|
+
functionName: 'function-not-exists',
|
|
104
|
+
functionArgs: [],
|
|
105
|
+
},
|
|
106
|
+
{
|
|
107
|
+
contract: contractPrincipalCV(
|
|
108
|
+
'SM3VDXK3WZZSA84XXFKAFAF15NNZX32CTSG82JFQ4',
|
|
109
|
+
'sbtc-token'
|
|
110
|
+
),
|
|
111
|
+
functionName: 'get-balance',
|
|
112
|
+
functionArgs: [stringAsciiCV('invalid-args')],
|
|
113
|
+
},
|
|
114
|
+
],
|
|
115
|
+
});
|
|
116
|
+
console.log(rs);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
async function main() {
|
|
120
|
+
await batchReadsExample();
|
|
121
|
+
await batchReadonlyExample();
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
main().catch(console.error);
|
|
125
|
+
|