Home
Cairopractice
Cancel

Underhanded Cairo, pt. 1

Here’s a pretty simple Cairo program: use debug::PrintTrait; fn calc(v1: u64, v2: u64) -> (u64, u64) { (v1 + v2, v2 * v2) } fn main() { let mut res: u64 = 0; let mut counter: u64 ...

Mock addresses for testing

When writing tests, we often need mock addresses to represent some entity like an account without actually deploying an initializing an account contract. The obvious thing to do is to use values li...

Updating structs

Do you remember how, in Cairo 0, there was no concise way how to update a struct? We had to write out all its members even if only one changed. It was pretty annoying, as RoboTeddy pointed out in h...

Idiomatic array looping

Loops. The feature that made Cairopractors jealous of every other programming language including Brainfuck. Fortunatelly Cairo 1 fixes that. We have been promised proper for-in loops, but that’s no...

Interface spoofing for fun and performance

What happens when types in a Starknet contract interface do not match the contract we’re interfacing with? Let’s find out. We’ll use a simple contract just to get some data: #[contract] mod Sourc...

Storing user defined types, part 2

In the first part of this series, we learnt how to implement StorageAccess for a custom data type so we can use it in contract storage. The struct itself was pretty simple, containing only primitiv...

Storing user defined types, part 1

When we want to store a custom struct in a Starknet contract, we immediatelly run into a problem. Take this simple contract for example: #[derive(Drop, Serde)] struct Pos2D { x: u16, y: u1...

Calling contracts using dispatch

A defining feature of programmable blockchains is the ability to call other contracts. Let’s explore how to do this in Starknet using Cairo. We start by declaring an interface of the called contra...