visit
forge init foundry-demo // forge-demo is name of the project
cd foundry-demo && forge build
To use OpenZeppelin, we need to install it as a dependency in our project, to do so use the command.
forge install OpenZeppelin/openzeppelin-contracts
// forge install is command which is used for installing dependencies
// <//github.com/OpenZeppelin/openzeppelin-contracts>
// use {{username}}/{{repo_name}} from the github url
After installing OpenZeppelin as a dependency, try importing something from it in the contract (your contract is there under the /src directory), If you are using VSCode, an error will pop up.
To fix this error, Run this command.
forge remappings > remappings.txt
ds-test/=lib/forge-std/lib/ds-test/src/
forge-std/=lib/forge-std/src/
openzeppelin-contracts/=lib/openzeppelin-contracts/contracts/
Rename the file src/Counter.sol
→ src/FDemo.sol
; the code for our ERC721 smart contract is as below.
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.13;
import "openzeppelin-contracts/token/ERC721/extensions/ERC721URIStorage.sol";
import "openzeppelin-contracts/utils/Counters.sol";
contract FDemo is ERC721URIStorage {
using Counters for Counters.Counter;
Counters.Counter private _tokenId;
constructor() ERC721("FDemo", "FD") {}
function mint(string memory tokenUri) external returns (uint256) {
uint256 newTokenId = _tokenId.current();
_mint(msg.sender, newTokenId);
_setTokenURI(newTokenId, tokenUri);
_tokenId.increment();
return newTokenId;
}
}
Let's start by renaming the test file to match the name of our contract Counter.t.sol
→ FDemo.t.sol
setUp
: An optional function invoked before each test case is run.function setUp() public {
testNumber = 42;
}
test
: Functions prefixed with test
are run as a test case.function testNumberIs42() public {
assertEq(testNumber, 42);
}
testFail
: The inverse of the test prefix — if the function does not revert, the test fails.function testNumberIs42() public {
assertEq(testNumber, 42);
}
So, right now, we only have one method mint
, so we will be writing a test case for this method,
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.13;
import "forge-std/Test.sol";
import "../src/FDemo.sol";
contract FoundryDemoTest is Test {
FDemo instance;
function setUp() public {
instance = new FDemo();
}
function testMint() public {
string memory dummyTokenUri = "ipfs://metadata_url";
uint256 tokenId = instance.mint(dummyTokenUri);
assertEq(dummyTokenUri, instance.tokenURI(tokenId));
}
}
Now, to run this test, we can use the command forge test
More details on the Traces:
To generate a gas report, use — gas-report with the test command.
forge test --gas-report
More details on the Gas Report here:
—rpc-url
: Rpc URL of the network on which we want to deploy our contract (in our case, we will be using the RPC URL of polygon Mumbai testnet)constructor-args
: Pass arguments to the constructorprivate-key
: Private key of deployers wallet
We can optionally pass --verify
&&--etherscan-api-key
if we want to verify our contract.
$ forge create --rpc-url <your_rpc_url> \\
--constructor-args "ForgeUSD" "FUSD" 18 00000000000 \\
--private-key <your_private_key> src/MyToken.sol:MyToken \\
--etherscan-api-key <your_etherscan_api_key> \\
--verify
forge create --rpc-url <//rpc.ankr.com/polygon_mumbai>
--private-key <your_private_key> src/FDemo.sol:FDemo
--etherscan-api-key <your_etherscan_api_key>
--verify
Complete code:
💡 Follow me on Twitter for more awesome stuff like this
Also published