Skip to content

Commit

Permalink
Switch to methods, renaming
Browse files Browse the repository at this point in the history
  • Loading branch information
wraitii committed Aug 24, 2023
1 parent 2f3f700 commit c1ed1bf
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 150 deletions.
1 change: 0 additions & 1 deletion src/briq_factory.cairo
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
mod components;
mod systems;
mod constants;
mod events;
90 changes: 42 additions & 48 deletions src/briq_factory/components.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ use array::{ArrayTrait, SpanTrait};
use option::OptionTrait;
use traits::{Into, TryInto};

use briq_protocol::world_config::SYSTEM_CONFIG_ID;

use briq_protocol::briq_factory::constants::{
BRIQ_FACTORY_CONFIG_ID, BRIQ_FACTORY_STORE_ID, DECIMALS, INFLECTION_POINT, SLOPE, RAW_FLOOR,
DECIMALS, INFLECTION_POINT, SLOPE, RAW_FLOOR,
LOWER_FLOOR, LOWER_SLOPE, DECAY_PER_SECOND, SURGE_SLOPE, MINIMAL_SURGE, SURGE_DECAY_PER_SECOND,
MIN_PURCHASE, BRIQ_MATERIAL
};
Expand All @@ -16,14 +18,6 @@ use briq_protocol::felt_math::{FeltOrd, FeltDiv};

use debug::PrintTrait;

#[derive(Component, Copy, Drop, Serde, SerdeLen)]
struct BriqFactoryConfig {
#[key]
config_id: u64,
value: u64
}


#[derive(Component, Copy, Drop, Serde, SerdeLen)]
struct BriqFactoryStore {
#[key]
Expand All @@ -34,36 +28,37 @@ struct BriqFactoryStore {
last_purchase_time: u64,
}

trait BriqFactoryStoreTrait {
fn get_store(world: IWorldDispatcher) -> BriqFactoryStore;
fn set_store(world: IWorldDispatcher, new_store: BriqFactoryStore);
fn get_current_t(world: IWorldDispatcher) -> felt252;
fn get_surge_t(world: IWorldDispatcher) -> felt252;
fn get_surge_price(world: IWorldDispatcher, amount: felt252) -> felt252;
fn get_price(world: IWorldDispatcher, amount: felt252) -> felt252;
trait BriqFactoryTrait {
fn get_briq_factory(world: IWorldDispatcher) -> BriqFactoryStore;
fn set_briq_factory(world: IWorldDispatcher, new_store: BriqFactoryStore);

fn get_current_t(self: @BriqFactoryStore) -> felt252;
fn get_surge_t(self: @BriqFactoryStore) -> felt252;
fn get_surge_price(self: @BriqFactoryStore, amount: felt252) -> felt252;
fn get_price(self: @BriqFactoryStore, amount: felt252) -> felt252;
fn get_lin_integral(
world: IWorldDispatcher, slope: felt252, floor: felt252, t2: felt252, t1: felt252
self: @BriqFactoryStore, slope: felt252, floor: felt252, t2: felt252, t1: felt252
) -> felt252;
fn get_lin_integral_negative_floor(
world: IWorldDispatcher, slope: felt252, floor: felt252, t2: felt252, t1: felt252
self: @BriqFactoryStore, slope: felt252, floor: felt252, t2: felt252, t1: felt252
) -> felt252;
fn integrate(world: IWorldDispatcher, t: felt252, amount: felt252) -> felt252;
fn integrate(self: @BriqFactoryStore, t: felt252, amount: felt252) -> felt252;
}

// #[generate_trait]
impl BriqFactoryStoreImpl of BriqFactoryStoreTrait {
impl BriqFactoryStoreImpl of BriqFactoryTrait {
#[always(inline)]
fn get_store(world: IWorldDispatcher) -> BriqFactoryStore {
get!(world, (BRIQ_FACTORY_STORE_ID), BriqFactoryStore)
fn get_briq_factory(world: IWorldDispatcher) -> BriqFactoryStore {
get!(world, (SYSTEM_CONFIG_ID), BriqFactoryStore)
}

#[always(inline)]
fn set_store(world: IWorldDispatcher, new_store: BriqFactoryStore) {
fn set_briq_factory(world: IWorldDispatcher, new_store: BriqFactoryStore) {
set!(world, (new_store));
}

fn get_current_t(world: IWorldDispatcher) -> felt252 {
let store = BriqFactoryStoreTrait::get_store(world);
fn get_current_t(self: @BriqFactoryStore) -> felt252 {
let store = *self; // TODO: clean this up

let time_since_last_purchase: felt252 = (starknet::info::get_block_timestamp()
- store.last_purchase_time)
Expand All @@ -77,8 +72,8 @@ impl BriqFactoryStoreImpl of BriqFactoryStoreTrait {
}
}

fn get_surge_t(world: IWorldDispatcher) -> felt252 {
let store = BriqFactoryStoreTrait::get_store(world);
fn get_surge_t(self: @BriqFactoryStore) -> felt252 {
let store = *self; // TODO: clean this up

let time_since_last_purchase: felt252 = (starknet::info::get_block_timestamp()
- store.last_purchase_time)
Expand All @@ -93,39 +88,38 @@ impl BriqFactoryStoreImpl of BriqFactoryStoreTrait {
}
}

fn get_surge_price(world: IWorldDispatcher, amount: felt252) -> felt252 {
let surge_t = BriqFactoryStoreTrait::get_surge_t(world);
fn get_surge_price(self: @BriqFactoryStore, amount: felt252) -> felt252 {
let surge_t = self.get_surge_t();

if (surge_t + amount) <= MINIMAL_SURGE() {
return 0;
};

if surge_t > MINIMAL_SURGE() {
BriqFactoryStoreTrait::get_lin_integral(
world,
self.get_lin_integral(
SURGE_SLOPE(),
0,
surge_t - MINIMAL_SURGE(),
surge_t + amount - MINIMAL_SURGE()
)
} else {
BriqFactoryStoreTrait::get_lin_integral(
world, SURGE_SLOPE(), 0, 0, surge_t + amount - MINIMAL_SURGE()
self.get_lin_integral(
SURGE_SLOPE(), 0, 0, surge_t + amount - MINIMAL_SURGE()
)
}
}


fn get_price(world: IWorldDispatcher, amount: felt252) -> felt252 {
let t = BriqFactoryStoreTrait::get_current_t(world);
let price = BriqFactoryStoreTrait::integrate(world, t, amount * DECIMALS());
let surge = BriqFactoryStoreTrait::get_surge_price(world, amount * DECIMALS());
fn get_price(self: @BriqFactoryStore, amount: felt252) -> felt252 {
let t = self.get_current_t();
let price = self.integrate(t, amount * DECIMALS());
let surge = self.get_surge_price(amount * DECIMALS());

price + surge
}

fn get_lin_integral(
world: IWorldDispatcher, slope: felt252, floor: felt252, t2: felt252, t1: felt252,
self: @BriqFactoryStore, slope: felt252, floor: felt252, t2: felt252, t1: felt252,
) -> felt252 {
assert(t2 < t1, 't1 >= t2');
// briq machine broke above 10^12 bricks of demand.
Expand All @@ -148,7 +142,7 @@ impl BriqFactoryStoreImpl of BriqFactoryStoreTrait {


fn get_lin_integral_negative_floor(
world: IWorldDispatcher, slope: felt252, floor: felt252, t2: felt252, t1: felt252,
self: @BriqFactoryStore, slope: felt252, floor: felt252, t2: felt252, t1: felt252,
) -> felt252 {
assert(t2 < t1, 't1 >= t2');
// briq machine broke above 10^12 bricks of demand.
Expand All @@ -170,24 +164,24 @@ impl BriqFactoryStoreImpl of BriqFactoryStoreTrait {
}


fn integrate(world: IWorldDispatcher, t: felt252, amount: felt252) -> felt252 {
fn integrate(self: @BriqFactoryStore, t: felt252, amount: felt252) -> felt252 {
if (t + amount) <= INFLECTION_POINT() {
return BriqFactoryStoreTrait::get_lin_integral(
world, LOWER_SLOPE(), LOWER_FLOOR(), t, t + amount
return self.get_lin_integral(
LOWER_SLOPE(), LOWER_FLOOR(), t, t + amount
);
}

if INFLECTION_POINT() <= t {
return BriqFactoryStoreTrait::get_lin_integral_negative_floor(
world, SLOPE(), RAW_FLOOR(), t, t + amount
return self.get_lin_integral_negative_floor(
SLOPE(), RAW_FLOOR(), t, t + amount
);
}

BriqFactoryStoreTrait::get_lin_integral(
world, LOWER_SLOPE(), LOWER_FLOOR(), t, INFLECTION_POINT()
self.get_lin_integral(
LOWER_SLOPE(), LOWER_FLOOR(), t, INFLECTION_POINT()
)
+ BriqFactoryStoreTrait::get_lin_integral_negative_floor(
world, SLOPE(), RAW_FLOOR(), INFLECTION_POINT(), t + amount
+ self.get_lin_integral_negative_floor(
SLOPE(), RAW_FLOOR(), INFLECTION_POINT(), t + amount
)
}
}
3 changes: 0 additions & 3 deletions src/briq_factory/constants.cairo
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
const BRIQ_FACTORY_CONFIG_ID: felt252 = 69;
const BRIQ_FACTORY_STORE_ID: felt252 = 420;

// 10**18
fn DECIMALS() -> felt252 {
1000000000000000000
Expand Down
7 changes: 0 additions & 7 deletions src/briq_factory/events.cairo
Original file line number Diff line number Diff line change
@@ -1,8 +1 @@
use starknet::ContractAddress;

#[derive( Drop, starknet::Event)]
struct BriqsBought {
buyer: ContractAddress,
amount: felt252,
price: felt252
}
75 changes: 36 additions & 39 deletions src/briq_factory/systems.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,26 @@ use traits::{Into, TryInto};
use dojo::world::{IWorldDispatcher, IWorldDispatcherTrait};

use briq_protocol::briq_factory::constants::{
BRIQ_FACTORY_CONFIG_ID, BRIQ_FACTORY_STORE_ID, DECIMALS, INFLECTION_POINT, SLOPE, RAW_FLOOR,
DECIMALS, INFLECTION_POINT, SLOPE, RAW_FLOOR,
LOWER_FLOOR, LOWER_SLOPE, DECAY_PER_SECOND, SURGE_SLOPE, MINIMAL_SURGE, SURGE_DECAY_PER_SECOND,
MIN_PURCHASE, BRIQ_MATERIAL
};

use briq_protocol::briq_factory::components::{
BriqFactoryConfig, BriqFactoryStore, BriqFactoryStoreTrait
BriqFactoryStore, BriqFactoryTrait
};

#[derive( Drop, starknet::Event)]
struct BriqsBought {
buyer: ContractAddress,
amount: u32,
price: u128
}

#[derive(Drop, Serde)]
struct BriqFactoryBuyParams {
material: u8,
amount: u16
material: u64,
amount: u32
}

#[system]
Expand All @@ -37,9 +43,7 @@ mod BriqFactoryMint {

use briq_protocol::world_config::{AdminTrait, get_world_config};
use briq_protocol::felt_math::{FeltOrd};
use briq_protocol::briq_factory::events::BriqsBought;
use super::{BriqFactoryStoreTrait, BriqFactoryBuyParams, DECIMALS, MIN_PURCHASE, BRIQ_MATERIAL};

use super::{BriqsBought, BriqFactoryTrait, BriqFactoryBuyParams, DECIMALS, MIN_PURCHASE, BRIQ_MATERIAL};

#[starknet::interface]
trait IERC20<TState> {
Expand All @@ -54,39 +58,32 @@ mod BriqFactoryMint {
}

fn execute(ctx: Context, params: BriqFactoryBuyParams) {
let BriqFactoryBuyParams{material, amount: amount_u16 } = params;
let amount: felt252 = amount_u16.into();
let BriqFactoryBuyParams{material, amount: amount_u32 } = params;
let amount: felt252 = amount_u32.into();
assert(amount >= MIN_PURCHASE(), 'amount too low !');

// ?? This also guarantees that amount isn't above 2**128 which is important to avoid an attack on amount * decimals;
let mut briq_factory = BriqFactoryTrait::get_briq_factory(ctx.world);

let price = briq_factory.get_price(amount);
let t = briq_factory.get_current_t();
let surge_t = briq_factory.get_surge_t();

// Transfer funds to receiver wallet
// TODO: use something other than the super-admin address for this.
let world_config = get_world_config(ctx.world);
let store = BriqFactoryStoreTrait::get_store(ctx.world);
let price = BriqFactoryStoreTrait::get_price(ctx.world, amount);
let buyer = get_caller_address();
let t = BriqFactoryStoreTrait::get_current_t(ctx.world);
let surge_t = BriqFactoryStoreTrait::get_surge_t(ctx.world);

// transfer buy_tokens from buyer to super_admin
IERC20Dispatcher {
contract_address: store.buy_token
contract_address: briq_factory.buy_token
}.transfer_from(buyer, world_config.super_admin, price.into());

// update store
let mut store = BriqFactoryStoreTrait::get_store(ctx.world);
store.last_purchase_time = get_block_timestamp();
store.last_stored_t = t + amount * DECIMALS();
store.surge_t = surge_t + amount * DECIMALS();
BriqFactoryStoreTrait::set_store(ctx.world, store);

// // mint briqs to buyer
// let token_id = BRIQ_MATERIAL();
let amount_u128: u128 = amount.try_into().unwrap();
// let data: Array<u8> = array![];
// IBriqDispatcher {
// contract_address: world_config.briq
// }.mint(buyer, token_id, amount_u128, data);
briq_factory.last_purchase_time = get_block_timestamp();
briq_factory.last_stored_t = t + amount * DECIMALS();
briq_factory.surge_t = surge_t + amount * DECIMALS();
BriqFactoryTrait::set_briq_factory(ctx.world, briq_factory);

// mint briqs to buyer
let amount_u128: u128 = amount.try_into().unwrap();
briq_protocol::briq_token::systems::update_nocheck(
ctx.world,
buyer,
Expand All @@ -98,7 +95,7 @@ mod BriqFactoryMint {
data: array![]
);

emit!(ctx.world, BriqsBought { buyer: buyer, amount: amount, price: price });
emit!(ctx.world, BriqsBought { buyer, amount: amount_u32, price: price.try_into().unwrap() });
}
}

Expand All @@ -121,22 +118,22 @@ mod BriqFactoryInitialize {
use briq_protocol::world_config::AdminTrait;

use super::BriqFactoryInitializeParams;
use super::{BriqFactoryStore, BriqFactoryStoreTrait};
use super::{BriqFactoryStore, BriqFactoryTrait};


fn execute(ctx: Context, params: BriqFactoryInitializeParams) {
// TODO: safety check
ctx.world.only_admins(@ctx.origin);

let BriqFactoryInitializeParams{t, surge_t, buy_token } = params;

let mut store = BriqFactoryStoreTrait::get_store(ctx.world);
let mut briq_factory = BriqFactoryTrait::get_briq_factory(ctx.world);

store.last_stored_t = t;
store.surge_t = surge_t;
store.buy_token = buy_token;
store.last_purchase_time = starknet::info::get_block_timestamp();
briq_factory.last_stored_t = t;
briq_factory.surge_t = surge_t;
briq_factory.buy_token = buy_token;
briq_factory.last_purchase_time = starknet::info::get_block_timestamp();

BriqFactoryStoreTrait::set_store(ctx.world, store);
BriqFactoryTrait::set_briq_factory(ctx.world, briq_factory);
}
}

Loading

0 comments on commit c1ed1bf

Please sign in to comment.