Skip to content

Commit

Permalink
✨ Migrate more models
Browse files Browse the repository at this point in the history
  • Loading branch information
bal7hazar committed Nov 18, 2024
1 parent ed13828 commit c939673
Show file tree
Hide file tree
Showing 4 changed files with 187 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/slot/src/lib.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ mod types {

mod models {
mod index;
mod account;
mod team;
mod deployment;
mod service;
}
92 changes: 92 additions & 0 deletions packages/slot/src/models/account.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// Intenral imports

use arcade_slot::models::index::Account;

// Errors

pub mod errors {
pub const ACCOUNT_ALREADY_EXISTS: felt252 = 'Account: already exists';
pub const ACCOUNT_NOT_EXIST: felt252 = 'Account: does not exist';
pub const ACCOUNT_INVALID_IDENTIFIER: felt252 = 'Account: invalid identifier';
pub const ACCOUNT_INVALID_USERNAME: felt252 = 'Account: invalid username';
}

#[generate_trait]
impl AccountImpl of AccountTrait {
#[inline]
fn new(
id: felt252, controllers: u32, name: felt252, username: felt252, socials: ByteArray,
) -> Account {
// [Check] Inputs
AccountAssert::assert_valid_identifier(id);
AccountAssert::assert_valid_username(username);
// [Return] Account
Account {
id: id,
controllers: controllers,
name: name,
username: username,
socials: socials,
credits: 0,
}
}
}

#[generate_trait]
impl AccountAssert of AssertTrait {
#[inline]
fn assert_does_not_exist(self: Account) {
assert(self.name == 0, errors::ACCOUNT_ALREADY_EXISTS);
}

#[inline]
fn assert_does_exist(self: Account) {
assert(self.name != 0, errors::ACCOUNT_NOT_EXIST);
}

#[inline]
fn assert_valid_identifier(identifier: felt252) {
assert(identifier != 0, errors::ACCOUNT_INVALID_IDENTIFIER);
}

#[inline]
fn assert_valid_username(username: felt252) {
assert(username != 0, errors::ACCOUNT_INVALID_USERNAME);
}
}

#[cfg(test)]
mod tests {
// Local imports

use super::{Account, AccountTrait, AccountAssert};

// Constants

const IDENTIFIER: felt252 = 'ID';
const USERNAME: felt252 = 'USERNAME';

#[test]
fn test_deployment_new() {
let account = AccountTrait::new(IDENTIFIER, 0, 'NAME', USERNAME, "{}");
assert_eq!(account.id, IDENTIFIER);
assert_eq!(account.controllers, 0);
assert_eq!(account.name, 'NAME');
assert_eq!(account.username, USERNAME);
assert_eq!(account.socials, "{}");
assert_eq!(account.credits, 0);
}

#[test]
fn test_deployment_assert_does_exist() {
let account = AccountTrait::new(IDENTIFIER, 0, 'NAME', USERNAME, "{}");
account.assert_does_exist();
}

#[test]
#[should_panic(expected: 'Account: already exists')]
fn test_deployment_revert_already_exists() {
let account = AccountTrait::new(IDENTIFIER, 0, 'NAME', USERNAME, "{}");
account.assert_does_not_exist();
}
}
13 changes: 13 additions & 0 deletions packages/slot/src/models/index.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,16 @@ pub struct Service {
version: felt252,
default_version: felt252,
}

#[derive(Clone, Drop, Serde)]
#[dojo::model]
pub struct Game {
#[key]
id: felt252,
name: felt252,
description: ByteArray,
priority: u8,
socials: ByteArray,
metadata: ByteArray,
active: bool,
}
80 changes: 80 additions & 0 deletions packages/slot/src/models/team.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// Intenral imports

use arcade_slot::models::index::Team;

// Errors

pub mod errors {
pub const TEAM_ALREADY_EXISTS: felt252 = 'Team: already exists';
pub const TEAM_NOT_EXIST: felt252 = 'Team: does not exist';
pub const TEAM_INVALID_IDENTIFIER: felt252 = 'Team: invalid identifier';
pub const TEAM_INVALID_NAME: felt252 = 'Team: invalid name';
}

#[generate_trait]
impl TeamImpl of TeamTrait {
#[inline]
fn new(id: felt252, name: felt252, description: ByteArray) -> Team {
// [Check] Inputs
TeamAssert::assert_valid_identifier(id);
TeamAssert::assert_valid_name(name);
// [Return] Team
Team { id: id, name: name, description: description, }
}
}

#[generate_trait]
impl TeamAssert of AssertTrait {
#[inline]
fn assert_does_not_exist(self: Team) {
assert(self.name == 0, errors::TEAM_ALREADY_EXISTS);
}

#[inline]
fn assert_does_exist(self: Team) {
assert(self.name != 0, errors::TEAM_NOT_EXIST);
}

#[inline]
fn assert_valid_identifier(identifier: felt252) {
assert(identifier != 0, errors::TEAM_INVALID_IDENTIFIER);
}

#[inline]
fn assert_valid_name(name: felt252) {
assert(name != 0, errors::TEAM_INVALID_NAME);
}
}

#[cfg(test)]
mod tests {
// Local imports

use super::{Team, TeamTrait, TeamAssert};

// Constants

const IDENTIFIER: felt252 = 'ID';
const NAME: felt252 = 'NAME';

#[test]
fn test_deployment_new() {
let team = TeamTrait::new(IDENTIFIER, 'NAME', "");
assert_eq!(team.id, IDENTIFIER);
assert_eq!(team.name, 'NAME');
assert_eq!(team.description, "");
}

#[test]
fn test_deployment_assert_does_exist() {
let team = TeamTrait::new(IDENTIFIER, 'NAME', "");
team.assert_does_exist();
}

#[test]
#[should_panic(expected: 'Team: already exists')]
fn test_deployment_revert_already_exists() {
let account = TeamTrait::new(IDENTIFIER, 0, 'NAME', USERNAME, "");
account.assert_does_not_exist();
}
}

0 comments on commit c939673

Please sign in to comment.