Quickstart
In this guide, you will learn how to deploy a smart contract to the CrossFi Chain. We will create a simple contract, test the contract and, then deploy it. This guide can be used later when you are deploying more complex contracts as the steps will be similiar.
Requirements
Basic understanding of Solidity
Tesnet XFI Coins (the faucets are in the test XFI Console)
Installing Foundry (Required)
In this guide, we will use Foundry to test and deploy our smart contract. Foundry lets us do this all within one tool set and using the command line (CLI).
First, we will make a file directory for our project and then install Foundry. If you already Foundry installed, you can skip this step.
Creating the Project
In your terminal, run the following command to make a folder and initialize npm.
Downloading and Installing Foundry
The below command will download the latest version of Foundry:
After the download is complete, run the foundryup
command in your terminal:
If using MacOS , you may receive the following error:
dyld[32719]: Library not loaded: /usr/local/opt/libusb/lib/libusb-1.0.0.dylib
To fix this run brew install libusb (Requires Homebrew)
Create the Contract
Now we will create the contract file and supporting files by using the below command:
You can now open up your code editor at the location of the newly created counter_contract
. A folder structure like below should be created:
Don't worry if you are not sure what all these folders are for now as we will use them throughout this guide.
Reviewing the Counter Smart Contract
In your code editor, open up the Counter.sol file that is located in /src/Counter.sol.
The code should be the same as below with potentially a different Solidity version:
This contract includes:
number - A number variable that is assigned as an unsigned integer with 256 bits.
setNumber - A public function that can be called to set the number variable
increment - A public function that increments the number variable by 1
Compile the Contract
Now we need to compile the contract. This is a necessary step to verify that our smart contract doesn't have any logical coding errors. It is not a test of the security of the smart contract.
We can do this by running the below command:
If successful, this will create a folder named out
that contains the ABI of our smart contract in the form of a JSON file named Counter.json.
Testing the Contract
For this guide, we will not have to create our tests as they are provided to us in the /test/Counter.t.sol
file. The code should look like this:
We can then execute the test code by running the following command:
After running this command, you should see that 2 tests passed as well as a gas amount for the Increment
function:
Deploying the Contract
Now it is time to deploy our smart contract on the testnet CrossFi chain. We can do this in a single command by using forge create.
You will need to use a private key that is connected to your wallet with XFI testnet tokens.
If the deployment is successful, you should see your contract address under the "Deployed to" in your terminal.
You can verify that your contract has been deployed to the CrossFi testnet by searching for its address using the test XFI Scan.
Interacting with the Contract
In the final step of this guide, we will interact with our contract by calling the public functions. We can do this by using Foundry and the following commands:
Setting the Number
To use the setNumber
function to set the new number, you can use the following command:
If successful, you will see the blockHash
and other transaction data.
Reading the Number
To read the current value of the number variable, you can use the following command:
Incrementing the Number
To use the increment
function to increment the number by 1, you can use the following command:
Conclusion
Congrats, you have now tested, compiled and, deployed a smart contract to the CrossFi Testnet!
The steps you completed in this guide are the same steps you can take as you continue building on CrossFi.
Last updated