Skip to content

Commit

Permalink
✨ Introduce examples
Browse files Browse the repository at this point in the history
  • Loading branch information
bal7hazar committed Nov 5, 2023
1 parent 2bfe516 commit f4365e4
Show file tree
Hide file tree
Showing 43 changed files with 7,111 additions and 12 deletions.
File renamed without changes.
53 changes: 53 additions & 0 deletions .github/workflows/examples.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
name: Test

on: [push, pull_request]

env:
DOJO_VERSION: v0.3.3
WORKING-DIRECTORY: examples

jobs:
check:
runs-on: ubuntu-latest
name: Check format
steps:
- uses: actions/checkout@v4
- uses: software-mansion/setup-scarb@v1
- name: Format
working-directory: ${{ env.WORKING-DIRECTORY }}
run: scarb fmt --package random --check
shell: bash

build:
runs-on: ubuntu-latest
name: Build package
steps:
- uses: actions/checkout@v4
- uses: ./.github/actions/setup
- name: Build
working-directory: ${{ env.WORKING-DIRECTORY }}
run: sozo build
shell: bash

market:
runs-on: ubuntu-latest
name: Test market example
steps:
- uses: actions/checkout@v4
- uses: ./.github/actions/setup
- name: Test
working-directory: ${{ env.WORKING-DIRECTORY }}
run: sozo test
shell: bash

projectile:
runs-on: ubuntu-latest
name: Test projectile example
steps:
- uses: actions/checkout@v4
- uses: ./.github/actions/setup
- name: Test
working-directory: ${{ env.WORKING-DIRECTORY }}
run: sozo test
shell: bash

5 changes: 1 addition & 4 deletions Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ version = "0.0.0"
description = "Community-maintained libraries for Cairo"
homepage = "/~https://github.com/dojoengine/origami"
members = [
"crates/algebra",
"crates/defi",
"crates/random",
"crates/security",
"crates/*",
]

[workspace.dependencies]
Expand Down
2 changes: 1 addition & 1 deletion crates/algebra/src/lib.cairo
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
mod vec2;
mod vector;
mod matrix;
mod matrix;
2 changes: 1 addition & 1 deletion crates/algebra/src/matrix.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -412,4 +412,4 @@ mod tests {
assert(inverse.get(2, 1) == -1, 'Matrix: inversion failed');
assert(inverse.get(2, 2) == 1, 'Matrix: inversion failed');
}
}
}
10 changes: 5 additions & 5 deletions crates/algebra/src/vec2.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -78,22 +78,22 @@ impl Vec2Impl<T, impl TCopy: Copy<T>, impl TDrop: Drop<T>> of Vec2Trait<T> {
/// Vec2<T> -> Vec2<T>
#[inline(always)]
fn xx(self: Vec2<T>) -> Vec2<T> {
Vec2 { x: self.x, y: self.x, }
Vec2 { x: self.x, y: self.x, }
}

#[inline(always)]
fn xy(self: Vec2<T>) -> Vec2<T> {
Vec2 { x: self.x, y: self.y, }
Vec2 { x: self.x, y: self.y, }
}

#[inline(always)]
fn yx(self: Vec2<T>) -> Vec2<T> {
Vec2 { x: self.y, y: self.x, }
Vec2 { x: self.y, y: self.x, }
}

#[inline(always)]
fn yy(self: Vec2<T>) -> Vec2<T> {
Vec2 { x: self.y, y: self.y, }
Vec2 { x: self.y, y: self.y, }
}
}

Expand Down Expand Up @@ -266,4 +266,4 @@ mod tests {
assert(vec2yy.y.mag == 2 * ONE_u128, 'invalid y.mag');
assert(vec2yy.y.sign == true, 'invalid y.sign');
}
}
}
2 changes: 1 addition & 1 deletion crates/algebra/src/vector.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,4 @@ mod tests {
let result = vector1.dot(vector2);
assert(result == 32, 'Vector: dot product failed'); // 1*4 + 2*5 + 3*6 = 32
}
}
}
1 change: 1 addition & 0 deletions examples/market/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
target
27 changes: 27 additions & 0 deletions examples/market/Scarb.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Code generated by scarb DO NOT EDIT.
version = 1

[[package]]
name = "cubit"
version = "1.2.0"
source = "git+/~https://github.com/influenceth/cubit?rev=b459053#b4590530d5aeae9aabd36740cc2a3d9e6adc5fde"

[[package]]
name = "dojo"
version = "0.3.3"
source = "git+/~https://github.com/dojoengine/dojo.git?tag=v0.3.3#3c9f109e667ca5d12739e6553fdb8261378f4ecf"
dependencies = [
"dojo_plugin",
]

[[package]]
name = "dojo_plugin"
version = "0.3.3"

[[package]]
name = "market"
version = "0.0.0"
dependencies = [
"cubit",
"dojo",
]
9 changes: 9 additions & 0 deletions examples/market/Scarb.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "market"
version = "0.0.0"
description = "Example of defi crate usage."
homepage = "/~https://github.com/dojoengine/origami/tree/examples/market"

[dependencies]
cubit = { git = "/~https://github.com/influenceth/cubit", rev = "b459053" }
dojo = { git = "/~https://github.com/dojoengine/dojo.git", tag = "v0.3.3" }
17 changes: 17 additions & 0 deletions examples/market/src/lib.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
mod models {
mod cash;
mod item;
mod liquidity;
mod market;
}

mod systems {
mod liquidity;
mod trade;
}

#[cfg(test)]
mod tests {
mod setup;
mod trade;
}
10 changes: 10 additions & 0 deletions examples/market/src/models/cash.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Starknet imports

use starknet::ContractAddress;

#[derive(Model, Copy, Drop, Serde)]
struct Cash {
#[key]
player: ContractAddress,
amount: u128,
}
12 changes: 12 additions & 0 deletions examples/market/src/models/item.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Starknet imports

use starknet::ContractAddress;

#[derive(Model, Copy, Drop, Serde)]
struct Item {
#[key]
player: ContractAddress,
#[key]
item_id: u32,
quantity: u128,
}
56 changes: 56 additions & 0 deletions examples/market/src/models/liquidity.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Starknet imports

use starknet::ContractAddress;

// Dojo imports

use dojo::database::schema::{Struct, Ty, SchemaIntrospection, Member, serialize_member};

// External imports

use cubit::f128::types::fixed::Fixed;

// Constants

const SCALING_FACTOR: u128 = 10000;

impl SchemaIntrospectionFixed of SchemaIntrospection<Fixed> {
#[inline(always)]
fn size() -> usize {
2
}

#[inline(always)]
fn layout(ref layout: Array<u8>) {
layout.append(128);
layout.append(1);
}

#[inline(always)]
fn ty() -> Ty {
Ty::Struct(
Struct {
name: 'Fixed',
attrs: array![].span(),
children: array![
serialize_member(
@Member { name: 'mag', ty: Ty::Primitive('u128'), attrs: array![].span() }
),
serialize_member(
@Member { name: 'sign', ty: Ty::Primitive('bool'), attrs: array![].span() }
)
]
.span()
}
)
}
}

#[derive(Model, Copy, Drop, Serde)]
struct Liquidity {
#[key]
player: ContractAddress,
#[key]
item_id: u32,
shares: Fixed,
}
Loading

0 comments on commit f4365e4

Please sign in to comment.