Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

suavex_call for anvil #1

Merged
merged 1 commit into from Feb 27, 2024
Merged

suavex_call for anvil #1

merged 1 commit into from Feb 27, 2024

Conversation

zeroXbrock
Copy link
Member

@zeroXbrock zeroXbrock commented Feb 24, 2024

enables suapp devs to use anvil as the eth provider for your suave mevm, forking any chain (e.g. eth mainnet) to test your suapps locally.

instructions

build this version of anvil and run it, then send a curl request to test:

cargo build --bin anvil
export FORK_URL=https://eth-goerli.g.alchemy.com/v2/YOUR_KEY_HERE
./target/debug/anvil --chain-id 5 -f $FORK_URL -p 8555
# call uniswapV2 router's `getAmountOut`
# note: curl, jq, base64, and hexdump are required
curl -s -X POST --data '{"jsonrpc":"2.0","method":"suavex_call","params":["0x7a250d5630b4cf539739df2c5dacb4c659f2488d", "BU1Q1AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABpaWlpAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEJCQkJCQkIAAAAAAAAAAAAAAAAAAAAAAAAAABMTExMTExMTExMTEw=="],"id":1}' -H "Content-Type: application/json" http://localhost:8555 | jq -r .result | base64 -d | hexdump -v -e '/1 "%02x"'

in another terminal, run suave devnet with the --suave.eth.remote_endpoint flag to connect it to anvil:

# in suave-geth/
./build/bin/suave \
    --dev \
    --dev.gaslimit=30000000 \
    --http \
    --http.port=8545 \
    --http.vhosts='*' \
    --http.corsdomain='*' \
    --ws \
    --ws.origins='*' \
    --datadir=./suave/devenv/data \
    --keystore=./suave/devenv/suave-ex-node/keystore \
    --unlock=0xB5fEAfbDD752ad52Afb7e1bD2E40432A485bBB7F \
    --password=./suave/devenv/suave-ex-node/password.txt \
    --suave.eth.remote_endpoint=http://localhost:8555

then write SUAVE-enabled smart contracts that use suavex_-reliant precompiles:

library UniV2Swop {
    address public constant router = 0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D;

    /// Returns market price sans fees.
    function getAmountOut(
        uint256 amountIn,
        uint256 reserveIn,
        uint256 reserveOut
    ) internal view returns (uint256 price) {
        bytes memory result = Suave.ethcall(
            router,
            abi.encodeWithSignature(
                "getAmountOut(uint256,uint256,uint256)",
                amountIn,
                reserveIn,
                reserveOut
            )
        );
        require(result.length > 0, "UniV2Swop: no result");
        (price) = abi.decode(result, (uint256));
    }
}

write SuaveEnabled tests with forge:

function testGetAmountOut() public {
    uint256 amountIn = 0x69696969;
    uint256 reserveIn = 0x42424242424242;
    uint256 reserveOut = 0x131313131313131313131313;
    uint256 amountOut = UniV2Swop.getAmountOut(
        amountIn,
        reserveIn,
        reserveOut
    );
    assertEq(amountOut, 558102013055677511204);
}

@zeroXbrock zeroXbrock changed the title suavex endpoints suavex endpoints for anvil Feb 24, 2024
@zeroXbrock zeroXbrock changed the title suavex endpoints for anvil suavex_call for anvil Feb 27, 2024
@zeroXbrock zeroXbrock marked this pull request as ready for review February 27, 2024 00:40
@zeroXbrock zeroXbrock merged commit 91ad869 into master Feb 27, 2024
@zeroXbrock zeroXbrock deleted the suavex_endpoints branch February 27, 2024 00:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant