I wrote this article on the Starknet Edu blog. You can find the article here.
Tag: starkware
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)
StarkNet ERC721 Workshop: Exercise 6
Note: This article was updated on Sep 20th, 2022 to use Cairo 0.10
Claiming an NFT
- Mint a NFT with metadata on this dummy ERC721 token, usable here
- Check it on Oasis
- Claim points on ex6_claim_metadata_token (2 pts)
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)
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)
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)
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)
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)
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 Implicit Arguments Exercise
Write a function that returns the multiplication of the first two arguments if the third argument is an even number or performs an addition if odd.
Continue reading “Cairo’s Implicit Arguments Exercise”Cairo’s Array Creation Exercise
Create an array with the values 1, 2 and 3, and print the last value to the terminal.
Continue reading “Cairo’s Array Creation Exercise”Cairo’s Tail Recursion Exercise
Implement the function y = x^n.
Continue reading “Cairo’s Tail Recursion Exercise”Cairo’s Function Exercise II
Use the information given in the last section, in order to write a piece of code that when executed puts the current values of ap, fp and pc in memory (say, write ap into [ap], fp into [ap + 1] and pc into [ap + 2]).
Continue reading “Cairo’s Function Exercise II”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
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):
- Move the instruction alloc_locals.
- 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
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
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
Cairo’s Revoked Reference Exercise
Run the following code, with –steps=32 –print_memory and explain what happens.
func main():
let x = [ap]
[ap] = 1; ap++
[ap] = 2; ap++
[ap] = x; ap++
jmp rel -1 # Jump to the previous instruction.
end
Cairo’s Relative Jump Exercise
What happens in the following code? (you can start by running it and looking at the memory; note that you will need the –no_end flag)
func main():
[fp + 1] = 2; ap++
[fp] = 5201798304953761792; ap++
jmp rel -1
end