Foundry
What is Foundry?
Foundry (opens in a new tab) is a toolset for Ethereum development written in Rust that assists developers in managing dependencies, compiling projects, running tests, deploying contracts, and interacting with blockchains through the command line interface.
Additionally, Foundry can directly communicate with Xterio Chain (BNB)'s Ethereum API, enabling the use of Foundry to deploy smart contracts into Xterio Chain (BNB).
In this example, let's deploy a simple contract in Xterio Testnet.
Prerequisites
Before we start, make sure you have met the following conditions:
- Have some tBNB on Xterio Testnet Network. You can follow our guide on Bridging for Xterio Chain (BNB).
- Rust must be installed on your computer. If it's not, follow this guide (opens in a new tab).
- Foundry must be installed on your computer. If it’s not, follow this guide (opens in a new tab).
Getting started
1. Create a project
Forge
is the command-line interface (CLI) tool for Foundry, allowing developers to execute operations directly from the terminal.
forge init my-project
2. Install OpenZeppelin contracts
as a dependency
forge install OpenZeppelin/openzeppelin-contracts
3. Navigate to the Source in the project and create your smart contracts
cd src
touch MyToken.sol
4. Input your smart contract or use the sample contract below.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "lib/openzeppelin-contracts/contracts/token/ERC20/ERC20.sol";
contract MyERC20 is ERC20 {
constructor() ERC20("MyToken", "MTK") {}
}
It's just a simple ERC20 token named "MyToken" with the symbol "MTK". You can change the token name and also write any other contracts. Just for notice if your file name is called differently, the commands to deploy will be slightly different.
5. Compile contract
forge build
Deploying Your Smart contract
Deploying a contract with Forge
is a simple process that can be done with a single command. However, it requires an RPC endpoint, a private key that has funds, and any arguments for the constructor of the contract.
For example, the MyToken.sol
contract requires an initial supply of tokens to be specified in its constructor, so the command to deploy it on a network will include the argument of 100.
Notice:
Always keep your private key confidential and secure. Exposing it publicly can allow unauthorized access, giving full control to anyone who possesses it.
To deploy the MyToken.sol
contract, use the command that corresponds to Xterio Chain (BNB)'s RPC URL while running the forge create
command:
forge create --rpc-url "https://xterio-testnet.alt.technology/"
--constructor-args 100 \
--private-key YOUR_PRIVATE_KEY \
src/MyToken.sol:MyToken
In the output you should get something like:
[⠢] Compiling... No files changed, compilation skipped
Deployer: 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266
Deployed to: 0x5fbdb2315678afecb367f032d93f642f64180aa3
Transaction hash: 0xe2a7aae94456f107fc09e29ef0ec4ce8c478064cdba3550fd61d91703548eaae
The Deployed to value is your contract address.