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

ExEx calling contracts to track state #23

Open
0xalex88 opened this issue Sep 18, 2024 · 0 comments
Open

ExEx calling contracts to track state #23

0xalex88 opened this issue Sep 18, 2024 · 0 comments

Comments

@0xalex88
Copy link

Hi,

would be nice to have an example on how to call smart contracts from within the EVM using an equivalent eth_call function.

I can add an example that tracks ERC20 balances by looking at addresses in the ERC20 transfer events, but I would need to know if this is the best way to call contracts:

let block_number = 123;
let db = StateBuilder::new()
    .with_database(StateProviderDatabase::new(
       self.provider
            .state_by_block_number_or_tag(block_number.into())?,
    ))
    .with_bundle_update()
    .build();
let evm_config = EthEvmConfig::new(self.provider.chain_spec());
let mut evm = evm_config.evm(db);

let mut balances = vec![];
for address in user_addresses {
let tx = Transaction::Legacy(TxLegacy {
    gas_limit: 50_000_000,
    to: TxKind::Call(self.farm.address),
    input: erc20::ERC20::ERC20Calls::balanceOf(erc20::ERC20::balanceOfCall {
        owner: address.clone(),
    })
    .abi_encode()
    .into(),
    ..Default::default()
});

let tx_env = evm.tx_mut();
tx_env.gas_limit = tx.gas_limit();
tx_env.nonce = None;
tx_env.caller = Address::ZERO;
tx_env.transact_to = TxKind::Call(tx.to().unwrap_or_default());
tx_env.value = tx.value();
tx_env.data = tx.input().clone();
tx_env.chain_id = tx.chain_id();

// evm_config.fill_tx_env(evm.tx_mut(), tx, Address::ZERO);
let result = evm.transact()?.result;
let output = result.output().expect("unable to execute balance check tx");

let balance = erc20::ERC20::balanceOfCall::abi_decode_returns(output, true)?;
balances.push(balance.value.into());
}

I came up with this looking at old reth issues/PRs and had to end up changing the tx_env manually because fill_tx_env doesn't support plain (unsigned) transactions anymore.
Also I'm not sure if there's an easier way to have an EVM running at a specific block by just using the provider instead of using StateBuilder, creating an EVM config etc.

@0xalex88 0xalex88 changed the title ExEx EVM calling contracts ExEx calling contracts to track state Sep 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant