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
Parameter | Type | Description |
---|---|---|
option.to | string | The to parameter of the transaction |
option.value | string , number , BigNumber | The value parameter of the transaction |
option.data | string | The data parameter of the transaction |
option.from | string | The from parameter of the transaction |
option.chainId | string , number | The chainId of the blockchain the transaction will be executed on |
option.walletAddress | string , null | The optional wallet to track in the transaction |
option.contractABIs | string[] , 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
}