bitpro가 원문에서 글자만 모은 내용이에요. 표·그림·수식은 빠지거나 깨질 수 있어요. 전체 13쪽 중 10쪽
/4] 🔨 Building fresh packages...
success Saved lockfile.
✨ Done in 9.73s.
Initialized a git repository.
Success! Created thirdweb-contracts at /Users/XYZ/Lightcurve-Code/L2/thirdweb-contracts
Inside that directory, you can run several commands:
yarn build
Compiles your contracts and detects thirdweb extensions implemented on them.
yarn deploy
Deploys your contracts with the thirdweb deploy flow.
yarn publish
Publishes your contracts with the thirdweb publish flow.
We suggest that you begin by typing:
cd thirdweb-contracts
Foundry applications created with thirdweb have a similar directory structure to the following:
.
├── .github
├── .cache
├── lib
├── node_modules
├── out
├── src
│ └── NFT.sol
├──
test
│ └── Contract.t.sol
├── .gitignore
├── .gitmodules
├── foundry.toml
└── README.md
Creating
the smart contract
For ease and security, thirdweb already provides base contracts, which can be easily customized via code.
Since we chose to create an
ERC721
token earlier, an
NFT.sol
file will be present in the
src/
folder.
The
NFT.sol
will already have the base code required for an NFT contract.
src/NFT.sol
// SPDX-License-Identifier: MIT
pragma
solidity
^
0.8.0
;
import
"@thirdweb-dev/contracts/base/ERC721Base.sol"
;
contract
NFT
is
ERC721Base
{
constructor
(
address
_defaultAdmin
,
string
memory
_name
,
string
memory
_symbol
,
address
_royaltyRecipient
,
uint128
_royaltyBps
)
ERC721Base
(
_defaultAdmin
,
_name
,
_symbol
,
_royaltyRecipient
,
_royaltyBps
)
{
}
}
Building
the smart contract
Once the smart contract's code is ready, it must be built using thirdweb.
To do that, simply run:
npx thirdweb build
If the smart contract doesn't have any errors, you will see the following output on the terminal:
.......
💎 thirdweb v0.13.62 💎
✔ Detected project type: foundry
✔ Compilation successful
✔ Choose which contracts to run detection on · NFT
🔎 Detected extension on NFT
✔️ ERC721
✔️ ERC721Burnable
✔️ ERC721Supply
✔️ ERC721AQueryable
✔️ ERC721Mintable
✔️ ERC721BatchMintable
✔️ Royalty
✔️ ContractMetadata
✔️ Ownable
✔️ Fallback
ℹ Suggested extensions
- ERC721Enumerable - https://portal.thirdweb.com/interfaces/erc721enumerable
- ERC721LazyMintable - https://portal.thirdweb.com/interfaces/erc721lazymintable
- ERC721SignatureMintV1 - https://portal.thirdweb.com/interfaces/erc721signaturemintv1
- ERC721SignatureMintV2 - https://portal.thirdweb.com/interfaces/erc721signaturemintv2
- ERC721TieredDrop - https://portal.thirdweb.com/interfaces/erc721tiereddrop
- ERC721ClaimCustom - https://portal.thirdweb.com/interfaces/erc721claimcustom
- ERC721ClaimZora - https://portal.thirdweb.com/interfaces/erc721claimzora
- ERC721ClaimConditionsV1 - https://portal.thirdweb.com/interfaces/erc721claimconditionsv1
- ERC721ClaimConditionsV2 - https://portal.thirdweb.com/interfaces/erc721claimconditionsv2
- ERC721ClaimPhasesV1 - https://portal.thirdweb.com/interfaces/erc721claimphasesv1
- ERC721ClaimPhasesV2 - https://portal.thirdweb.com/interfaces/erc721claimphasesv2
- ERC721SharedMetadata - https://portal.thirdweb.com/interfaces/erc721sharedmetadata
- ERC721LoyaltyCard - https://portal.thirdweb.com/interfaces/erc721loyaltycard
- ERC721UpdatableMetadata - https://portal.thirdweb.com/interfaces/erc721updatablemetadata
- Permissions - https://portal.thirdweb.com/interfaces/permissions
ℹ Once you're done writing your contracts, you can run the following command to deploy them:
yarn deploy
Deploying
the smart contract
All checks passed, which means that the smart contract is ready to be deployed using thirdweb.
To do that, simply run:
npx thirdweb deploy
If the smart contract doesn't have any errors, you will see the following output on the terminal:
.......
💎 thirdweb v0.13.60 💎
✔ Detected project type: foundry
✔ Compilation successful
✔ Choose which contract(s) to deploy · NFT
✔ Upload successful
✔ Open this link to deploy your contracts: https://thirdweb.com/contracts/deploy/QmSJExQJfPYFuaRZuDu9XRR2jUu9yp3kaFX3Sdc1KRWxiP
The terminal will also open a browser window, directing towards a unique URL, as highlighted above.
Fill out the form as suggested in the
Fill Parameter
section.
Once you fill out the details of the ERC721 smart contract, click on the
Deploy Now
button and it will deploy the contract to the chosen network of your wallet.
info
If you want to test your Foundry-based contract, follow the steps mentioned in the
Testing the Smart Contract
guide.
Interacting with the Smart Contract
Once the contract is deployed, you can interact with it via Explore.
The following videos describe step-by-step, how to interact with different token contracts on Lisk.
How to interact with a deployed
ERC-20
contract via Explore?
How to interact with a deployed
ERC-721
contract via Explore?
How to interact with a deployed
ERC-1155
contract via Explore?
Deploying pre-built contracts via thirdweb Explore
via Solidity SDK
Prerequisites
Node.js v18+
Installing and Configuring thirdweb
Logging in with your API key
Creating a project
Creating
the smart contract
Building
the smart contract
Deploying
the smart contract
Interacting with the Smart Contract
## /guides/interacting-with-the-blockchain/viem
On this page
viem
a TypeScript interface for Ethereum that provides low-level stateless primitives for interacting with Ethereum.
You can use viem to interact with smart contracts deployed on Lisk.
Viem provides first-class support for chains implemented on the OP stack, see
viem.sh > OP stack
.
Install
To install viem run the following command:
npm
install
--save
viem
Setup
Before you can start using viem, you need to setup a
Client
with a desired
Transport
and
Chain
.
Public Client
In order to read data from Lisk, you need to create a
Public client
.
By extending the public client with
publicActionsL2
or
publicActionsL1
, you have access to additional methods for Lisk and other chains built on top of the OP stack.
See
Layer 2 extensions
for more information.
Lisk
Lisk Sepolia
public-client.js
import
{
createPublicClient
,
http
}
from
'viem'
import
{
lisk
,
ethereum
}
from
'viem/chains'
import
{
publicActionsL2
,
publicActionsL1
}
from
'viem/op-stack'
export
const
publicClientL2
=
createPublicClient
(
{
chain
:
lisk
,
transport
:
http
(
)
}
)
.
extend
(
publicActionsL2
(
)
)
export
const
publicClientL1
=
createPublicClient
(
{
chain
:
ethereum
,
transport
:
http
(
)
}
)
.
extend
(
publicActionsL1
(
)
)
public-client.js
import
{
createPublicClient
,
http
}
from
'viem'
import
{
liskSepolia
,
sepolia
}
from
'viem/chains'
import
{
publicActionsL2
,
publicActionsL1
}
from
'viem/op-stack'
export
const
publicClientL2
=
createPublicClient
(
{
chain
:
liskSepolia
,
transport
:
http
(
)
}
)
.
extend
(
publicActionsL2
(
)
)
export
const
publicClientL1
=
createPublicClient
(
{
chain
:
sepolia
,
transport
:
http
(
)
}
)
.
extend
(
publicActionsL1
(
)
)
Wallet Client
In order to write data to Lisk, you need to create a Wallet client (
createWalletClient
) and specify an
Account
to use.
By extending the wallet client with
walletActionsL1
or
walletActionsL2
, you have access to additional methods for Lisk and other chains built on top of the OP stack.
See
Layer 2 extensions
for more information.
JSON-RPC Account
Local Account
Lisk
Lisk Sepolia
wallet-client.js
import
{
createWalletClient
,
custom
}
from
'viem'
import
{
ethereum
,
lisk
}
from
'viem/chains'
import
{
walletActionsL1
,
walletActionsL2
}
from
'viem/op-stack'
// Retrieve Account from an EIP-1193 Provider.
const
[
account
]
=
await
window
.
ethereum
.
request
(
{
method
:
'eth_requestAccounts'
}
)
export
const
walletClientL1
=
createWalletClient
(
{
account
,
chain
:
ethereum
,
transport
:
custom
(
window
.
ethereum
)
}
)
.
extend
(
walletActionsL1
(
)
)
;
export
const
walletClientL2
=
createWalletClient
(
{
account
,
chain
:
lisk
,
transport
:
custom
(
window
.
ethereum
)
}
)
.
extend
(
walletActionsL2
(
)
)
;
wallet-client.js
import
{
createWalletClient
,
custom
}
from
'viem'
import
{
sepolia
,
liskSepolia
}
from
'viem/chains'
import
{
walletActionsL1
,
walletActionsL2
}
from
'viem/op-stack'
// Retrieve Account from an EIP-1193 Provider.
const
[
account
]
=
await
window
.
ethereum
.
request
(
{
method
:
'eth_requestAccounts'
}
)
export
const
walletClientL1
=
createWalletClient
(
{
account
,
chain
:
sepolia
,
transport
:
custom
(
window
.
ethereum
)
}
)
.
extend
(
walletActionsL1
(
)
)
;
export
const
walletClientL2
=
createWalletClient
(
{
account
,
chain
:
liskSepolia
,
transport
:
custom
(
window
.
ethereum
)
}
)
.
extend
(
walletActionsL2
(
)
)
;
Replace
<PRIVATE_KEY>
with the private key of the account you want to use.
The
0x
prefix indicates that the following string is a hexadecimal number.
Lisk
Lisk Sepolia
wallet-client.js
import
{
createWalletClient
,
http
}
from
'viem'
import
{
privateKeyToAccount
}
from
'viem/accounts'
import
{
ethereum
,
lisk
}
from
'viem/chains'
import
{
walletActionsL1
,
walletActionsL2
}
from
'viem/op-stack'
// Wallet client for Ethereum
export
const
walletClientL1
=
createWalletClient
(
{
account
:
privateKeyToAccount
(
'0x<PRIVATE_KEY>'
)
,
chain
:
ethereum
,
transport
:
http
(
)
}
)
.
extend
(
walletActionsL1
(
)
)
;
// Wallet client for Lisk
export
const
walletClientL2
=
createWalletClient
(
{
account
:
privateKeyToAccount
(
'0x<PRIVATE_KEY>'
)
,
chain
:
lisk
,
transport
:
http
(
)
}
)
.
extend
(
walletActionsL2
(
)
)
;
wallet-client.js
import
{
createWalletClient
,
http
}
from
'viem'
import
{
privateKeyToAccount
}
from
'viem/accounts'
import
{
sepolia
,
liskSepolia
}
from
'viem/chains'
import
{
walletActionsL1
,
walletActionsL2
}
from
'viem/op-stack'
// Wallet client for Ethereum
export
const
walletClientL1
=
createWalletClient
(
{
account
:
privateKeyToAccount
(
'0x<PRIVATE_KEY>'
)
,
chain
:
sepolia
,
transport
:
http
(
)
}
)
.
extend
(
walletActionsL1
(
)
)
;
// Wallet client for Lisk
export
const
walletClientL2
=
createWalletClient
(
{
account
:
privateKeyToAccount
(
'0x<PRIVATE_KEY>'
)
,
chain
:
liskSepolia
,
transport
:
http
(
)
}
)
.
extend
(
walletActionsL2
(
)
)
;
info
In addition to making a JSON-RPC request (
eth_requestAccounts
) to get an Account, viem provides various helper methods for creating an
Account
, including:
privateKeyToAccount
,
mnemonicToAccount
, and
hdKeyToAccount
.
Reading data from the blockchain
Create a
public client
and use it to read and access data from Lisk using
Public Actions
and
OP stack public actions
.
Public Actions are client methods that map one-to-one with a "public" Ethereum RPC method (
eth_blockNumber
,
eth_getBalance
, etc.).
For example, you can use the
getBlockNumber
action to get the latest block:
read-example.js
import
{
parseEther
}
from
'viem'
import
{
publicClientL2
}
from
'./public-client.js'
const
blockNumber
=
await
publicClientL2
.
getBlockNumber
(
)
;
export
const
l1Gas
=
await
publicClientL2
.
estimateL1Gas
(
{
account
:
'0x3C46A11471f285E36EE8d089473ce98269D1b081'
,
to
:
'0x70997970c51812dc3a010c7d01b50e0d17dc79c8'
,
value
:
parseEther
(
'0.1'
)
}
)
console
.
log
(
blockNumber
)
;
console
.
log
(
l1Gas
)
;
Writing data to the blockchain
Create a
wallet client
and use it to read and access data from Lisk using
Wallet Actions
and
OP stack wallet actions
.
Wallet Actions are actions that map one-to-one with a "wallet" or "signable" Ethereum RPC method (
eth_requestAccounts
,
eth_sendTransaction
, etc).
For example, you can use the
sendTransaction
action to post a transaction:
write-example.js
import
{
walletClientL2
}
from
'./wallet-client.js'
const
hash
=
await
walletClientL2
.
sendTransaction
(
{
to
:
'0x...'
,
value
:
1000000000000000000n
}
)
console
.
log
(
hash
)
;
Interacting with smart contracts
You can use viem to interact with a smart contract on Lisk by creating a
Contract
instance using
getContract
and passing it the contract ABI, contract address, and and
Public
and/or
Wallet
Client:
contract-example.js
client.js
abi.js
contract-example.js
import
{
getContract
}
from
'viem'
import
{
wagmiAbi
}
from
'./abi.js'
import
{
publicClient
,
walletClient
}
from
'./client.js'
// 1. Create contract instance
const
contract
=
getContract
(
{
address
:
'CONTRACT_ADDRESS'
,
abi
:
wagmiAbi
,
// 1a. Insert a single client
//client: publicClient,
// 1b. Or public and/or wallet clients
client
:
{
public
:
publicClient
,
wallet
:
walletClient
}
}
)
// 2. Call contract methods, fetch events, listen to events, etc.
const
result
=
await
contract
.
read
.
totalSupply
(
)
const
logs
=
await
contract
.
getEvents
.
Transfer
(
)
const
unwatch
=
contract
.
watchEvent
.
Transfer
(
{
from
:
'0xA0Cf798816D4b9b9866b5330EEa46a18382f251e'
}
,
{
onLogs
(
logs
)
{
console
.
log
(
logs
)
}
}
)
Lisk
Lisk Sepolia
client.js
import
{
createPublicClient
,
createWalletClient
,
http
,
custom
}
from
'viem'
import
{
lisk
}
from
'viem/chains'
import
{
EthereumProvider
}
from
'@walletconnect/ethereum-provider'
import
{
publicActionsL2
}
from
'viem/op-stack'
export
const
publicClient
=
createPublicClient
(
{
chain
:
lisk
,
transport
:
http
(
)
,
}
)
.
extend
(
publicActionsL2
(
)
)
;
// eg: Metamask
export
const
walletClient
=
createWalletClient
(
{
chain
:
lisk
,
transport
:
custom
(
window
.
ethereum
)
,
}
)
// eg: WalletConnect
const
provider
=
await
EthereumProvider
.
init
(
{
projectId
:
"abcd1234"
,
showQrModal
:
true
,
chains
:
[
1
]
,
}
)
export
const
walletClientWC
=
createWalletClient
(
{
chain
:
lisk
,
transport
:
custom
(
provider
)
,
}
)
client.js
import
{
createPublicClient
,
createWalletClient
,
http
,
custom
}
from
'viem'
import
{
liskSepolia
}
from
'viem/chains'
import
{
EthereumProvider
}
from
'@walletconnect/ethereum-provider'
import
{
publicActionsL2
}
from
'viem/op-stack'
export
const
publicClient
=
createPublicClient
(
{
chain
:
liskSepolia
,
transport
:
http
(
)
,
}
)
.
extend
(
publicActionsL2
(
)
)
;
// eg: Metamask
export
const
walletClient
=
createWalletClient
(
{
chain
:
liskSepolia
,
transport
:
custom
(
window
.
ethereum
)
,
}
)
// eg: WalletConnect
const
provider
=
await
EthereumProvider
.
init
(
{
projectId
:
"abcd1234"
,
showQrModal
:
true
,
chains
:
[
1
]
,
}
)
export
const
walletClientWC
=
createWalletClient
(
{
chain
:
liskSepolia
,
transport
:
custom
(
provider
)
,
}
)
tip
The ABI of a contract can be found on the respective contract page in
BlockScout
.
abi.js
export
const
wagmiAbi
=
[
[
{
"inputs"
:
[
]
,
"stateMutability"
:
"nonpayable"
,
"type"
:
"constructor"
}
,
{
"anonymous"
:
false
,
"inputs"
:
[
{
"indexed"
:
true
,
"internalType"
:
"address"
,
"name"
:
"sender"
,
"type"
:
"address"
}
,
{
"indexed"
:
false
,
"internalType"
:
"string"
,
"name"
:
"message"
,
"type"
:
"string"
}
]
,
"name"
:
"NewHello"
,
"type"
:
"event"
}
,
{
"inputs"
:
[
{
"internalType"
:
"uint256"
,
"name"
:
""
,
"type"
:
"uint256"
}
]
,
"name"
:
"blacklist"
,
"outputs"
:
[
{
"internalType"
:
"string"
,
"name"
:
""
,
"type"
:
"string"
}
]
,
"stateMutability"
:
"view"
,
"type"
:
"function"
}
,
{
"inputs"
:
[
]
,
"name"
:
"counter"
,
"outputs"
:
[
{
"internalType"
:
"uint32"
,
"name"
:
""
,
"type"
:
"uint32"
}
]
,
"stateMutability"
:
"view"
,
"type"
:
"function"
}
,
{
"inputs"
:
[
{
"internalType"
:
"string"
,
"name"
:
"_message"
,
"type"
:
"string"
}
]
,
"name"
:
"createHello"
,
"outputs"
:
[
]
,
"stateMutability"
:
"nonpayable"
,
"type"
:
"function"
}
,
{
"inputs"
:
[
]
,
"name"
:
"maxLength"
,
"outputs"
:
[
{
"internalType"
:
"uint32"
,
"name"
:
""
,
"type"
:
"uint32"
}
]
,
"stateMutability"
:
"view"
,
"type"
:
"function"
}
,
{
"inputs"
:
[
{
"internalType"
:
"address"
,
"name"
:
""
,
"type"
:
"address"
}
]
,
"name"
:
"message"
,
"outputs"
:
[
{
"internalType"
:
"string"
,
"name"
:
""
,
"type"
:
"string"
}
]
,
"stateMutability"
:
"view"
,
"type"
:
"function"
}
,
{
"inputs"
:
[
]
,
"name"
:
"minLength"
,
"outputs"
:
[
{
"internalType"
:
"uint32"
,
"name"
:
""
,
"type"
:
"uint32"
}
]
,
"stateMutability"
:
"view"
,
"type"
:
"function"
}
,
{
"inputs"
:
[
]
,
"name"
:
"owner"
,
"outputs"
:
[
{
"internalType"
:
"address"
,
"name"
:
""
,
"type"
:
"address"
}
]
,
"stateMutability"
:
"view"
,
"type"
:
"function"
}
,
{
"inputs"
:
[
{
"internalType"
:
"string[]"
,
"name"
:
"_newBlackList"
,
"type"
:
"string[]"
}
]
,
"name"
:
"setBlacklist"
,
"outputs"
:
[
]
,
"stateMutability"
:
"nonpayable"
,
"type"
:
"function"
}
,
{
"inputs"
:
[
{
"internalType"
:
"uint32"
,
"name"
:
"_newMin"
,
"type"
:
"uint32"
}
,
{
"internalType"
:
"uint32"
,
"name"
:
"_newMax"
,
"type"
:
"uint32"
}
]
,
"name"
:
"setMinMaxMessageLength"
,
"outputs"
:
[
]
,
"stateMutability"
:
"nonpayable"
,
"type"
:
"function"
}
]
]
;
info
CONTRACT_ADDRESS
is the address of the deployed contract.
Install
Setup
Public Client
Wallet Client
Reading data from the blockchain
Writing data to the blockchain
Interacting with smart contracts
## /guides/interacting-with-the-blockchain/ethers
On this page
ethers.js
is a JavaScript library that allows developers to interact with EVM-compatible blockchain networks.
You can use ethers.js to interact with smart contracts deployed on the Lisk network.
Install
To install ethers.js run the following command:
npm
install
--save
ethers
Setup
Before you can start using ethers.js, you need to import it into your project.
Add the following line of code to the top of your file to import ethers.js:
const
ethers
=
require
(
'ethers'
)
;
Connecting to Lisk
You can connect to Lisk by instantiating a new ethers.js
JsonRpcProvider
object with an RPC URL of the Lisk network:
Lisk
Lisk Sepolia
const
ethers
=
require
(
'ethers'
)
;
const
url
=
'https://rpc.api.lisk.com'
;
const
provider
=
new
ethers
.
JsonRpcProvider
(
url
)
;
const
ethers
=
require
(
'ethers'
)
;
const
url
=
'https://rpc.sepolia-api.lisk.com'
;
const
provider
=
new
ethers
.
JsonRpcProvider
(
url
)
;
note
A
Provider
(in ethers.js) is a class that provides an abstraction for a connection to the Ethereum Network. It provides read-only access to the Blockchain and its status.
Reading data from the blockchain
Once you have created a provider, you can use it to read data from the Lisk network.
For example, you can use the
getBlockNumber
method to get the latest block:
async
function
getLatestBlock
(
)
{
const
latestBlock
=
await
provider
.
getBlockNumber
(
)
;
console
.
log
(
"The latest block's number is:"
,
latestBlock
)
;
}
getLatestBlock
(
)
;
Complete code example
const
ethers
=
require
(
'ethers'
)
;
// For Lisk Sepolia network
const
url
=
'https://rpc.sepolia-api.lisk.com'
;
// For Lisk network
// const url = 'https://rpc.api.lisk.com';
const
provider
=
new
ethers
.
JsonRpcProvider
(
url
)
;
async
function
getLatestBlock
(
)
{
const
latestBlock
=
await
provider
.
getBlockNumber
(
)
;
console
.
log
(
"The latest block's number is:"
,
latestBlock
)
;
}
getLatestBlock
(
)
;
Writing data to the blockchain
In order to write data to the Lisk network, you need to create a
Signer
.
note
A
Signer
is a class that (usually) in some way directly or indirectly has access to a private key, which can sign messages and transactions to authorize the network to charge your account ether to perform operations.
You can create a
Signer
by instantiating 프로젝트로 돌아가기