Skip to content

Instantly share code, notes, and snippets.

@milancermak
milancermak / pow.cairo
Last active September 1, 2023 09:39
Cairo pow comparison
View pow.cairo
fn pow_old(base: u128, mut exp: u128) -> u128 {
if exp == 0 {
1
} else {
base * pow_old(base, exp - 1)
}
}
fn pow_new(base: u128, exp: u128) -> u128 {
@milancermak
milancermak / UDC_abi.json
Created May 30, 2023 08:47
A short on how to declare & deploy to Starknet right now
View UDC_abi.json
[
{
"data": [
{
"name": "address",
"type": "felt"
},
{
"name": "deployer",
"type": "felt"
@milancermak
milancermak / game.cairo
Created March 31, 2023 18:56
StorageAccess with embedded structs
View game.cairo
use starknet::StorageAccess;
use starknet::StorageBaseAddress;
use starknet::SyscallResult;
use starknet::storage_read_syscall;
use starknet::storage_write_syscall;
use starknet::storage_address_from_base_and_offset;
use traits::Into;
use traits::TryInto;
use option::OptionTrait;
@milancermak
milancermak / fighter.cairo
Created March 24, 2023 18:42
Custom type storage and serde
View fighter.cairo
#[contract]
mod FighterComponent {
use option::OptionTrait;
use starknet::SyscallResult;
use traits::Into;
use traits::TryInto;
#[derive(Copy, Drop)]
struct Fighter {
attack: u16,
@milancermak
milancermak / devnet.tf
Created December 23, 2022 21:37
Starknet devnet + Apibara <3 Terraform <3 DO
View devnet.tf
// https://slugs.do-api.dev/
resource "digitalocean_droplet" "sn_devnet" {
image = "docker-20-04"
name = "starknet-devnet"
region = "ams3"
size = "s-1vcpu-1gb"
ssh_keys = [
data.digitalocean_ssh_key.tf_ssh_key.id
]
@milancermak
milancermak / pow10.cairo
Created July 29, 2022 21:04
pow10.cairo
View pow10.cairo
func pow10(i) -> (res):
let (p) = get_label_location(data)
return ([p + i])
data:
dw 10 ** 0
dw 10 ** 1
dw 10 ** 2
dw 10 ** 3
dw 10 ** 4
@milancermak
milancermak / contract.cairo
Last active July 26, 2022 14:29
shared external funcs in cairo
View contract.cairo
%lang starknet
from openzeppelin.access.ownable import Ownable
# this import brings public function from ownable_external.cairo
# into this contract; the OWNABLE is just a placeholder, because
# something has to be imported, Cairo doesn't support
# `from foo import *` nor `import foo`
from contracts.ownable_external import OWNABLE
View static_analysis.yml
name: Cairo static analysis
on:
push:
branches:
- master
pull_request:
jobs:
test:
@milancermak
milancermak / README.md
Last active February 24, 2022 21:00
keccak256 of uint256 in Cairo
View README.md

This gist contains the following Solidity code translated to Cairo:

uint256 x = 20
uint256 out = uint256(keccak256(abi.encode(x)))

It uses the keccak256 implementation from starknet-l2-storage-verifier lib. The function deals with all the peculiarities of the necessary conversion (endianness, splitting of Uint256 to four 64-bit words and back again). There's also two helper python function to illustrate how to pass arguments in and how to deal with the result.

@milancermak
milancermak / calc_rent_exemption.py
Last active October 28, 2021 10:40
Calculate minimal rent exemption balance for a Solana account
View calc_rent_exemption.py
"""
A simple cmd line script to calculate the minimal rent exemption balance
of a Solana account.
Usage:
python calc_rent_exemption.py ACCONUT_ADDRESS
"""