Skip to content

Instantly share code, notes, and snippets.

@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
"""
@milancermak
milancermak / app-session.ts
Last active December 8, 2022 03:00
Custom session storage for a Shopify app in SQL using Prisma
View app-session.ts
import { PrismaClient } from '@prisma/client'
import Shopify from '@shopify/shopify-api'
import { Session } from '@shopify/shopify-api/dist/auth/session';
const prisma = new PrismaClient({ log: ['info', 'warn', 'error'] })
async function storeCallback(session: Session): Promise<boolean> {
const payload: { [key: string]: any } = { ...session }
return prisma.appSession.upsert({
create: { id: session.id, payload: payload },
@milancermak
milancermak / ddbwriter_main.py
Created March 7, 2019 20:03
Experiments with DynamoDB and Connection HTTP headers
View ddbwriter_main.py
import json
import os
import time
import uuid
import boto3
def set_connection_header(request, operation_name, **kwargs):
# request.headers['Connection'] = 'keep-alive'
@milancermak
milancermak / buildspec_container.yml
Created February 15, 2019 20:03
CodePipeline + Fargate
View buildspec_container.yml
---
version: 0.2
phases:
pre_build:
commands:
- $(aws ecr get-login --region $AWS_DEFAULT_REGION --no-include-email)
- COMMIT_HASH="$(echo $CODEBUILD_RESOLVED_SOURCE_VERSION | cut -c 1-7)"
- IMAGE_TAG="${COMMIT_HASH:=latest}"
- printenv