Skip to content

Commit

Permalink
Feat simulate script (#9)
Browse files Browse the repository at this point in the history
* feat simultae tx with foundry

* add fork environment simulate

* add simulate readMe

---------

Co-authored-by: gs8nrv <[email protected]>
  • Loading branch information
sogipec and GuillaumeNervoXS authored Jul 24, 2023
1 parent daf245f commit 1031fe7
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@
#MNEMONIC_POLYGON=""
#POLYGON_ETHERSCAN_API_KEY=""

#ETH_NODE_URI_FORK=http://localhost:8545
#MNEMONIC_FORK=""

30 changes: 28 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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: <https://github.com/Picodes/headers>

## 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: <https://hardhat.org/hardhat-runner/docs/guides/command-line-completion>

## Foundry Installation

Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions foundry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
23 changes: 23 additions & 0 deletions scripts/foundry/Simulate.s.sol
Original file line number Diff line number Diff line change
@@ -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();
}
}

0 comments on commit 1031fe7

Please sign in to comment.