NEWTON

NEWTON


Popular tags

    How to submit a StarkNet contract?

    Asked

    3 months ago

    240

    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

      #17daysOfCairocairo-beginnerscairoplayground

    Newton

    asked

    3 months ago


    1 answers

    1

    Accepted answer

    Solution

    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.

    Cairo contract compiled

    Cairo programs vs Cairo Contracts

    When you write Cairo code you can build two different kind of application

    • Cairo Programs and
    • Cairo Contracts

    Cairo Programs

    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

    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

    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

    Link of the contract in Voyager

    Interaction with the "add" function

    Kevin Bravo

    answered

    3 months ago

    Your answer

    NEWTON

    NEWTON