in this project, we inspect historical data on avalanche c-chain to simulate sandwich attacks in the gmx protocol.
-
define the desired assets and/or protocols you want to research. in this example, we are looking at gmx and running the test
test/testGmx()
. -
find out the methods that trigger prices updates (e.g.
swap()
on gmx's router). -
add/clone all the contracts needed for the methods above to
contracts/
. the main code we will be running is located insidetest/
(foundry's forge is a testing framework). -
use any blockchain analytics tools (e.g., dune or avax apis) to search for past blocks with a suspecting price movement (e.g., set a threshold that could be interesting to look at).
-
create a list with all the blocks you found and add them to
data/blocks.txt
. there is already two examples to get you started (a block from this week and another from last summer).
- install foundry (this will create
lib/forge-std
). this is not only needed for runningforge
per se, but also because our tests importforge-std/Test.sol
. note that our directory setup is defined insidefoundry.toml
:
[profile.default]
src = 'data'
out = 'out'
libs = ['lib']
fs_permissions = [{ access = "read", path = "./"}]
-
install a solidity compiler. you need to look at which solidity version your protocol is using. for instance, for gmx we have to use 0.6.12.
-
export an env variable for avalanche c-chain's RPC url (e.g., from infura's or ankr's or your own node):
> export RPC_URL=<RPC_URL>
- build the contracts and run with:
> make test
[⠢] Compiling...
[⠔] Compiling 17 files with 0.6.12
[⠑] Solc 0.6.12 finished in 2.31s
Compiler run successful
Running 1 test for test/Gmx.sol:getHistorical
[PASS] testHistoricalGmx() (gas: 81654567)
Logs:
🧱 block number: 19443666
🪙 token 1: USDC
🪙 token 2: WETH.e
🔂 loops in the simulation: 12
🧱 block number: 19905430
🔂 loops in the simulation: 10
🥪 possible $ profit (usd): 9720
Test result: ok. 1 passed; 0 failed; finished in 2.01s