LogoLogo
SupportGithubSign InGet Access
  • Introduction
  • GETTING STARTED
    • SDK Quick Start
    • API Quick Start
  • Guides
    • Web
      • Create a wallet
      • Send tokens
      • Sign a transaction
      • Simulate a transaction
      • Back up a wallet
      • Recover a wallet
      • Cross-device sessions
      • Manage wallet lifecycle states
      • Web authentication methods
      • Perform swaps
      • Add custom signature hooks
      • MPC progress callbacks
      • Portal API methods
      • Configure a custom subdomain
      • Eject a wallet
      • Using the EIP-1193 Provider
      • Legacy documentation
        • Back up a wallet
          • Backup Options
        • Recover a wallet
    • iOS
      • Create a wallet
      • Send tokens
      • Sign a transaction
      • Simulate a transaction
      • Back up a wallet
      • Recover a wallet
      • Cross-device sessions
      • Manage wallet lifecycle states
      • Connect with WalletConnect
      • Build a WebView
      • Perform swaps
      • Add custom signature hooks
      • MPC progress callbacks
      • Portal API methods
      • Manage ERC20 tokens
      • Eject a wallet
      • Legacy documentation
        • Back up a wallet
          • Backup Options
          • Passkey + Enclave Storage
        • Recover a wallet
      • Troubleshooting Tips
      • Feature Flags
    • Android
      • Create a wallet
      • Send tokens
      • Sign a transaction
      • Simulate a transaction
      • Back up a wallet
      • Recover a wallet
      • Cross-device sessions
      • Manage wallet lifecycle states
      • Connect with WalletConnect
      • Build a WebView
      • Perform swaps
      • Add custom signature hooks
      • MPC progress callbacks
      • Portal API methods
      • Eject a wallet
      • Legacy documentation
        • Back up a wallet
          • Backup Options
        • Recover a wallet
    • React Native
      • Create a wallet
      • Send tokens
      • Sign a transaction
      • Simulate a transaction
      • Back up a wallet
      • Recover a wallet
      • Cross-device sessions
      • Manage wallet lifecycle states
      • Connect with WalletConnect
      • Build a WebView
      • Perform swaps
      • Add custom signature hooks
      • MPC progress callbacks
      • Portal API methods
      • Eject a wallet
      • Legacy documentation
        • Back up a wallet
          • Backup Options
        • Recover a wallet
    • Enclave MPC API
      • Create a client
      • Create a wallet
      • Send tokens
      • Sign Ethereum transactions
      • Sign Solana transactions
      • Sign Tron transactions
      • Sign Stellar Transaction
      • Concurrent Transactions
      • Back up a wallet
      • Eject a wallet
  • Reference
    • iOS
      • createWallet
      • backupWallet
      • recoverWallet
      • ejectPrivateKeys
      • registerBackupMethod
      • setGDriveConfiguration
      • setPasskeyConfiguration
      • setPasskeyAuthenticationAnchor
      • setPassword
      • availableRecoveryMethods
      • doesWalletExist
      • isWalletBackedUp
      • isWalletOnDevice
      • isWalletRecoverable
      • getBalances
      • getAssets
      • getNftAssets
      • getTransactions
      • sendSol
      • evaluateTransaction
      • buildEip155Transaction
      • buildSolanaTransaction
      • getWalletCapabilities
    • Android
      • Reference Documentation
    • React Native
      • @portal-hq/core
      • Storage adapters
        • Cloud storage
          • @portal-hq/gdrive-storage
          • @portal-hq/icloud-storage
        • Mobile storage
          • @portal-hq/keychain
          • @portal-hq/mobile-key-values
    • Enclave MPC API
      • V1 endpoints
    • Client API
      • V3 endpoints
      • V1 endpoints
    • Custodian API
      • V3 endpoints
      • V1 endpoints
    • Swaps API
      • V3 endpoints
      • V1 endpoints
  • Resources
    • Flutter
      • iOS
      • Android
    • Error codes
      • Overview
      • MPC errors
      • Network errors
      • General errors
      • Encryption errors
      • Portal Connect errors
    • Portal's MPC architecture
    • Authentication and API Keys
    • Self-Managed Backups
    • Alert Webhooks
    • Wallet lifecycle
    • Backup options
      • Password/PIN
      • GDrive
      • iCloud
      • Passkey + Enclave
    • WalletConnect metadata
    • Advanced security scanning
    • Account abstraction
    • Security firewall
    • Eject
    • Security
    • Blockchain support
    • Chain ID formatting
    • Testnet faucets
    • Going to Production
    • Rate Limits
    • Multi-backup migration guide
    • Multi-wallet migration guides
      • Migrating from Android SDK v3.x.x to v4.x.x
      • Migrating from iOS SDK v3.0.x to v3.2.x
  • Support
    • Changelog
      • Android
      • iOS
      • React Native
      • Web
      • Past Releases
        • 2024 Releases
        • 2023 Releases
    • Celo Hackathon Hub
    • Glossary
Powered by GitBook
On this page

Was this helpful?

  1. Guides
  2. Web

Simulate a transaction

Before committing to a transaction, it's often useful to simulate its outcome to understand the potential changes and detect any errors preemptively.

Portal provides the function portal.simulateTransaction, which gives you insights into what will happen upon executing the transaction.

Here's how it works:

const handleSimulateTransaction = async () => {
  // First, construct a transaction.
  const transaction = {
    to: '0xRecipientAddressHere', // {string} The recipient address.
    value: '0x10DE4A2A',  // {?string} The value to be sent in Wei.
    data: undefined,  // {?string} Data for the transaction (for contract interactions).
    maxFeePerGas: undefined, // {?string} Maximum fee per gas.
    maxPriorityFeePerGas: undefined, // {?string} Maximum priority fee per gas.
    gas: undefined, // {?string} The gas limit.
    gasPrice: undefined // {?string} Gas price in Wei.
  };
  
  // Next, simulate the transaction.
  const chainId = 'eip155:1'
  const simulatedResult = await portal.simulateTransaction(chainId, transaction);

  // Finally, you can handle or display the simulation results as needed.
  if (simulatedResult.error) {
    console.error("Transaction Error:", simulatedResult.error.message);
  } else if (simulatedResult.requestError) {
    console.error("Request Error:", simulatedResult.requestError.message);
  } else {
    console.log("Simulated Transaction Results:", simulatedResult.changes);
  }
}

This function will return a JSON object that breaks down the transaction simulation:

  • changes: An array detailing all the potential transaction outcomes. Each change has the following structure:

    • amount: The amount being transferred.

    • assetType: The type of asset being dealt with (NATIVE or ERC20 or ERC721 or ERC1155 or SPECIAL_NFT).

    • changeType: The type of change (APPROVE or TRANSFER).

    • contractAddress: Address of the contract being interacted with (null for native transactions).

    • decimals: Decimals used in the asset.

    • from: The sender address.

    • name: The name of the asset.

    • rawAmount: The unformatted amount being transferred.

    • symbol: The asset's symbol.

    • to: The receiver's address.

    • tokenId: An identifier for tokens (null for assets that are not tokens).

  • gasUsed: The gas used by the transaction.

  • error: An object that contains the error message if the transaction would fail upon execution.

  • requestError: An object that contains the error message if there was an issue with the request, such as a malformed transaction argument being provided.

By incorporating transaction simulations, you can provide your users with a preview of the transaction outcomes and preemptively detect and handle errors, ensuring a smoother user experience.

And now you are simulating transactions with Portal! 🙌 🚀 Next, we'll explore how to back up the wallet for recovery if the user loses device access.

PreviousSign a transactionNextBack up a wallet

Last updated 12 months ago

Was this helpful?