Typesafe opinionated abstractions for developing Cairo 1.0 smart contracts

Overview

Suna Cover Photo

Suna PRs Welcome License: MIT CI Badge

Built with auditless/cairo-template

Typesafe opinionated abstractions for developing Cairo 1.0 smart contracts. Originally created to facilitate Yagi Finance smart contract development.

What is included

  • suna.math.u256: Missing trait implementations for the u256 data type: U256Zeroable and a truncated division implementation U256TruncatedDiv of Div<u256>
  • suna.math.u60f18: An unsigned fixed point decimal type based on u256; MulDiv trait and operators

Warning

Suna is an experimental and unaudited library and is subject to a lot of iteration. There may be bugs.

How to use the library

You can directly add Suna to your Scarb dependencies:

[dependencies]
suna = { git = "https://github.com/auditless/suna.git" }

The below examples are illustrative and you can find more functions by reading the code and tests directly.

u256 trait implementations

Most Defi applications will use the u256 type to deal with token amounts. Unfortunately the trait implementations are not yet complete. To add support for u256 division/mod operations, add the following:

use suna::math::u256::U256TruncatedDiv;
use suna::math::u256::U256TruncatedDivEq;
use suna::math::u256::U256TruncatedRem;
use suna::math::u256::U256TruncatedRemEq;

let a: u256 = 10.into();
let b: u256 = 2.into();
let c = a / b; // this will now work

MulDiv trait

When building a yield/pooling application, you may need a way to calculate how much of an underlying asset a given share owner controls. You can do it as follows:

use suna::math::u60f18::U256MulDiv;

let total_supply: u256 = 10000.into();
let shares: u256 = 33.into();
let total_assets: u256 = 853000000000000000000.into();
// Calculates shares * total_assets / total_supply safely
let assets_owned = U256MulDiv::mul_div(shares, total_assets, total_supply);

An 18-decimal fixed point type U60F18

You may also want to maintain certain fractional values such as weights or interest rates in your application using 18 decimals. To do that, you can use our U60F18 type which supports conversion from u256 and many of the standard operators:

use traits::Into;
use suna::math::u60f18::U256ToU60F18;
use suna::math::u60f18::U60F18DivEq;

// Represent an interest rate of 2%
let mut interest_rate: U60F18 = 2.into();
interest_rate /= 100.into();

Design principles

  • Be useful to developers building production Cairo contracts
  • Design from first principles with both Rust and smart contract idioms in mind
  • Build typesafe and efficient abstractions that are consistently designed
  • Respect and embrace the corelib trait hierarchy
  • Aspire to build well-documented and declarative code

How to contribute

  • Read the Cairo 1.0 setup guide at https://github.com/auditless/cairo-template
  • Check our issues for scoped tasks or propose/request a new one by opening an issue
  • Submit a PR linking the relevant issue
  • You may also submit a PR that fixes a bug or nit directly

Thanks to

  • The Quaireaux team for paving the way for Cairo 1.0 library development
  • The Scarb contributors for creating a pioneering package manager which we rely on
  • The OpenZeppelin team for the mulDiv interface
  • S. Tsuchiya for the cover photo
  • Last but not least, the StarkWare team for building the first smart contract language that is a joy to use

License

MIT © Auditless Limited

You might also like...
A library to provide abstractions to access common utilities when developing Dioxus applications.

🧰 Dioxus Standard Library 🚀 A platform agnostic library for supercharging your productivity with Dioxus. dioxus-std is a Dioxus standard library tha

Minimal Substrate node configured for smart contracts via pallet-contracts.

substrate-contracts-node This repository contains Substrate's node-template configured to include Substrate's pallet-contracts ‒ a smart contract modu

A typesafe, flexible, simple, and user-friendly unit system library for Rust that has good error messages.

uy A typesafe, flexible, simple, and user-friendly unit system library for Rust that has good error messages. Usage uy not only stores the unit of a v

Blazing fast toolkit for developing Starknet contracts.
Blazing fast toolkit for developing Starknet contracts.

Starknet Foundry Blazingly fast toolkit for developing Starknet contracts designed & developed by ex Protostar team from Software Mansion based on nat

TestSuite4 is a framework designed to simplify development and testing of TON Contracts. It includes light-weight emulator of blockchain making it easy to develop contracts.

TestSuite4 0.1.2 TestSuite4 is a framework designed to simplify development and testing of TON Contracts. It contains lightweight blockchain emulator

evm2near compiles Solidity contracts into NEAR WebAssembly contracts.

EVM → NEAR evm2near is a project for compiling EVM bytecode into wasm bytecode, with the particular goal of having that wasm artifact be executable on

Ticketed Discreet Log Contracts (DLCs) to enable instant buy-in for wager-like contracts on Bitcoin.
Ticketed Discreet Log Contracts (DLCs) to enable instant buy-in for wager-like contracts on Bitcoin.

dlctix Ticketed Discreet Log Contracts (DLCs) to enable instant buy-in for wager-like contracts on Bitcoin. This project is part of the Backdrop Build

Smart contracts for Ref Finance

Ref Finance Contracts This mono repo contains the source code for the smart contracts of Ref Finance on NEAR. Contracts Contract Reference Description

Skyward Finance smart-contracts

Build and Init ./build.sh near dev-deploy res/skyward.was export CONTRACT_ID=skyward.testnet near call $CONTRACT_ID new --account_id=$CONTRACT_ID Regi

Rust client to Opensea's APIs and Ethereum smart contracts

opensea.rs Rust bindings & CLI to the Opensea API and Contracts CLI Usage Run cargo r -- --help to get the top level help menu: opensea-cli 0.1.0 Choo

A framework for creating PoC's for Solana Smart Contracts in a painless and intuitive way

Solana PoC Framework DISCLAIMER: any illegal usage of this framework is heavily discouraged. Most projects on Solana offer a more than generous bug bo

My code for the terra.academy course on CosmWasm smart contracts

CosmWasm Starter Pack This is a template to build smart contracts in Rust to run inside a Cosmos SDK module on all chains that enable it. To understan

Smart contracts powering Spectrum Protocol on Terra

Spectrum Core Contracts This monorepository contains the source code for the core smart contracts implementing Spectrum Protocol on the Terra blockcha

Smart Contracts to be used by terra-bot

CosmWasm Starter Pack This is a template to build smart contracts in Rust to run inside a Cosmos SDK module on all chains that enable it. To understan

A template to build smart contracts in Rust to run inside a Cosmos SDK module on all chains that enable it.

CosmWasm Starter Pack This is a template to build smart contracts in Rust to run inside a Cosmos SDK module on all chains that enable it. To understan

Create your personal token with rust smart contracts

Solana Rust Token 💰 This application written Rust using Anchor ⚓

This is a node implementation of Thippy, a Substrate parachain for smart contracts

Thippy ‒- A Smart Contracts Parachain This is a node implementation of Thippy, a Substrate parachain for smart contracts. Developing Smart Contracts f

Rust library for build smart contracts on Internet Computer, by the Spinner.Cash team.

Spinner Rust library for building smart contracts on the Internet Computer. More specifically it is used by Spinner.Cash, a decentralized layer-2 prot

Rust implementation for Thippy -- a Substrate parachain for smart contracts.

Thippy ‒- A Smart Contracts Parachain This is a node implementation of Thippy, a Substrate parachain for smart contracts. Developing Smart Contracts f

Comments
  • feat: rename to felt252

    feat: rename to felt252

    This pr aims to do the following

    • Renames felt to felt252
    • Fix #1

    Note: Uses this version of scarb and cairo

    scarb 0.1.0-rc.2 (8e8d51d80 2023-03-20)
    cairo: 1.0.0-alpha.6
    

    image

    opened by ftupas 0
  • Add storage access implementation to `U60F18`

    Add storage access implementation to `U60F18`

    Add a storage access trait implementation that would allow U60F18 be used in contract storage.

    See here for other existing implementations: https://github.com/starkware-libs/cairo/blob/main/corelib/src/starknet/storage_access.cairo.

    opened by Pet3ris 0
Owner
Auditless
Auditless
A template to kickstart your Cairo 1.0 development

Cairo 1 template How to use Install Rust Click the "Use this template" button to use this as a basis for your repo or create a codespace on Github. To

Extropy.io 4 Dec 23, 2022
A compiler to convert Cairo's intermediate representation "Sierra" code to MLIR.

Cairo Sierra to MLIR compiler A compiler to convert Cairo's intermediate representation "Sierra" code to MLIR. Documentation There is an mdbook in the

Lambdaclass 5 Mar 16, 2023
An operating system kernel running on RISC-V arch. Developing...

An operating system kernel running on RISC-V arch How to build it Environmental requirement rustup ( >=1.57.0-nightly) Qemu ( >=5.0.0) Step Build loca

JAYICE 4 Dec 15, 2022
A smart table widget for fltk-rs

fltk-table A smart table widget for fltk-rs. It aims to reduce the amount of boilerplate required to create a table. Usage [dependencies] fltk = "1.2"

fltk-rs 11 Dec 8, 2022
A minimal template for building smart contracts with Cairo 1.0

Minimal Cairo 1.0 Template Built with auditless/cairo-template A minimal template for building smart contracts with Cairo 1.0 using the Quaireaux proj

Auditless 68 Apr 21, 2023
Helpful functions and macros for developing smart contracts on NEAR Protocol.

near-contract-tools Helpful functions and macros for developing smart contracts on NEAR Protocol. This package is a collection of common tools and pat

Jacob 27 Dec 17, 2022
Helpful functions and macros for developing smart contracts on NEAR Protocol.

near-contract-tools Helpful functions and macros for developing smart contracts on NEAR Protocol. This package is a collection of common tools and pat

NEARFoundation 9 Aug 3, 2022
zink! is a library for developing ink! smart contracts with useful Rust macros that extend functionality and reduce boilerplate code.

zink! Smart Contract Macros This is a helper library for developing ink! smart contracts. It contains useful Rust macros that extend functionality and

Scio Labs 3 Nov 3, 2023
Easy c̵̰͠r̵̛̠ö̴̪s̶̩̒s̵̭̀-t̶̲͝h̶̯̚r̵̺͐e̷̖̽ḁ̴̍d̶̖̔ ȓ̵͙ė̶͎ḟ̴͙e̸̖͛r̶̖͗ë̶̱́ṉ̵̒ĉ̷̥e̷͚̍ s̷̹͌h̷̲̉a̵̭͋r̷̫̊ḭ̵̊n̷̬͂g̵̦̃ f̶̻̊ơ̵̜ṟ̸̈́ R̵̞̋ù̵̺s̷̖̅ţ̸͗!̸̼͋

Rust S̵̓i̸̓n̵̉ I̴n̴f̶e̸r̵n̷a̴l mutability! Howdy, friendly Rust developer! Ever had a value get m̵̯̅ð̶͊v̴̮̾ê̴̼͘d away right under your nose just when

null 294 Dec 23, 2022
1️⃣ el lisp number uno - one lisp to rule them all 🏆

luno el lisp number uno luno is the one lisp to rule them all. Still experimental, do not use it in production yet. goals embeddable small size simple

Eva Pace 3 Apr 25, 2022