Skip to content

Commit

Permalink
contracts & client: increase resource precision from 3 to 9
Browse files Browse the repository at this point in the history
  • Loading branch information
credence0x committed Jan 7, 2025
1 parent e789617 commit 2644784
Show file tree
Hide file tree
Showing 13 changed files with 38 additions and 29 deletions.
6 changes: 3 additions & 3 deletions client/apps/docs/utils/formatting.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ export function formatNumberWithSpaces(number: number): string {

export const formatAmount = (amount: number) => {
if (amount < 1) {
return `${amount * 1000}`;
} else if (amount < 1000) {
return `${amount * EternumGlobalConfig.resources.resourcePrecision}`;
} else if (amount < EternumGlobalConfig.resources.resourcePrecision) {
return `${amount.toFixed(amount % 1 === 0 ? 0 : (amount % 1) % 0.1 === 0 ? 1 : 2)}K`;
} else {
return `${(amount / 1000).toFixed(amount % 1000 === 0 ? 0 : (amount % 1000) % 10 === 0 ? 1 : 2)}M`;
return `${(amount / EternumGlobalConfig.resources.resourcePrecision).toFixed(amount % EternumGlobalConfig.resources.resourcePrecision === 0 ? 0 : (amount % EternumGlobalConfig.resources.resourcePrecision) % 10 === 0 ? 1 : 2)}M`;
}
};

Expand Down
12 changes: 9 additions & 3 deletions client/apps/game/src/hooks/helpers/battles/__test__/__mock__.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BattleSide } from "@bibliothecadao/eternum";
import { BattleSide, EternumGlobalConfig } from "@bibliothecadao/eternum";
import { Components, ComponentValue } from "@dojoengine/recs";
import { BattleInfo } from "../useBattles";

Expand Down Expand Up @@ -33,8 +33,14 @@ export const generateMockBattle = (
},
attackers_resources_escrow_id: 1n,
defenders_resources_escrow_id: 1n,
attack_army_health: { current: isAttackerAlive ? 1000n : CONSIDERED_AS_DEAD_HEALTH, lifetime: 1000n },
defence_army_health: { current: isDefenderAlive ? 1000n : CONSIDERED_AS_DEAD_HEALTH, lifetime: 1000n },
attack_army_health: {
current: isAttackerAlive ? EternumGlobalConfig.resources.resourcePrecision : CONSIDERED_AS_DEAD_HEALTH,
lifetime: EternumGlobalConfig.resources.resourcePrecision
},
defence_army_health: {
current: isDefenderAlive ? EternumGlobalConfig.resources.resourcePrecision : CONSIDERED_AS_DEAD_HEALTH,
lifetime: EternumGlobalConfig.resources.resourcePrecision
},
attack_delta: 1n,
defence_delta: 1n,
last_updated: 1n,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import Button from "@/ui/elements/Button";
import { NumberInput } from "@/ui/elements/NumberInput";
import { ResourceIcon } from "@/ui/elements/ResourceIcon";
import { calculateDonkeysNeeded, currencyFormat, getTotalResourceWeight, multiplyByPrecision } from "@/ui/utils/utils";
import { ResourcesIds, findResourceById } from "@bibliothecadao/eternum";
import { EternumGlobalConfig, ResourcesIds, findResourceById } from "@bibliothecadao/eternum";
import { Dispatch, SetStateAction, memo, useCallback, useEffect, useMemo, useState } from "react";

import { ID } from "@bibliothecadao/eternum";
Expand Down Expand Up @@ -64,7 +64,7 @@ export const RealmTransfer = memo(
const cleanedCalls = calls.map(({ sender_entity_id, recipient_entity_id, resources }) => ({
sender_entity_id,
recipient_entity_id,
resources: [resources[0], BigInt(Number(resources[1]) * 1000)],
resources: [resources[0], BigInt(Number(resources[1]) * EternumGlobalConfig.resources.resourcePrecision)],
}));

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Headline } from "@/ui/elements/Headline";
import { ResourceCost } from "@/ui/elements/ResourceCost";
import { multiplyByPrecision } from "@/ui/utils/utils";
import {
EternumGlobalConfig,
HYPERSTRUCTURE_CONSTRUCTION_COSTS_SCALED,
HYPERSTRUCTURE_CREATION_COSTS,
ID,
Expand Down Expand Up @@ -123,7 +124,7 @@ const StructureInfo = ({
key={index}
type="horizontal"
resourceId={ResourcesIds.AncientFragment}
amount={cost[Number(resourceId)].min_amount * 1000}
amount={cost[Number(resourceId)].min_amount * EternumGlobalConfig.resources.resourcePrecision}
balance={balance.balance}
/>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
} from "@/ui/utils/utils";
import {
DONKEY_ENTITY_TYPE,
EternumGlobalConfig,
ONE_MONTH,
ResourcesIds,
findResourceById,
Expand Down Expand Up @@ -497,7 +498,7 @@ const OrderRow = memo(
const OrderCreation = memo(
({ entityId, resourceId, isBuy = false }: { entityId: ID; resourceId: ResourcesIds; isBuy?: boolean }) => {
const [loading, setLoading] = useState(false);
const [resource, setResource] = useState(1000);
const [resource, setResource] = useState(EternumGlobalConfig.resources.resourcePrecision);
const [lords, setLords] = useState(100);
const [bid, setBid] = useState(String(lords / resource));
const { nextBlockTimestamp } = useNextBlockTimestamp();
Expand Down
4 changes: 2 additions & 2 deletions client/sdk/packages/eternum/src/constants/global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ export const STAMINA_TRAVEL_COST = 10;
export const STAMINA_EXPLORE_COST = 20;

// ----- Resources ----- //
export const RESOURCE_PRECISION = 1000;
export const RESOURCE_MULTIPLIER = 1000;
export const RESOURCE_PRECISION = 1_000_000_000;
export const RESOURCE_MULTIPLIER = 1_000_000_000;
export const RESOURCE_AMOUNT_PER_TICK = 10;
export const STARTING_RESOURCES_INPUT_PRODUCTION_FACTOR = 6;

Expand Down
2 changes: 1 addition & 1 deletion contracts/src/constants.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const GRAMS_PER_KG: u128 = 1_000;
const MAX_REALMS_PER_ADDRESS: u16 = 8_000;

// resource precision
const RESOURCE_PRECISION: u128 = 1_000;
const RESOURCE_PRECISION: u128 = 1_000_000_000;


// WONDER QUEST REWARD BOOST
Expand Down
6 changes: 3 additions & 3 deletions contracts/src/models/combat.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,10 @@ impl HealthImpl of HealthTrait {

/// Ensure that if the deduction makes the health less than one troop,
/// the troop dies immediately
/// e.g if single troop health = 7 * 1000(normalization), then a troop with 3 soldiers will have
/// health a total health of 7_000 * 3 = 21_000.
/// e.g if single troop health = 7 * RESOURCE_PRECISION(normalization), then a troop with 3 soldiers will have
/// health a total health of 7 * 1_000_000_000 (RP) * 3 = 21_000_000_000
/// if deduction is 80 per second, we want it such that deduction happens
/// at the rate of 80 per second till it takes 7_000 * (3 -1) =14_000 health,
/// at the rate of 80 per second till it takes 7_000_000_000 * (3 -1) =14_000_000_000 health,
/// then after that, the next step depletes the health completely. i.e to 0

// note: at this point, we know that
Expand Down
6 changes: 3 additions & 3 deletions contracts/src/systems/bank/contracts/liquidity.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ mod liquidity_systems {
use dojo::world::{IWorldDispatcher, IWorldDispatcherTrait};
// Eternum imports
use s0_eternum::alias::ID;
use s0_eternum::constants::DEFAULT_NS;
use s0_eternum::constants::{RESOURCE_PRECISION,DEFAULT_NS};
use s0_eternum::constants::ResourceTypes;
use s0_eternum::models::bank::liquidity::{Liquidity};
use s0_eternum::models::bank::market::{Market, MarketTrait};
Expand All @@ -40,7 +40,7 @@ mod liquidity_systems {
resource_type: u8,
lords_amount: u128,
resource_amount: u128,
// price in lords for 1000 resource
// price in lords for 1 * RESOURCE_PRECISION resource
resource_price: u128,
add: bool,
timestamp: u64,
Expand Down Expand Up @@ -146,7 +146,7 @@ mod liquidity_systems {
ref world: WorldStorage, market: Market, entity_id: ID, lords_amount: u128, resource_amount: u128, add: bool
) {
let resource_price = if market.has_liquidity() {
market.quote_amount(1000)
market.quote_amount(1 * RESOURCE_PRECISION)
} else {
0
};
Expand Down
10 changes: 5 additions & 5 deletions contracts/src/systems/bank/contracts/swap.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ mod swap_systems {
use option::OptionTrait;

use s0_eternum::alias::ID;
use s0_eternum::constants::DEFAULT_NS;
use s0_eternum::constants::{RESOURCE_PRECISION, DEFAULT_NS};
use s0_eternum::constants::{ResourceTypes, WORLD_CONFIG_ID};
use s0_eternum::models::bank::bank::{Bank};
use s0_eternum::models::bank::market::{Market, MarketTrait};
Expand All @@ -46,7 +46,7 @@ mod swap_systems {
resource_amount: u128,
bank_owner_fees: u128,
lp_fees: u128,
// price in lords for 1000 resource
// price in lords for 1 * RESOURCE_PRECISION resource
resource_price: u128,
buy: bool,
timestamp: u64,
Expand Down Expand Up @@ -98,7 +98,7 @@ mod swap_systems {
amount,
bank_lords_fee_amount,
lps_fee,
market.buy(0, 1, 1000),
market.buy(0, 1, RESOURCE_PRECISION),
true
);

Expand Down Expand Up @@ -152,7 +152,7 @@ mod swap_systems {
amount,
bank_lords_fee_amount,
lps_fee,
market.buy(0, 1, 1000),
market.buy(0, 1, RESOURCE_PRECISION),
false
);

Expand Down Expand Up @@ -185,7 +185,7 @@ mod swap_systems {
resource_amount,
bank_owner_fees,
lp_fees,
resource_price: market.quote_amount(1000),
resource_price: market.quote_amount(RESOURCE_PRECISION),
buy,
timestamp: starknet::get_block_timestamp()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use s0_eternum::alias::ID;
use s0_eternum::constants::LevelIndex;
use s0_eternum::constants::{REALM_LEVELING_CONFIG_ID, WORLD_CONFIG_ID};

use s0_eternum::constants::{ResourceTypes, TickIds};
use s0_eternum::constants::{ResourceTypes, TickIds, RESOURCE_PRECISION};
use s0_eternum::models::combat::{Army, BattleSide, Troops};
use s0_eternum::models::config::{TickConfig, MapConfig, StaminaConfig, StaminaRefillConfig, LevelingConfig, TickImpl};
use s0_eternum::models::map::Tile;
Expand Down Expand Up @@ -191,8 +191,8 @@ use starknet::contract_address_const;

const TICK_INTERVAL_IN_SECONDS: u64 = 200;
const MAX_STAMINA: u16 = 30;
const ORIGINAL_WHEAT_BALANCE: u128 = 1000;
const ORIGINAL_FISH_BALANCE: u128 = 1000;
const ORIGINAL_WHEAT_BALANCE: u128 = 1 * RESOURCE_PRECISION;
const ORIGINAL_FISH_BALANCE: u128 = 1 * RESOURCE_PRECISION;

fn setup_hex_travel() -> (WorldStorage, ID, Position, ITravelSystemsDispatcher) {
let mut world = spawn_eternum();
Expand Down
2 changes: 1 addition & 1 deletion contracts/src/utils/testing/config.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ fn get_combat_config() -> TroopConfig {
crossbowman_strength: 1,
advantage_percent: 1000,
disadvantage_percent: 1000,
max_troop_count: 10_000_000_000_000 * 1000,
max_troop_count: 10_000_000_000_000 * RESOURCE_PRECISION,
pillage_health_divisor: 8,
army_free_per_structure: 100,
army_extra_per_building: 100,
Expand Down
3 changes: 2 additions & 1 deletion discord-bot/src/events/battle_pillage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use crate::{

use super::{ToDiscordMessage, UNKNOWN_USER};

const RESOURCE_PRECISION : u128 = 1_000_000_000;
#[derive(CairoSerde, Clone)]
pub struct BattlePillage {
pub id: u32,
Expand Down Expand Up @@ -42,7 +43,7 @@ impl ToDiscordMessage for BattlePillage {
.pillaged_resources
.iter()
.map(|(resource_id, amount)| {
format!("{} {}", amount / 1000, ResourceIds::from(*resource_id),)
format!("{} {}", amount / RESOURCE_PRECISION, ResourceIds::from(*resource_id),)
})
.collect::<Vec<String>>()
.join(", ");
Expand Down

0 comments on commit 2644784

Please sign in to comment.