A first quick disclaimer. Don't expect any kind of reward from the following, except learning some things about StarkNet and trying out a cool new L2.
We don't have a token.
So, you heard that
#StarkNet
Alpha is on mainnet (Yes, Ethereum Mainnet) and you want to try it out. Without spending money, preferrably.
I got you ππ§Ά
So what does it mean for StarkNet to be on Mainnet?
- All the logic executed on StarkNet is secured by Ethereum
- You can transfer arbitrary messages between both chains
- You can use these messages to build bridges (more below)
Some cool apps will ship in the coming days that let you test the execution layer on StarkNet.
A token bridge will also be there, and let you transmit tokens back and forth
Today I want to show you messaging MainNet <-> StarkNet
We are bringing one of the first DAPP on
#StarkNet
π
π π
Feel free to play around with light version of
@ibetyouxyz
and share all suggestions with us. π
Proud to be part of
@StarkWareLtd
ecosystem! β¨
Why messaging?
Because it's a basic building block that allows you to create rich L1 <-> L2 interactions, and because it's tangible. You get to see the results of things you did on SN, on MN.
So we're going to write a graffiti on MN. We write a string message on SN, and it appears on MN inside an event.
It's simple, and you can play with it through Etherscan and Voyager (SN block explorer).
SN doesn't handle strings natively yet; you'll need to convert you string to hexadecimal, then decimal. Suboptimal, I know, but we're on the frontier, bear with me.
I use this and this
Inside the function, it's very simple. Messages to L1 are sent as arrays through send_message_to_l1. The contract stores the L1 recipient address (to_address).
So we retrieve to_address from storage; fit your message in an array; send.
Note: transactions are currently free on SN! You don't need to pay for gas. You don't need money to get started.
This will change in the future. SN is in Alpha: we are testing a feature set (executing code, MN messages) which will grow
You've now sent your message. Cool! Where can you see it?
Well, transaction take time to show up in voyager. Like, a very long time (2h).
We're working on it and this should change soon. In the meanwhile don't resend. You tx is there. Don't panic
Voyager doesn't show "pending" transactions, only transactions that are in a block. And Starknet on mainnet currently has a block time of 2h.
Why? Because there is little trafic for now, and each block cost money.
@FeedTheFed
can explain better :-)
The frequency will increase as trafic increase. Our testnet has a block every 2mn or so .
Once your tx is included in a block, you'll see it appear as "accepted on L2".
And every ~10h, a "proof" is generated. At that point, the validity of all tx executions on SN is "proved" mathematically to MN, and state is updated accordingly.
You can see these updates here
That is when your message is transmitted. Going back to the SN smart contract, you can see a "messages" tab. This will list all messages to/from L1, to your contract.
If you click on a message, you can see its status
Pending = In a L2 block, waiting for L1 state update
Sent = Sent to L1, waiting for L1 counterpart to read it
Consumed = Retrieved and interpreted by L1 contract
Interesting detail. Note that your message is sent to L1, but not stored in L1. Only its hash, and the recipient.
In order to consume our message, we'll have to re send the message to L1, to recalculate the hash.
Consuming our message means calling consumeMessageFromL2() from a L1 contract. In our case, StarkNetGraffiti has a function graffFromStarknetOnMainnet() that does the trick. StarkNetGraffiti specifies the L2 contract sender address, and the message.
Ok. So at that point, my L1 smart contract can:
- Retrieve a message he knows is for it (only it can consume it)
- Knows the sender of
- Read and use the message
And that's your basic building block
My message here is a simple int. But consumeMessageFromL2() receives an array. As long as I cast them as uint256, I can pass however many I want.
I used my message to emit an event. Mostly because it's the cheapest action I can think of.
But I could have
- Minted an NFT
- Transferred tokens
- Deposit on
@AaveAave
- Swap on
@Uniswap
- Execute a goverance decision
- <insert arbitrary idea you may have>
And voila: An event on L1, that was triggered by an action on L2. Nice!
Want to try it yourself?
The SN contract
The L1 contract
Sending on L2 is free, consuming on L1 is not. I'll unpack a few messages for you if you ask :-)
That's it for now. I hope you learned something :-) Let me know if this is useful, and if you have questions.
Repo incoming, needs a bit of cleaning up
And soon, we'll go the other way - send a message on L1, graff on L2. Stay tuned π
!WARNING!
Be careful, there are no safeguards when you send a transaction through Etherscan. This error means your transaction will fail and you'll lose a bunch of ETH. DON'T SEND