gasless contract simulation

simulating contracts without spending gas is obviously useful. for example this can be used to build security scanners, secure your contract interactions, etc.

i’ll note a couple pocs to try it in javascript, solidity, and rust.

javascript

javascript is a good way to get started. lets use bun to write a quick proof of concept using a bit from ethers.

test/ethersjs.ts
import { Contract } from "ethers"

const c = new Contract(
  address,
  ["function transfer(address,uint256) public"],
  signer
)

// access the contract through .staticCall to simulate
const sim = c.staticCall.transfer()

cool. you can find the documentation for this here.

solidity

in solidity, i find it’s easiest to use a node and fork some blockchain. anvil from foundry is one of the best.

you can fork any chain locally like so:

anvil --fork-url https://example.com

if you use forge from foundry to manage your project, it’s very easy to write tests and then pass the same flag, for example.

test/TestContract.sol
contract TestExample is Test {
  function setUp() {

  }

  function testExample() {

  }
}

then you can test it against your desired network by using

forge test --fork-url https://example.com

rust

the above options are highly convenient but very slow.

if you find your project needs to test with more performance, rust is always a fun choice 🦀

test/main.rs

use ethers::prelude::*;

async fn main() {
  Ok(Ok())
}

another paradigm open source project mimics ethers on rust aptly named ethers-rs. here is a minimal poc mirroring the above examples, they have many high quality ones that may be way more interesting, but yknow