Using Foundry
Setting up the environment
Foundry is a smart contract development toolchain. It can be used to manage dependencies, compile a project, run tests and deploy smart contracts. It also lets one interact with blockchain from the CLI or via Solidity scripts.
Install Foundry
If you have not installed Foundry, go to book.getfoundry and select Installation from the side menu. Follow the instructions to download Using Foundryup.
Next, select Creating a New Project from the sidebar. Initialize and give your new project a name: forge init zkevm-sbt
In case of a library not loaded error
, you should run the below command and then repeat the above process again:
If you have never installed Rust or need an update, visit the website here.
Build a project and test
Run the command forge build
to build the project. The output should look something like this:Successful forge build commandNow, test the build with forge test
Testing Forge BuildYou can check out the contents of the newly built project by switching to your IDE. In case of VSCode, just type: code .
Writing the smart contract
Find the OpenZeppelin Wizard in your browser, and use the wizard to create an out-of-the-box NFT contract.
Select the
ERC721
tab for an NFT smart contract.Name the NFT and give it an appropriate symbol. Example: Name
SoEarly
and SymbolSOE
.Go ahead and select features for your token. Simply tick the relevant boxes.
You can tick the URI Storage box if you wish to attach some image or special text to the token.
Open your CLI and install dependencies with this command:
Remap dependencies to easy-to-read filenames with the command:
Inside the new
remapping.txt
file, rename the referencingopenzeppelin-contracts
toopenzeppelin
, which is the name used when importing. That is, changeopenzeppelin-contracts/=lib/openzeppelin-contracts
→openzeppelin/=lib/openzeppelin-contracts
.Copy the smart contract code in OpenZeppelin: Copy to Clipboard
In the IDE, open a new
.sol
file, name it and paste the copied code to this file. This is in fact the actual smart contract for the NFT.
Add control on token transfers
The aim here is to put rules in place stipulating that the token cannot be transferred without burning it.
Go to the OpenZeppelin documentation.
Look up the signature by searching for
_beforetokentransfererc721
.Scroll down to
ERC 721
and copy the corresponding text on the right side:
Create a new function in the code for the smart contract token called
_beforeTokenTransfer
Set a token URI (optional)
A token URI is a function that returns the address where the metadata of a specific token is stored. The metadata is a . json
file where all the data associated with the NFT is stored. Our aim here is to attach some images to the created token.The stored data typically consists of the name of the token, brief description and URL where the image is stored.
Choose an image and give it a name relatable to the token
Find some free of charge IPFS storage for the image, like NFT.STORAGE
Upload the image to the storage using your GitHub account
Add URI json file
This is the file that contains the metadata for the token which includes the image address (i.e., the IPFS address of the image).
In the IDE, create a new
.json
file which you can calltokenuri.json
Populate the
tokenuri.json
file with the token-name, description and URL where the image is stored:
Upload the
tokenuri.json
file to the same storage where the image was uploadedCopy the address to the
Sbt.sol
inside thesafeMint
functionRemove the
uri
parameter so as to hardcode it. This makes all tokens have the sameuri
image, but every consecutively minted pair of tokens will differ by 1 in numerical value of theirtokenId
.
Populate the .env
file
In order to deploy on the DuckChain Testnet, populate the .env
file in the usual way. That is,
Create a
.env.sample
file within thesrc
folderPopulate
.env.sample
file with yourACCOUNT_PRIVATE_KEY
and the DuckChain Testnet’sRPC URL
found here. So the.env.sample
file will look like this:
Copy the contents of the
.env.sample
file to the.env
file,
Warning
Make sure .env
is in the .gitignore
file to avoid uploading your ACCOUNT_PRIVATE_KEY
.
Deploy your contract
In the CLI, use the following command to ensure grabbing variables from
.env
:
Check if the correct
RPC URL
is read from the.env
file:
You can now use the next command:
which executes the following:
Does a
forge create
.Passes the
RPC_URL
andPVTKEY
.References the actual smart contract.
For example, when deploying the
Sbt.sol
contract, the command will look like this:
The above command compiles and deploys the contract to the DuckChain Testnet. The output on the CLI looks like this one below.Successful Deploy Sbt.sol
Check deployed contract in explorer
Copy the address of your newly deployed contract (i.e. the
Deployed to:
address as in the above example output).Go to DuckChain Testnet Explorer, and paste the address in the
Search by address
field.Check
Transaction Details
reflecting theFrom
address, which is the owner’s address and theTo
address, which is the sameDeployed to:
address seen in the CLI.
Last updated