|
| 1 | +// Copyright 2025 IOTA Stiftung |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +import { EpochInfo, IotaTransactionBlockResponse } from "@iota/iota-sdk/client"; |
| 5 | +import { NotarizationClient, State } from "@iota/notarization/node"; |
| 6 | +import { getFundedClient } from "../util"; |
| 7 | + |
| 8 | +const STATE_METADATA: string | null = null; // "State metadata example"; |
| 9 | +const IMMUTABLE_DESCRIPTION: string | null = null; // "This metadata will not change"; |
| 10 | + |
| 11 | +let REFERENCE_GAS_PRICE: bigint | null = null; |
| 12 | + |
| 13 | +const BILLION = 1000000000; |
| 14 | +const MINIMUM_STORAGE_COST = 0.0029488; // Unit is IOTA not Nanos |
| 15 | + |
| 16 | +function print_gas_cost(transaction_type: String, flexDataSize: number, response: IotaTransactionBlockResponse) { |
| 17 | + const gasUsed = response.effects?.gasUsed; |
| 18 | + const referenceGasPrice = REFERENCE_GAS_PRICE ? Number(REFERENCE_GAS_PRICE) : -1; // Fallback to -1 if EpochInfo is not available |
| 19 | + |
| 20 | + if (gasUsed != undefined) { |
| 21 | + const totalGasCost = parseInt(gasUsed.computationCost) + parseInt(gasUsed.storageCost) |
| 22 | + - parseInt(gasUsed.storageRebate); |
| 23 | + const storageCost = parseInt(gasUsed.storageCost) / BILLION; |
| 24 | + const computationCostNanos = parseInt(gasUsed.computationCost); |
| 25 | + const storageCostAboveMin = storageCost - MINIMUM_STORAGE_COST; |
| 26 | + console.log( |
| 27 | + "-------------------------------------------------------------------------------------------------------", |
| 28 | + ); |
| 29 | + console.log(`--- Gas cost for '${transaction_type}' transaction`); |
| 30 | + console.log( |
| 31 | + "-------------------------------------------------------------------------------------------------------", |
| 32 | + ); |
| 33 | + console.log(`referenceGasPrice: ${referenceGasPrice}`); |
| 34 | + console.log(`computationCost: ${computationCostNanos / BILLION}`); |
| 35 | + console.log(`Computation Units: ${computationCostNanos / referenceGasPrice}`); |
| 36 | + console.log(`storageCost: ${storageCost}`); |
| 37 | + console.log(`flexDataSize: ${flexDataSize}`); |
| 38 | + console.log(`storageCost above minimum (0.0029488): ${storageCostAboveMin}`); |
| 39 | + console.log(`storageCostAboveMin per flexDataSize: ${storageCostAboveMin / (flexDataSize - 1)}`); |
| 40 | + console.log(`storageRebate: ${parseInt(gasUsed.storageRebate) / BILLION}`); |
| 41 | + console.log(`totalGasCost (calculated): ${totalGasCost / BILLION}`); |
| 42 | + console.log( |
| 43 | + "-------------------------------------------------------------------------------------------------------", |
| 44 | + ); |
| 45 | + } else { |
| 46 | + console.log("Gas used information is not available."); |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +function randomString(length = 50) { |
| 51 | + return [...Array(length + 10)].map((value) => (Math.random() * 1000000).toString(36).replace(".", "")).join("") |
| 52 | + .substring(0, length); |
| 53 | +} |
| 54 | + |
| 55 | +async function create_dynamic_notarization( |
| 56 | + notarizationClient: NotarizationClient, |
| 57 | + stateDataSize: number, |
| 58 | +): Promise<{ notarization: any; response: IotaTransactionBlockResponse }> { |
| 59 | + console.log(`Creating a dynamic notarization for state updates with ${stateDataSize} bytes of state data`); |
| 60 | + |
| 61 | + let stateData = randomString(stateDataSize); |
| 62 | + |
| 63 | + const { output: notarization, response: response } = await notarizationClient |
| 64 | + .createDynamic() |
| 65 | + .withStringState(stateData, STATE_METADATA) |
| 66 | + .withImmutableDescription(IMMUTABLE_DESCRIPTION) |
| 67 | + .finish() |
| 68 | + .buildAndExecute(notarizationClient); |
| 69 | + |
| 70 | + console.log("✅ Created dynamic notarization:", notarization.id); |
| 71 | + const flexDataSize = stateData.length + (STATE_METADATA ? STATE_METADATA.length : 0) |
| 72 | + + (IMMUTABLE_DESCRIPTION ? IMMUTABLE_DESCRIPTION.length : 0); |
| 73 | + print_gas_cost("Create", flexDataSize, response); |
| 74 | + |
| 75 | + return { notarization, response }; |
| 76 | +} |
| 77 | + |
| 78 | +/** Create, update and destroy a Dynamic Notarization to estimate gas cost */ |
| 79 | +export async function createUpdateDestroy(): Promise<void> { |
| 80 | + console.log("Create, update and destroy a Dynamic Notarization to estimate gas cost"); |
| 81 | + |
| 82 | + const notarizationClient = await getFundedClient(); |
| 83 | + |
| 84 | + const iotaClient = notarizationClient.iotaClient(); |
| 85 | + REFERENCE_GAS_PRICE = await iotaClient.getReferenceGasPrice(); |
| 86 | + console.log( |
| 87 | + "Successfully fetched the referenceGasPrice: ", |
| 88 | + REFERENCE_GAS_PRICE != null ? REFERENCE_GAS_PRICE : "Not Available", |
| 89 | + ); |
| 90 | + |
| 91 | + let notarization; |
| 92 | + |
| 93 | + // Create several dynamic notarizations with different initial state sizes. The notarization with the largest state size will be used for updates. |
| 94 | + console.log("\n🆕 Creating dynamic notarizations with different initial state sizes..."); |
| 95 | + for (let i = 1; i <= 4; i++) { |
| 96 | + const result = await create_dynamic_notarization(notarizationClient, 10 * i * i); // 10, 40, 90, 160 bytes |
| 97 | + notarization = result.notarization; |
| 98 | + } |
| 99 | + |
| 100 | + // Perform multiple state updates |
| 101 | + console.log("\n🔄 Performing state updates..."); |
| 102 | + |
| 103 | + for (let i = 1; i <= 3; i++) { |
| 104 | + console.log(`\n--- Update ${i} ---`); |
| 105 | + |
| 106 | + // Create new state with updated content and metadata |
| 107 | + const newContent = randomString(i * 50); // Set this size to 138 bytes to keep total flex data size equal to the latest created notarization |
| 108 | + const newMetadata = `Version ${i + 1}.0 - Update ${i}`; |
| 109 | + |
| 110 | + // Update the state |
| 111 | + const { output: _, response: response } = await notarizationClient |
| 112 | + .updateState( |
| 113 | + State.fromString(newContent, newMetadata), |
| 114 | + notarization.id, |
| 115 | + ) |
| 116 | + .buildAndExecute(notarizationClient); |
| 117 | + |
| 118 | + console.log(`✅ State update ${i} completed`); |
| 119 | + const flexDataSize = newContent.length + newMetadata.length; |
| 120 | + print_gas_cost("Update", flexDataSize, response); |
| 121 | + } |
| 122 | + |
| 123 | + // Destroy the dynamic notarization |
| 124 | + try { |
| 125 | + const { output: _, response: response } = await notarizationClient |
| 126 | + .destroy(notarization.id) |
| 127 | + .buildAndExecute(notarizationClient); |
| 128 | + console.log("✅ Successfully destroyed unlocked dynamic notarization"); |
| 129 | + print_gas_cost("Destroy", 1, response); |
| 130 | + } catch (e) { |
| 131 | + console.log("❌ Failed to destroy:", e); |
| 132 | + } |
| 133 | +} |
0 commit comments