-
Notifications
You must be signed in to change notification settings - Fork 0
/
page.tsx
543 lines (459 loc) · 18 KB
/
page.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
"use client";
import { useEffect, useState, useCallback } from "react";
import {
Text,
Card,
Alert,
Button,
Icon,
CopyText,
} from "@stellar/design-system";
import { useLatestTxn } from "@/query/useLatestTxn";
import * as StellarXdr from "@/helpers/StellarXdr";
import { Box } from "@/components/layout/Box";
import { XdrPicker } from "@/components/FormElements/XdrPicker";
import { PrettyJson } from "@/components/PrettyJson";
import { XdrTypeSelect } from "@/components/XdrTypeSelect2";
import { useIsXdrInit } from "@/hooks/useIsXdrInit";
import { useStore } from "@/store/useStore";
import * as StellarSDK from '@stellar/stellar-sdk';
import {computeBandwidthFee, computeEventsOrReturnValueFee, computeHistoricalFee, computeInstructionFee, computeReadBytesFee, computeReadEntriesFee, computeWriteBytesFee, computeWriteEntriesFee } from "../../estimate/fees/components/Params";
import { computeRentFee } from "../../estimate/fees/components/Rent";
const MIN_TEMP_TTL = 17280
const MIN_PERSIST_TTL = 2073600
interface ContractCosts {
cpu_insns: number;
mem_bytes: number;
entry_reads: number;
entry_writes: number;
read_bytes: number;
write_bytes: number;
events_and_return_bytes: number;
txn_size: number;
current_ledger: number;
ledger_changes: LedgerEntryRentChange[];
resource_fee_in_xlm: number;
}
class LedgerEntryRentChange {
entryType: string;
isPersistent: boolean;
oldSizeBytes: number;
newSizeBytes: number;
oldLiveUntilLedger: number;
newLiveUntilLedger: number;
constructor(
entryType: string,
isPersistent: boolean,
oldSizeBytes: number,
newSizeBytes: number,
oldLiveUntilLedger: number,
newLiveUntilLedger: number
) {
this.entryType = entryType;
// Whether this is persistent or temporary entry.
this.isPersistent = isPersistent;
// Size of the entry in bytes before it has been modified, including the key.
// 0 for newly-created entries.
this.oldSizeBytes = oldSizeBytes;
// Size of the entry in bytes after it has been modified, including the key.
this.newSizeBytes = newSizeBytes;
// Live until ledger of the entry before it has been modified.
// Should be less than the current ledger for newly-created entries.
this.oldLiveUntilLedger = oldLiveUntilLedger;
// Live until ledger of the entry after it has been modified.
this.newLiveUntilLedger = newLiveUntilLedger;
}
}
async function sorobill(sim: any, tx_xdr: any) {
const events = sim.result.events.map((e: any) => {
const buffer = Buffer.from(e, 'base64');
let parsedEvent = StellarSDK.xdr.DiagnosticEvent.fromXDR(buffer);
if (parsedEvent.event().type().name !== 'contract')
return 0;
return parsedEvent.event().toXDR().length;
});
// const returnValueSize = sim.result.results[0]?.xdr.length ?? 0;
// console.log("Simulate: Return Value Size", returnValueSize);
/// The return value is also considered as an event in stellar-core terms, confusing huh, but the truth
/// It seems to be the case, that if the smart contract function returns nothing
/// It still returns a empty ScVal type I guess, which occupies some 8 bytes
//? I am not sure if the 8 bytes is stored in the tx on the ledger or no
const events_and_return_bytes = (
events.reduce(
(accumulator: any, currentValue: any) => accumulator + currentValue, 0 // Initialize accumulator with 0
) + (sim.result.results[0] ? sim.result.results[0].xdr.length : 0) // Return value size
);
const sorobanTransactionData = StellarSDK.xdr.SorobanTransactionData.fromXDR(sim.result.transactionData, 'base64');
const resources = sorobanTransactionData.resources();
const stroopValue = sorobanTransactionData.resourceFee().toString()
let xlmValue = Number(stroopValue) * 10**(-7);
xlmValue = Number(xlmValue.toFixed(7));
// const rwro = [
// sorobanTransactionData.resources().footprint().readWrite()
// .flatMap((rw) => rw.toXDR().length),
// sorobanTransactionData.resources().footprint().readOnly()
// .flatMap((ro) => ro.toXDR().length)
// ].flat();
const metrics = {
mem_byte: Number(sim.result.cost.memBytes),
cpu_insn: Number(sim.result.cost.cpuInsns)
};
let arr: LedgerEntryRentChange[] = [];
let latestLedger = sim.result.latestLedger;
//@ts-ignore
sim.result.stateChanges.forEach(entry => {
// console.log(entry)
let beforeSize = 0;
let afterSize = 0;
let isPersistent = false;
let lastModifiedLedger = 0;
let oldLiveUntilLedger = 0;
let newLiveUntilLedger = 0;
let entryType = "";
if (entry.type == "created") {
entryType = entry.type;
let afterEntry = StellarSDK.xdr.LedgerEntry.fromXDR(entry.after, 'base64');
let liveUntilLedgerSeq;
try {
liveUntilLedgerSeq = afterEntry.data().ttl().liveUntilLedgerSeq();
} catch (error) {
if (error instanceof TypeError) {
// ttl is not present
console.log("TTL is not present for this entry");
// You might want to set a default value or handle this case differently
liveUntilLedgerSeq = null; // or some default value
} else {
// If it's not a TypeError, rethrow the error
throw error;
}
}
if (afterEntry.data().contractData().durability().name == "temporary") {
isPersistent = false;
if (liveUntilLedgerSeq !== null) {
oldLiveUntilLedger = 0
newLiveUntilLedger = afterEntry.data().ttl().liveUntilLedgerSeq()
} else {
oldLiveUntilLedger = 0
newLiveUntilLedger = latestLedger + MIN_TEMP_TTL
}
} else if (afterEntry.data().contractData().durability().name == "persistent") {
isPersistent = true
if (liveUntilLedgerSeq !== null) {
oldLiveUntilLedger = 0
newLiveUntilLedger = afterEntry.data().ttl().liveUntilLedgerSeq();
} else {
oldLiveUntilLedger = 0
newLiveUntilLedger = latestLedger + MIN_PERSIST_TTL
}
}
beforeSize = 0;
afterSize = entry.after.length;
} else if (entry.type == "updated") {
entryType = entry.type;
let beforeEntry = StellarSDK.xdr.LedgerEntry.fromXDR(entry.before, 'base64');
let afterEntry = StellarSDK.xdr.LedgerEntry.fromXDR(entry.after, 'base64');
// if (afterEntry.data().contractData()) {
// return;
// }
let afterLiveUntilLedgerSeq;
try {
afterLiveUntilLedgerSeq = afterEntry.data().ttl().liveUntilLedgerSeq();
} catch (error) {
if (error instanceof TypeError) {
// ttl is not present
console.log("TTL is not present for this entry");
// You might want to set a default value or handle this case differently
afterLiveUntilLedgerSeq = null; // or some default value
} else {
// If it's not a TypeError, rethrow the error
throw error;
}
}
let beforeLiveUntilLedgerSeq;
try {
beforeLiveUntilLedgerSeq = beforeEntry.data().ttl().liveUntilLedgerSeq();
} catch (error) {
if (error instanceof TypeError) {
// ttl is not present
console.log("TTL is not present for this entry");
// You might want to set a default value or handle this case differently
beforeLiveUntilLedgerSeq = null; // or some default value
} else {
// If it's not a TypeError, rethrow the error
throw error;
}
}
if (beforeEntry.data().contractData().durability().name == "temporary") {
isPersistent = false;
if (beforeLiveUntilLedgerSeq !== null) {
oldLiveUntilLedger = entry.data().ttl().liveUntilLedgerSeq();
} else {
if (lastModifiedLedger == 0) {
oldLiveUntilLedger = 0
} else {
oldLiveUntilLedger = lastModifiedLedger + MIN_TEMP_TTL
}
}
if (afterLiveUntilLedgerSeq !== null) {
newLiveUntilLedger = afterEntry.data().ttl().liveUntilLedgerSeq()
} else {
newLiveUntilLedger = latestLedger + MIN_TEMP_TTL
}
} else if (beforeEntry.data().contractData().durability().name == "persistent") {
isPersistent = true
if (beforeLiveUntilLedgerSeq !== null) {
oldLiveUntilLedger = beforeEntry.data().ttl().liveUntilLedgerSeq()
} else {
if (lastModifiedLedger == 0) {
oldLiveUntilLedger = 0
} else {
oldLiveUntilLedger = lastModifiedLedger + MIN_PERSIST_TTL
}
}
if (afterLiveUntilLedgerSeq !== null) {
newLiveUntilLedger = afterEntry.data().ttl().liveUntilLedgerSeq();
} else {
newLiveUntilLedger = latestLedger + MIN_PERSIST_TTL
}
}
beforeSize = entry.before.length;
afterSize = entry.after.length;
} else if (entry.type == "deleted") {
// Do nothing I guess
// TODO: Deleted Entries
// TODO: Check if deleted entries
return;
}
arr.push(new LedgerEntryRentChange(
entryType, // Type of Entry
isPersistent, // isPersistent (temporary)
beforeSize, // oldSizeBytes
afterSize, // newSizeBytes (no change)
oldLiveUntilLedger, // oldLiveUntilLedger
newLiveUntilLedger // newLiveUntilLedger
))
});
const stats: ContractCosts = {
cpu_insns: metrics.cpu_insn,
mem_bytes: metrics.mem_byte,
entry_reads: resources.footprint().readOnly().length + resources.footprint().readWrite().length,
entry_writes: resources.footprint().readWrite().length,
read_bytes: resources.readBytes(),
write_bytes: resources.writeBytes(),
txn_size: tx_xdr.length,
events_and_return_bytes,
current_ledger: latestLedger,
ledger_changes: arr,
resource_fee_in_xlm: xlmValue,
};
return stats;
}
export default function ViewXdr() {
const { xdr, network } = useStore();
const { updateXdrBlob, updateXdrType, resetXdr } = xdr;
const [contractCosts, setContractCosts] = useState<ContractCosts | null>(null);
const [isSimulating, setIsSimulating] = useState(false);
const [totalEstimatedFee, setTotalEstimatedFee] = useState<string | null>(null);
const isXdrInit = useIsXdrInit();
const {
data: latestTxn,
error: latestTxnError,
isSuccess: isLatestTxnSuccess,
isFetching: isLatestTxnFetching,
isLoading: isLatestTxnLoading,
} = useLatestTxn(network.horizonUrl);
useEffect(() => {
if (isLatestTxnSuccess && latestTxn) {
updateXdrBlob(latestTxn);
updateXdrType("TransactionEnvelope");
}
}, [isLatestTxnSuccess, latestTxn, updateXdrBlob, updateXdrType]);
const isFetchingLatestTxn = isLatestTxnFetching || isLatestTxnLoading;
const xdrDecodeJson = useCallback(() => {
if (!(isXdrInit && xdr.blob && xdr.type)) {
return null;
}
try {
const xdrJson = StellarXdr.decode(xdr.type, xdr.blob);
return {
jsonString: xdrJson,
error: "",
};
} catch (e) {
return {
jsonString: "",
error: `Unable to decode input as ${xdr.type}: ${e}`,
};
}
}, [isXdrInit, xdr.blob, xdr.type]);
const simulateTransaction = useCallback(async () => {
if (!xdr.blob || isSimulating) return;
setIsSimulating(true);
let requestBody = {
"jsonrpc": "2.0",
"id": 8675309,
"method": "simulateTransaction",
"params": {
"transaction": xdr.blob,
"resourceConfig": {
"instructionLeeway": 0 // stellar.expert does the same
}
}
};
try {
let res = await fetch(network.rpcUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(requestBody),
});
if (!res.ok) {
throw new Error(`HTTP error! status: ${res.status}`);
}
let simulateResponse = await res.json();
// console.log("fool ", simulateResponse)
let sorocosts = await sorobill(simulateResponse, xdr.blob);
const instructionFee = computeInstructionFee(sorocosts.cpu_insns.toString());
const readEntriesFee = computeReadEntriesFee(sorocosts.entry_reads.toString());
const writeEntriesFee = computeWriteEntriesFee(sorocosts.entry_writes.toString());
const readBytesFee = computeReadBytesFee(sorocosts.read_bytes.toString());
const writeBytesFee = computeWriteBytesFee(sorocosts.write_bytes.toString());
const historicalFee = computeHistoricalFee(sorocosts.txn_size.toString());
const bandwidthFee = computeBandwidthFee(sorocosts.txn_size.toString());
const eventsFee = computeEventsOrReturnValueFee(sorocosts.events_and_return_bytes.toString());
let newTotalRentFee = Number(computeRentFee(sorocosts.ledger_changes, sorocosts.current_ledger)) / 10000000;
// let newTotalRentFeeNum = Number(Number(newTotalRentFee).toFixed(7));
console.log("Rent Fee ", newTotalRentFee);
let totalEstimatedFee = Number((instructionFee + readEntriesFee + writeEntriesFee + readBytesFee +
writeBytesFee + historicalFee + bandwidthFee + eventsFee ) / 10000000);
totalEstimatedFee = Number(Number(totalEstimatedFee + newTotalRentFee).toFixed(7));
// const feeInXLM = totalFee / 10000000;
console.log("Total Estimated Fee", totalEstimatedFee)
setContractCosts(sorocosts);
const server = new StellarSDK.SorobanRpc.Server('https://soroban-testnet.stellar.org:443');
let inclusionFee = await server.getFeeStats();
let inclusionFeeMaxNum = Number(inclusionFee.sorobanInclusionFee.max) ;
let totalFee = (Number(totalEstimatedFee + inclusionFeeMaxNum * 10**(-7))).toString();
setTotalEstimatedFee(totalFee);
} catch (error) {
console.error("Error simulating transaction:", error);
} finally {
setIsSimulating(false);
}
}, [xdr.blob]);
useEffect(() => {
simulateTransaction();
}, [simulateTransaction]);
const xdrJsonDecoded = xdrDecodeJson();
const prettifyJsonString = (jsonString: string) => {
try {
const parsedJson = JSON.parse(jsonString);
return JSON.stringify(parsedJson, null, 2);
} catch (e) {
return jsonString;
}
};
const combinedJson = useCallback(() => {
const contractCostInside: ContractCosts = contractCosts || {
cpu_insns: 0,
mem_bytes: 0,
entry_reads: 0,
entry_writes: 0,
read_bytes: 0,
write_bytes: 0,
events_and_return_bytes: 0,
txn_size: 0,
current_ledger: 0,
ledger_changes: [],
resource_fee_in_xlm: 0
};
const readableJson = {
"Contract Costs": {
"CPU Instructions": contractCostInside.cpu_insns,
"Number of ledger entries read": contractCostInside.entry_reads,
"Number of ledger entries written": contractCostInside.entry_writes,
"Number of bytes read": contractCostInside.read_bytes,
"Number of bytes written": contractCostInside.write_bytes,
"Transaction size (bytes)": contractCostInside.txn_size,
"Events/return value size (bytes)": contractCostInside.events_and_return_bytes,
"Current Ledger": contractCostInside.current_ledger,
"Ledger entry changes": contractCostInside.ledger_changes,
},
"Fees": {
"Estimated Total fee (XLM)": Number(Number(totalEstimatedFee).toFixed(7)),
// "Max Estimated Fee (XLM)": totalEstimatedFee !== null ? totalEstimatedFee : "Not available"
}
};
return readableJson;
}, [contractCosts, totalEstimatedFee]);
return (
<Box gap="md">
<div className="PageHeader">
<Text size="md" as="h1" weight="medium">
Check resource estimates and fees by simulating transactions
</Text>
</div>
<Card>
<Box gap="lg">
<XdrPicker
id="view-xdr-blob"
label="Transaction XDR"
value={xdr.blob}
hasCopyButton
note="Input a base-64 encoded unsigned / signed XDR blob of a transaction"
onChange={(e) => {
updateXdrBlob(e.target.value);
}}
error={latestTxnError?.toString()}
disabled={isFetchingLatestTxn}
/>
<XdrTypeSelect error={xdrJsonDecoded?.error} />
<Box gap="lg" direction="row" align="center" justify="end">
<Button
size="md"
variant="error"
icon={<Icon.RefreshCw01 />}
onClick={() => {
resetXdr();
setContractCosts(null);
setTotalEstimatedFee(null);
}}
disabled={!xdr.blob}
>
Clear XDR
</Button>
</Box>
{combinedJson() && (
<Box gap="lg">
<div className="PageBody__content PageBody__scrollable">
<PrettyJson json={combinedJson()} />
</div>
<Box gap="md" direction="row" justify="end">
<CopyText
textToCopy={prettifyJsonString(JSON.stringify(combinedJson()))}
>
<Button
size="md"
variant="tertiary"
icon={<Icon.Copy01 />}
iconPosition="left"
>
Copy JSON
</Button>
</CopyText>
</Box>
</Box>
)}
</Box>
</Card>
<Alert variant="primary" placement="inline">
The fee simulation shows the estimated resource usage, including CPU instructions, memory usage, ledger entry accesses, ledger I/O operations, transaction size, events, and return value size, which directly affect the transaction fees, while the overall fee is shown by adding the resource fees and current inclusion fees.
</Alert>
<Alert variant="primary" placement="inline">
Note that while simulation provides a good estimate,
actual execution may vary slightly due to network conditions or changes in the ledger state between simulation and execution.
</Alert>
</Box>
);
}