<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Samarth Saxena</title>
    <description>The latest articles on DEV Community by Samarth Saxena (@awesamarth).</description>
    <link>https://dev.to/awesamarth</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2237950%2Ffa8e6129-4fd3-4d22-b293-ec5ef52a4fb6.jpeg</url>
      <title>DEV Community: Samarth Saxena</title>
      <link>https://dev.to/awesamarth</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/awesamarth"/>
    <language>en</language>
    <item>
      <title>Mainnet Forking in Foundry</title>
      <dc:creator>Samarth Saxena</dc:creator>
      <pubDate>Fri, 15 Nov 2024 15:53:25 +0000</pubDate>
      <link>https://dev.to/awesamarth/mainnet-forking-in-foundry-79f</link>
      <guid>https://dev.to/awesamarth/mainnet-forking-in-foundry-79f</guid>
      <description>&lt;h1&gt;
  
  
  Introduction
&lt;/h1&gt;

&lt;p&gt;When developing smart contracts, we often need to interact with contracts that have already been deployed. It would not be a smart decision to deploy the contract and test it on Mainnet using real ETH. But, thankfully for us, there is a solution and that is what we will be covering in this guide. We'll keep it short and simple for now.&lt;/p&gt;

&lt;h1&gt;
  
  
  What is forking?
&lt;/h1&gt;

&lt;p&gt;Forking in this context is very similar forking a GitHub repository. Just like you are able to get the entirety of the repo in your account and make changes to it without affecting the original repo, forking a blockchain makes its entire state up till the time of fork available to you for interaction locally without actually sending transactions to that chain. No changes are made to the state of the chain you fork from and all your interactions remain on your local blockchain node.&lt;/p&gt;

&lt;h1&gt;
  
  
  Forking any Network
&lt;/h1&gt;

&lt;p&gt;The process of forking a chain is pretty simple in Foundry. All you need is an RPC URL for the network you want to fork. Mind you, if you use a public RPC, chances are that it won't contain the entire state of the blockchain up till that time and calling functions on contracts may not work as you'd expect them to. Moreover, you may get rate limited. If you have a key from QuickNode, Infura or other providers, you should use that instead.&lt;/p&gt;

&lt;p&gt;For this example, we'll fork Ethereum Mainnet. Spin up a local blockchain node using Anvil like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;anvil --fork-url mainnet_rpc_url
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This might take more time than when we run just &lt;code&gt;anvil&lt;/code&gt;, as it should, since we are connecting an RPC endpoint to our node for interacting with the state of the chain up till that point. All the transactions we perform after this will remain on our local anvil node, unless we specify that we want to send them to some other network.&lt;/p&gt;

&lt;h1&gt;
  
  
  Check if it worked
&lt;/h1&gt;

&lt;p&gt;Let's see if we were able to successfully fork Ethereum Mainnet&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cast block-number
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For a node started using just &lt;code&gt;anvil&lt;/code&gt;, ie. without the &lt;code&gt;fork-url&lt;/code&gt; option, we would have received 0 as the output. But in our case, you should see something like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpihb7vbrch5jrzvlnbsc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpihb7vbrch5jrzvlnbsc.png" width="671" height="48"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is the block number at which the fork was created. So our local node can query the state of the blockchain upto this block in the live chain.&lt;/p&gt;

&lt;p&gt;For further checking, let's try to see the balance of the account with the ENS name &lt;code&gt;ben.eth&lt;/code&gt;, whose address is &lt;code&gt;0xcd2e72aebe2a203b84f46deec948e6465db51c75&lt;/code&gt;. Run this command in your terminal&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cast balance 0x91364516d3cad16e1666261dbdbb39c881dbe9ee
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the result is 0, make sure you've followed all the steps correctly. If the result is 0 even after that, ben.eth's wallet probably got hacked and drained. Unfortunate.&lt;/p&gt;

&lt;h1&gt;
  
  
  A glimpse of what can be
&lt;/h1&gt;

&lt;p&gt;When you want to run tests on the forked blockchain, you'll do so in the following manner:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function setUp() public { 
// vm is a variable included in the forge standard library that is used to manipulate the execution environment of our tests 
// create a fork of Ethereum mainnet using the specified RPC URL and store its id in mainnetFork 
mainnetFork = vm.createFork(MAINNET_RPC_URL); 
//select the fork thus obtained, using its id vm.selectFork(mainnetFork); 
//.... calls on contracts which exist on the forked chains}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Don't worry, we'll cover smart contract testing soon in this series. I added this snippet just to show you how what's possible.&lt;/p&gt;

&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;That's it! You've successfully forked a live chain and called functions on it! In the next guide, we'll learn how to verify smart contracts using Etherscan's API via Foundry. Stay tuned till then. Thanks a lot for reading to the end. Bye for now👋👋&lt;/p&gt;

</description>
      <category>blockchain</category>
      <category>solidity</category>
      <category>smartcontracts</category>
      <category>foundry</category>
    </item>
    <item>
      <title>How to deploy smart contracts using Foundry</title>
      <dc:creator>Samarth Saxena</dc:creator>
      <pubDate>Fri, 01 Nov 2024 13:09:45 +0000</pubDate>
      <link>https://dev.to/awesamarth/how-to-deploy-smart-contracts-using-foundry-4no8</link>
      <guid>https://dev.to/awesamarth/how-to-deploy-smart-contracts-using-foundry-4no8</guid>
      <description>&lt;h1&gt;
  
  
  Introduction
&lt;/h1&gt;

&lt;p&gt;If you've been following this series so far, you already know how to write and compile smart contracts in Foundry. You also know how to use keystores to safely store and use your private keys. Now it is time to learn how you can deploy smart contracts. This article will cover both local and on-chain deployment.&lt;/p&gt;

&lt;h1&gt;
  
  
  Which way, Solidity dev?
&lt;/h1&gt;

&lt;p&gt;There are two ways you can deploy your contracts using Foundry- the first is by using the &lt;code&gt;forge create&lt;/code&gt; command and the second is by using Solidity scripts. Solidity scripting is a way to declaratively deploy contracts using Solidity, instead of using the more limiting and less user friendly &lt;code&gt;forge create&lt;/code&gt;. If you are deploying a simple contract which has no constructor arguments and no reliance on other contracts that need to be deployed, it makes sense to just use &lt;code&gt;forge create&lt;/code&gt; instead of making a separate script.&lt;/p&gt;

&lt;p&gt;If you're coming from a Hardhat background, think of Solidity scripts as the scripts you write in Hardhat, except they're in Solidity instead of JavaScript, and they are run on the fast Foundry EVM backend, which provides dry-run capabilities.&lt;/p&gt;

&lt;p&gt;We'll cover both ways in this article.&lt;/p&gt;

&lt;h1&gt;
  
  
  Initial Setup
&lt;/h1&gt;

&lt;p&gt;If you haven't created a foundry project, go ahead and create one using this command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;forge init project-name
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Open &lt;code&gt;src/Counter.sol&lt;/code&gt; and add a constructor function to the contract&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;constructor (uint _initNumber){ number= _initNumber;}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Your final code should look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// SPDX-License-Identifier: UNLICENSEDpragma solidity ^0.8.13;contract Counter { uint256 public number; constructor (uint _initNumber){ number= _initNumber; } function setNumber(uint256 newNumber) public { number = newNumber; } function increment() public { number++; }}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Navigate to your projects folder using your terminal and compile the contract like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cd project-nameforge build# you can also use forge compile
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You should see something like this in your terminal:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fj5h6yi28yjskpm5etgxr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fj5h6yi28yjskpm5etgxr.png" width="452" height="97"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We'll use &lt;code&gt;anvil&lt;/code&gt; to start a local blockchain with chainId 31337. Go to your terminal and run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;anvil
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You'll then see a list of accounts and their corresponding private keys which already have a balance of 10000 ETH on chain ID 31337. Copy one of these private keys, make a &lt;code&gt;.env&lt;/code&gt; file in the root of your project and paste it there like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;LOCAL_PRIVATE_KEY=thecopiedkey
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We're not using keystores for local deployments since this is a known private key which everyone sees when using Foundry and is only used locally.&lt;/p&gt;

&lt;p&gt;Now enter the URL of a Sepolia RPC endpoint in the &lt;code&gt;.env&lt;/code&gt; file. I'm using a public RPC in this example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SEPOLIA_RPC_URL=https://1rpc.io/sepolia
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now open your &lt;code&gt;foundry.toml&lt;/code&gt; file. It should be present in the root of the project. At the end of the file, add:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[rpc_endpoints]sepolia = "${SEPOLIA_RPC_URL}"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This creates an RPC alias for Sepolia and provides cheatcodes to access the RPC endpoint we added.&lt;/p&gt;

&lt;p&gt;Keep your &lt;code&gt;anvil&lt;/code&gt; terminal running. Open a new terminal and run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;source .env
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This updates your terminals environment variables and adds the ones we added in the &lt;code&gt;.env&lt;/code&gt; file.&lt;/p&gt;

&lt;p&gt;Awesome! Now let's see how we can deploy our contract&lt;/p&gt;

&lt;h1&gt;
  
  
  Deploying via forge create
&lt;/h1&gt;

&lt;p&gt;The contract that needs to be deployed is a pretty simple one and it takes one constructor argument. Let's see how we can deploy it using &lt;code&gt;forge create&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Local Deployment
&lt;/h2&gt;

&lt;p&gt;In your terminal, run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;forge create --rpc-url http://localhost:8545 --private-key $LOCAL_PRIVATE_KEY src/Counter.sol:Counter --constructor-args 23
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We've specified the constructor argument as 23 here, so the value of &lt;code&gt;number&lt;/code&gt; in our contract will be set to 23. If the transaction goes through, you'll see something like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8fn1j9howmxjveuoyk4x.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8fn1j9howmxjveuoyk4x.png" width="800" height="119"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Sepolia Deployment
&lt;/h2&gt;

&lt;p&gt;This process is pretty similar to deploying locally. After all, we're only deploying to a chain which has a different ID. The only things we'll need are an account which has Sepolia ETH and an RPC URL. For accessing an account which has Sepolia ETH, we'll use keystores. We already set up one such account in the previous article and named it dev. You can read it &lt;a href="https://dev.to/awesamarth/the-best-way-to-import-your-private-key-in-foundry-hld-temp-slug-9431996"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;In your terminal, run this command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;forge create --rpc-url $SEPOLIA_RPC_URL --account dev src/Counter.sol:Counter --constructor-args 23
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This time we're using our keystore, so it will ask you to enter its password. If you enter the right password and the transaction goes through, you'll see something like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjd8zkzj6l5gu431bni9y.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjd8zkzj6l5gu431bni9y.png" width="800" height="132"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Deploying via scripts
&lt;/h1&gt;

&lt;p&gt;Open &lt;code&gt;script/Counter.sol&lt;/code&gt;. For the most part, the required code is already written there. Lets go through it step-by-step&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// SPDX-License-Identifier: UNLICENSEDpragma solidity ^0.8.13;import {Script, console} from "forge-std/Script.sol";import {Counter} from "../src/Counter.sol";contract CounterScript is Script { Counter public counter; function setUp() public {} function run() public { vm.startBroadcast(); counter = new Counter(); vm.stopBroadcast(); }}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;import&lt;/code&gt; statements get &lt;code&gt;Script&lt;/code&gt; and &lt;code&gt;console&lt;/code&gt; from the forge standard library, and the Counter contract we modified.&lt;/p&gt;

&lt;p&gt;We then specify that the contract in this file inherits the &lt;code&gt;Script&lt;/code&gt; contract from the forge standard library. Notice that everything in Foundry is a Solidity smart contract.&lt;/p&gt;

&lt;p&gt;Then, we declare a new variable &lt;code&gt;counter&lt;/code&gt; of the type &lt;code&gt;Counter&lt;/code&gt; (our contract)&lt;/p&gt;

&lt;p&gt;&lt;code&gt;setUp&lt;/code&gt; is a function that runs every time we run a script. Since we don't have anything to set up right now, we'll leave this empty.&lt;/p&gt;

&lt;p&gt;By default, scripts are executed by calling the function named &lt;code&gt;run&lt;/code&gt;. In &lt;code&gt;run&lt;/code&gt;, everything between &lt;code&gt;vm.startBroadcast&lt;/code&gt; and &lt;code&gt;vm.stopBroadcast&lt;/code&gt; creates transactions that can later be signed and sent on-chain- local, testnet, or mainnet.&lt;/p&gt;

&lt;p&gt;If you have the right extensions for Solidity, you might be seeing an error in the line which creates a new instance of Counter.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvxmhblg4hdrmmabb5l45.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvxmhblg4hdrmmabb5l45.png" width="800" height="97"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is because we now need to specify a value of type &lt;code&gt;uint256&lt;/code&gt; as the argument of our contracts constructor. Just add a number in the brackets like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;counter = new Counter(2);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will set the number to be 2 when the contract is deployed.&lt;/p&gt;

&lt;h2&gt;
  
  
  Local Deployment
&lt;/h2&gt;

&lt;p&gt;Just run this command in your terminal:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;forge script script/Counter.s.sol:CounterScript --fork-url http://localhost:8545 --broadcast --private-key $LOCAL_PRIVATE_KEY
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here &lt;code&gt;http://localhost:8545&lt;/code&gt; is the URL that is used to fetch the state of the local chain (id 31337) and then sending the transaction to it. You should see something like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu206ninkf28q4u3on938.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu206ninkf28q4u3on938.png" width="800" height="390"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;and in the terminal running &lt;code&gt;anvil&lt;/code&gt;:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Febksvrb7oenn9a64pbwk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Febksvrb7oenn9a64pbwk.png" width="792" height="275"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Sepolia Deployment
&lt;/h2&gt;

&lt;p&gt;Again, this process is pretty similar to deploying locally and we only need an account which has Sepolia ETH along with an RPC URL for Ethereum Sepolia. We'll use keystores for this case too.&lt;/p&gt;

&lt;p&gt;Open your terminal and write:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;forge script --chain sepolia script/Counter.s.sol:CounterScript --rpc-url $SEPOLIA_RPC_URL --broadcast --account dev
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It will ask you to enter the password of the keystore (dev). If you enter the correct password, it attempts to send your transaction using the account corresponding to the keystore. If it succeeds, you'll get something like this&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqmeeq1uu2o65a42yvjc3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqmeeq1uu2o65a42yvjc3.png" width="800" height="453"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;That's pretty much it! You now know how you can deploy your contracts locally and on-chain using keystores and environment variables. In the next lesson, we'll learn how we can fork Ethereum Mainnet to get the entire state of the blockchain locally. Sounds interesting? Well that's because it is! Stay tuned, see you all soon. Thanks a lot for reading to the end 🫡🫡&lt;/p&gt;

</description>
      <category>solidity</category>
      <category>smartcontracts</category>
      <category>foundry</category>
      <category>hardhat</category>
    </item>
    <item>
      <title>The best way to import your private key in Foundry</title>
      <dc:creator>Samarth Saxena</dc:creator>
      <pubDate>Sat, 12 Oct 2024 08:42:07 +0000</pubDate>
      <link>https://dev.to/awesamarth/the-best-way-to-import-your-private-key-in-foundry-bkf</link>
      <guid>https://dev.to/awesamarth/the-best-way-to-import-your-private-key-in-foundry-bkf</guid>
      <description>&lt;h1&gt;
  
  
  Introduction
&lt;/h1&gt;

&lt;p&gt;In my 2-something years of being a web3 dev, I've seen quite a lot of my frens and oomfs on Twitter lose all their funds because of one silly mistake: pushing their private keys to GitHub. It may sound like a very obvious and easy-to-avoid mistake but its more common than one may think. In this guide, we'll cover the most correct method of using your private key in Foundry. This method optimally balances convenience and safety. Let's get started!&lt;/p&gt;

&lt;h1&gt;
  
  
  Development Account
&lt;/h1&gt;

&lt;p&gt;It is considered best practice to create a separate account in your wallet for development purposes and not add any Mainnet tokens to it. Wallets like MetaMask and Rainbow allow you to give names to your accounts, so make sure to give it a name like dev as it will help you to easily identify which account you're on. If you have to deploy a contract to Mainnet, add just enough funds to ensure your transaction goes through. This separation of accounts will prevent you from getting rugged even if your private key somehow gets leaked.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ff3v3gcl2meo2u0d9qg2l.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ff3v3gcl2meo2u0d9qg2l.png" width="432" height="729"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;account with testnet funds&lt;/em&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Keystores in Foundry
&lt;/h1&gt;

&lt;p&gt;Cast will allow you to import your private key into an encrypted keystore. The command for doing this is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cast wallet import account_name --interactive
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It will then ask you to enter your private key. Once you do that, it will ask you to enter a password for that key. Whenever you want to use that key, you'll have to enter your password for authorization, so make sure you remember it. Once you've entered a password, it will be used to encrypt your private key and a keystore will be created. By default, its location will be &lt;code&gt;~/.foundry/keystores&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbvci4c6tg96aqil9yxmf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbvci4c6tg96aqil9yxmf.png" width="800" height="84"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This method ensures that your private key is not stored in plaintext anywhere and can only be decrypted by someone who has its password. You can import more accounts in a similar fashion and for each one, you'll be asked to enter a corresponding password.&lt;/p&gt;

&lt;p&gt;You can also import wallets by using their mnemonic phrases like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cast wallet import arbitrary_name --mnemonic "test test test test test test test test test test test test"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Using private keys
&lt;/h1&gt;

&lt;p&gt;When you're deploying a contract, you will have to use your private key. For getting the list of available keystores, run this command in your terminal&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cast wallet list
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will show you a list of imported keystores&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fm1seub6p2w7pxe0k6w0j.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fm1seub6p2w7pxe0k6w0j.png" width="678" height="41"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We still haven't covered how to deploy contracts using Foundry, but for now, just know that for using the private keys you imported into keystores, the process is as simple as adding the account name to the command and then entering your password. Here's what one of the methods of deployment looks like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;forge create ContractName --rpc-url rpc_url --account account_name
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once you run this command, you will be prompted to enter the password that corresponds to &lt;code&gt;account_name&lt;/code&gt;. If the password is correct, your private key gets decrypted and is used to deploy the contract via the RPC URL specified. Don't worry too much if you don't understand this command as well cover it in the next guide.&lt;/p&gt;

&lt;h1&gt;
  
  
  The Pledge and a word of caution
&lt;/h1&gt;

&lt;p&gt;In May 2022, &lt;a href="https://x.com/PatrickAlphaC" rel="noopener noreferrer"&gt;Patrick Collins&lt;/a&gt; created the .env pledge, by taking which you promise to never use .env files for storing private keys of accounts that have mainnet funds in them. This pledge has evolved over time as now you don't even need env files for deploying contracts, like we just did. You can go ahead and read the pledge &lt;a href="https://github.com/smartcontractkit/full-blockchain-solidity-course-js/discussions/5" rel="noopener noreferrer"&gt;here&lt;/a&gt;. Make sure to read it to the end and comment I WILL BE SAFE if you agree to take this pledge.&lt;/p&gt;

&lt;p&gt;A leaked &lt;code&gt;.env&lt;/code&gt; file is not the only reason why developers wallets get hacked. If someone on Twitter or anywhere else asks you to download their software for testing and debugging, consider it an immediate red flag and run as fast as you can. Any shady software that you install has the potential to be a tool for stealing your private keys, because in the end, even MetaMask stores your keys offline and encrypts them with your password. If this password somehow gets stolen by the shady software in question, it can use it to decrypt your keys and steal your funds.&lt;/p&gt;

&lt;p&gt;The bottom line is, you have to be safe at every step of the way in your web3 dev journey.&lt;/p&gt;

&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;Congratulations! You now know how to safely import your private key into a keystore and use it to deploy contracts. There is more than one way of deploying contracts using Foundry. In the next guide well dive deeper into this process and learn how to deploy smart contracts locally and on-chain. See you then 🫡🫡&lt;/p&gt;

</description>
      <category>ethereum</category>
      <category>solidity</category>
      <category>foundry</category>
      <category>blockchain</category>
    </item>
    <item>
      <title>How to easily import dependencies and use remappings in Foundry</title>
      <dc:creator>Samarth Saxena</dc:creator>
      <pubDate>Thu, 03 Oct 2024 10:13:47 +0000</pubDate>
      <link>https://dev.to/awesamarth/how-to-easily-import-dependencies-and-use-remappings-in-foundry-25p</link>
      <guid>https://dev.to/awesamarth/how-to-easily-import-dependencies-and-use-remappings-in-foundry-25p</guid>
      <description>&lt;h1&gt;
  
  
  Introduction
&lt;/h1&gt;

&lt;p&gt;In this &lt;a href="https://awesamarth.hashnode.dev/series/foundry-mode" rel="noopener noreferrer"&gt;series of blogs&lt;/a&gt;, we've so far seen how we can install Foundry and write and compile simple contracts in it. If you have dependencies like &lt;a href="https://docs.openzeppelin.com/contracts/5.x/api/token/erc20" rel="noopener noreferrer"&gt;ERC20 by OpenZeppelin&lt;/a&gt; in your contract, the process of importing and using them is slightly different from what you might be used to. In this short guide, let's see how were supposed to go about it.&lt;/p&gt;

&lt;h1&gt;
  
  
  Importing dependencies
&lt;/h1&gt;

&lt;p&gt;We will not be getting our dependencies from npm. So, instead of writing &lt;code&gt;npm install dependency&lt;/code&gt; like in Hardhat, you'll be using &lt;code&gt;forge install dependency&lt;/code&gt; and here, dependency can be a raw URL (&lt;code&gt;https://foo.com/dep&lt;/code&gt;), an SSH URL (&lt;code&gt;git@github.com:owner/repo&lt;/code&gt;), or the path to a GitHub repository (&lt;code&gt;owner/repo&lt;/code&gt;). If you still have the Foundry project we made in the last blog, point your terminal to it. For this guide, let's suppose we want to use OpenZeppelins ERC20 contract. To do that, run&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;forge install openzeppelin/openzeppelin-contracts
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This clones OpenZeppelins openzeppelin-contracts repository and installs it as a git submodule.&lt;/p&gt;

&lt;p&gt;Now, create a new contract called Token.sol inside &lt;code&gt;src&lt;/code&gt; and past the following code inside it&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// SPDX-License-Identifier: MITpragma solidity ^0.8.25;import "@openzeppelin/contracts/token/ERC20/ERC20.sol";contract NewToken is ERC20 { constructor(string memory _name, string memory _symbol) ERC20(_name, _symbol) { _mint(msg.sender, 10 * 10 ** 18); }}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;At this point, your IDE may be giving you many errors saying that it cannot find OpenZeppelin contracts, but running &lt;code&gt;forge compile&lt;/code&gt; works without errors. Whats going on?&lt;/p&gt;

&lt;h1&gt;
  
  
  Forge remappings
&lt;/h1&gt;

&lt;p&gt;When you run &lt;code&gt;forge install&lt;/code&gt;, Foundry adds your dependencies to the &lt;code&gt;lib&lt;/code&gt; folder. If you click on it right now, you will see 2 folders: &lt;code&gt;forge-std&lt;/code&gt; and &lt;code&gt;openzeppelin-contracts&lt;/code&gt;. The contract we want to use is stored in &lt;code&gt;lib/openzeppelin-contracts/contracts/.ERC721Enumerable.sol&lt;/code&gt; so we can either import it using this path or we can remap &lt;code&gt;lib/openzeppelin-contracts/contracts/&lt;/code&gt; to &lt;code&gt;@openzeppelin/contracts/&lt;/code&gt; so that we can use the format were so used to. Well heres the good part: Foundry does this for us automatically! To see all the remappings inferred by Foundry, run&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;forge remappings
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now to get rid of the errors that we see in our IDEs, we just need to create a file with all the remappings inside. We can do this easily by using:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;forge remappings &amp;gt; remappings.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will create a file called &lt;code&gt;remappings.txt&lt;/code&gt; which will have all the remappings. It should look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@openzeppelin/contracts/=lib/openzeppelin-contracts/contracts/ds-test/=lib/openzeppelin-contracts/lib/forge-std/lib/ds-test/src/erc4626-tests/=lib/openzeppelin-contracts/lib/erc4626-tests/forge-std/=lib/forge-std/src/openzeppelin-contracts/=lib/openzeppelin-contracts/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The path on the left hand side of the &lt;code&gt;=&lt;/code&gt; sign is equivalent to the path on its right hand side in each line.&lt;/p&gt;

&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;That's it! You've successfully installed, imported and remapped dependencies in your Foundry project. Great work! As we inch closer to actually deploying a contract it is important to understand how private your private key is supposed to be. So, in the next blog, well be discussing how you can safely store and use your private key for deploying contracts. See you then 🫡🫡&lt;/p&gt;

</description>
      <category>solidity</category>
      <category>blockchain</category>
      <category>web3</category>
      <category>decentralization</category>
    </item>
    <item>
      <title>How to write and compile smart contracts in Foundry</title>
      <dc:creator>Samarth Saxena</dc:creator>
      <pubDate>Sat, 28 Sep 2024 13:56:11 +0000</pubDate>
      <link>https://dev.to/awesamarth/how-to-write-and-compile-smart-contracts-in-foundry-57l9</link>
      <guid>https://dev.to/awesamarth/how-to-write-and-compile-smart-contracts-in-foundry-57l9</guid>
      <description>&lt;h1&gt;
  
  
  Introduction
&lt;/h1&gt;

&lt;p&gt;In the previous lesson of this series, we saw how we can install Foundry on our machines. ICYMI, go ahead and read it &lt;a href="https://dev.to/awesamarth/how-to-install-foundry-on-windowsmacoslinux-2pjg-temp-slug-8495651"&gt;here&lt;/a&gt;. What good is a tool if it isn't being put to use? So, in this lesson, we'll learn how to create a new Foundry project, write smart contracts in it, and compile these contracts. Like the last one, this is also going to be a short guide as there's not really much to it.&lt;/p&gt;

&lt;h1&gt;
  
  
  Creating a new Foundry project
&lt;/h1&gt;

&lt;p&gt;For creating a new Foundry project, open your terminal and run this command&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;forge init name-of-folder
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Just this one command will initialize a Foundry project with some boilerplate code written for us already. Now &lt;code&gt;cd&lt;/code&gt; into the folder you just created and open your code editor there. If you're using VS Code, you can just do:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cd name-of-foldercode . -r
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  What does what
&lt;/h1&gt;

&lt;p&gt;Here's what your file structure should look like at this point:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhdc8u613snu41b6hs2sn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhdc8u613snu41b6hs2sn.png" width="299" height="404"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Contracts
&lt;/h2&gt;

&lt;p&gt;All your contracts should be inside &lt;code&gt;src&lt;/code&gt;. As visible, we've been provided with a contract called Counter that is written in the file &lt;code&gt;Counter.sol&lt;/code&gt;. This is a simple contract which allows the users to arbitrarily set the &lt;code&gt;number&lt;/code&gt; variable or just increment it. Feel free to make changes to this contract.&lt;/p&gt;

&lt;h2&gt;
  
  
  Scripts
&lt;/h2&gt;

&lt;p&gt;Your scripts should go inside the &lt;code&gt;scripts&lt;/code&gt; folder. Unlike Hardhat, scripts are written in Solidity itself and are used to deploy contracts declaratively. You can take a look at the example script for our Counter contract which has been provided in this folder already and you'll see that the &lt;code&gt;run&lt;/code&gt; function starts a broadcast, ie. allows making transactions (here, using the default Foundry sender &lt;code&gt;1804c8AB1F12E6bbf3894d4083f33e07309d1f38&lt;/code&gt; since we haven't provided an account in &lt;code&gt;startBroadcast&lt;/code&gt;s parameters) that can be sent onchain, creates a new instance of the Counter contract and then stops the broadcast.&lt;/p&gt;

&lt;h2&gt;
  
  
  Tests
&lt;/h2&gt;

&lt;p&gt;Tests for your smart contracts should go inside the &lt;code&gt;tests&lt;/code&gt; folder. Again, unlike Hardhat, we use Solidity to write tests instead of JavaScript. A sample test file with 2 tests has been provided to us. Go through the file &lt;code&gt;Counter.t.sol&lt;/code&gt; and you'll see that the first test is checking whether the increment functionality of the contract is working, and the second test is trying to use a random number as the parameter of the &lt;code&gt;setNumber&lt;/code&gt; function in the Counter contract. We'll discuss testing in more details as this series progresses.&lt;/p&gt;

&lt;h1&gt;
  
  
  Writing contracts
&lt;/h1&gt;

&lt;p&gt;Now let's write a contract. Create a new file called &lt;code&gt;Greeter.sol&lt;/code&gt; in the &lt;code&gt;src&lt;/code&gt; folder. In it, write the following code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//SPDX-License-Identifier: MITpragma solidity ^0.8.24;contract Greeter { string public greeting; function setGreeting(string memory _greeting) public { greeting = _greeting; }}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is another very simple contract that lets the user change a public variable called &lt;code&gt;greeting&lt;/code&gt;.&lt;/p&gt;

&lt;h1&gt;
  
  
  Compiling contracts
&lt;/h1&gt;

&lt;p&gt;To compile all the contracts inside the &lt;code&gt;src&lt;/code&gt; folder, just write this one-line command in your terminal&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;forge build# orforge compile
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The output should be something like this&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2ddrzlyncdzltu96nwf5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2ddrzlyncdzltu96nwf5.png" width="632" height="118"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you want to only compile a particular file, change the command in this fashion:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;forge build directory/name_of_file.sol# for example:forge build src/Greeter.sol
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You'll now see something like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frtqlst3u93l94azon60x.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frtqlst3u93l94azon60x.png" width="730" height="105"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;That's it! Youve successfully created and compiled a smart contract! You now also know what the file structure of a Foundry project looks like and which files go where. Note that we haven't deployed any contracts yet. There are two more blogs that precede it. Ill be publishing those 2 in the coming few days. Till then, stay tuned. Thanks a lot for reading to the end. I hope you learnt something new! See you in the next one 🫡🫡&lt;/p&gt;

</description>
      <category>foundry</category>
      <category>blockchain</category>
      <category>solidity</category>
      <category>smartcontracts</category>
    </item>
    <item>
      <title>How to Install Foundry on Windows/macOS/Linux</title>
      <dc:creator>Samarth Saxena</dc:creator>
      <pubDate>Tue, 24 Sep 2024 13:17:06 +0000</pubDate>
      <link>https://dev.to/awesamarth/how-to-install-foundry-on-windowsmacoslinux-2kmi</link>
      <guid>https://dev.to/awesamarth/how-to-install-foundry-on-windowsmacoslinux-2kmi</guid>
      <description>&lt;h1&gt;
  
  
  Introduction
&lt;/h1&gt;

&lt;p&gt;If you are a web3 developer, I'm sure you must have heard of Foundry somewhere- maybe in a YouTube video, maybe in a Twitter thread, or maybe from a friend of yours. Foundry is a smart contract development toolchain. You can write, compile, deploy and test smart contracts as well as interact with them using Foundry.&lt;/p&gt;

&lt;h2&gt;
  
  
  Foundry vs Remix IDE
&lt;/h2&gt;

&lt;p&gt;You may be asking yourself: why should I choose Foundry over Remix IDE? Well, first of all, Foundry is installed locally on your system. So if you're making, let's say, a website, you can initialize a Foundry project alongside your other files and then make changes there directly. This allows you to structure your files in an organized manner and work quickly. Moreover, Remix can be pretty unreliable sometimes and you might end up losing all your progress unexpectedly. Most importantly however, Foundry shines through with its testing suite which allows you test your contracts significantly faster than Remix and it thus provides a seamless development experience.&lt;/p&gt;

&lt;p&gt;OK enough yapping. Now without further ado, let's see how we can install this Swiss Army Knife on our machines.&lt;/p&gt;

&lt;h1&gt;
  
  
  Installing on Linux/macOS
&lt;/h1&gt;

&lt;p&gt;If you have Linux or macOS, the process is pretty straightforward.&lt;/p&gt;

&lt;p&gt;First download Foundryup, which is the official installer for the Foundry toolchain, by running this command in your terminal&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;curl -L https://foundry.paradigm.xyz | bash
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Follow the on-screen instructions and the &lt;code&gt;foundryup&lt;/code&gt; command will become available in your terminal. Then, just run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;foundryup
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will automatically install the latest (nightly) versions of the: &lt;code&gt;forge&lt;/code&gt;, &lt;code&gt;cast&lt;/code&gt;, &lt;code&gt;anvil&lt;/code&gt;, and &lt;code&gt;chisel&lt;/code&gt; tools, which are a part of Foundry.&lt;/p&gt;

&lt;p&gt;And that's pretty much it lmao. Linux users have it easy this time. A welcome change, I'm sure. Jump to the Did it work section.&lt;/p&gt;

&lt;h1&gt;
  
  
  Installing on Windows
&lt;/h1&gt;

&lt;p&gt;You can just use WSL and then refer to the commands in the previous section. However, if you don't want to install WSL, read on.&lt;/p&gt;

&lt;p&gt;Make sure you have the Rust compiler and Cargo already installed on your machine. If you don't, download rustup from &lt;a href="https://rustup.rs/" rel="noopener noreferrer"&gt;this link&lt;/a&gt; and run it to install both. You'll also need a recent version of Visual Studio, with the Desktop Development With C++ workload installed. Rustup will prompt you to install it. This is what you'll see when you run the downloaded file:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmcm73y4ap0i6rzleg4dz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmcm73y4ap0i6rzleg4dz.png" alt="Enter 1 here" width="800" height="456"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Enter 1 here and let the setup run&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Once Visual Studio is installed, you will get this screen:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6x8xxd3evye3keq7k53z.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6x8xxd3evye3keq7k53z.png" width="800" height="456"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;(just press enter here)&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;If everything is successful, you will get something like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Folg2l5mwn6cfm9b6pw3o.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Folg2l5mwn6cfm9b6pw3o.png" width="725" height="230"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Awesome! Now press enter, relaunch your terminal and run this command&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cargo install --git https://github.com/foundry-rs/foundry --profile release --locked forge foundry-cast chisel anvil
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This command will install the Foundry suite, ie. Forge, Cast, Anvil and Chisel on your machine. This might take some time as it will download and compile multiple &lt;a href="https://doc.rust-lang.org/rust-by-example/crates.html" rel="noopener noreferrer"&gt;crates&lt;/a&gt;. This will happen for all 4 tools so grab a cup of coffee or something while you wait. Once that is done, you'll see:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwc23sd2st4206al5i8bf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwc23sd2st4206al5i8bf.png" width="800" height="111"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;💡&lt;/p&gt;

&lt;p&gt;Don't worry if it seems stuck at any point&lt;/p&gt;

&lt;h1&gt;
  
  
  Did it work?
&lt;/h1&gt;

&lt;p&gt;To check if it worked, relaunch your terminal and run these commands one by one. Each should give you some output&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;forge --version

anvil --version

chisel --version

cast --version
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The output should be something like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxcv03r8brgxcy5wcsmti.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxcv03r8brgxcy5wcsmti.png" width="800" height="454"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;Congratulations, you've successfully installed Foundry on your machine! In the next instalment of this series, we'll see how we can create a new Foundry project and write smart contracts in it. Stay tuned till then. Bye for now! 👋👋&lt;/p&gt;

</description>
      <category>blockchain</category>
      <category>web3</category>
      <category>foundry</category>
      <category>ethereum</category>
    </item>
  </channel>
</rss>
