(1) RAD::Foundry setup and contracts

IsaIsa
4 min read

Forging the base

After setting up Radicle, we need to setup Foundry and start to build our contracts. 'Foundry 101' has a Github repo that we'll utilise for our code to form the base, then, following along make changes where needed. Radicle will replace any add, commits and pushes usually done to Github, lets hope we don't run into too many issues along the way.

forge init

Oops.. What's this?!

As we've created a README.md document in the working directory (previous article), we need to use the --force command when initialising Foundry.

forge init --force

Viola!

Target directory is not empty, but `--force` was specified
Initializing /home/pxng0lin/web3/projects/radicle_xyz/rad-foundry-fund-me...
Installing forge-std in /home/pxng0lin/web3/projects/radicle_xyz/rad-foundry-fund-me/lib/forge-std (url: Some("https://github.com/foundry-rs/forge-std"), tag: None)
Cloning into '/home/pxng0lin/web3/projects/radicle_xyz/rad-foundry-fund-me/lib/forge-std'...
remote: Enumerating objects: 2310, done.
remote: Counting objects: 100% (2305/2305), done.
remote: Compressing objects: 100% (805/805), done.
remote: Total 2310 (delta 1534), reused 2145 (delta 1428), pack-reused 5
Receiving objects: 100% (2310/2310), 658.95 KiB | 7.66 MiB/s, done.
Resolving deltas: 100% (1534/1534), done.
    Installed forge-std v1.8.2
    Initialized forge project

Nothing is really different at this stage, I noticed the .github/workflows was created with the test.yml file, will see if this has any significance later.

image of directories and files in vs code editor after init

We make our first copy-paste here now, using the Github repo from the course resources.

  • Creating a new file named FundMe.sol in the src directory

  • Then we navigate to the remix-fund-me-f23/FundMe.sol contract in the repo and paste this into our new FundMe.sol file.

  • Repeat the steps for the second contract PriceConverter.sol


Radicle push

Now, since we are using Radicle, I want to test that my changes are working.

Following the Radicle user guide, we will commit and push the changes to my Radicle repo.

Once you’re finished, add and commit your changes with git add and git commit just as you would when collaborating on any other Git repository. Then use git push rad master to synchronize the changes with your node (be sure to replace master with your default branch, in case that’s not it).

So for my rad push, I'll be pushing to the main, as this was the branch name I changed to upon initialisation.

git push rad main

Error!

error: error connecting to ssh-agent: Environment variable `SSH_AUTH_SOCK` not found
error: failed to push some refs to 'rad://z43pr3L72n8wT74KSHcty9fEY5JaL/z6Mkg3Tu7aGDn3pLrshRiaCFLQJwEHyFKTCwYFoKDHts1YV2'

Our first error! It seems not having ssh-agent running is causing issues. I ran the following command to start ssh-agent. The command will check the agent is running, if not, then it will start it; the latter is the case for us.

eval "$(ssh-agent -s)"

After this, we run the rad auth command to ensure our private key is added

rad auth
✓ Passphrase: ********
✓ Radicle key added to ssh-agent

So, lets try again to push to Radicle... Success!

git push rad main
✓ Canonical head updated to 9d054f3d641253a3babd505e0b05807e4065e670
✓ Synced with 4 node(s)

  https://app.radicle.xyz/nodes/seed.radicle.garden/rad:z43pr3L72n8wT74KSHcty9fEY5JaL/tree/9d054f3d641253a3babd505e0b05807e4065e670

To rad://z43pr3L72n8wT74KSHcty9fEY5JaL/z6Mkg3Tu7aGDn3pLrshRiaCFLQJwEHyFKTCwYFoKDHts1YV2
   9ff86ae..9d054f3  main -> main

Imports and Dependencies

Upon running forge build to compile our new contracts, we get some errors. The course explains why and gives the solution, so we will implement this now.

Firstly, we need the repo that we're going to install from, Chainlink. In the terminal we use the command forge install smartcontractkit/chainlink-brownie-contracts --no-commit (notice that we don't use the full address).

Now we need to implement, so our contracts can use them, this is done using remappings in the foundry.toml file. The remapping changes @chainlink/contracts to lib/chainlink/chainlink-brownie-contracts/contracts when importing contracts into our project. It's like a shortcut that lets you use a shorter name to refer to specific locations, making it easier to import contracts.

[profile.default]
src = "src"
out = "out"
libs = ["lib"]
remappings = ["@chainlink/contracts/=lib/chainlink/chainlink-brownie-contracts/contracts"]

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

Save the toml file, go to the terminal and run forge build, Success!

[⠊] Compiling...
[⠢] Compiling 3 files with Solc 0.8.25
[⠆] Solc 0.8.25 finished in 68.45ms
Compiler run successful!

Lastly, a little alpha from Patrick to help with identifying errors in our contracts easier. Adjusting the NotOwner() custom error and prefixing the contract name.

error FundMe__NotOwner();

Ok lets wrap up here, so far so good. I don't want to reinvent the wheel for the course. Rather, this is more to ensure my Radicle repo is capturing my changes, and we've achieved this so far.

0
Subscribe to my newsletter

Read articles from Isa directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Isa
Isa

Former analyst with expertise in data, forecasting, and resource modeling, transitioned to cybersecurity over the past 4 years (as of May 2024). Passionate about security and problem-solving, utilising skills in data and analysis, for cybersecurity challenges. Experience: Extensive background in data analytics, forecasting, and predictive modelling. Experience with platforms like Bugcrowd, Intigriti, and HackerOne. Transitioned to Web3 cybersecurity with Immunefi, exploring smart contract vulnerabilities. Spoken languages: English (Native, British), Arabic (Fus-ha)