diff --git a/docs/02-developers/02-requirements/index.md b/docs/02-developers/02-requirements/index.md index d8552217..536a96ec 100644 --- a/docs/02-developers/02-requirements/index.md +++ b/docs/02-developers/02-requirements/index.md @@ -67,6 +67,23 @@ npm i -g hardhat-shorthand ::: +## Install Foundry +:::note[Development Environments] +You don't need to use both development environments (Hardhat and Foundry) together, you can use your favorite one. +::: + +To install Foundry in your system, run the following command: +```bash +curl -L https://foundry.paradigm.xyz | bash +``` +This will install Foundryup. Simply follow the on-screen instructions, and the ```foundryup``` command will become available in your CLI. + +Running ```foundryup``` will automatically install the latest (nightly) versions of the ```precompiled binaries```: ```forge```, ```cast```, ```anvil```, and ```chisel```. For additional options, such as installing a specific version or commit, run ```foundryup --help```. + +:::note[Windows Users] +If you’re using Windows, you’ll need to install and use [Git BASH](https://gitforwindows.org/) or [WSL](https://learn.microsoft.com/en-us/windows/wsl/install) as your terminal, since Foundryup currently doesn’t support Powershell or Command Prompt (Cmd). +::: + ## Command Line Tools ### POSIX Compliant Shell diff --git a/docs/02-developers/04-quickstart/foundry.md b/docs/02-developers/04-quickstart/foundry.md new file mode 100644 index 00000000..139a4a13 --- /dev/null +++ b/docs/02-developers/04-quickstart/foundry.md @@ -0,0 +1,166 @@ +--- +sidebar_label: Using Foundry +sidebar_position: 500 +title: Rootstock Foundry Starter kit +description: 'Whether you are a seasoned developer or just starting your journey into smart contract development, the foundry starter kit provides a solid foundation for building decentralized applications (dApps) on the Rootstock network.' +tags: [rsk, rootstock, tutorials, developers, foundry, quick starts, dApps, smart contracts] +--- + +Whether you’re a seasoned developer or just starting your journey into smart contract development, the foundry starter kit provides a solid foundation for building decentralized applications (dApps) on the Rootstock network. + +Rootstock is fully EVM (Ethereum Virtual Machine) compatible. It brings the power of smart contracts to Bitcoin, allowing developers to leverage Bitcoin’s security while benefiting from Ethereum’s ecosystem. + +## Prerequisites + +Before starting the dApp, make sure to have the following prerequisites: + +1. **Familiarity with Smart Contracts:** + - If you’re new to smart contracts, consider learning the basics. Understanding how smart contracts work will enhance your experience with Rootstock development. + +2. **Foundry installation using [Foundryup](https://book.getfoundry.sh/getting-started/installation#using-foundryup):** +- This installing information is taken from the official [Foundry documentation](https://book.getfoundry.sh/getting-started/installation#using-foundryup), in case you need to go in detail. +- Foundryup is the official installer for the Foundry toolchain. You can learn more about it [here](https://github.com/foundry-rs/foundry/blob/master/foundryup/README.md). +- If you encounter any issues during installation, refer to the Foundryup [FAQ](https://book.getfoundry.sh/faq.html) for assistance. +- Precompiled binaries can be downloaded from the Foundry [GitHub releases page](https://github.com/foundry-rs/foundry/releases). For easier management, we recommend using Foundryup. + +To install Foundry in your system, run the following command: +```bash +curl -L https://foundry.paradigm.xyz | bash +``` +This will install Foundryup. Simply follow the on-screen instructions, and the ```foundryup``` command will become available in your CLI. + +Running ```foundryup``` will automatically install the latest (nightly) versions of the ```precompiled binaries```: ```forge```, ```cast```, ```anvil```, and ```chisel```. For additional options, such as installing a specific version or commit, run ```foundryup --help```. + +**_NOTE: If you’re using Windows, you’ll need to install and use [Git BASH](https://gitforwindows.org/) or [WSL](https://learn.microsoft.com/en-us/windows/wsl/install) as your terminal, since Foundryup currently doesn’t support Powershell or Command Prompt (Cmd)._** + +3. **Basic Knowledge of Foundry:** +- Familiarity with Foundry's core concepts and functionalities is recommended. If you're new to Foundry, refer to the [Rootstock Foundry Guide](/developers/smart-contracts/foundry/). + +:::tip[Rootstock Blockchain Developer Course] + +Learn how to write, test, secure, deploy and verify smart contracts on the Rootstock blockchain network. Enroll for the [Rootstock Blockchain Developer Course](/resources/courses/). +::: + +## Setting Up the Sample dApp + +### Clone the Repository + +Open your terminal or command prompt and run the following command to clone the repository from GitHub: + +```bash +git clone https://github.com/rsksmart/rootstock-foundry-starterkit.git +``` + +### Install Dependencies + +Navigate to the cloned repository folder: + +```bash +cd rootstock-foundry-starterkit +``` + +Install all required dependencies using forge: + +```bash +forge install OpenZeppelin/openzeppelin-contracts +``` + +### Obtain Rootstock Testnet and Mainnet RPC URLs + +This section will walk you through adding Rootstock Testnet and Mainnet RPC URLs to your development environment. These URLs are essential for connecting your application to the Rootstock network and interacting with smart contracts. + +There are two ways to obtain RPC URLs: + +#### Using Public RPC URLs + +- Visit the [MetaMask Integration on the Rootstock Dev Portal](/dev-tools/wallets/metamask/). This guide provides instructions on setting up MetaMask for Rootstock. While following these steps, pay close attention to the sections on adding custom networks. You'll find the RPC URLs for Rootstock Testnet and Mainnet listed. + +#### Using RPC API +- Create an account at the [Rootstock RPC API](https://rpc.rootstock.io/). Once logged in, navigate to your dashboard and copy the API Key. + + +### Adding environment variables to your project + +After obtaining the RPC URLs, create a file named `.env` in your project's root directory (important: this file should not be committed to version control). Add the necessary environment variables to the `.env` file: +``` +PRIVATE_KEY: Your private key (e.g., from your Metamask account details). +``` +**_NOTE: Make sure the private key you paste, starts with 0x, if does not contain it, please put 0x before your private key. (0x123...)_** + +## Running tests to an ERC20 Token Contract +This section runs tests on an ERC20 token contract (fungible token), this is done according to the script located at ```test/Erc20Token.t.sol```. It does test deployment, minting, and transfer of tokens. + +For this, run the next forge command: + +```bash +forge test +``` + +It should return an output similar to the following: + +```bash +Compiler run successful! + +Ran 2 tests for test/Erc20Token.t.sol:ERC20TokenTest +[PASS] testInitialSupply() (gas: 9849) +[PASS] testTransfer() (gas: 43809) +Suite result: ok. 2 passed; 0 failed; 0 skipped; finished in 8.73ms (1.51ms CPU time) + +Ran 1 test suite in 143.90ms (8.73ms CPU time): 2 tests passed, 0 failed, 0 skipped (2 total tests) +``` +**_NOTE: If you need additional tests, or want to go deep on this step, visit the [Foundry Tests Documentation](https://book.getfoundry.sh/forge/tests)._** + +## Deploying an ERC20 Token Contract +This section deploys an ERC20 token contract (fungible token) on the Rootstock network. This contract is located at ```src/Erc20Token.sol``` file, it uses the script located at ```script/Deploy.s.sol``` for this operation. + +Run the following command, replacing `https://public-node.testnet.rsk.co` with either `rskTestnet` or `rskMainnet` rpc url if you have, depending on your desired deployment environment: + +```bash +forge script script/Deploy.s.sol --rpc-url https://public-node.testnet.rsk.co --broadcast --legacy +``` +:::info[Info] + +- [EIP-1559](https://github.com/ethereum/EIPs/blob/master/EIPS/eip-1559.md) is not supported or not activated on the Rootstock RPC url +- The `--legacy` flag is passed to use legacy transactions instead of `EIP-1559`. +- You can remove the `--broadcast` flag if you wan to simulate the transaction without broadcasting it. +::: + +> If you get an error like `Transaction dropped from the mempool: ` or the ```transaction not completed```, check the tx-id in the explorer. The tx may have went successful but the error is still in the logs. Here are the [mainnet](https://explorer.rootstock.io/) and [testnet](https://explorer.testnet.rootstock.io/) explorers. + +> Also you can see the transaction registry locally, by checking the folder ```broadcast/Deploy.s.sol/``` and opening the file called ```run-latest.json```, if you check the fields, there is one called ```contractAddress``` which contains the new address deployed for our ERC20 smart contract. + +The result should look like this: +```bash +Sending transactions [0 - 0]. +⠁ [00:00:00] [###############################################################################################################################################] 1/1 txes (0.0s)## +Waiting for receipts. +⠉ [00:00:25] [###########################################################################################################################################] 1/1 receipts (0.0s) +##### 31 +✅ [Success]Hash: 0x48ea2b06b39cd436a2d7564e20ea5bb598ddc2769e6b18c855170f0e9e4d5687 +Contract Address: 0x499e802a6825d30482582d9b9dd669ba82ba8ba4 +Block: 5071408 +Gas Used: 106719 + +========================== + +ONCHAIN EXECUTION COMPLETE & SUCCESSFUL. +Total Paid: 0. ETH (106719 gas * avg 0 gwei) +``` + +## Interacting with the Contract +If the contract is already deployed, then you can interact with it using ```cast``` this command allows you to interact with the contract, in this case, read the balance of an account. + +### Reading the Balance of an Account +In your terminal, run the following command, replacing the placeholders with actual values: + +```bash +cast call "balanceOf(address)(uint256)" --rpc-url +``` +The result should look like this: +```bash +1000000000000000000000 [1e21] +``` + +## Final Comments + +Explore and be curious about the folders and files of this starter kit. You'll learn how to import `.env` variables for deploying smart contracts, test smart contracts with solidity. Feel free to customize this starter kit to suit your project’s needs. Happy coding! \ No newline at end of file diff --git a/docs/02-developers/04-quickstart/hardhat.md b/docs/02-developers/04-quickstart/hardhat.md index 1cf88b74..0e4034c4 100644 --- a/docs/02-developers/04-quickstart/hardhat.md +++ b/docs/02-developers/04-quickstart/hardhat.md @@ -8,7 +8,7 @@ tags: [rsk, rootstock, tutorials, developers, hardhat, quick starts, dApps, smar Whether you’re a seasoned developer or just starting your journey into smart contract development, the hardhat starter kit provides a solid foundation for building decentralized applications (dApps) on the Rootstock network. -Rootstock is fully compatible with Ethereum. It brings the power of smart contracts to Bitcoin, allowing developers to leverage Bitcoin’s security while benefiting from Ethereum’s ecosystem. +Rootstock is fully EVM (Ethereum Virtual Machine) compatible. It brings the power of smart contracts to Bitcoin, allowing developers to leverage Bitcoin’s security while benefiting from Ethereum’s ecosystem. ## Prerequisites diff --git a/docs/02-developers/04-quickstart/index.md b/docs/02-developers/04-quickstart/index.md index 5c701930..61370ed7 100644 --- a/docs/02-developers/04-quickstart/index.md +++ b/docs/02-developers/04-quickstart/index.md @@ -36,6 +36,16 @@ values={[ description="Smart Contract examples, Tests, Deployments and Tasks for Common ERC Standards (ERC20, ERC721, ERC1155)." /> + Visit the [installation guides](https://book.getfoundry.sh/getting-started/installation) for more information. + +## Create a foundry project + +To start a new project with Foundry, use [forge init](https://book.getfoundry.sh/reference/forge/forge-init.html). + +```bash +forge init hello_foundry +``` + +> See more details on how to [create a new project](https://book.getfoundry.sh/projects/creating-a-new-project) using the Foundry guide. + diff --git a/docs/02-developers/05-smart-contracts/05-foundry/deploy-smart-contracts.md b/docs/02-developers/05-smart-contracts/05-foundry/deploy-smart-contracts.md new file mode 100644 index 00000000..e9145ab9 --- /dev/null +++ b/docs/02-developers/05-smart-contracts/05-foundry/deploy-smart-contracts.md @@ -0,0 +1,66 @@ +--- +sidebar_label: Deploy Smart Contract +sidebar_position: 105 +title: Deploy Smart Contract +description: "Learn how to deploy your Rootstock smart contract using forge." +tags: [guides, developers, smart contracts, rsk, rootstock, foundry, dApps] +--- + +In this section, you'll deploy a `counter` smart contract to the Rootstock network using Foundry. + +## Step 1: Deployment Script +You will see a directory called `deploy` in the root of your project. This is where you can view/write your deployment scripts. The demo `counter.sol` comes with a deployment script `counter.s.sol`, which contains: + +```solidity +// SPDX-License-Identifier: UNLICENSED +pragma solidity ^0.8.13; + +import {Script, console} from "forge-std/Script.sol"; +import {Counter} from "../src/Counter.sol"; + +contract CounterScript is Script { + function setUp() public {} + + function run() public { + vm.startBroadcast(vm.envUint("PRIVATE_KEY")); + + new Counter(); + + vm.stopBroadcast(); + } +} +``` +## Step 2: Deploy Your Contract on Rootstock Network +Run the following command, replacing `https://public-node.testnet.rsk.co` with either `rskTestnet` or `rskMainnet` rpc url if you have, depending on your desired deployment environment: + +```bash +forge script script/Deploy.s.sol --rpc-url https://public-node.testnet.rsk.co --broadcast --legacy +``` +:::info[Info] + +- [EIP-1559](https://github.com/ethereum/EIPs/blob/master/EIPS/eip-1559.md) is not supported or not activated on the Rootstock RPC url +- The `--legacy` flag is passed to use legacy transactions instead of `EIP-1559`. +- You can remove the `--broadcast` flag if you wan to simulate the transaction without broadcasting it. +::: + +> If you get an error like `Transaction dropped from the mempool: ` or the ```transaction not completed```, check the tx-id in the explorer. The tx may have went successful but the error is still in the logs. Here are the [mainnet](https://explorer.rootstock.io/) and [testnet](https://explorer.testnet.rootstock.io/) explorers. + +> Also you can see the transaction registry locally, by checking the folder ```broadcast/Counter.s.sol/``` and opening the file called ```run-latest.json```, if you check the fields, there is one called ```contractAddress``` which contains the new address deployed for our ERC20 smart contract. + +The result in the console should look like this: +```bash +Sending transactions [0 - 0]. +⠁ [00:00:00] [###############################################################################################################################################] 1/1 txes (0.0s)## +Waiting for receipts. +⠉ [00:00:25] [###########################################################################################################################################] 1/1 receipts (0.0s) +##### 31 +✅ [Success]Hash: 0x48ea2b06b39cd436a2d7564e20ea5bb598ddc2769e6b18c855170f0e9e4d5687 +Contract Address: 0x499e802a6825d30482582d9b9dd669ba82ba8ba4 +Block: 5071408 +Gas Used: 106719 + +========================== + +ONCHAIN EXECUTION COMPLETE & SUCCESSFUL. +Total Paid: 0. ETH (106719 gas * avg 0 gwei) +``` diff --git a/docs/02-developers/05-smart-contracts/05-foundry/index.md b/docs/02-developers/05-smart-contracts/05-foundry/index.md index 16f95160..4182699c 100644 --- a/docs/02-developers/05-smart-contracts/05-foundry/index.md +++ b/docs/02-developers/05-smart-contracts/05-foundry/index.md @@ -1,193 +1,27 @@ --- -section_position: 200 sidebar_label: Getting Started with Foundry +section_position: 200 title: Getting Started with Foundry description: 'How to write, test, and deploy smart contracts with Foundry' tags: [rsk, foundry, developers, developer tools, rootstock, testing, dApps, smart contracts] --- -In this guide, we will learn about Foundry and its benefits for smart contract development, how to setup your environment, create a Foundry project and execute a deployment script. - -## Prerequisites - -To get started with Foundry, ensure the following tools are installed: -- The [Rust](https://rust-lang.org/) Compiler -- Cargo Package Manager - -> For an easy installation of the above tools, use the [rustup installer](https://rustup.rs). - -## Installation - -To install, use Foundryup. Foundryup is the Foundry toolchain installer. You can find more information in the [Foundry README](https://github.com/foundry-rs/foundry/blob/master/foundryup/README.md). - -```bash -curl -L https://foundry.paradigm.xyz | bash -``` - -Running foundryup by itself will install the latest (nightly) precompiled binaries: `forge`, `cast`, `anvil`, and `chisel`. - -> Visit the [installation guides](https://book.getfoundry.sh/getting-started/installation) for more information. - -## Create a foundry project - -To start a new project with Foundry, use [forge init](https://book.getfoundry.sh/reference/forge/forge-init.html). - -```bash -forge init hello_foundry -``` - -> See more details on how to [create a new project](https://book.getfoundry.sh/projects/creating-a-new-project) using the Foundry guide. - -## Write your first contract - -Let’s view the file structure for a default foundry project: - -```bash -$ cd hello_foundry -$ tree . -d -L 1 -. -├── lib -├── script -├── src -└── test - -4 directories -``` - -The `src` directory contains counter smart contract with test written in the `test` directory. Now, let's build the foundry project. - -```bash -forge build -``` - -And then run tests. - -```bash -forge test -``` - -## Deploy contract on the Rootstock - -To deploy the counter contract on Rootstock mainnet or testnet, further configure Foundry by setting up a Rootstock RPC url and a private key of an account that’s funded with tRBTC. - -### Environment Configuration - -Once you have an account with a private key, create a `.env` file in the root of the foundry project and add the variables. - -Foundry automatically loads a `.env` file present in the project directory. - -The `.env` file should follow this format: - -```bash -ROOTSTOCK_RPC_URL=https://rpc.testnet.rootstock.io/{YOUR_APIKEY} -PRIVATE_KEY=0x... -``` - -At the root of the project, run: - -```bash -# To load the variables in the .env file -source .env -``` - -### Modify Deployment Script - -Modify the deployment counter deploy script in the `scripts` directory to use the private key by modifying the run method, see below example: +:::note[Before you begin] -```solidity -// SPDX-License-Identifier: UNLICENSED -pragma solidity ^0.8.13; - -import {Script, console} from "forge-std/Script.sol"; - -import "../src/Counter.sol"; - - -contract CounterScript is Script { - function setUp() public {} - - function run() public { - vm.startBroadcast(vm.envUint("PRIVATE_KEY")); - - new Counter(); - - vm.stopBroadcast(); - - } -} - -``` - -By default, scripts are executed by calling the function named `run` at the entrypoint. - -```solidity -uint256 deployerPrivateKey = vm.envUint("PRIVATE_KEY"); -``` - -> - CAUTION: Be cautious when exposing private keys in a `.env` file and loading them into programs. - > - This is only recommended for use with non-privileged deployers or for local / test setups. - -When calling `vm.startBroadcast()`, the contract creation will be recorded by Forge, and we can broadcast the transaction to deploy the contract on-chain. - -### Execute the deployment script - -We will use Forge to run our script and broadcast the transactions - this can take a little while, since Forge also waits for the transaction receipts. - -```bash -forge script script/Counter.s.sol --rpc-url $ROOTSTOCK_RPC_URL --broadcast --legacy -``` - -:::info[Info] - -- [EIP-1559](https://github.com/ethereum/EIPs/blob/master/EIPS/eip-1559.md) is not supported or not activated on the Rootstock RPC url -- The `--legacy` flag is passed to use legacy transactions instead of `EIP-1559`. +> If you're new to Web3 and Smart Contract Development, begin by exploring the [Rootstock network](/developers/blockchain-essentials/overview/). Then progress step by step to the [quick start Guide with Foundry](/developers/quickstart/foundry/) for a comprehensive understanding of the network and getting started with writing, testing, and deploying smart contracts on Rootstock. +> Note: This guide is optimized for Node.js version 18 or earlier. If you're using a later version, consider using a version manager like [NVM](https://github.com/nvm-sh/nvm/blob/master/README.md) to switch to a compatible version. ::: -The result should look like this: - -```bash -[⠰] Compiling... -No files changed, compilation skipped -Script ran successfully. - -== Logs == - Counter: - -## Setting up 1 EVM. - -========================== - -Chain 31 - -Estimated gas price: 0.065164 gwei - -Estimated total gas used for script: 138734 - -Estimated amount required: 0.000009040462376 ETH - -========================== -## -Sending transactions [0 - 0]. -⠁ [00:00:00] [###############################################################################################################################################] 1/1 txes (0.0s)## -Waiting for receipts. -⠉ [00:00:25] [###########################################################################################################################################] 1/1 receipts (0.0s) -##### 31 -✅ [Success]Hash: 0x015de35ffae94f491d4630f2aec84c49ae8170d5ecf3f4c1cdc8718bc4a00052 -Contract Address: 0x64B24E046259042e16a337Be4648CeAAF8Eb72C6 -Block: 5071408 -Gas Used: 106719 - -========================== - -ONCHAIN EXECUTION COMPLETE & SUCCESSFUL. -Total Paid: 0. ETH (106719 gas * avg 0 gwei) - -Transactions saved to: /hello_foundry/broadcast/Counter.s.sol/31/run-latest.json - -Sensitive values saved to: /hello_foundry/cache/Counter.s.sol/31/run-latest.json -``` - -> The broadcast directory will be updated automatically with the latest output of the deployment. - -> See the [foundry deployment documentation](https://book.getfoundry.sh/tutorials/solidity-scripting#deploying-our-contract). \ No newline at end of file +## Navigating the Guide + +| Resource | Description | +| ----------------------------------------------------------- | ---------------------------------------------------------------------------------------------- | +| [Prerequisites](/developers/requirements/) | Learn about the tools you need to have in place to follow along with this guide.| +| [Create a Foundry Project](/developers/smart-contracts/foundry/create-foundry-project/) | Learn how to set up your environment for development using Foundry.| +| [Configure Foundry for Rootstock](/developers/smart-contracts/foundry/configure-foundry-rootstock/) | Learn how to configure your Foundry project for development on Rootstock testnet and mainnet.| +| [Smart Contract](/developers/smart-contracts/foundry/smart-contracts/) | Check foundry demo smart contract.| +| [Test Smart Contract](/developers/smart-contracts/foundry/test-smart-contracts/) | Learn how to test your smart contract using `forge`. | +| [Deploy Smart Contract](/developers/smart-contracts/foundry/deploy-smart-contracts/) | Learn how to deploy your smart contract using `forge`. | +| [Interact with Smart Contract](/developers/smart-contracts/foundry/interact-with-contract/) | Learn how to interact with your smart contract using `cast`. | +| [Debugging and Troubleshooting Tips](/developers/smart-contracts/foundry/troubleshooting/) | Learn about the common issues you can come across while building following this guide and how you can solve them. | \ No newline at end of file diff --git a/docs/02-developers/05-smart-contracts/05-foundry/interact-with-contract.md b/docs/02-developers/05-smart-contracts/05-foundry/interact-with-contract.md new file mode 100644 index 00000000..aca596db --- /dev/null +++ b/docs/02-developers/05-smart-contracts/05-foundry/interact-with-contract.md @@ -0,0 +1,24 @@ +--- +sidebar_label: Interact with the Smart Contract +sidebar_position: 106 +title: Interact with the Smart Contract +description: "Learn how to interact with your smart contract using cast" +tags: [guides, developers, smart contracts, rsk, rootstock, hardhat, dApps, ethers] +--- + + +Interacting with a smart contract is a crucial part of the development process. Here, we'll focus on using `cast`, a command-line tool that allows you to interact with your smart contract. + +## Interacting with the Contract +If the contract is already deployed, then you can interact with it using ```cast``` this command allows you to interact with the contract, in this case, read the balance of an account. + +### Reading the Balance of an Account +In your terminal, run the following command, replacing the placeholders with actual values: + +```bash +cast call "balanceOf(address)(uint256)" --rpc-url +``` +The result should look like this: +```bash +1000000000000000000000 [1e21] +``` diff --git a/docs/02-developers/05-smart-contracts/05-foundry/smart-contracts.md b/docs/02-developers/05-smart-contracts/05-foundry/smart-contracts.md new file mode 100644 index 00000000..1ca81be9 --- /dev/null +++ b/docs/02-developers/05-smart-contracts/05-foundry/smart-contracts.md @@ -0,0 +1,72 @@ +--- +sidebar_label: Smart Contract +sidebar_position: 103 +title: Smart Contract +description: "Learn how to write a smart contract using Solidity and OpenZeppellin" +tags: [guides, developers, smart contracts, rsk, rootstock, hardhat, dApps, ethers] +--- + +## Folder Structure + +Let’s view the file structure for a default foundry project: + +```bash +$ cd hello_foundry +$ tree . -d -L 1 +. +├── lib +├── script +├── src +└── test + +4 directories +``` + +The `src` directory contains counter smart contract with test written in the `test` directory. Now, let's build the foundry project. + +```bash +forge build +``` + +And then run tests. + +```bash +forge test +``` + +## Demo smart contract +In the `src` folder, you will find a simple smart contract called `counter.sol`. Which contains a simple counter contract. + +```solidity +// SPDX-License-Identifier: UNLICENSED +pragma solidity ^0.8.13; + +contract Counter { + uint256 public number; + + function setNumber(uint256 newNumber) public { + number = newNumber; + } + + function increment() public { + number++; + } +} +``` + +## Compile the Contract +To build the contract, run the following command in the project's root directory. + +```bash +forge build +``` + +This will compile your smart contracts and generate `out` directory: + +```bash +forge build +[⠊] Compiling... +[⠒] Compiling 36 files with Solc 0.8.24 +[⠑] Solc 0.8.24 finished in 1.56s +Compiler run successful! +``` diff --git a/docs/02-developers/05-smart-contracts/05-foundry/test-smart-contracts.md b/docs/02-developers/05-smart-contracts/05-foundry/test-smart-contracts.md new file mode 100644 index 00000000..be3ee4c4 --- /dev/null +++ b/docs/02-developers/05-smart-contracts/05-foundry/test-smart-contracts.md @@ -0,0 +1,64 @@ +--- +sidebar_label: Test Smart Contract +sidebar_position: 104 +title: Testing Smart Contracts using Foundry +description: "Learn how to test your Rootstock smart contract using Foundry" +tags: [guides, developers, smart contracts, rsk, rootstock, foundry, dApps, ethers] +--- + +In this section, you'll set up a smart contract test and test it using `forge`. + +### Step 1: Test Script +You will see a directory called `test` in the root of your project. This is where you can view/write your tests. The demo `counter.sol` comes with a test script `counter.t.sol`, which contains: + +```solidity +// SPDX-License-Identifier: UNLICENSED +pragma solidity ^0.8.13; + +import {Test, console} from "forge-std/Test.sol"; +import {Counter} from "../src/Counter.sol"; + +contract CounterTest is Test { + Counter public counter; + + function setUp() public { + counter = new Counter(); + counter.setNumber(0); + } + + function test_Increment() public { + counter.increment(); + assertEq(counter.number(), 1); + } + + function testFuzz_SetNumber(uint256 x) public { + counter.setNumber(x); + assertEq(counter.number(), x); + } +} +``` + +### Step 2: Run the Test +To run the test, execute the following command in the root of your project: + +```shell +forge test +``` + +This will run the test script and display the results in the terminal. +```shell +forge test +[⠊] Compiling... +[⠊] Compiling 33 files with Solc 0.8.24 +[⠒] Solc 0.8.24 finished in 947.64ms +Compiler run successful! +Ran 2 tests for test/Counter.t.sol:CounterTest +[PASS] testFuzz_SetNumber(uint256) (runs: 256, μ: 30899, ~: 31288) +[PASS] test_Increment() (gas: 31303) +Suite result: ok. 2 passed; 0 failed; 0 skipped; finished in 11.85ms (6.49ms CPU time) + +Ran 2 test suites in 137.32ms (19.93ms CPU time): 6 tests passed, 0 failed, 0 skipped (6 total tests) +``` +:::note[Additional tests] +If you need additional tests, or want to go deep on this step, visit the [Foundry Tests Documentation](https://book.getfoundry.sh/forge/tests).. +::: \ No newline at end of file diff --git a/docs/02-developers/05-smart-contracts/05-foundry/troubleshooting.md b/docs/02-developers/05-smart-contracts/05-foundry/troubleshooting.md new file mode 100644 index 00000000..63a4b735 --- /dev/null +++ b/docs/02-developers/05-smart-contracts/05-foundry/troubleshooting.md @@ -0,0 +1,13 @@ +--- +sidebar_label: Debugging and Troubleshooting +sidebar_position: 106 +title: Common Errors and Tips +description: "Learn about some potential issues you can run into and tips on how to resolve them." +tags: [guides, developers, smart contracts, rsk, rootstock, foundry, dApps, ethers] +--- + +This section provides help on some potential issues you may run into and tips on how to resolve them. + +## Errors +- Error Transaction dropped from the mempool or Error Transaction not completed: check the tx-id in the explorer. The tx may have went successful but the error is still in the logs. Here are the [mainnet](https://explorer.rootstock.io/) and [testnet](https://explorer.testnet.rootstock.io/) explorers. +- Error Failed to get EIP-1559 fees: EIP-1559 is not supported or not activated on the Rootstock RPC url. The `--legacy` flag is passed to use legacy transactions instead of `EIP-1559`. \ No newline at end of file