Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test ci #4

Merged
merged 28 commits into from
Oct 2, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
d7f8389
feat: add auction component + get_zone in position
aymericdelab Sep 18, 2023
47a60cb
feat: create_labor_auction
aymericdelab Sep 19, 2023
4716038
feat: create_labor_auction test + dojo_defi linear
aymericdelab Sep 19, 2023
29271c4
feat: use vrgda from dojo_defi + create labor cost
aymericdelab Sep 20, 2023
216f4c2
feat: add labor auction in build_labor
aymericdelab Sep 20, 2023
fa46316
feat: make build_labor tests work
aymericdelab Sep 21, 2023
7124c41
feat: purchase labor
aymericdelab Sep 21, 2023
824ae9c
fix: replace dojo_defi temp fix
aymericdelab Sep 21, 2023
3821b47
feat: use 1000 precision for resources
aymericdelab Sep 22, 2023
298a4b8
feat: new labor costs + fix precision bugs
aymericdelab Sep 22, 2023
215d82d
feat: purchase labor in client + bug fixes + contract optimization
aymericdelab Sep 23, 2023
a4144a1
Merge branch 'refactor-amounts' into labor-vrgda
aymericdelab Sep 23, 2023
6a4d4a2
feat: added realm zone labor price in ui
aymericdelab Sep 23, 2023
6de141f
Merge branch 'main' into labor-vrgda
aymericdelab Sep 23, 2023
0ba132f
fix: test issue
aymericdelab Sep 23, 2023
916a0b7
switch to v0.2.2
aymericdelab Sep 25, 2023
6bc6f5c
refactor: change world address (back to v0.2.2)
aymericdelab Sep 25, 2023
2be6426
fix: fix bug labor contract
aymericdelab Sep 25, 2023
d4e18a6
fix: fix labor build/ harvest client issues
aymericdelab Sep 25, 2023
4a3cac7
fix: dojo version
aymericdelab Sep 25, 2023
a1861df
fix: remove target_price because not used
aymericdelab Sep 26, 2023
1a0852c
fix: dojo version
aymericdelab Sep 26, 2023
fad8dd6
fix: fix labor bugs + refactor
aymericdelab Sep 27, 2023
d46a962
refactor: vrgda
aymericdelab Sep 27, 2023
3e0723d
feat: add time interval for price update
aymericdelab Sep 28, 2023
16ae108
Update test.yml
credence0x Sep 28, 2023
63136f2
Update Scarb.toml
credence0x Sep 28, 2023
f614971
add SerdeLen to ResourceCost
credence0x Sep 28, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat: add time interval for price update
  • Loading branch information
aymericdelab committed Sep 28, 2023
commit 3e0723d5bf8ad14c714de2e81af63622f8218059
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,32 @@ function getTotalPrice(
units_per_day,
num_days,
sold,
labor_multiplier
labor_multiplier,
price_update_interval
) {
let new_sold = sold;
let total_price = 0;

let multiplier = Math.exp(
Math.log(1 - decay) * (num_days - new_sold / units_per_day)
);
for (let i = 0; i < units; i++) {
let multiplier = Math.exp(
Math.log(1 - decay) * (num_days - new_sold / units_per_day)
);
let price = target_price * multiplier * labor_multiplier;

total_price += parseInt(price);
new_sold += 1;
if (new_sold % price_update_interval == 0) {
multiplier = Math.exp(
Math.log(1 - decay) * (num_days - new_sold / units_per_day)
);
}
}

return total_price;
}

// Example usage
let price = getTotalPrice(0.1, 1000, 20, 50, 0, 20, 2);
let price = getTotalPrice(0.1, 1000, 20, 50, 0, 0, 1, 10);
console.log("total price", price);
let balance = 79603;
let balance = 100000;
console.log("resource left", balance - price);
3 changes: 3 additions & 0 deletions contracts/src/components/labor_auction.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ struct LaborAuction {
per_time_unit: u128,
start_time: u64,
sold: u128,
price_update_interval: u128,
}

#[generate_trait]
Expand Down Expand Up @@ -142,6 +143,7 @@ mod tests {
per_time_unit: 50,
start_time: 0,
sold: 50,
price_update_interval: 10,
};

// 2 days in the future
Expand All @@ -167,6 +169,7 @@ mod tests {
per_time_unit: 50,
start_time: 0,
sold: 50,
price_update_interval: 10,
};

set!(world, (auction));
Expand Down
21 changes: 16 additions & 5 deletions contracts/src/systems/labor/purchase_labor.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ mod PurchaseLabor {
use eternum::components::labor::{Labor, LaborTrait};
use eternum::components::labor_auction::{LaborAuction, LaborAuctionTrait};

use debug::PrintTrait;

use eternum::alias::ID;
use eternum::components::owner::Owner;
use eternum::components::position::{Position, PositionTrait};
Expand Down Expand Up @@ -45,12 +47,15 @@ mod PurchaseLabor {
let mut labor_units_remaining = labor_units;
let mut total_costs: Felt252Dict<u128> = Default::default();

let mut labor_cost_multiplier = labor_auction.get_price();

loop {
if labor_units_remaining == 0 {
break;
}

let mut index = 0_usize;

loop {
if index == labor_cost_resources.resource_types_count.into() {
break ();
Expand All @@ -60,17 +65,20 @@ mod PurchaseLabor {
ctx.world, (resource_type, labor_cost_resource_type).into(), LaborCostAmount
);

let labor_cost_multiplier = labor_auction.get_price();
let cost_fixed = FixedTrait::new_unscaled(labor_cost_per_unit.value, false)
* labor_cost_multiplier.into();
let cost: u128 = cost_fixed.try_into().unwrap();

let total_cost = total_costs.get(labor_cost_resource_type.into());
total_costs.insert(labor_cost_resource_type.into(), total_cost + cost);

index += 1;
};

labor_auction.sell();
if (labor_auction.sold) % labor_auction.price_update_interval == 0 {
labor_cost_multiplier = labor_auction.get_price();
};
labor_units_remaining -= 1;
};

Expand Down Expand Up @@ -132,6 +140,7 @@ mod tests {

// testing
use eternum::utils::testing::spawn_eternum;
use debug::PrintTrait;

use traits::Into;
use result::ResultTrait;
Expand All @@ -149,6 +158,7 @@ mod tests {
let mut create_labor_auction_calldata = array![];
Serde::serialize(@_0_1, ref create_labor_auction_calldata); // decay
Serde::serialize(@50, ref create_labor_auction_calldata); // unit per time
Serde::serialize(@10, ref create_labor_auction_calldata); // interval per price change
world.execute('CreateLaborAuction', create_labor_auction_calldata);

// set realm entity
Expand Down Expand Up @@ -239,12 +249,13 @@ mod tests {

// assert resources are the right amount
let coal_resource = get!(world, (realm_entity_id, ResourceTypes::COAL), Resource);
coal_resource.balance.print();
assert(coal_resource.resource_type == ResourceTypes::COAL, 'failed resource type');
assert(coal_resource.balance == 79_603, 'failed resource amount');
assert(coal_resource.balance == 79_790, 'failed resource amount');

let stone_resource = get!(world, (realm_entity_id, ResourceTypes::STONE), Resource);
assert(stone_resource.resource_type == ResourceTypes::STONE, 'failed resource type');
assert(stone_resource.balance == 79_603, 'failed resource amount');
assert(stone_resource.balance == 79_790, 'failed resource amount');

// assert labor resource is right amount
let gold_labor_resource = get!(world, (realm_entity_id, resource_type + 28), Resource);
Expand All @@ -270,11 +281,11 @@ mod tests {
// assert resources are the right amount
let coal_resource = get!(world, (realm_entity_id, ResourceTypes::COAL), Resource);
assert(coal_resource.resource_type == ResourceTypes::COAL, 'failed resource type');
assert(coal_resource.balance == 79_603, 'failed resource amount');
assert(coal_resource.balance == 79_790, 'failed resource amount');

let stone_resource = get!(world, (realm_entity_id, ResourceTypes::STONE), Resource);
assert(stone_resource.resource_type == ResourceTypes::STONE, 'failed resource type');
assert(stone_resource.balance == 79_603, 'failed resource amount');
assert(stone_resource.balance == 79_790, 'failed resource amount');

// assert labor resource is right amount
let fish_labor_resource = get!(world, (realm_entity_id, resource_type - 3), Resource);
Expand Down
13 changes: 5 additions & 8 deletions contracts/src/systems/labor_auction/create_labor_auction.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ mod CreateLaborAuction {

use dojo::world::Context;

fn execute(ctx: Context, decay_constant: u128, per_time_unit: u128) {
fn execute(ctx: Context, decay_constant: u128, per_time_unit: u128, price_update_interval: u128) {
let start_time = starknet::get_block_timestamp();

let mut zone: u8 = 1;
Expand All @@ -21,6 +21,7 @@ mod CreateLaborAuction {
per_time_unit,
start_time,
sold: 0,
price_update_interval,
};

set!(ctx.world, (auction));
Expand Down Expand Up @@ -54,10 +55,12 @@ mod tests {
let zone: u8 = 5;
let decay_constant: u128 = _0_1;
let per_time_unit: u128 = 50;
let price_update_interval: u128 = 10;

let mut calldata = array![];
Serde::serialize(@decay_constant, ref calldata);
Serde::serialize(@per_time_unit, ref calldata);
Serde::serialize(@price_update_interval, ref calldata);
world.execute('CreateLaborAuction', calldata);

let labor_auction = get!(world, (zone), LaborAuction);
Expand All @@ -67,12 +70,6 @@ mod tests {
assert(labor_auction.per_time_unit == per_time_unit, 'per_time_unit');
assert(labor_auction.sold == 0, 'sold');
assert(labor_auction.decay_constant_sign == false, 'decay_constant_sign');

let labor_auction_food = get!(world, (zone), LaborAuction);
assert(labor_auction_food.zone == zone, 'zone');
assert(labor_auction_food.decay_constant_mag == decay_constant, 'decay_constant_mag');
assert(labor_auction_food.per_time_unit == per_time_unit, 'per_time_unit');
assert(labor_auction_food.sold == 0, 'sold');
assert(labor_auction_food.decay_constant_sign == false, 'decay_constant_sign');
assert(labor_auction.price_update_interval == 10, 'price_update_interval');
}
}
Loading