# Ethereum

## Interact with Ethereum using Foundry <a href="#interact-with-ethereum-using-foundry" id="interact-with-ethereum-using-foundry"></a>

Foundry is a blazing fast, portable and modular toolkit for Ethereum application development written in Rust.

### Setup Foundry <a href="#setup-foundry" id="setup-foundry"></a>

Please refer to the [Foundry's repository](https://github.com/foundry-rs/foundry) for details.\
To install **`foundryup`**, run the following command to install **foundry** toolchain.

```shellscript
curl -L https://foundry.paradigm.xyz | bash
foundryup
```

We can set the environment variable for **Ethereum RPC URL** to interact the Ethereum blockchain so that we don’t need to set the RPC url flag when running each command.

```shellscript
export ETH_RPC_URL="http://10.0.0.1:12345/path/to/rpc"
```

### Investigating a Chain <a href="#investigating-a-chain" id="investigating-a-chain"></a>

**`cast`** command of Foundry performs Ethereum RPC calls.

```shellscript
# Get the Ethereum chain id
cast chain-id
# Get the symbolic name of the current chain
cast chain
# Get the current client version
cast client

# Get the current gas price
cast gas-price

# Get the latest block number
cast block-number
# Get information about a block
cast block
```

### Investigating Account <a href="#investigating-account" id="investigating-account"></a>

```shellscript
# Get the balance of an account in wei
cast balance <account_address or ens_name>
cast balance 0x123...
cast balance beer.eth
```

### Investigating Contract <a href="#investigating-contract" id="investigating-contract"></a>

```shellscript
# Get the source code of a contract from Etherscan
cast source <contract_address> -e <etherscan_api_key>
```

### Call Functions <a href="#call-functions" id="call-functions"></a>

If we know the functions of a target contract, we can simply call them. Note that these command do NOT send transactions, so cannot change states or values in the contract.

```shellscript
cast call --private-key <private_key_addr> <contract_addr> "getFlag()(string memory)"

cast call --private-key <private_key_addr> <contract_addr> "isSolved()(bool)"
```

### Send Transactions <a href="#send-transactions" id="send-transactions"></a>

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.

```shellscript
# 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 0x123... 0xabc... "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 0x123... 0xabc... "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 0x123... 0xabc... --value 10gwei
```

If we got error like unsupported feature: eip1559 , add `--legacy` flag for the command.
