TVMJS — TRON Virtual Machine in TypeScript

website·Indexed 32 days ago

https://walletadapter.org/docs/

A TypeScript implementation of the TRON Virtual Machine (TVM), designed to provide execution parity with java-tron. TVMJS is a monorepo of modular packages that implements TRON-specific semantics, including TRC-10 token opcodes, signature and permission precompiles, the TRON energy model, and multi-signature account models.

Tokens
30.5K
Snippets
130
Records
233
Agent score
65%

What's inside TVMJS

  1. Overview of TVMJS

    TVMJS is a TypeScript implementation of the TRON Virtual Machine (TVM), the execution environment used by java-tron nodes. It allows developers to execute TRON contract bytecode within JavaScript and TypeScript environments. It is a fork of EthereumJS, extended to support TRON-specific opcodes, TRC-10 token transfers, precompiled contracts, multi-signature permissions, and the TRON account model.
  2. Overview of TVMJS

    TVMJS is a modular TypeScript implementation of the TRON Virtual Machine (TVM). It allows developers to run TRON contract bytecode in any JavaScript environment, including local machines, CI pipelines, and web browsers. Key features include support for TRC-10 token operations, signature and permission precompiles, the TRON account model (Owner/Active/Witness permissions), and energy-based execution metering.
  3. Overview of TronWallet Adapter

    TronWallet Adapter is a secure and scalable library that provides a unified API for interacting with various TRON wallets. It simplifies the process of wallet detection, connection, message signing, and transaction signing for dApps, supporting both browser extensions and mobile applications.
  4. Overview of EVM Wallet Adapter

    The EVM Wallet Adapter provides a unified, abstract interface for interacting with various EVM-compatible wallets. It encapsulates connection logic, event handling, signing methods, and chain management into a single API, removing the need for developers to manage unique provider injections (like window.ethereum or window.binance) individually.
  5. Understand TRON Energy Model vs Ethereum Gas

    TRON uses 'energy' instead of 'gas' for execution metering. While the accounting structure is similar to Ethereum, several key parameters differ. Note that TVMJS energy accounting may differ from java-tron; simulation results in TVMJS should be verified against the target network before production use.
  6. Choose the appropriate TVMJS execution entry point

    TVMJS provides four levels of abstraction for executing code depending on the required context (bytecode, state, transaction, or block).
    Entry pointPackageRunsUse case
    tvm.runCode()@tvmjs/tvmRaw bytecode, no account contextStudying opcode sequences or code fragments
    tvm.runCall()@tvmjs/tvmA message call against stateSimulating a call or a deployment
    runTx(vm, ...)@tvmjs/vmOne signed transactionReplaying or predicting a transaction
    runBlock(vm, ...)@tvmjs/vmEvery transaction in a blockBlock replay and consistency testing
  7. TVMJS package architecture

    TVMJS is organized as a monorepo of modular packages. Most users will interact with @tvmjs/vm for high-level transaction and block execution, or @tvmjs/tvm for raw bytecode execution.

    The dependency hierarchy is as follows:

    • @tvmjs/vm: Top-level execution context (transactions and blocks).
    • @tvmjs/tvm: Bytecode interpreter (opcodes, energy, precompiles).
    • @tvmjs/statemanager: Account and storage state management.
    • @tvmjs/mpt: Merkle Patricia Trie implementation.
    • @tvmjs/tx: Transaction types, including TRC-10 fields.
    • @tvmjs/block: Block and header types.
    • @tvmjs/blockchain: Chain management.
    • @tvmjs/common: Shared chain and hardfork configuration.
    @tvmjs/vm            top-level execution context — transactions and blocks
     ├── @tvmjs/tvm      bytecode interpreter — opcodes, energy, precompiles
     │    └── @tvmjs/statemanager   account and storage state
     │         └── @tvmjs/mpt       Merkle Patricia Trie
     ├── @tvmjs/tx       transaction types, including TRC-10 fields
     ├── @tvmjs/block    block and header types
     ├── @tvmjs/blockchain   chain management
     └── @tvmjs/common   shared chain and hardfork configuration
  8. Discover multiple TRON wallets using TIP-6963

    TIP-6963 is a standard that allows multiple injected TRON wallets to coexist in a browser. Instead of dApps relying on a single window.tron provider, wallets emit a TIP6963:announceProvider event to register themselves. dApps can listen for this event to collect and display a full list of available compatible wallets to the user. Compatible wallets include TokenPocket (v2.6.0+), OneKey (v6.1.0+), and TronLink (v4.7.5+).
  9. Initialize and connect using WalletConnectAdapter

    The WalletConnectAdapter allows users to connect to a TRON dApp using mobile wallets via QR codes. It requires a projectId from WalletConnect and optional metadata about the dApp.
    import { WalletConnectAdapter } from '@tronweb3/tronwallet-adapter-walletconnect';
    
    const adapter = new WalletConnectAdapter({
      network: 'Nile',
      options: {
        projectId: 'your-project-id',
        metadata: {
          name: 'Your DApp Name',
          description: 'Your DApp Description',
          url: 'https://your-dapp.com',
          icons: ['https://your-dapp.com/icon.png'],
        },
      },
    });
    
    await adapter.connect();
  10. TRON Virtual Machine (TVMJS) Core Capabilities

    TVMJS allows running and testing TRON contract bytecode locally, in CI, or in browsers via packages like @tvmjs/vm and @tvmjs/tvm. Key features include:

    • TRC-10 Support: Implementation of CALLTOKEN, TOKENBALANCE, CALLTOKENVALUE, and CALLTOKENID (0xd0–0xd3) for transfers and queries.
    • Precompiles: Support for BatchValidateSign (0x09), ValidateMultiSign (0x0a), and TRON-specific hashing at 0x03.
    • Contract Detection: ISCONTRACT (0xd4) for detecting contracts.
    • VM Constraints: A 1,024-item operand stack and a call-depth limit of 64.
    • Costs: Deployment costs 200 Energy per byte of deployed runtime code.
  11. Observe VM-level block and transaction events

    The @tvmjs/vm package emits block and transaction events using the same guarantees as the TVM event emitter. Detailed event lists and the distinction between synchronous and asynchronous handlers are documented in the @tvmjs/vm README.