NEWTON
Asked
5 months ago
262
views
1
Hello! This is day 16 of the 17 days of the Cairo Challenge. In this exercise it is required to submit your first StarkNet contract. When you try to make it, you get an error:
Error: code:20:1: Unsupported %lang directive. Are you using the correct compiler? %lang starknet ^************^
Can you show your solution to playground exercise “StarkNet contract” and also send a link to your contract, and a screenshot where you use your add
method?
// [StarkNet](https://starkware.co/product/starknet/) is a permissionless decentralized ZK-Rollup operating // as an L2 network over Ethereum, where any dApp can achieve // unlimited scale for its computation, without compromising // Ethereum's composability and security. // // This is a simple StarkNet contract. // Note that you won't be able to use the playground to compile and run it, // but you can deploy it on the [StarkNet Planets Alpha network](https://medium.com/starkware/starknet-planets-alpha-on-ropsten-e7494929cb95)! // // 1. Click on "Deploy" to deploy the contract. // For more information on how to write Cairo contracts see the // ["Hello StarkNet" tutorial](https://cairo-lang.org/docs/hello_starknet). // 2. Click on the contract address in the output pane to open // [Voyager](https://goerli.voyager.online/) - the StarkNet block explorer. // 3. Wait for the page to load the information // (it may take a few minutes until a block is created). // 4. In the "STATE" tab, you can call the "add()" transaction. // The "%lang" directive declares this code as a StarkNet contract. %lang starknet @view func add(x: felt, y: felt) -> (res: felt) { return (res=x + y); }
Answers to this question are a part of the ✨ 17 days of Cairo Lang with Playground & Newton. ✨
Vote for your favorite answer - the best answer will win a $10 award. A new day – a new reward! During the next 17 days, our goal is to attract more developers to the Cairo language and to systematize the knowledge of Cairo lang. Read rules
Newton
asked
5 months ago
1
Accepted answer
Cairo Contracts have to be compiled using a different compiler than Cairo Programs, that's why we get an error when trying to compile it in the Cairo playground. Just go to the Starknet Playground instead and compile it there.
When you write Cairo code you can build two different kind of application
Those are just the same kind of programs that you, probably, were writing while answer the #17daysOfCairo challenge.
They are stateless program that cannot interact with other Cairo programs. they not require any special syntax to be written just that you been doing
Cairo contracts instead are applications that runs on the Starknet VM (an L2 ZK-Rollup scaling solution in Ethereum) This contracts can have state (they have access to the Starknet's state, so they can modify state variables) and also they can communicate and interact with other Cairo contracts on Starknet.
To be able to write Cairo Contracts you have to add a special declaration at the beginning of you Cairo Code %lang starknet
%lang starknet func do_something() -> (res: felt) ...
This would tell the compiler: "Heyy! this is not a Cairo Program, you need a starknet compiler to be able to compile it"
You can compile it by downloading and using one of the packages to build in starknet (Hardhat, Protostar, Nile) but this would require you to set and environment. 😒
Instead, you could use the Starknet Playground which has a built-in compiler in it, for those occasions where you only need to test something.
Voyager is the block explorer of Starknet contracts, similar to [Etherscan(https://etherscan.io/) for Ethereum.
There you could search and see transactions, contracts, tokens... and even interact with the contracts that have been deployed on the network. Like ours.
To be able to interact with the contract you can go to the ’Read contract” or ”Write Contract” tab
There you could see all the functions that the contract has and you can interact with all of them, reading or wrating values
Kevin Bravo
answered
5 months ago
How to use get_fp_and_pc in Cairo Lang?
How to write a function in Cairo Lang (StarkNet)?
Cairo Lang / StarkNet: What are Revoked references? What is alloc_locals?
What is SHARP in Cairo Language?
How to divide felt in Cairo Lang using unsigned_div_rem?
What is the difference between tempvar/let in Cairo Lang? How to use allow_locals and local?
How to make math operation with Field Elements (felts) in Cairo lang?
Is Kakarot zkEVM a smart contract or a blockchain?
why does keccak256 replace sha256 in warp?
How can I create an array and populate it in the hint Cairo lang/Protostar?
How does Argent X wallet estimate fees in Starknet before sending a tx?
What is the technical difference of Kakarot compare to Nethermind's Warp?
How can I connect a StarkNet account using starknetjs?
How does warp handles the contract solidity version?