Skip to content
Simulate Transactions

Simulate Transactions

It is hard to understand what will happen in a blockchain transaction before submitting it on chain. Even worse, some transactions are fraudulent and need to be detected before they're every submitted.

We built a simulation engine that determines the result of a transaction before it is submitted. This enables dapps and users to proactively fix transactions, resulting in a much better user experience.

By default, we give transparency into any token transfers and token approvals within a transaction. We also help understand why a transaction will revert and the gas used in a transaction.

We also support adding arbitrary contract interfaces to fit any smart contract.

Getting Started

For this example, we'll demo simulating an NFT purchase.

import { ethers } from "ethers";
import { getTransactionTrace } from "@simplecrypto/crypto-sdk"

/**
 * The address of your NFT. This is an example Simple Crypto NFT that is deployed on Polygon.
 */
const NFT_CONTRACT_ADDRESS = "0xc47785Cbe2CE8382d618C5Af4d8a343901c5672d";

const FROM_ADDRESS = "0x000000Cbe2CE8382d618C5Af4d8a343901c5672d"

const to = NFT_CONTRACT_ADDRESS;
const data = new ethers.utils.Interface(["function buy(address to) public"]).encodeFunctionData("buy", [FROM_ADDRESS]);
const from = FROM_ADDRESS;
const chainId = 137 // Polygon Mainnet
const walletAddress = FROM_ADDRESS;

const trace = getTransactionTrace({
  to,
  value,
  data,
  from,
  chainId,
  walletAddress
});

API Reference

getTransactionTrace

getTransactionTrace(transactionOptions: TransactionOptions)

Arguments

ParameterTypeDescription
option.tostringThe to parameter of the transaction
option.valuestring, number, BigNumberThe value parameter of the transaction
option.datastringThe data parameter of the transaction
option.fromstringThe from parameter of the transaction
option.chainIdstring, numberThe chainId of the blockchain the transaction will be executed on
option.walletAddressstring, nullThe optional wallet to track in the transaction
option.contractABIsstring[], object[]Specific contract ABIs to decode in the transaction

Returns

Promise<TransactionTrace>. TransactionTrace and related types are defined below.

type TransactionTrace = {
  swapData?: SwapData;
  approvalData?: ApprovalData;
  error?: string;
  gasUsed: number;
};

type ApprovalData = {
  spender: string;
  amount: string;
};

type SwapData = {
  in: Array<TransferData>;
  out: Array<TransferData>;
};

type TransferData = {
  to: string;
  from: string;
  contractInterface: ContractInterfaces;
  amount: string;
  contractAddress?: string;
  tokenId?: string;
};

export enum ContractInterfaces {
    ERC20 = "ERC20",
    ERC721 = "ERC721",
    ERC1155 = "ERC1155",
    NativeTokenTransfer = "NativeTokenTransfer", // not a contract interface but works for our purposes
}
Last updated on February 7, 2023