Using Foundry

This page describes how to deploy smart contracts using Foundry.

Foundry

Foundry is a smart contract development toolchain. Foundry manages your dependencies, compiles your project, runs tests, deploys, and lets you interact with the chain from the command-line and via Solidity scripts.

Foundry book, see: https://book.getfoundry.sh/

Steps

  1. Create a project with Foundry, forge init and cd into it:

forge init hello_foundry
  1. Let's check out what forge generated for us:

cd hello_foundry
tree . -d -L 1
.
├── lib
├── script
├── src
└── test

We Can build the project with forge build :

forge build
[⠊] Compiling...
[⠰] Installing solc version 0.8.17
[⠊] Successfully installed solc 0.8.17
[⠊] Compiling 6 files with 0.8.17
[⠢] Solc 0.8.17 finished in 978.66ms
Compiler run successful

And run the tests with forge test :

forge test
[⠢] Compiling...
No files changed, compilation skipped

Running 2 tests for test/Contract.t.sol:TestContract
[PASS] testBar() (gas: 401)
[PASS] testFoo(uint256) (runs: 256, μ: 3425, ~: 3425)
Test result: ok. 2 passed; 0 failed; finished in 11.55ms
  1. Deploy the contract from your project, located at src/Counter.sol. Replace YOUR_PRIVATE_KEY with your private key, mentioned in the previous prerequisites section.

forge create --rpc-url {$RPC_URL} \
    --value {$LOCK_AMOUNT} \
    --constructor-args {$UNLOCK_TIME} \
    --private-key {$YOUR_PRIVATE_KEY} \
    --legacy \
    src/Counter.sol:Counter
  • --legacy flag means Use legacy transactions instead of EIP1559 ones. This is auto-enabled for common networks without EIP1559.

  • <unlock_time> is the Unix timestamp after which the funds locked in the contract will become available for withdrawal. Try setting this to some Unix timestamp in the future, like 1696118400 (this Unix timestamp corresponds to October 1, 2023).

  • <lock_amount> is the amount of test ETH to be locked in the contract. Try setting this to some small amount, like 0.0000001ether.

  • For information on additional flag options, follow this link

Last updated