contracts/
directory, create a contract called NFT_with_reputation.cairo
:
NFT_with_reputation.cairo
contract with the following code:
nftId
is used to identify the NFT token ID when it is minted.rep_up
can be called by any contract on L2 to increase the reputation of the NFT.rep_down
can be called by any contract on L2 to decrease the reputation of the NFT and send the reputation points as a message to the L1 contract.get_rep
can be called by any contract on L2 to get the current reputation of the NFT.l1_handler
processes the message from the L1 contract.L1L2ERC721Rep.sol
contract:
withdrawToIdle
external function withdraws the NFT reputation points sent to L1 using the L2 rep_down
function. Mechanically, once you hit the L2 rep_down
function, the L2 state is pushed as a message into the L1 Starknet core contract. The rep_down
message then sits in the L1 Starknet core contract until it gets consumed by triggering the withdrawToIdle
function in your L1 contract.repUp
external function of the L2 contract computed for REPUP_SELECTOR
. To compute:repUp
):
repUp
function to send a message to the L2 NFT contract to increase the NFT reputation.
If this seems a little complicated at this point, do not worry—just keep following the tutorial and you will have a full hands-on walkthrough, which will help understand each of the steps better.
In Remix, compile the L1L2ERC721Rep.sol
contract.
In Remix, on the deployment tab:
L1L2ERC721Rep
.0xde29d060D45901Fb19ED6C6e959EB22d8626708e
. This is the address of the L1 Starknet core contract on the Ethereum Sepolia testnet.L1L2ERC721Rep.sol
contract.L1_MESSAGING_CONTRACT
in the L2 contract code that you have prepared with the actual L1 contract address.
In your Nile project directory, compile the contract:
NFT_with_reputation
for this tutorial.NFTwithRep
, ticker NFTRP
, and owner 0x02a152cb1a753a858c950bb710d957e7f3f78d87435831e7fae394f9233e60fa
:
starknet tx_status --hash HASH --network=alpha-sepolia
command.
Example:
ACCEPTED_ON_L2
status, the contract is running on the Starknet network.
REJECTED
status, reattempt the transaction and up the suggested network fee when prompted by ArgentX.mint
, provide a deployed account to mint an NFT to in the to
field and any ID in tokenId
. This will be the ID of the minted NFT that you will assign the reputation points to.rep_up
, provide the ID of your minted NFT in nftId
and any integer for reputation points in points
. For example, assign 1500
.get_rep
.
Now that you have the points assigned, you will decrease the reputation partially and send the reputation points to L1.
rep_down
, provide the ID of your minted NFT in nftId
and any integer for reputation points in points
. For example, remove 700
points.RECEIVED
— the transaction is received by the Starknet node.PENDING
– the transaction is valid and is in a pending block.ACCEPTED_ON_L2
– the transaction is in an accepted block on L2.ACCEPTED_ON_L1
— the transaction is pushed onto L2 as a message and ready to be consumed on L1.RECEIVED
to ACCEPTED_ON_L1
will take about 30 minutes on average on the testnet and 2 hours on the mainnet.
You can check the deployment status with the starknet tx_status --hash HASH --network=alpha-sepolia
command.
Example of an L2 transaction sending a message to L1 with nftId
: 1
and rep_down
: 700
: 0x6f753d8249a1f94d6ede18e34717cddf35936c1bcce329be599ad6c1ca7afaf.
Example of the message arriving on L1 (#89): 0x56a8e663d0e607fc6eb74b6b7bb713c5acdc262130e7790a8b252c192b41fcc9.
You now have the message in the Starknet core contract on L1 waiting to be consumed by triggering your L1 contract.
withdrawToIdle
, provide:
l2ContractAddress
— the address of your NFT contract deployed on L2nftId
— the ID of the minted NFT. For this tutorial, it is 1
.points
— the number of reputation points you removed from the NFT contract on L2. For this tutorial, it is 700
.repUp
on L1.
First, check the current reputation points of the NFT on L2 by calling get_rep
on the L2 contract. If you are following the example numbers in this tutorial, you should get the reputation of 800
for nftId
: 1
on L2.
Now up the reputation by 300
points by sending a message from L1 to L2:
repUp
, provide:
l2ContractAddress
— the address of your NFT contract deployed on L2nftId
— the ID of the minted NFT. For this tutorial, it is 1
.points
— the number of reputation points you want to put back in the NFT reputation on the L2 contract. For this tutorial, it is 300
.get_rep
on the L2 contract. You should get the reputation of 1100
for nftId
: 1
on L2.
Congratulations! You have completed the full L1 <-> L2 messaging exercise on Starknet.