Skip to content

Commit

Permalink
Merge branch 'main' into task-hash-transforms-update
Browse files Browse the repository at this point in the history
  • Loading branch information
Buckram123 committed Dec 21, 2022
2 parents d695712 + 426c2c7 commit f5a9a71
Show file tree
Hide file tree
Showing 25 changed files with 745 additions and 101 deletions.
2 changes: 1 addition & 1 deletion checksum
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
6f12020a97b6e77b0a47ef33937faef858af5077ab5f0c330164dab7130d8e8d cw_croncat.wasm
c678d3d62965d9ba4982766fbd4bafcb8d7c0df2bdc089da0f975f3d78c84c0d cw_croncat.wasm
348ce203ce7a18c2e28f001139c1f7a215f7a569c735825dc819dc79692aaffb cw_rules.wasm
31 changes: 26 additions & 5 deletions ci/gas-benchmark/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,39 @@ use cosm_orc::{
};
use cosmwasm_std::Binary;
use cw20::Cw20Coin;
use cw_croncat::contract::{GAS_ACTION_FEE_JUNO, GAS_BASE_FEE_JUNO, GAS_DENOMINATOR_DEFAULT_JUNO};
use cw_croncat::contract::{
GAS_ACTION_FEE, 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,
};
use cw_rules_core::msg::QueryResponse;
use cw_rules_core::{msg::QueryResponse, types::CroncatQuery};

const fn add_agent_fee(num: u64) -> u64 {
num + (num * 5 / 100)
}

fn min_gas_for_actions(actions: &[Action]) -> u64 {
actions.iter().fold(0, |acc, action| {
acc + action.gas_limit.unwrap_or(GAS_ACTION_FEE_JUNO)
acc + action.gas_limit.unwrap_or(GAS_ACTION_FEE)
})
}

fn min_gas_for_queries(queries: Option<&Vec<CroncatQuery>>) -> u64 {
if let Some(queries) = queries {
queries.iter().fold(GAS_WASM_QUERY_FEE, |acc, query| {
acc + match query {
CroncatQuery::HasBalanceGte(_) => GAS_QUERY_FEE,
_ => GAS_WASM_QUERY_FEE,
}
})
} else {
0
}
}

pub(crate) fn init_contracts(
orc: &mut CosmOrc,
key: &SigningKey,
Expand Down Expand Up @@ -71,6 +87,8 @@ pub(crate) fn init_contracts(
cw_rules_addr: rules_res.address,
owner_id: Some(admin_addr.to_owned()),
gas_action_fee: None,
gas_query_fee: None,
gas_wasm_query_fee: None,
gas_fraction: None,
agent_nomination_duration: None,
gas_base_fee: None,
Expand Down Expand Up @@ -175,8 +193,11 @@ where
.map(|(_, _, prefix)| (*prefix).to_owned())
.collect();
for (task, extra_funds, prefix) in tasks {
let gas_for_task = GAS_BASE_FEE_JUNO + min_gas_for_actions(&task.actions);
let gas_to_attached_deposit = add_agent_fee(gas_for_task) / GAS_DENOMINATOR_DEFAULT_JUNO;
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 amount = (gas_to_attached_deposit + extra_funds) * 3;
create_task(task, orc, user_key, prefix, &denom, amount)?;
}
Expand Down
2 changes: 2 additions & 0 deletions ci/gas-benchmark/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,8 @@ fn main() -> Result<()> {
agent_fee: None,
gas_base_fee: None,
gas_action_fee: None,
gas_query_fee: None,
gas_wasm_query_fee: None,
gas_fraction: None,
proxy_callback_gas: None,
min_tasks_per_agent: None,
Expand Down
4 changes: 2 additions & 2 deletions ci/local_config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ chain_cfg:
chain_id: "testing"
rpc_endpoint: "http://localhost:26657/"
grpc_endpoint: "http://localhost:9090/"
gas_prices: 0.1
gas_adjustment: 1.2
gas_prices: 0.04
gas_adjustment: 1.5
45 changes: 22 additions & 23 deletions contracts/cw-croncat/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,18 @@ const DEFAULT_NOMINATION_DURATION: u16 = 360;

/// default for juno
/// This based on non-wasm operations, wasm ops seem impossible to predict
pub const GAS_BASE_FEE_JUNO: u64 = 300_000;
/// Gas cost per single action
pub const GAS_ACTION_FEE_JUNO: u64 = 130_000;
pub const GAS_BASE_FEE: u64 = 300_000;
/// Gas needed for single action
pub const GAS_ACTION_FEE: u64 = 130_000;
/// Gas needed for single non-wasm query
pub const GAS_QUERY_FEE: u64 = 5_000;
/// Gas needed for single wasm query
pub const GAS_WASM_QUERY_FEE: u64 = 60_000;
/// We can't store gas_price as floats inside cosmwasm
/// so insted of something like 0.1 we use GasFraction{1/10}
pub const GAS_DENOMINATOR_DEFAULT_JUNO: u64 = 9;
/// 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;

// #[cfg(not(feature = "library"))]
impl<'a> CwCroncat<'a> {
Expand All @@ -44,18 +50,6 @@ impl<'a> CwCroncat<'a> {
info.sender
};

let gas_action_fee = if let Some(action_fee) = msg.gas_action_fee {
action_fee.u64()
} else {
GAS_ACTION_FEE_JUNO
};

let gas_base_fee = if let Some(base_fee) = msg.gas_base_fee {
base_fee.u64()
} else {
GAS_BASE_FEE_JUNO
};

let config = Config {
paused: false,
owner_id,
Expand All @@ -66,13 +60,18 @@ impl<'a> CwCroncat<'a> {
available_balance,
staked_balance: GenericBalance::default(),
agent_fee: 5,
gas_fraction: GasFraction {
numerator: 1,
denominator: GAS_DENOMINATOR_DEFAULT_JUNO,
},
gas_fraction: msg.gas_fraction.unwrap_or(GasFraction {
numerator: GAS_NUMERATOR_DEFAULT,
denominator: GAS_DENOMINATOR,
}),
proxy_callback_gas: 3,
gas_base_fee,
gas_action_fee,
gas_base_fee: msg.gas_base_fee.map(Into::into).unwrap_or(GAS_BASE_FEE),
gas_action_fee: msg.gas_action_fee.map(Into::into).unwrap_or(GAS_ACTION_FEE),
gas_query_fee: msg.gas_query_fee.map(Into::into).unwrap_or(GAS_QUERY_FEE),
gas_wasm_query_fee: msg
.gas_wasm_query_fee
.map(Into::into)
.unwrap_or(GAS_WASM_QUERY_FEE),
slot_granularity_time: 10_000_000_000, // 10 seconds
native_denom: msg.denom,
cw20_whitelist: vec![],
Expand Down
9 changes: 7 additions & 2 deletions contracts/cw-croncat/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,13 @@ pub(crate) fn proxy_call_submsgs_price(
cfg: Config,
next_idx: u64,
) -> Result<(Vec<SubMsg>, Coin), ContractError> {
let (sub_msgs, gas_total) =
task.get_submsgs_with_total_gas(cfg.gas_base_fee, cfg.gas_action_fee, next_idx)?;
let (sub_msgs, gas_total) = task.get_submsgs_with_total_gas(
cfg.gas_base_fee,
cfg.gas_action_fee,
cfg.gas_query_fee,
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 price = coin(price_amount, cfg.native_denom);
Expand Down
81 changes: 44 additions & 37 deletions contracts/cw-croncat/src/owner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,53 +82,60 @@ impl<'a> CwCroncat<'a> {
agent_fee,
gas_base_fee,
gas_action_fee,
gas_query_fee,
gas_wasm_query_fee,
gas_fraction,
proxy_callback_gas,
min_tasks_per_agent,
agents_eject_threshold,
// treasury_id,
} => {
let owner_id = if let Some(addr) = owner_id {
Some(api.addr_validate(&addr)?)
} else {
None
};
self.config
.update(deps.storage, |mut config| -> Result<_, ContractError> {
if info.sender != config.owner_id {
.update(deps.storage, |old_config| -> Result<_, ContractError> {
if info.sender != old_config.owner_id {
return Err(ContractError::Unauthorized {});
}

if let Some(owner_id) = owner_id {
let owner_id = api.addr_validate(&owner_id)?;
config.owner_id = owner_id;
}
// if let Some(treasury_id) = treasury_id {
// config.treasury_id = Some(treasury_id);
// }
if let Some(slot_granularity_time) = slot_granularity_time {
config.slot_granularity_time = slot_granularity_time;
}
if let Some(paused) = paused {
config.paused = paused;
}
if let Some(gas_base_fee) = gas_base_fee {
config.gas_base_fee = gas_base_fee.u64();
}
if let Some(gas_action_fee) = gas_action_fee {
config.gas_action_fee = gas_action_fee.u64();
}
if let Some(gas_fraction) = gas_fraction {
config.gas_fraction = gas_fraction;
}
if let Some(proxy_callback_gas) = proxy_callback_gas {
config.proxy_callback_gas = proxy_callback_gas;
}
if let Some(agent_fee) = agent_fee {
config.agent_fee = agent_fee;
}
if let Some(min_tasks_per_agent) = min_tasks_per_agent {
config.min_tasks_per_agent = min_tasks_per_agent;
}
if let Some(agents_eject_threshold) = agents_eject_threshold {
config.agents_eject_threshold = agents_eject_threshold;
}
Ok(config)
let new_config = Config {
paused: paused.unwrap_or(old_config.paused),
owner_id: owner_id.unwrap_or(old_config.owner_id),
min_tasks_per_agent: min_tasks_per_agent
.unwrap_or(old_config.min_tasks_per_agent),
agent_active_indices: old_config.agent_active_indices,
agents_eject_threshold: agents_eject_threshold
.unwrap_or(old_config.agents_eject_threshold),
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_base_fee: gas_base_fee
.map(Into::into)
.unwrap_or(old_config.gas_base_fee),
gas_action_fee: gas_action_fee
.map(Into::into)
.unwrap_or(old_config.gas_action_fee),
gas_query_fee: gas_query_fee
.map(Into::into)
.unwrap_or(old_config.gas_query_fee),
gas_wasm_query_fee: gas_wasm_query_fee
.map(Into::into)
.unwrap_or(old_config.gas_wasm_query_fee),
proxy_callback_gas: proxy_callback_gas
.unwrap_or(old_config.proxy_callback_gas),
slot_granularity_time: slot_granularity_time
.unwrap_or(old_config.slot_granularity_time),
cw20_whitelist: old_config.cw20_whitelist,
native_denom: old_config.native_denom,
available_balance: old_config.available_balance,
staked_balance: old_config.staked_balance,
limit: old_config.limit,
};
Ok(new_config)
})?;
}
_ => unreachable!(),
Expand Down
2 changes: 2 additions & 0 deletions contracts/cw-croncat/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ pub struct Config {
pub gas_fraction: GasFraction,
pub gas_base_fee: u64,
pub gas_action_fee: u64,
pub gas_query_fee: u64,
pub gas_wasm_query_fee: u64,
pub proxy_callback_gas: u32,
pub slot_granularity_time: u64,

Expand Down
2 changes: 2 additions & 0 deletions contracts/cw-croncat/src/tasks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,8 @@ impl<'a> CwCroncat<'a> {
&cfg.owner_id,
cfg.gas_base_fee,
cfg.gas_action_fee,
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)?;
Expand Down
8 changes: 8 additions & 0 deletions contracts/cw-croncat/src/tests/agent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,8 @@ fn test_instantiate_sets_balance() {
cw_rules_addr: "grapestem".to_string(),
owner_id: None,
gas_action_fee: None,
gas_query_fee: None,
gas_wasm_query_fee: None,
gas_fraction: None,
agent_nomination_duration: None,
gas_base_fee: None,
Expand Down Expand Up @@ -313,6 +315,8 @@ fn register_agent_fail_cases() {
proxy_callback_gas: None,
gas_base_fee: None,
gas_action_fee: None,
gas_query_fee: None,
gas_wasm_query_fee: None,
slot_granularity_time: None,
};
app.execute_contract(
Expand Down Expand Up @@ -349,6 +353,8 @@ fn register_agent_fail_cases() {
slot_granularity_time: None,
gas_base_fee: None,
gas_action_fee: None,
gas_query_fee: None,
gas_wasm_query_fee: None,
};
app.execute_contract(
Addr::unchecked(ADMIN),
Expand Down Expand Up @@ -751,6 +757,8 @@ fn test_get_agent_status() {
denom: NATIVE_DENOM.to_string(),
owner_id: None,
gas_action_fee: None,
gas_query_fee: None,
gas_wasm_query_fee: None,
gas_fraction: None,
agent_nomination_duration: Some(360),
cw_rules_addr: "todo".to_string(),
Expand Down
15 changes: 10 additions & 5 deletions contracts/cw-croncat/src/tests/balance.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use crate::balancer::{Balancer, BalancerMode, RoundRobinBalancer};
use crate::contract::{GAS_ACTION_FEE_JUNO, GAS_BASE_FEE_JUNO, GAS_DENOMINATOR_DEFAULT_JUNO};
use crate::contract::{
GAS_ACTION_FEE, 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};
Expand All @@ -22,18 +25,20 @@ fn mock_config() -> Config {
staked_balance: GenericBalance::default(),
agent_fee: 5,
gas_fraction: GasFraction {
numerator: 1,
denominator: GAS_DENOMINATOR_DEFAULT_JUNO,
numerator: GAS_NUMERATOR_DEFAULT,
denominator: GAS_DENOMINATOR,
},
gas_action_fee: GAS_ACTION_FEE_JUNO,
gas_action_fee: GAS_ACTION_FEE,
gas_query_fee: GAS_QUERY_FEE,
gas_wasm_query_fee: GAS_WASM_QUERY_FEE,
proxy_callback_gas: 3,
slot_granularity_time: 60_000_000_000,
native_denom: NATIVE_DENOM.to_owned(),
cw20_whitelist: vec![],
agent_nomination_duration: 9,
limit: 100,
cw_rules_addr: Addr::unchecked("todo"),
gas_base_fee: GAS_BASE_FEE_JUNO,
gas_base_fee: GAS_BASE_FEE,
}
}
#[test]
Expand Down
9 changes: 6 additions & 3 deletions contracts/cw-croncat/src/tests/contract.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::contract::GAS_DENOMINATOR_DEFAULT_JUNO;
use crate::contract::GAS_DENOMINATOR;
use crate::contract::GAS_NUMERATOR_DEFAULT;
use crate::state::QueueItem;
use crate::tests::helpers::mock_init;
use crate::tests::helpers::AGENT0;
Expand All @@ -23,6 +24,8 @@ fn configure() {
denom: NATIVE_DENOM.to_string(),
owner_id: None,
gas_action_fee: None,
gas_query_fee: None,
gas_wasm_query_fee: None,
gas_fraction: None,
agent_nomination_duration: Some(360),
cw_rules_addr: "todo".to_string(),
Expand Down Expand Up @@ -54,8 +57,8 @@ fn configure() {
assert_eq!(5, value.agent_fee);
assert_eq!(
GasFraction {
numerator: 1,
denominator: GAS_DENOMINATOR_DEFAULT_JUNO
numerator: GAS_NUMERATOR_DEFAULT,
denominator: GAS_DENOMINATOR
},
value.gas_fraction
);
Expand Down
Loading

0 comments on commit f5a9a71

Please sign in to comment.