Skip to content

Commit

Permalink
Added min gas env variable and behavior (#276)
Browse files Browse the repository at this point in the history
* Added min gas env variable and behavior

* Remove useless export

* Changelog + merge
  • Loading branch information
Kayanski authored Dec 12, 2023
1 parent 7f22062 commit d17909a
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 7 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
- Add `BankQuerier` as trait bound to `CwEnv`.
- Add `WasmCodeQuerier` as trait bound to `CwEnv`.
- Changed the snapshot feature for better snapshot readability.
- Added Readonly flag to the daemon state
- Added min gas fee environment variable
- Updated `cosmrs` to `0.15.0`
- Updated tonic to `0.10.2`
- Bumped MSRV to 1.72 because of dependency `cosmrs@0.15.0`
Expand Down
4 changes: 1 addition & 3 deletions cw-orch-daemon/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ pub mod queriers;
mod traits;
pub mod tx_broadcaster;
pub mod tx_builder;
pub use self::{
builder::*, channel::*, core::*, error::*, state::*, sync::*, traits::*, tx_resp::*,
};
pub use self::{builder::*, channel::*, core::*, error::*, state::*, sync::*, tx_resp::*};
pub use cw_orch_networks::chain_info::*;
pub use cw_orch_networks::networks;
pub use sender::Wallet;
Expand Down
6 changes: 5 additions & 1 deletion cw-orch-daemon/src/sender.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,17 @@ impl Sender<All> {
/// Compute the gas fee from the expected gas in the transaction
/// Applies a Gas Buffer for including signature verification
pub(crate) fn get_fee_from_gas(&self, gas: u64) -> Result<(u64, u128), DaemonError> {
let gas_expected = if let Some(gas_buffer) = CwOrchEnvVars::load()?.gas_buffer {
let mut gas_expected = if let Some(gas_buffer) = CwOrchEnvVars::load()?.gas_buffer {
gas as f64 * gas_buffer
} else if gas < BUFFER_THRESHOLD {
gas as f64 * SMALL_GAS_BUFFER
} else {
gas as f64 * GAS_BUFFER
};

if let Some(min_gas) = CwOrchEnvVars::load()?.min_gas {
gas_expected = (min_gas as f64).max(gas_expected);
}
let fee_amount = gas_expected
* (self.daemon_state.chain_data.fees.fee_tokens[0]
.fixed_min_gas_price
Expand Down
3 changes: 1 addition & 2 deletions cw-orch-daemon/src/tx_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,7 @@ impl TxBuilder {
Ok(fee)
}

/// Builds the raw tx with a given body and fee and signs it.
/// Sets the TxBuilder's gas limit to its simulated amount for later use.
/// Simulates the transaction and returns the necessary gas fee returned by the simulation on a node
pub async fn simulate(&self, wallet: &Sender<All>) -> Result<u64, DaemonError> {
// get the account number of the wallet
let BaseAccount {
Expand Down
3 changes: 2 additions & 1 deletion docs/src/contracts/env-variable.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,10 @@ CW_ORCH_SERIALIZE_JSON = "false"
# Optional - Absolute Path. Sets the directory where the state file will be saved.
# This is not enforced to be an absolute path but this is highly recommended
CW_ORCH_STATE_FOLDER = "~/.cw-orchestrator"
# Optional - Integer. This allows setting a minimum of gas used when broadcasting transactions
CW_ORCH_MIN_GAS = 100000
# Optional - boolean. Disable the "Enable Logs" message.
CW_ORCH_DISABLE_ENABLE_LOGS_MESSAGE = "false"

```

## Mnemonics
Expand Down
7 changes: 7 additions & 0 deletions packages/cw-orch-core/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub const STATE_FOLDER_ENV_NAME: &str = "CW_ORCH_STATE_FOLDER";
pub const STATE_FILE_ENV_NAME: &str = "STATE_FILE";
pub const ARTIFACTS_DIR_ENV_NAME: &str = "ARTIFACTS_DIR";
pub const GAS_BUFFER_ENV_NAME: &str = "CW_ORCH_GAS_BUFFER";
pub const MIN_GAS_ENV_NAME: &str = "CW_ORCH_MIN_GAS";
pub const MAX_TX_QUERIES_RETRY_ENV_NAME: &str = "CW_ORCH_MAX_TX_QUERY_RETRIES";
pub const MIN_BLOCK_SPEED_ENV_NAME: &str = "CW_ORCH_MIN_BLOCK_SPEED";
pub const SERIALIZE_ENV_NAME: &str = "CW_ORCH_SERIALIZE_JSON";
Expand Down Expand Up @@ -50,6 +51,11 @@ pub struct CwOrchEnvVars {
/// If not specified, a more complex algorithm is applied for dealing with small gas fee cases
pub gas_buffer: Option<f64>,

/// Optional - Integer
/// Defaults to None
/// Minimum gas amount. Useful when transaction still won't pass even when setting a high gas_buffer or for mixed transaction scripts
pub min_gas: Option<u64>,

/// Optional - Integer
/// Defaults to [`DEFAULT_TX_QUERY_RETRIES`]
/// This changes the number of tx queries before it fails if it doesn't find any result
Expand Down Expand Up @@ -110,6 +116,7 @@ impl Default for CwOrchEnvVars {
state_file: PathBuf::from_str("state.json").unwrap(),
artifacts_dir: None,
gas_buffer: None,
min_gas: None,
max_tx_query_retries: DEFAULT_TX_QUERY_RETRIES,
min_block_speed: 1,
serialize_json: false,
Expand Down

0 comments on commit d17909a

Please sign in to comment.