Skip to content

Latest commit

 

History

History
217 lines (156 loc) · 7.7 KB

File metadata and controls

217 lines (156 loc) · 7.7 KB

Simulator

Table of Contents


Overview

The Simulator class implements the ISimulator interface and provides methods for simulating cross-chain transactions and estimating execution fees. It offers functionality to simulate individual transactions or batches of transactions for fee estimation and validation purposes.

The simulator performs TAC-side simulation to estimate gas costs, validate transaction logic, and calculate the required fees for successful cross-chain execution.


Getting Simulator Instance

There are two ways to obtain a simulator instance for transaction simulation. Using the SDK approach is strongly recommended as it handles all the configuration and dependencies automatically.

Through SDK (Recommended)

The recommended approach is to use the TacSdk which creates and manages the simulator internally with proper configuration:

import { TacSdk, Network } from "@tonappchain/sdk";
import { TonApiClient } from '@ton-api/client';
import { getHttpV4Endpoint } from '@orbs-network/ton-access';

// Create SDK instance with simulator
const sdk = await TacSdk.create({
    network: Network.TESTNET, // or Network.MAINNET
});

// Use simulation methods directly on SDK
const simulationResult = await sdk.getSimulationInfo(evmProxyMsg, sender, assets, options);
const batchResults = await sdk.simulateTransactions(sender, transactions);

Direct Instantiation

You can also create a simulator instance directly, but this requires manual setup of all dependencies:

import { 
    Simulator, 
    Configuration, 
    OperationTracker, 
    ConsoleLogger,
    Network 
} from "@tonappchain/sdk";
import { TonApiClient } from '@ton-api/client';
import { getHttpV4Endpoint } from '@orbs-network/ton-access';

// Create configuration
const config = await Configuration.create(
    Network.TESTNET,
    artifacts, // Network artifacts
    tonParams, // TON parameters
    tacParams, // TAC parameters
    customEndpoints, // Optional custom endpoints
    delay // Optional delay
);

// Create operation tracker
const operationTracker = new OperationTracker(Network.TESTNET, config.liteSequencerEndpoints);

// Create logger (optional)
const logger = new ConsoleLogger();

// Create simulator instance
const simulator = new Simulator(config, operationTracker, logger);

// Use simulator methods
const simulationResult = await simulator.getSimulationInfo(sender, crosschainTx);
const batchResults = await simulator.getSimulationsInfo(sender, transactions);

Note: The direct instantiation approach requires more setup and knowledge of internal dependencies. The SDK approach is recommended for most use cases as it provides a simpler API and handles configuration automatically.


Methods

getSimulationInfo

getSimulationInfo(sender: SenderAbstraction, tx: CrosschainTx): Promise<ExecutionFeeEstimationResult>

Purpose

Simulates a single cross-chain transaction and provides TVM fees and simulation info. This method performs TAC-side simulation to estimate gas costs, validate transaction logic, and calculate the required fees including protocol fees, executor fees, and gas limits. The sender abstraction is used to provide transaction context such as wallet state.

Parameters

  • sender: A SenderAbstraction instance used to provide context (e.g., wallet state) for the simulation
  • tx: A CrosschainTx object representing the cross-chain transaction to simulate

Returns detailed fee estimation and execution information for the simulated transaction.


getSimulationsInfo

getSimulationsInfo(sender: SenderAbstraction, txs: CrosschainTx[]): Promise<ExecutionFeeEstimationResult[]>

Purpose

Simulates a list of cross-chain transactions for a given sender and provides fee estimation results for each transaction. This method is useful for batch simulation and fee estimation, processing each transaction sequentially and returning results in the same order as the input.

Parameters

  • sender: A SenderAbstraction instance used to provide context (e.g., wallet state) for all simulations
  • txs: An array of CrosschainTx objects representing the cross-chain transactions to simulate

Returns Promise<ExecutionFeeEstimationResult[]>

Returns an array of ExecutionFeeEstimationResult objects, one for each input transaction in the same order.


estimateTONFee

estimateTONFee(asset: Asset, params: GeneratePayloadParams): bigint

Purpose

Estimates the TVM transaction fee required for processing a single asset in cross-chain operations. This method calculates fees for the complete transaction pipeline for one asset, including wallet interactions, token transfers/burns, proxy contracts, and cross-chain layer.

Parameters

  • asset: An Asset instance representing the token/NFT to process.
  • params: GeneratePayloadParams object containing payload generation parameters including excess receiver, EVM data, and fee parameters.

Returns bigint

Returns the estimated fee in nanotons (1 TON = 10^9 nanotons) for processing the provided asset.


Example Usage

import { TacSdk, Network, SenderFactory } from "@tonappchain/sdk";
import { TonConnectUI } from '@tonconnect/ui';
import { TonApiClient } from '@ton-api/client';
import { getHttpV4Endpoint } from '@orbs-network/ton-access';

// Create SDK instance (recommended approach)
const sdk = await TacSdk.create({
    network: Network.TESTNET
});

// Create a sender
const tonConnectUI = new TonConnectUI({
    manifestUrl: 'https://example.com/tonconnect-manifest.json'
});
const sender = await SenderFactory.getSender({
    tonConnect: tonConnectUI
});

// Create EVM proxy message
const evmProxyMsg = {
    evmTargetAddress: "0x...",
    methodName: "swap",
    encodedParameters: "0x..."
};

// Simulate a single transaction using SDK
const simulationResult = await sdk.getSimulationInfo(
    evmProxyMsg, 
    sender, 
    [asset1, asset2], // optional assets
    {
        // optional transaction options
        calculateRollbackFee: true,
        allowSimulationError: false
    }
);

console.log("Fee estimation:", simulationResult);

// Simulate multiple transactions using SDK
const transactions = [
    { evmProxyMsg: evmProxyMsg1, assets: [asset1], options: {} },
    { evmProxyMsg: evmProxyMsg2, assets: [asset2], options: {} }
];
const batchResults = await sdk.simulateTransactions(sender, transactions);

console.log("Batch simulation results:", batchResults);