From 1031fe72824d7a5d03908e38f2932142617c7463 Mon Sep 17 00:00:00 2001 From: Pablo Veyrat <50438397+sogipec@users.noreply.github.com> Date: Mon, 24 Jul 2023 19:24:58 +0200 Subject: [PATCH] Feat simulate script (#9) * feat simultae tx with foundry * add fork environment simulate * add simulate readMe --------- Co-authored-by: gs8nrv <55771972+GuillaumeNervoXS@users.noreply.github.com> --- .env.example | 2 ++ README.md | 30 ++++++++++++++++++++++++++++-- foundry.toml | 1 + package.json | 4 ++++ scripts/foundry/Simulate.s.sol | 23 +++++++++++++++++++++++ 5 files changed, 58 insertions(+), 2 deletions(-) create mode 100644 scripts/foundry/Simulate.s.sol diff --git a/.env.example b/.env.example index a1e2780..3b47034 100644 --- a/.env.example +++ b/.env.example @@ -9,4 +9,6 @@ #MNEMONIC_POLYGON="" #POLYGON_ETHERSCAN_API_KEY="" +#ETH_NODE_URI_FORK=http://localhost:8545 +#MNEMONIC_FORK="" diff --git a/README.md b/README.md index 3b286de..f789021 100644 --- a/README.md +++ b/README.md @@ -30,11 +30,11 @@ Warning: always keep your confidential information safe. ## Headers -To automatically create headers, follow: https://github.com/Picodes/headers +To automatically create headers, follow: ## Hardhat Command line completion -Follow these instructions to have hardhat command line arguments completion: https://hardhat.org/hardhat-runner/docs/guides/command-line-completion +Follow these instructions to have hardhat command line arguments completion: ## Foundry Installation @@ -118,6 +118,32 @@ yarn hardhat:coverage yarn foundry:coverage ``` +### Simulate + +You can simulate your transaction live or in fork mode. For both option you need to +complete the `scripts/foundry/Simulate.s.sol` with your values: address sending the tx, +address caled and the data to give to this address call. + +For live simulation + +```bash +yarn foundry:simulate +``` + +For fork simulation + +```bash +yarn foundry:fork +yarn foundry:simulate:fork +``` + +For fork simulation at a given block + +```bash +yarn foundry:fork:block ${XXXX} +yarn foundry:simulate:fork +``` + ### Gas report ```bash diff --git a/foundry.toml b/foundry.toml index 0855911..cb1c722 100644 --- a/foundry.toml +++ b/foundry.toml @@ -20,6 +20,7 @@ runs = 500 [rpc_endpoints] mainnet = "${ETH_NODE_URI_MAINNET}" polygon = "${ETH_NODE_URI_POLYGON}" +fork = "${ETH_NODE_URI_FORK}" goerli = "${ETH_NODE_URI_GOERLI}" [etherscan] diff --git a/package.json b/package.json index dc31b61..4ccfb4c 100644 --- a/package.json +++ b/package.json @@ -7,8 +7,12 @@ "foundry:compile": "forge build --optimize --optimizer-runs 1000", "foundry:coverage": "forge coverage --report lcov && forge coverage", "foundry:deploy": "forge script --broadcast --verify -vvvv", + "foundry:fork": "source .env && anvil --fork-url $ETH_NODE_URI_MAINNET ", + "foundry:fork:block": "source .env && anvil --fork-url $ETH_NODE_URI_MAINNET --fork-block-number ", "foundry:gas": "forge test --gas-report", "foundry:run": "docker run -it --rm -v $(pwd):/app -w /app ghcr.io/foundry-rs/foundry sh", + "foundry:simulate": "forge script scripts/foundry/Simulate.s.sol --broadcast -vvvv", + "foundry:simulate:fork": "forge script scripts/foundry/Simulate.s.sol --fork-url fork --broadcast -vvvv", "foundry:test": "forge test -vvv", "hardhat:compile": "hardhat compile", "hardhat:coverage": "hardhat coverage", diff --git a/scripts/foundry/Simulate.s.sol b/scripts/foundry/Simulate.s.sol new file mode 100644 index 0000000..1b7173a --- /dev/null +++ b/scripts/foundry/Simulate.s.sol @@ -0,0 +1,23 @@ +// SPDX-License-Identifier: MIT +pragma solidity ^0.8.12; + +import "forge-std/Script.sol"; +import "../../contracts/example/MockAgEUR.sol"; +import { console } from "forge-std/console.sol"; + +contract Simulate is Script { + error WrongCall(); + + function run() external { + // TODO replace with your inputs + address sender = address(0x0274a704a6D9129F90A62dDC6f6024b33EcDad36); + address contractAddress = address(0x3Ef3D8bA38EBe18DB133cEc108f4D14CE00Dd9Ae); + // remove the 0x + bytes + memory data = hex"71ee95c0000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000c00000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000014000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000274a704a6d9129f90a62ddc6f6024b33ecdad3600000000000000000000000000000000000000000000000000000000000000010000000000000000000000001f9840a85d5af5bf1d1762f925bdaddc4201f984000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000427abf85e5d121ac000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000811e1782af6373843046497b3be2c5b25f13037b02c218c5a1a5be24666434d169ca949b7c3d8c763a566075f024a19b4565aba390e00f197fff97adb2f9ef8b0eee43dfe473f564bbd71b106e37928d4f052afe2abf8a42e5f07aded57af2766943bc7bedecf531200d4ca454185d135e6933aa3cff5c53a55f45033135d3a01aae71a2ff3d1e34342bdb86d6348a9da5c8384085f743bb2451aa846b5f667690ccb7b7b1fe363d3e286addaaff93de4a258308fae35fb008580bc25873284f63b37592378acc9f27d211017550c57ae97d0e9cc944d3f90bf54147a69fedeed81940d2088ad7b84767ae8569a9202e23aa0f8204a30b365916d5cefb60c1dfe"; + + vm.prank(sender); + (bool success, ) = contractAddress.call(data); + if (!success) revert WrongCall(); + } +}