# Using Foundry

Foundry is a smart contract development toolchain that manages your dependencies, compiles your project, runs tests, deploys, and lets you interact with the chain from the CLI and via Solidity scripts.<br>

See the finished code at <https://github.com/thundercore/foundry-thundercore>

## One Time Setup <a href="#docs-internal-guid-576e8550-7fff-e213-0ee6-30645f6f0179" id="docs-internal-guid-576e8550-7fff-e213-0ee6-30645f6f0179"></a>

### Installing Foundry

See the [Installation](https://book.getfoundry.sh/getting-started/installation) chapter in the Foundry Book.

### Creating A Wallet <a href="#docs-internal-guid-347e2ee9-7fff-0b78-3071-e5ceb36ee85b" id="docs-internal-guid-347e2ee9-7fff-0b78-3071-e5ceb36ee85b"></a>

You can use "cast wallet new" to create a ThunderCore compatible wallet.

Here we name our newly created wallet deployer by moving the encrypted keystore file generated by "cast":

```
$ k=$(cast wallet new --json ~/.foundry/keystores | jq -r .[0].path)
Enter secret:
$ mv $k ~/.foundry/keystores/deployer
```

### Encrypted Keystores <a href="#docs-internal-guid-ecb06d1d-7fff-7593-6127-23484391c041" id="docs-internal-guid-ecb06d1d-7fff-7593-6127-23484391c041"></a>

Foundry generates standard encrypted Ethereum keystore files under $HOME/.foundry/keystores

* The format is [widely supported](https://ethereum.stackexchange.com/questions/12830/how-to-get-private-key-from-account-address-and-password)
* See [Web3 Secret Storage Definition](https://github.com/ethereum/wiki/wiki/Web3-Secret-Storage-Definition)

Private keys in the keystores can be exported with:

```
 ethkey inspect ~/.foundry/keystores/deployer
```

by using the \`ethkey\` utility from [go-ethereum](https://geth.ethereum.org/docs/getting-started/installing-geth).

### Obtaining Funds <a href="#docs-internal-guid-73c15ba0-7fff-ebf2-8be8-4979d115ce09" id="docs-internal-guid-73c15ba0-7fff-ebf2-8be8-4979d115ce09"></a>

* You'll need some gas tokens to deploy contracts.
* To test on ThunderCore testnet, you can get some testnet tokens from ThunderCore Testnet Faucet <https://faucet-testnet.thundercore.com>
* For ThunderCore mainnet, see <https://www.thundercore.com/buy-tt>

## Create A New Project <a href="#docs-internal-guid-1ad3df02-7fff-1fad-56da-bdead40937a4" id="docs-internal-guid-1ad3df02-7fff-1fad-56da-bdead40937a4"></a>

To start a new project with Foundry, use forge init:

```
$ forge init <project_name>
```

The src folder may already contain Counter.sol, a minimal Solidity contract.

## Setup Foundry For ThunderCore <a href="#docs-internal-guid-6c6bb751-7fff-7f53-89cc-445e88e1ee23" id="docs-internal-guid-6c6bb751-7fff-7f53-89cc-445e88e1ee23"></a>

```
$ cd <project_name>
$ EDIT foundry.toml
```

Edit foundry.toml to contain:

```
[profile.default]
evm_version = "london"

[rpc_endpoints]
thunder-mainnet = "https://mainnet-rpc.thundercore.com"
thunder-testnet = "https://testnet-rpc.thundercore.com"

# See more config options
# https://github.com/foundry-rs/foundry/blob/master/crates/config/README.md#all-options
```

## Compiling <a href="#docs-internal-guid-b0f8161f-7fff-5e73-6fcc-8f2f90d1f84d" id="docs-internal-guid-b0f8161f-7fff-5e73-6fcc-8f2f90d1f84d"></a>

Run the command forge build to build the project.

```
$ forge build
```

And test the build with "forge test".

```
$ forge test
```

### Deploy to ThunderCore Network <a href="#docs-internal-guid-2bfba379-7fff-fb8d-e762-ffa710db7615" id="docs-internal-guid-2bfba379-7fff-fb8d-e762-ffa710db7615"></a>

Edit script/Counter.s.sol and add "new Counter()" at the end of run():

```
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.24;

import {Script, console} from "forge-std/Script.sol";
import "../src/Counter.sol";

contract CounterScript is Script {
	function setUp() public {}

	function run() public {
    		vm.broadcast();
    		new Counter();
	}
}

```

Use "forge script" to deploy your contract to ThunderCore:

```
$ forge script --rpc-url thunder-testnet --broadcast --account deployer ./script/Counter.s.sol
[⠰] Compiling...
No files changed, compilation skipped
Enter keystore password:
Script ran successfully.

## Setting up 1 EVM.

==========================

Chain 18

Estimated gas price: 2360 gwei

Estimated total gas used for script: 138734

Estimated amount required: 0.32741224 ETH

==========================
##
Sending transactions [0 - 0].
⠁ [00:00:00] [#] 1/1 txes (0.0s)##
Waiting for receipts.
⠉ [00:00:12] [#] 1/1 receipts (0.0s)
##### 18
✅  [Success]Hash: 0xe459d759617b8f6054646cd0aa37e2c14db66a051747b0c32609b2e3c87c3d78
Contract Address: 0x8133D6f94ae2cB864540BF9a3c736dd6f6F2E426
Block: 1754750
Paid: 0.2454537 ETH (106719 gas * 2300 gwei)



==========================

ONCHAIN EXECUTION COMPLETE & SUCCESSFUL.
Total Paid: 0.2454537 ETH (106719 gas * avg 2300 gwei)

Transactions saved to: (...)/broadcast/Counter.s.sol/18/run-latest.json

```

### Check the deployed contract in Explorer <a href="#docs-internal-guid-bea309e3-7fff-f192-baa0-ed2eb47129f2" id="docs-internal-guid-bea309e3-7fff-f192-baa0-ed2eb47129f2"></a>

Copy the address of your newly deployed contract from the output. Then go to the ThunderCore Blockchain Explorer, and paste the address in the Search by address field.

Mainnet: <https://explorer-mainnet.thundercore.com>

Testnet: <https://explorer-testnet.thundercore.com>

## Interact With The Counter Contract <a href="#docs-internal-guid-b0ef8ca9-7fff-0f45-30c3-0c59c1316c0c" id="docs-internal-guid-b0ef8ca9-7fff-0f45-30c3-0c59c1316c0c"></a>

We can read the Counter value via cast call:

```
$ cast call --rpc-url thunder-testnet <YOUR_CONTRACT_ADDR> 'number()'
0x0000000000000000000000000000000000000000000000000000000000000000
```

We can increment the Counter value via "cast send":

```
$ cast send --rpc-url thunder-testnet --account deployer <YOUR_CONTRACT_ADDR> 'increment()'
Enter keystore password:

blockHash           	0x03cdd8cb4bafb1028d3893ac34abd68b2f92a0b13c193dcf4b202812a4d2225d
(...)

```

After a successful transaction that calls increment , reading the counter value again should now return 1 instead of 0.

## Conclusion <a href="#docs-internal-guid-55b41487-7fff-adde-6ada-e1f85108fc43" id="docs-internal-guid-55b41487-7fff-adde-6ada-e1f85108fc43"></a>

In this guide, we've accomplished:

1. Created a new wallet in the Foundry keystore named deployer ($HOME/.foundry/deployer)
2. Obtained funds for gas to the deployer account
3. Created a new Foundry project
4. Configured that project for ThunderCore via foundry.toml
5. Deployed our contract to the ThunderCore blockchain
6. Interacted with our contract to read and write information

And that's the basics of how to develop on ThunderCore via Foundry! Remember the finished code is available at <https://github.com/thundercore/foundry-thundercore>
