Interact with Ethereum using Foundry
Last modified: 2023-03-26
Blockchain
Ethereum
Web3
Foundry is a blazing fast, portable and modular toolkit for Ethereum application development written in Rust.
Installation
Please refer to the Foundry's repository for details.
curl -L https://foundry.paradigm.xyz | bash
Interact with Contract
We can interact with the contract that is already deployed in Ethereum chain if we have the private key of the account and the contract address.
export ETH_RPC_URL="http://10.0.0.1:12345"
# Call the function of the contract
cast send --private-key <private_key_addr> <contract_addr> "exampleFunc(uint256)" <argument_value_of_the_function>
cast send --private-key 0xa5de3dc36b45a5c676321dab3456b22bd4ab63379fe27d136fa4b597ea63c4a6 0x2b572cFa179008D4a36980D6E02c3C5276c34117 "deposit(uint256)" 10
# Trigger the fallback function
# Call the nonexisting function e.g. "dummy"
cast send --private-key <private_key_addr> <contract_addr> "dummy()"
cast send --private-key 0xa5de3dc36b45a5c676321dab3456b22bd4ab63379fe27d136fa4b597ea63c4a6 0x2b572cFa179008D4a36980D6E02c3C5276c34117 "dummy()"
# Trigger the receive function
# Send Ether to call the receive function
cast send --private-key <private_key_addr> <contract_addr> --value 10gwei
cast send --private-key 0xa5de3dc36b45a5c676321dab3456b22bd4ab63379fe27d136fa4b597ea63c4a6 0x2b572cFa179008D4a36980D6E02c3C5276c34117 --value 10gwei