Skip to content

Commit

Permalink
Merge branch 'main' into refactoring-scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
d3v3us committed Dec 22, 2022
2 parents c7136f3 + b8b6213 commit 938829c
Show file tree
Hide file tree
Showing 38 changed files with 702 additions and 2,472 deletions.
5 changes: 5 additions & 0 deletions .rusty-hook.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[hooks]
pre-commit = "just lint"

[logging]
verbose = true
67 changes: 62 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions checksum
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
e841b8f67c8209ade26674edd432bb1d7dc9dcaa470651ebf379c62d41c294c8 cw_croncat.wasm
de2d1a0c648e41760020dd261f818da085c358240059acf85128f60eb0e05db2 cw_rules.wasm
f02ba448d956da4cc508cafdf14dd010e3ac0d37f5d40fa3116d27093d3a6ce1 cw_croncat.wasm
348ce203ce7a18c2e28f001139c1f7a215f7a569c735825dc819dc79692aaffb cw_rules.wasm
40 changes: 33 additions & 7 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_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;
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,7 +87,9 @@ pub(crate) fn init_contracts(
cw_rules_addr: rules_res.address,
owner_id: Some(admin_addr.to_owned()),
gas_action_fee: None,
gas_fraction: None,
gas_query_fee: None,
gas_wasm_query_fee: None,
gas_price: None,
agent_nomination_duration: None,
gas_base_fee: None,
};
Expand Down Expand Up @@ -175,8 +193,16 @@ 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 = 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
4 changes: 3 additions & 1 deletion ci/gas-benchmark/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,9 @@ fn main() -> Result<()> {
agent_fee: None,
gas_base_fee: None,
gas_action_fee: None,
gas_fraction: None,
gas_query_fee: None,
gas_wasm_query_fee: None,
gas_price: None,
proxy_callback_gas: None,
min_tasks_per_agent: None,
agents_eject_threshold: 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
1 change: 1 addition & 0 deletions contracts/cw-croncat/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,4 @@ cwd-core = { version = "0.2.0", git = "https://github.com/DA0-DA0/dao-contracts"
cwd-interface = { version = "0.2.0", git = "https://github.com/DA0-DA0/dao-contracts" }
cwd-proposal-single = { version = "0.2.0", git = "https://github.com/DA0-DA0/dao-contracts" }
cwd-voting = { version = "0.2.0", git = "https://github.com/DA0-DA0/dao-contracts.git" }
rusty-hook = "0.11.2"
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
58 changes: 29 additions & 29 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 @@ -16,13 +16,22 @@ 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 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 All @@ -44,18 +53,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 +63,19 @@ impl<'a> CwCroncat<'a> {
available_balance,
staked_balance: GenericBalance::default(),
agent_fee: 5,
gas_fraction: GasFraction {
numerator: 1,
denominator: GAS_DENOMINATOR_DEFAULT_JUNO,
},
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,
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 Expand Up @@ -123,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 Expand Up @@ -209,9 +211,7 @@ impl<'a> CwCroncat<'a> {
QueryMsg::GetWalletBalances { wallet } => {
to_binary(&self.query_wallet_balances(deps, wallet)?)
}
QueryMsg::GetState { from_index, limit } => {
to_binary(&self.get_state(deps, env, from_index, limit)?)
}

QueryMsg::GetTaskHash { task } => to_binary(&self.query_get_task_hash(*task)?),
}
}
Expand Down
Loading

0 comments on commit 938829c

Please sign in to comment.