Skip to content

Commit

Permalink
Merge pull request #237 from Buckram123/split-gas-adjustment
Browse files Browse the repository at this point in the history
[REFACTOR] split gas_adjustments from gas_numerator
  • Loading branch information
Buckram123 authored Dec 21, 2022
2 parents 3c9750a + 14c81bb commit f76122f
Show file tree
Hide file tree
Showing 26 changed files with 327 additions and 123 deletions.
2 changes: 1 addition & 1 deletion checksum
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
c678d3d62965d9ba4982766fbd4bafcb8d7c0df2bdc089da0f975f3d78c84c0d cw_croncat.wasm
5b3d3dcfb83ae291ee39c7c3f34626995036caa6deec74a08f08b163fbbb9ecc cw_croncat.wasm
348ce203ce7a18c2e28f001139c1f7a215f7a569c735825dc819dc79692aaffb cw_rules.wasm
17 changes: 11 additions & 6 deletions ci/gas-benchmark/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ use cosm_orc::{
use cosmwasm_std::Binary;
use cw20::Cw20Coin;
use cw_croncat::contract::{
GAS_ACTION_FEE, GAS_BASE_FEE, GAS_DENOMINATOR, GAS_NUMERATOR_DEFAULT, GAS_QUERY_FEE,
GAS_WASM_QUERY_FEE,
GAS_ACTION_FEE, GAS_ADJUSTMENT_NUMERATOR_DEFAULT, GAS_BASE_FEE, GAS_DENOMINATOR,
GAS_NUMERATOR_DEFAULT, GAS_QUERY_FEE, GAS_WASM_QUERY_FEE,
};
use cw_croncat_core::{
msg::{TaskRequest, TaskResponse, TaskWithQueriesResponse},
types::Action,
types::{Action, GasPrice},
};
use cw_rules_core::{msg::QueryResponse, types::CroncatQuery};

Expand Down Expand Up @@ -89,7 +89,7 @@ pub(crate) fn init_contracts(
gas_action_fee: None,
gas_query_fee: None,
gas_wasm_query_fee: None,
gas_fraction: None,
gas_price: None,
agent_nomination_duration: None,
gas_base_fee: None,
};
Expand Down Expand Up @@ -196,8 +196,13 @@ where
let gas_for_task = GAS_BASE_FEE
+ min_gas_for_actions(&task.actions)
+ min_gas_for_queries(task.queries.as_ref());
let gas_to_attached_deposit =
add_agent_fee(gas_for_task) * GAS_NUMERATOR_DEFAULT / GAS_DENOMINATOR;
let gas_to_attached_deposit = GasPrice {
numerator: GAS_NUMERATOR_DEFAULT,
denominator: GAS_DENOMINATOR,
gas_adjustment_numerator: GAS_ADJUSTMENT_NUMERATOR_DEFAULT,
}
.calculate(add_agent_fee(gas_for_task))
.unwrap() as u64;
let amount = (gas_to_attached_deposit + extra_funds) * 3;
create_task(task, orc, user_key, prefix, &denom, amount)?;
}
Expand Down
2 changes: 1 addition & 1 deletion ci/gas-benchmark/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ fn main() -> Result<()> {
gas_action_fee: None,
gas_query_fee: None,
gas_wasm_query_fee: None,
gas_fraction: None,
gas_price: None,
proxy_callback_gas: None,
min_tasks_per_agent: None,
agents_eject_threshold: None,
Expand Down
7 changes: 3 additions & 4 deletions contracts/cw-croncat/src/agent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use std::ops::Div;

use crate::ContractError::*;
use cw_croncat_core::msg::{AgentResponse, AgentTaskResponse, GetAgentIdsResponse};
use cw_croncat_core::types::{calculate_required_amount, Agent, AgentStatus};
use cw_croncat_core::types::{gas_amount_with_agent_fee, Agent, AgentStatus};

impl<'a> CwCroncat<'a> {
/// Get a single agent details
Expand Down Expand Up @@ -140,10 +140,9 @@ impl<'a> CwCroncat<'a> {

// REF: https://github.com/CosmWasm/cw-tokens/tree/main/contracts/cw20-escrow
// Check if native token balance is sufficient for a few txns, in this case 4 txns
// TODO: Adjust gas & costs based on real usage cost
let agent_wallet_balances = deps.querier.query_all_balances(account.clone())?;
let gas_cost = calculate_required_amount(c.gas_action_fee, c.agent_fee)?;
let unit_cost = c.gas_fraction.calculate(4 * gas_cost, 1)?;
let gas_amount_with_agent_fee = gas_amount_with_agent_fee(c.gas_action_fee, c.agent_fee)?;
let unit_cost = c.gas_price.calculate(4 * gas_amount_with_agent_fee)?;
if !has_coins(
&agent_wallet_balances,
&Coin::new(unit_cost, c.native_denom),
Expand Down
17 changes: 10 additions & 7 deletions contracts/cw-croncat/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use cosmwasm_std::{
};
use cw2::set_contract_version;
use cw_croncat_core::msg::{ExecuteMsg, InstantiateMsg, QueryMsg};
use cw_croncat_core::types::{GasFraction, SlotType};
use cw_croncat_core::types::{GasPrice, SlotType};

// version info for migration info
const CONTRACT_NAME: &str = "crates.io:cw-croncat";
Expand All @@ -25,10 +25,13 @@ pub const GAS_QUERY_FEE: u64 = 5_000;
pub const GAS_WASM_QUERY_FEE: u64 = 60_000;
/// We can't store gas_price as floats inside cosmwasm
/// so insted of having 0.04 we use GasFraction{4/100}
/// and multiply numerator by `gas_adjustment` (1.5)
pub const GAS_NUMERATOR_DEFAULT: u64 = 6;
pub const GAS_DENOMINATOR: u64 = 100;

/// and after that multiply Gas by `gas_adjustment` {150/100} (1.5)
pub mod gas_price_defaults {
pub const GAS_NUMERATOR_DEFAULT: u64 = 4;
pub const GAS_ADJUSTMENT_NUMERATOR_DEFAULT: u64 = 150;
pub const GAS_DENOMINATOR: u64 = 100;
}
pub use gas_price_defaults::*;
// #[cfg(not(feature = "library"))]
impl<'a> CwCroncat<'a> {
pub fn instantiate(
Expand Down Expand Up @@ -60,9 +63,10 @@ impl<'a> CwCroncat<'a> {
available_balance,
staked_balance: GenericBalance::default(),
agent_fee: 5,
gas_fraction: msg.gas_fraction.unwrap_or(GasFraction {
gas_price: msg.gas_price.unwrap_or(GasPrice {
numerator: GAS_NUMERATOR_DEFAULT,
denominator: GAS_DENOMINATOR,
gas_adjustment_numerator: GAS_ADJUSTMENT_NUMERATOR_DEFAULT,
}),
proxy_callback_gas: 3,
gas_base_fee: msg.gas_base_fee.map(Into::into).unwrap_or(GAS_BASE_FEE),
Expand Down Expand Up @@ -122,7 +126,6 @@ impl<'a> CwCroncat<'a> {
)
.add_attribute("native_denom", config.native_denom)
.add_attribute("agent_fee", config.agent_fee.to_string())
//.add_attribute("gas_fraction", config.gas_fraction.to_string())
.add_attribute("proxy_callback_gas", config.proxy_callback_gas.to_string())
.add_attribute(
"slot_granularity_time",
Expand Down
6 changes: 3 additions & 3 deletions contracts/cw-croncat/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use cosmwasm_std::{
use cw20::{Cw20CoinVerified, Cw20ExecuteMsg};
use cw_croncat_core::msg::ExecuteMsg;
use cw_croncat_core::traits::{BalancesOperations, FindAndMutate};
use cw_croncat_core::types::{calculate_required_amount, AgentStatus};
use cw_croncat_core::types::{gas_amount_with_agent_fee, AgentStatus};
pub use cw_croncat_core::types::{GenericBalance, Task};
//use regex::Regex;
use schemars::JsonSchema;
Expand Down Expand Up @@ -235,8 +235,8 @@ pub(crate) fn proxy_call_submsgs_price(
cfg.gas_wasm_query_fee,
next_idx,
)?;
let gas_amount = calculate_required_amount(gas_total, cfg.agent_fee)?;
let price_amount = cfg.gas_fraction.calculate(gas_amount, 1)?;
let gas_amount_with_agent_fee = gas_amount_with_agent_fee(gas_total, cfg.agent_fee)?;
let price_amount = cfg.gas_price.calculate(gas_amount_with_agent_fee)?;
let price = coin(price_amount, cfg.native_denom);
Ok((sub_msgs, price))
}
Expand Down
7 changes: 3 additions & 4 deletions contracts/cw-croncat/src/owner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ impl<'a> CwCroncat<'a> {
agents_eject_threshold: c.agents_eject_threshold,
native_denom: c.native_denom,
agent_fee: c.agent_fee,
gas_fraction: c.gas_fraction,
gas_price: c.gas_price,
proxy_callback_gas: c.proxy_callback_gas,
slot_granularity_time: c.slot_granularity_time,
cw_rules_addr: c.cw_rules_addr,
Expand Down Expand Up @@ -84,7 +84,7 @@ impl<'a> CwCroncat<'a> {
gas_action_fee,
gas_query_fee,
gas_wasm_query_fee,
gas_fraction,
gas_price,
proxy_callback_gas,
min_tasks_per_agent,
agents_eject_threshold,
Expand Down Expand Up @@ -112,7 +112,7 @@ impl<'a> CwCroncat<'a> {
agent_nomination_duration: old_config.agent_nomination_duration,
cw_rules_addr: old_config.cw_rules_addr,
agent_fee: agent_fee.unwrap_or(old_config.agent_fee),
gas_fraction: gas_fraction.unwrap_or(old_config.gas_fraction),
gas_price: gas_price.unwrap_or(old_config.gas_price),
gas_base_fee: gas_base_fee
.map(Into::into)
.unwrap_or(old_config.gas_base_fee),
Expand Down Expand Up @@ -165,7 +165,6 @@ impl<'a> CwCroncat<'a> {
)
.add_attribute("native_denom", c.native_denom)
.add_attribute("agent_fee", c.agent_fee.to_string())
//.add_attribute("gas_price", c.gas_fraction.to_string())
.add_attribute("proxy_callback_gas", c.proxy_callback_gas.to_string())
.add_attribute("slot_granularity_time", c.slot_granularity_time.to_string()))
}
Expand Down
4 changes: 2 additions & 2 deletions contracts/cw-croncat/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use serde::{Deserialize, Serialize};
use crate::helpers::Task;
use cw_croncat_core::{
query::CroncatQuerier,
types::{Agent, GasFraction, GenericBalance, SlotType},
types::{Agent, GasPrice, GenericBalance, SlotType},
};

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
Expand Down Expand Up @@ -38,7 +38,7 @@ pub struct Config {

// Economics
pub agent_fee: u64,
pub gas_fraction: GasFraction,
pub gas_price: GasPrice,
pub gas_base_fee: u64,
pub gas_action_fee: u64,
pub gas_query_fee: u64,
Expand Down
6 changes: 3 additions & 3 deletions contracts/cw-croncat/src/tasks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use cw_croncat_core::msg::{
};
use cw_croncat_core::traits::{BalancesOperations, FindAndMutate, Intervals};
use cw_croncat_core::types::{
calculate_required_amount, BoundaryValidated, GenericBalance, SlotType, Task,
gas_amount_with_agent_fee, BoundaryValidated, GenericBalance, SlotType, Task,
};

impl<'a> CwCroncat<'a> {
Expand Down Expand Up @@ -265,8 +265,8 @@ impl<'a> CwCroncat<'a> {
cfg.gas_query_fee,
cfg.gas_wasm_query_fee,
)?;
let gas_price = calculate_required_amount(gas_amount, cfg.agent_fee)?;
let price = cfg.gas_fraction.calculate(gas_price, 1)?;
let gas_amount_with_agent_fee = gas_amount_with_agent_fee(gas_amount, cfg.agent_fee)?;
let price = cfg.gas_price.calculate(gas_amount_with_agent_fee)?;
amount_for_one_task
.native
.find_checked_add(&coin(price, &cfg.native_denom))?;
Expand Down
11 changes: 6 additions & 5 deletions contracts/cw-croncat/src/tests/agent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use cw_croncat_core::msg::{
AgentResponse, AgentTaskResponse, ExecuteMsg, GetAgentIdsResponse, InstantiateMsg, QueryMsg,
TaskRequest, TaskResponse,
};
use cw_croncat_core::types::{Action, Agent, AgentStatus, GasFraction, GenericBalance, Interval};
use cw_croncat_core::types::{Action, Agent, AgentStatus, GasPrice, GenericBalance, Interval};
use cw_multi_test::{App, AppResponse, BankSudo, Executor, SudoMsg};

use super::helpers::{
Expand Down Expand Up @@ -258,7 +258,7 @@ fn test_instantiate_sets_balance() {
gas_action_fee: None,
gas_query_fee: None,
gas_wasm_query_fee: None,
gas_fraction: None,
gas_price: None,
agent_nomination_duration: None,
gas_base_fee: None,
},
Expand Down Expand Up @@ -311,7 +311,7 @@ fn register_agent_fail_cases() {
agent_fee: None,
min_tasks_per_agent: None,
agents_eject_threshold: None,
gas_fraction: None,
gas_price: None,
proxy_callback_gas: None,
gas_base_fee: None,
gas_action_fee: None,
Expand Down Expand Up @@ -345,9 +345,10 @@ fn register_agent_fail_cases() {
agent_fee: None,
min_tasks_per_agent: None,
agents_eject_threshold: None,
gas_fraction: Some(GasFraction {
gas_price: Some(GasPrice {
numerator: 1,
denominator: 1,
gas_adjustment_numerator: 1,
}),
proxy_callback_gas: None,
slot_granularity_time: None,
Expand Down Expand Up @@ -759,7 +760,7 @@ fn test_get_agent_status() {
gas_action_fee: None,
gas_query_fee: None,
gas_wasm_query_fee: None,
gas_fraction: None,
gas_price: None,
agent_nomination_duration: Some(360),
cw_rules_addr: "todo".to_string(),
gas_base_fee: None,
Expand Down
9 changes: 5 additions & 4 deletions contracts/cw-croncat/src/tests/balance.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use crate::balancer::{Balancer, BalancerMode, RoundRobinBalancer};
use crate::contract::{
GAS_ACTION_FEE, GAS_BASE_FEE, GAS_DENOMINATOR, GAS_NUMERATOR_DEFAULT, GAS_QUERY_FEE,
GAS_WASM_QUERY_FEE,
GAS_ACTION_FEE, GAS_ADJUSTMENT_NUMERATOR_DEFAULT, GAS_BASE_FEE, GAS_DENOMINATOR,
GAS_NUMERATOR_DEFAULT, GAS_QUERY_FEE, GAS_WASM_QUERY_FEE,
};
use crate::state::{Config, TaskInfo};
use crate::tests::helpers::{default_task, AGENT0, AGENT1, AGENT2, AGENT3, AGENT4};
use cosmwasm_std::testing::{mock_dependencies_with_balance, mock_env};
use cosmwasm_std::{coins, Addr};
use cw_croncat_core::types::{GasFraction, GenericBalance, SlotType};
use cw_croncat_core::types::{GasPrice, GenericBalance, SlotType};

use crate::CwCroncat;

Expand All @@ -24,9 +24,10 @@ fn mock_config() -> Config {
available_balance: GenericBalance::default(),
staked_balance: GenericBalance::default(),
agent_fee: 5,
gas_fraction: GasFraction {
gas_price: GasPrice {
numerator: GAS_NUMERATOR_DEFAULT,
denominator: GAS_DENOMINATOR,
gas_adjustment_numerator: GAS_ADJUSTMENT_NUMERATOR_DEFAULT,
},
gas_action_fee: GAS_ACTION_FEE,
gas_query_fee: GAS_QUERY_FEE,
Expand Down
12 changes: 7 additions & 5 deletions contracts/cw-croncat/src/tests/contract.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::contract::GAS_ADJUSTMENT_NUMERATOR_DEFAULT;
use crate::contract::GAS_DENOMINATOR;
use crate::contract::GAS_NUMERATOR_DEFAULT;
use crate::state::QueueItem;
Expand All @@ -12,7 +13,7 @@ use cosmwasm_std::testing::{
};
use cosmwasm_std::{coins, from_binary, Addr, Binary, Event, Reply, SubMsgResponse, SubMsgResult};
use cw_croncat_core::msg::{GetConfigResponse, QueryMsg};
use cw_croncat_core::types::GasFraction;
use cw_croncat_core::types::GasPrice;
use cw_croncat_core::types::SlotType;

#[test]
Expand All @@ -26,7 +27,7 @@ fn configure() {
gas_action_fee: None,
gas_query_fee: None,
gas_wasm_query_fee: None,
gas_fraction: None,
gas_price: None,
agent_nomination_duration: Some(360),
cw_rules_addr: "todo".to_string(),
gas_base_fee: None,
Expand Down Expand Up @@ -56,11 +57,12 @@ fn configure() {
assert_eq!("atom", value.native_denom);
assert_eq!(5, value.agent_fee);
assert_eq!(
GasFraction {
GasPrice {
numerator: GAS_NUMERATOR_DEFAULT,
denominator: GAS_DENOMINATOR
denominator: GAS_DENOMINATOR,
gas_adjustment_numerator: GAS_ADJUSTMENT_NUMERATOR_DEFAULT,
},
value.gas_fraction
value.gas_price
);
assert_eq!(3, value.proxy_callback_gas);
assert_eq!(10_000_000_000, value.slot_granularity_time);
Expand Down
4 changes: 2 additions & 2 deletions contracts/cw-croncat/src/tests/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub fn mock_init(store: &CwCroncat, deps: DepsMut<Empty>) -> Result<Response, Co
gas_action_fee: None,
gas_query_fee: None,
gas_wasm_query_fee: None,
gas_fraction: None,
gas_price: None,
agent_nomination_duration: Some(360),
cw_rules_addr: "todo".to_string(),
gas_base_fee: None,
Expand Down Expand Up @@ -183,7 +183,7 @@ pub fn proper_instantiate() -> (App, CwTemplateContract, Addr) {
gas_action_fee: None,
gas_query_fee: None,
gas_wasm_query_fee: None,
gas_fraction: None,
gas_price: None,
agent_nomination_duration: None,
};
let cw_template_contract_addr = app
Expand Down
Loading

0 comments on commit f76122f

Please sign in to comment.