bitpro가 원문에서 글자만 모은 내용이에요. 표·그림·수식은 빠지거나 깨질 수 있어요. 전체 13쪽 중 8쪽
ogs:
Error: a == b not satisfied [address]
Left: 0x328809Bc894f92807417D2dAD6b7C998c1aFdac6
Right: 0x1D96F2f6BeF1202E4Ce1Ff6Dad0c2CB002861d3e
[PASS] testMintPass() (gas: 132327)
Test result: FAILED. 1 passed; 1 failed; 0 skipped; finished in 1.13ms
Ran 1 test suite in 1.13ms: 1 tests passed, 1 failed, 0 skipped (2 total tests)
Failing tests:
Encountered 1 failing test in test/NFT.t.sol:NFTTest
[FAIL. Reason: assertion failed] testMintFail() (gas: 147160)
Encountered a total of 1 failing tests, 1 tests succeeded
The first test:
testMintPass
passed successfully as the criteria for the
mint()
function were met.
We passed the recipient address to the
mint()
function as required, hence the success.
The second test:
testMintFail
failed since we asserted that the addresses of
alice
and
bob
were the same.
The highlighted log output elaborates on how the assertion was false.
Deploying
the smart contract
After successfully building the smart contract, you can now deploy it to the Lisk network.
For this example, we will use the Lisk Sepolia network to deploy the
NFT
contract.
Add the
--verify
,
--verifier
,
--verifier-url
, and the sender account's
--private-key
flag to the
forge create
command to directly verify the smart contract on BlockScout.
Lisk
Lisk Sepolia
forge create --rpc-url https://rpc.api.lisk.com
\
--etherscan-api-key
123
\
--verify
\
--verifier
blockscout
\
--verifier-url https://blockscout.lisk.com/api
\
--private-key
<
PRIVATE_KEY
>
\
src/NFT.sol:NFT
forge create --rpc-url https://rpc.sepolia-api.lisk.com
\
--etherscan-api-key
123
\
--verify
\
--verifier
blockscout
\
--verifier-url https://sepolia-blockscout.lisk.com/api
\
--private-key
<
PRIVATE_KEY
>
\
src/NFT.sol:NFT
If the deployment is successful, the output should look like the following:
# The aforementioned command will first deploy the contract and display the following output:
[⠒] Compiling...
No files changed, compilation skipped
Deployer: 0x5e1A92F84cA1CE280B3Cb29d79C3368f45b41EBB
Deployed to: 0x108872F713A27bc22ca1db8CEefCAC8CbeDdF9E5
Transaction hash: 0xf465528f43e5cbc9b5206e46048feba0b920179813c3eb8c3bdbccbfd13d731e
# Once the contract is deployed successfully, the above-mentioned command will then verify the contract as well!
Starting contract verification...
Waiting for blockscout to detect contract deployment...
Start verifying contract `0x108872F713A27bc22ca1db8CEefCAC8CbeDdF9E5` deployed on 4202
Submitting verification for [src/NFT.sol:NFT] 0x108872F713A27bc22ca1db8CEefCAC8CbeDdF9E5.
Submitted contract for verification:
Response: `OK`
GUID: `108872f713a27bc22ca1db8ceefcac8cbeddf9e565e71790`
URL: https://sepolia-blockscout.lisk.com/address/0x108872f713a27bc22ca1db8ceefcac8cbeddf9e5
Contract verification status:
Response: `OK`
Details: `Pending in queue`
Contract verification status:
Response: `OK`
Details: `Pass - Verified`
Contract successfully verified
After the smart contract is deployed and verified, you can interact with it by calling its public functions.
Verifying
the smart contract
Each deployed contract should be verified so that users and other developers can inspect the source code, and be sure that it matches the deployed bytecode on the blockchain.
Further, if you want to allow others to interact with your contract using the block explorer such as Blockscout's
Read contract
and
Write Contract
interfaces, it first needs to be verified.
The above contract has
already been verified
, so you should be able to view your version on a block explorer already, but we'll still walk through how to verify a contract on the Lisk Sepolia testnet.
info
You can't re-verify a contract identical to one that has already been verified. If you attempt to do so, such as verifying the above contract, you'll get an error similar to:
Start verifying contract `0x108872F713A27bc22ca1db8CEefCAC8CbeDdF9E5` deployed on 4202
Contract [src/NFT.sol:NFT] "0x108872F713A27bc22ca1db8CEefCAC8CbeDdF9E5" is already verified. Skipping verification.
In case your smart contract isn't verified, grab the deployed address and run:
Lisk
Lisk Sepolia
forge verify-contract
<
CONTRACT_ADDRESS
>
\
./src/
<
CONTRACT_FILE
>
.sol:
<
CONTRACT_NAME
>
\
--chain
1135
\
--watch
\
--verifier
blockscout
\
--verifier-url https://blockscout.lisk.com/api
forge verify-contract
<
CONTRACT_ADDRESS
>
\
./src/
<
CONTRACT_FILE
>
.sol:
<
CONTRACT_NAME
>
\
--chain
4202
\
--watch
\
--verifier
blockscout
\
--verifier-url https://sepolia-blockscout.lisk.com/api
You should see an output similar to the following:
Starting contract verification...
Waiting for blockscout to detect contract deployment...
Start verifying contract `0xcCaA1C3eb8FEb5b09a5Eac1359BC4c70E18e29d9` deployed on 4202
Submitting verification for [src/NFT.sol:NFT] 0xcCaA1C3eb8FEb5b09a5Eac1359BC4c70E18e29d9.
Submitted contract for verification:
Response: `OK`
GUID: `ccaa1c3eb8feb5b09a5eac1359bc4c70e18e29d965e5c95a`
URL: https://sepolia-blockscout.lisk.com/address/0xccaa1c3eb8feb5b09a5eac1359bc4c70e18e29d9
Contract verification status:
Response: `OK`
Details: `Pending in queue`
Contract verification status:
Response: `OK`
Details: `Pass - Verified`
Contract successfully verified
Use the contract's address e.g.,
0xcCaA1C3eb8FEb5b09a5Eac1359BC4c70E18e29d9
to search for your contract on
Blockscout
to confirm that it is verified.
Interacting with the Smart Contract
As mentioned earlier, if you verified the smart contract on Blockscout, you can use the
Read contract
and
Write contract
sections under the
Contract
tab to interact with the deployed contract.
The
Read contract
tab can be used without connecting a wallet, however, to use the
Write contract
tab, you'll need to connect your wallet first.
You can do that by clicking the
Connect wallet
button.
Using
cast
for interaction
With Foundry's rich command-line tool:
cast
it is possible to interact with any deployed contract whether it is reading or writing data on the blockchain.
Let's perform a call without publishing a transaction (a read), then sign and publish a transaction (a write) to the deployed contract.
Performing a call
A key component of the Foundry toolkit,
cast
enables us to interact with contracts, send transactions, and get onchain data using Ethereum RPC calls.
First, we will perform a call from an account, without publishing a transaction.
Fill out the following
<PLACEHOLDERS>
and then, run the command:
Lisk
Lisk Sepolia
cast call
<
DEPLOYED_CONTRACT_ADDRESS
>
--rpc-url https://rpc.api.lisk.com
"balanceOf(address)"
<
YOUR_ACCOUNT_ADDRESS_HERE
>
cast call
<
DEPLOYED_CONTRACT_ADDRESS
>
--rpc-url https://rpc.sepolia-api.lisk.com
"balanceOf(address)"
<
YOUR_ACCOUNT_ADDRESS_HERE
>
You should receive
0x0000000000000000000000000000000000000000000000000000000000000000
in response, which equals
0
in hexadecimal.
This makes sense as you've only deployed the NFT contract for now, however, no NFTs have been minted yet, and therefore your account's balance is zero.
Signing and sending a transaction
Now let's sign and send a transaction, calling the
mint(address)
function on the
NFT
contract we just deployed.
Again, fill out the following
<PLACEHOLDERS>
and then, run the command:
Lisk
Lisk Sepolia
cast send
<
DEPLOYED_CONTRACT_ADDRESS
>
--rpc-url https://rpc.api.lisk.com
"mint(address)"
<
RECIPIENT_ADDRESS_HERE
>
--private-key
<
SENDER_PRIVATE_KEY
>
cast send
<
DEPLOYED_CONTRACT_ADDRESS
>
--rpc-url https://rpc.sepolia-api.lisk.com
"mint(address)"
<
RECIPIENT_ADDRESS_HERE
>
--private-key
<
SENDER_PRIVATE_KEY
>
info
As the
cast send
command writes data on the blockchain, it needs a sender account's private key to be passed to the
--private-key
flag.
The transaction will be sent successfully if the sender account has sufficient funds.
The aforesaid is not required for
cast call
command, because that only retrieves already published data from the smart contract.
If the transaction execution is successful, Foundry will respond with information about the transaction, including the
blockNumber
,
gasUsed
,
transactionHash
, and much more.
blockHash 0xfa9d32794b0fc9c1a10d39c5289613dfe80b55f8ead06475ca877a389e088e67
blockNumber 2165375
contractAddress
cumulativeGasUsed 137472
effectiveGasPrice 3000000253
from 0x5e1A92F84cA1CE280B3Cb29d79C3368f45b41EBB
gasUsed 93597
logs [{"address":"0x108872f713a27bc22ca1db8ceefcac8cbeddf9e5","topics":["0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef","0x0000000000000000000000000000000000000000000000000000000000000000","0x000000000000000000000000488ba3c013020bd1712ed6a1997c4212d9711954","0x0000000000000000000000000000000000000000000000000000000000000001"],"data":"0x","blockHash":"0xfa9d32794b0fc9c1a10d39c5289613dfe80b55f8ead06475ca877a389e088e67","blockNumber":"0x210a7f","transactionHash":"0x76750ee1aaeed89c8f165d6f547002eb3bb833a142f73d63c1c3c9980fce8796","transactionIndex":"0x1","logIndex":"0x0","removed":false}]
logsBloom 0x00000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000040000000000000000200000000008000000000000000000040000000000000000000000000000020000000000000000080800000000000000000000000010000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002000001000000000000400000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000000000
root
status 1
transactionHash 0x76750ee1aaeed89c8f165d6f547002eb3bb833a142f73d63c1c3c9980fce8796
transactionIndex 1
type 2
to 0x1088…f9e5
l1Fee "0x30fb62bfb0c"
l1GasPrice "0x6d49929"
l1GasUsed "0x8a4"
Finally, you can confirm the minting by
performing the call
again.
We should see that our balance increased from
0
to
1
.
Lisk
Lisk Sepolia
cast call
<
DEPLOYED_CONTRACT_ADDRESS
>
--rpc-url https://rpc.api.lisk.com
"balanceOf(address)"
<
YOUR_ACCOUNT_ADDRESS_HERE
>
cast call
<
DEPLOYED_CONTRACT_ADDRESS
>
--rpc-url https://rpc.sepolia-api.lisk.com
"balanceOf(address)"
<
YOUR_ACCOUNT_ADDRESS_HERE
>
And the response:
0x0000000000000000000000000000000000000000000000000000000000000001
(
1
in hex) — congratulations, you deployed a contract and minted an NFT with Foundry!
See the minted token for this guide on the
Blockscout explorer
.
That's it! Although this is just the tip of the iceberg, there is a lot more to learn about Foundry.
For all things Foundry, check out the
Foundry book
, or head to the official Telegram
dev chat
or
support chat
.
Prerequisites
Wallet funds
Creating a project
Creating
the smart contract
Compiling
the smart contract
Testing
the smart contract
Deploying
the smart contract
Verifying
the smart contract
Interacting with the Smart Contract
Using
cast
for interaction
Performing a call
Signing and sending a transaction
## /guides/deploying-smart-contract/with-Hardhat
On this page
On this page, you will learn how to create, deploy and verify a smart contract with HardHat to the
Lisk Sepolia
testnet.
Prerequisites
Node v18+
This guide requires you to have Node version 18+ installed.
Download
Node v18+
If you are using
nvm
to manage your node versions, you can just run
nvm install 18
.
Wallet funds
Deploying contracts
to the blockchain requires a
gas fee
.
Therefore, you will need to fund your wallet with ETH to cover those gas fees.
For this guide, you will be deploying a contract to the Lisk Sepolia Testnet.
You can deposit the required tokens by using the
Lisk Bridge
.
In case your wallet doesn't hold enough
SepoliaETH
, use one of the available faucets for the
Ethereum Sepolia
Testnet like
https://sepoliafaucet.com
to receive free Testnet ETH.
Then, use the aforementioned Lisk Bridge to send tokens from the
Ethereum Sepolia Testnet
to the
Lisk Sepolia Testnet
.
note
You can deploy a contract on Lisk Mainnet by adopting the same process.
For deploying to Mainnet, ensure that your wallet has enough ETH.
The subsequent text contains commands for both Lisk and Lisk Sepolia for your ease.
For more information, see the
available Lisk networks
and
how to connect a wallet with them
.
Creating a project
Before you can begin deploying smart contracts to Lisk, you need to set up your development environment by creating a Node.js project.
To create a new Node.js project, first create a root folder for the project and move into it:
mkdir
my-project
&&
cd
my-project
Inside the new folder, run:
npm
init
--y
Next, you will need to install
Hardhat
and create a new Hardhat project.
To install Hardhat, run:
npm
install
--save-dev hardhat
To create a new Hardhat project, run:
npx hardhat
Select
Create a TypeScript project
then press
Enter
to confirm the project root.
Select
y
for both adding a
.gitignore
and loading the sample project.
Optionally, you can decide to share crash reports and usage data with HardHat.
✔ What do you want to do? · Create a TypeScript project
✔ Hardhat project root: · /Users/lisk/git/my-project
✔ Do you want to add a .gitignore? (Y/n) · y
✔ Help us improve Hardhat with anonymous crash reports & basic usage data? (Y/n) · y
✔ Do you want to install this sample project's dependencies with npm (@nomicfoundation/hardhat-toolbox)? (Y/n) · y
It will take a moment for the project setup process to complete.
Configuring Hardhat with Lisk
In order to deploy smart contracts to the Lisk network, you will need to configure your Hardhat project and add the Lisk network.
This example uses
dotenv
to load the
WALLET_KEY
environment variable from a
.env
file to
process.env.WALLET_KEY
.
You should use a similar method to avoid hardcoding your private keys within your source code.
npm
install
--save-dev dotenv
Once you have
dotenv
installed, create a
.env
file with the following content:
WALLET_KEY=<YOUR_PRIVATE_KEY>
Substitute
<YOUR_PRIVATE_KEY>
with the private key for your wallet.
caution
WALLET_KEY
is the private key of the wallet to use when deploying a contract.
Follow the instructions of your wallet on how to get your private key.
E.g. for
MetaMask
, please follow
these instructions
.
It is critical that you do NOT commit this to a public repo
To configure Hardhat to use Lisk, add Lisk as a network to your project's
hardhat.config.ts
file:
Lisk
Lisk Sepolia
hardhat.config.ts
import
{
HardhatUserConfig
}
from
"hardhat/config"
;
import
"@nomicfoundation/hardhat-toolbox"
;
require
(
'dotenv'
)
.
config
(
)
;
const
config
:
HardhatUserConfig
=
{
solidity
:
"0.8.23"
,
networks
:
{
// for mainnet
'lisk'
:
{
url
:
'https://rpc.api.lisk.com'
,
accounts
:
[
process
.
env
.
WALLET_KEY
as
string
]
,
gasPrice
:
1000000000
,
}
,
}
,
}
;
export
default
config
;
hardhat.config.ts
import
{
HardhatUserConfig
}
from
"hardhat/config"
;
import
"@nomicfoundation/hardhat-toolbox"
;
require
(
'dotenv'
)
.
config
(
)
;
const
config
:
HardhatUserConfig
=
{
solidity
:
"0.8.23"
,
networks
:
{
// for testnet
'lisk-sepolia'
:
{
url
:
'https://rpc.sepolia-api.lisk.com'
,
accounts
:
[
process
.
env
.
WALLET_KEY
as
string
]
,
gasPrice
:
1000000000
,
}
,
}
,
}
;
export
default
config
;
Creating the contract
For ease and security, we’ll use the
ERC721
interface provided by the
OpenZeppelin Contracts library
to create an NFT smart contract.
With OpenZeppelin, we don’t need to write the whole ERC-721 interface. Instead, we can import the library contract and use its functions.
To add the OpenZeppelin Contracts library to your project, run:
npm
install
--save
@openzeppelin/contracts
In your project, delete the
contracts/Lock.sol
contract that was generated with the project.
(You can also delete the
test/Lock.ts
test file, but you should add your own tests ASAP!).
Add the code below to a new file called
contracts/NFT.sol
.
contracts/NFT.sol
// SPDX-License-Identifier: MIT
pragma
solidity
^
0.8.23
;
import
"@openzeppelin/contracts/token/ERC721/ERC721.sol"
;
contract
NFT
is
ERC721
{
uint256
public
currentTokenId
;
constructor
(
)
ERC721
(
"NFT Name"
,
"NFT"
)
{
}
function
mint
(
address
recipient
)
public
returns
(
uint256
)
{
uint256
newItemId
=
++
currentTokenId
;
_safeMint
(
recipient
,
newItemId
)
;
return
newItemId
;
}
}
Compiling the smart contract
To compile the contract using Hardhat, simply run:
npx hardhat compile
After successful compilation, you should see a new folder
artifacts/
, which contains the
compilation artifacts
.
Testing the contract
To test our NFT contract, we’ll use:
Hardhat Network
– A local Ethereum network for development.
It comes built-in with Hardhat and is used as the default network.
Ethers.js
– To interact with the contract.
Mocha
– For test runner and assertions.
Setting up the test file
Create a new directory called
test
inside our project root directory.
Then, create a new file in there called
NFT.js
, and add the following code:
test/NFT.js
const
{
expect
}
=
require
(
"chai"
)
;
describe
(
"NFT"
,
function
(
)
{
let
nftToken
;
beforeEach
(
async
(
)
=>
{
// Deploy contract
const
NFT
=
await
ethers
.
getContractFactory
(
"NFT"
)
;
nftToken
=
await
NFT
.
deploy
(
)
;
}
)
;
it
(
"Should allow to mint a new NFT"
,
async
function
(
)
{
const
[
owner
]
=
await
ethers
.
getSigners
(
)
;
// Before minting the NFT, the account balance for this NFT should be zero.
expect
(
await
nftToken
.
balanceOf
(
owner
.
address
)
)
.
to
.
equal
(
0
)
;
// Mint the NFT
await
nftToken
.
mint
(
owner
.
address
)
;
// After minting the NFT, the account balance for this NFT should be 1 for the account that minted the token.
expect
(
await
nftToken
.
balanceOf
(
owner
.
address
)
)
.
to
.
equal
(
1
)
;
}
)
;
}
)
;
First, we import the
expect
function from the
Chai
library, to be able to use it in our unit test for the contract.
If you set up your project to use the hardhat toolbox like explained in step
Creating a project
, you already have ethers available in your project out of the box.
Otherwise, you can install the ethers plugin for Hardhat as described in the
hardhat-ethers
' reference page.
Hardhat-Ethers plugin has the same API as
ethers.js
, with some additional
Hardhat-specific
features, that we are going to use in the test for the contract.
Next, we deploy the NFT contract inside the
beforeEach()
hook, ensuring the contract is always deployed before each test. This setup makes it easy to add additional test cases later.```
Finally, we define a test case
Should allow to mint a new NFT
to verify that calling the
.mint()
function of the NFT contract mints a new NFT and adds it to the balance of the account that minted it.
Running the Test
Now, run
npx hardhat test
in your terminal.
You should see the following output:
% npx hardhat
test
NFT
✔ Should allow to mint a new NFT
(
92ms
)
1
passing
(
1s
)
This indicates that the test was executed successfully.
For more information on how to test smart contracts with Hardhat, check the
Hardhat documentation
.
Deploying the smart contract
Once your contract has been successfully compiled, you can deploy the contract to the Lisk Sepolia test network.
To deploy the contract to the Lisk Sepolia test network, you'll need to modify the
scripts/deploy.ts
in your project:
scripts/deploy.ts
import
{
ethers
}
from
'hardhat'
;
async
function
main
(
)
{
const
nft
=
await
ethers
.
deployContract
(
'NFT'
)
;
await
nft
.
waitForDeployment
(
)
;
console
.
log
(
'NFT Contract Deployed at '
+
nft
.
target
)
;
}
// We recommend this pattern to be able to use async/await everywhere
// and properly handle errors.
main
(
)
.
catch
(
(
error
)
=>
{
console
.
error
(
error
)
;
process
.
exitCode
=
1
;
}
)
;
You'll also need Testnet ETH in your walle프로젝트로 돌아가기