StarkNet ERC721 Workshop: Exercise 7

Note: This article was updated on Sep 20th, 2022 to use Cairo 0.10

Adding Metadata

  • Create a new ERC721 contract that supports metadata. You can use this contract as a base
  • The base token URI is the chosen IPFS gateway
  • You can upload your NFTs directly on this website
  • Your tokens should be visible on Oasis once minted!
  • Deploy your new contract
  • Call submit_exercise() in the Evaluator to configure the contract you want evaluated
  • Claim points on ex7_add_metadata (2 pts)
Continue reading “StarkNet ERC721 Workshop: Exercise 7”

StarkNet ERC721 Workshop: Exercise 5

Note: This article was updated on Sep 20th, 2022 to use Cairo 0.10

Adding Permissions and Payments

  • Use dummy token faucet to get dummy tokens
  • Use ex5a_i_have_dtk() to show you managed to use the faucet (2 pts)
  • Create a function to allow breeder registration.
  • This function should charge the registrant for a fee, paid in dummy tokens (check registration_price)
  • Add permissions. Only allow listed breeders should be able to create animals
  • Deploy your new contract
  • Call submit_exercise() in the Evaluator to configure the contract you want evaluated
  • Call ex5b_register_breeder() to prove your function works. If needed, send dummy tokens first to the evaluator (2pts)
Continue reading “StarkNet ERC721 Workshop: Exercise 5”

StarkNet ERC721 Workshop: Exercise 4

Note: This article was updated on Sep 20th, 2022 to use Cairo 0.10

Burning NFTs

  • Create a function to allow breeders to declare dead animals (burn the NFT)
  • Deploy your new contract
  • Call submit_exercise() in the Evaluator to configure the contract you want evaluated
  • Call ex4_declare_dead_animal() to get points (2 pts)
Continue reading “StarkNet ERC721 Workshop: Exercise 4”

StarkNet ERC721 Workshop: Exercise 3

Note: This article was updated on Sep 20th, 2022 to use Cairo 0.10

Minting NFTs

  • Create a function to allow breeders to mint new animals with the specified characteristics
  • Deploy your new contract
  • Call submit_exercise() in the Evaluator to configure the contract you want evaluated
  • Call ex3_declare_new_animal() to get points (2 pts)
Continue reading “StarkNet ERC721 Workshop: Exercise 3”

StarkNet ERC721 Workshop: Exercise 2

Note: This article was updated on Sep 20th, 2022 to use Cairo 0.10

Creating Token Attributes

  • Call ex2a_get_animal_rank() to get assigned a random creature to create.
  • Read the expected characteristics of your animal from the Evaluator
  • Create the tools necessary to record animals characteristics in your contract and enable the evaluator contract to retrieve them trough get_animal_characteristics function on your contract (check this)
  • Deploy your new contract
  • Mint the animal with the desired characteristics and give it to the evaluator
  • Call submit_exercise() in the Evaluator to configure the contract you want evaluated
  • Call ex2b_test_declare_animal() to receive points (2 pts)
Continue reading “StarkNet ERC721 Workshop: Exercise 2”

StarkNet ERC721 Workshop: Exercise 1

Note: This article was updated on Sep 20th, 2022 to use Cairo 0.10

Deploying an ERC721

  • Create an ERC721 token contract. You can use this implementation as a base
  • Deploy it to the testnet (check the constructor for the needed arguments. Also note that the arguments should be decimals.)
$ starknet-compile contracts/ERC721/ERC721.cairo --output artifacts/ERC721.json
$ starknet deploy --contract artifacts/ERC721.json --inputs arg1 arg2 arg3 --network alpha-goerli
  • Give token #1 to Evaluator contract
  • Call submit_exercise() in the Evaluator to configure the contract you want evaluated (4 pts)
  • Call ex1_test_erc721() in the evaluator to receive your points (2 pts)
Continue reading “StarkNet ERC721 Workshop: Exercise 1”

StarkNet ERC721 Workshop: Setup

Note: I wrote a better guide on how to setup a local development environment here. Use that guide instead of this one as I’ve updated all the articles on this series to use it.

In this tutorial we will be completing the ERC721 workshop created by the StarkWare team so developers can get familiarized with creating smart contracts with Cairo and deploying them to StarkNet’s test network, Goerli.

I’ll be breaking down the workshop into multiple articles to keep each post to a manageable size and to make it easy to find by search engines. Make sure to follow the link at the end of each article to go to the next section.

Continue reading “StarkNet ERC721 Workshop: Setup”

Cairo’s Function Exercise I

Compile and run (with at least 10 steps) the following code. Use the –print_memory and –relocate_prints flags for cairo-run.

func main():
    call foo
    call foo
    call foo

    ret
end

func foo():
    [ap] = 1000; ap++
    ret
end

Try to think what happens when the cpu gets to the ret instruction (which of the registers ap, fp, pc should change when ret is executed and to what values?).

Continue reading “Cairo’s Function Exercise I”

Cario’s Reassigned and Revoked Reference Example

The example code found on this section of the Cairo docs helps us understand why references created with the let keyword are fundamentally different to values created with the tempvar and local keywords and why references are sometimes revoked when performing jumps.

func foo(x):
    let y = 1
    jmp x_not_zero if x != 0

    x_is_zero:
    [ap] = y; ap++  # y == 1.
    let y = 2
    [ap] = y; ap++  # y == 2.
    jmp done

    x_not_zero:
    [ap] = y; ap++  # y == 1.
    let y = 3
    [ap] = y; ap++  # y == 3.

    done:
    # Here, y is revoked, and cannot be accessed.
    ret
end
Continue reading “Cario’s Reassigned and Revoked Reference Example”

Cairo’s Local Variables Exercise II

Can you spot an inefficiency in the following code? Hint: take a look here. Fix the inefficiency in two ways (implement each of the following fixes separately):

  1. Move the instruction alloc_locals.
  2. Use tempvar instead of local.
func pow4(n) -> (m : felt):
    alloc_locals
    local x

    jmp body if n != 0
    [ap] = 0; ap++
    ret

    body:
    x = n * n
    [ap] = x * x; ap++
    ret
end

func main():
    pow4(n=5)
    ret
end
Continue reading “Cairo’s Local Variables Exercise II”

Cairo’s Local Variables Exercise I

What’s wrong with the following code? (Hint: try to replace ap += SIZEOF_LOCALS with alloc_locals and see what happens) Can you fix it without changing the order of the variable definitions in the code?

func main():
    tempvar x = 0

    local y
    ap += SIZEOF_LOCALS
    y = 6
    ret
end
Continue reading “Cairo’s Local Variables Exercise I”

Cairo’s Temporary Variables Exercise

Rewrite the solution to Exercise – A simple Cairo program using temporary variables.

func main():
    [ap] = 100; ap++
    [ap] = [ap - 1] * [ap - 1]; ap++ # x * x (x^2)
    [ap] = [ap - 1] * [ap - 2]; ap++ # x^2 * x (x^3)
    [ap] = [ap - 2] * 23; ap++       # x^2 * 23
    [ap] = [ap - 4] * 45; ap++       # x * 45
    [ap] = [ap - 3] + [ap - 2]; ap++ # x^3 + x^2 * 23
    [ap] = [ap - 1] + [ap - 2]; ap++ # x^3 + x^2 * 23 + x * 45
    [ap] = [ap - 1] + 67; ap++       # x^3 + x^2 * 23 + x * 45 + 67
    ret
end
Continue reading “Cairo’s Temporary Variables Exercise”