Skip to content

Commit

Permalink
chore: Switch to near-gas crate (#240)
Browse files Browse the repository at this point in the history
  • Loading branch information
iho authored Sep 24, 2023
1 parent caf1564 commit 5a6631a
Show file tree
Hide file tree
Showing 13 changed files with 170 additions and 254 deletions.
133 changes: 123 additions & 10 deletions Cargo.lock

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

7 changes: 7 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@ near-primitives = "0.17.0"
near-jsonrpc-client = "0.6.0"
near-jsonrpc-primitives = "0.17.0"

near-gas = { version = "0.2.3", features = [
"serde",
"borsh",
"schemars",
"interactive-clap",
] }

interactive-clap = "0.2.4"
interactive-clap-derive = "0.2.4"
keyring = "2.0.5"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::str::FromStr;

use serde_json::json;

#[derive(Debug, Clone, interactive_clap::InteractiveClap)]
Expand Down Expand Up @@ -99,9 +97,7 @@ impl From<SignerAccountIdContext> for crate::commands::ActionContext {
near_primitives::transaction::FunctionCallAction {
method_name: "create_account".to_string(),
args,
gas: crate::common::NearGas::from_str("30 TeraGas")
.unwrap()
.inner,
gas: crate::common::NearGas::from_tgas(30).as_gas(),
deposit: item
.account_properties
.initial_balance
Expand Down
6 changes: 1 addition & 5 deletions src/commands/account/storage_management/storage_deposit.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::str::FromStr;

use inquire::Select;

#[derive(Debug, Clone, interactive_clap::InteractiveClap)]
Expand Down Expand Up @@ -122,9 +120,7 @@ impl SignerAccountIdContext {
args: serde_json::json!({ "account_id": &receiver_account_id })
.to_string()
.into_bytes(),
gas: crate::common::NearGas::from_str("50 TeraGas")
.unwrap()
.inner,
gas: crate::common::NearGas::from_tgas(50).as_gas(),
deposit: deposit.to_yoctonear(),
},
)],
Expand Down
6 changes: 1 addition & 5 deletions src/commands/account/storage_management/storage_withdraw.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::str::FromStr;

#[derive(Debug, Clone, interactive_clap::InteractiveClap)]
#[interactive_clap(input_context = super::ContractContext)]
#[interactive_clap(output_context = WithdrawArgsContext)]
Expand Down Expand Up @@ -70,9 +68,7 @@ impl SignerAccountIdContext {
})
.to_string()
.into_bytes(),
gas: crate::common::NearGas::from_str("50 TeraGas")
.unwrap()
.inner,
gas: crate::common::NearGas::from_tgas(50).as_gas(),
deposit: crate::common::NearBalance::from_yoctonear(1)
.to_yoctonear(),
},
Expand Down
12 changes: 5 additions & 7 deletions src/commands/contract/call_function/as_transaction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,25 +106,23 @@ impl PrepaidGas {
_context: &CallFunctionPropertiesContext,
) -> color_eyre::eyre::Result<Option<crate::common::NearGas>> {
eprintln!();
let gas: u64 = loop {
let gas = loop {
match crate::common::NearGas::from_str(
&Text::new("Enter gas for function call:")
.with_initial_value("100 TeraGas")
.prompt()?,
) {
Ok(input_gas) => {
let crate::common::NearGas { inner: num } = input_gas;
let gas = num;
if gas <= 300000000000000 {
break gas;
if input_gas <= near_gas::NearGas::from_tgas(300) {
break input_gas;
} else {
eprintln!("You need to enter a value of no more than 300 TERAGAS")
}
}
Err(err) => return Err(color_eyre::Report::msg(err)),
}
};
Ok(Some(gas.into()))
Ok(Some(gas))
}
}

Expand Down Expand Up @@ -239,7 +237,7 @@ impl From<SignerAccountIdContext> for crate::commands::ActionContext {
near_primitives::transaction::FunctionCallAction {
method_name: item.function_name.clone(),
args: item.function_args.clone(),
gas: item.gas.inner,
gas: item.gas.as_gas(),
deposit: item.deposit.to_yoctonear(),
},
)],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,25 +106,23 @@ impl PrepaidGas {
_context: &CallFunctionActionContext,
) -> color_eyre::eyre::Result<Option<crate::common::NearGas>> {
eprintln!();
let gas: u64 = loop {
let gas = loop {
match crate::common::NearGas::from_str(
&Text::new("Enter gas for function call:")
.with_initial_value("100 TeraGas")
.prompt()?,
) {
Ok(input_gas) => {
let crate::common::NearGas { inner: num } = input_gas;
let gas = num;
if gas <= 300000000000000 {
break gas;
if input_gas <= near_gas::NearGas::from_tgas(300) {
break input_gas;
} else {
eprintln!("You need to enter a value of no more than 300 TERAGAS")
}
}
Err(err) => return Err(color_eyre::Report::msg(err)),
}
};
Ok(Some(gas.into()))
Ok(Some(gas))
}
}

Expand Down Expand Up @@ -169,7 +167,7 @@ impl DepositContext {
near_primitives::transaction::FunctionCallAction {
method_name: previous_context.function_name.clone(),
args: previous_context.function_args.clone(),
gas: previous_context.gas.inner,
gas: previous_context.gas.as_gas(),
deposit: deposit.to_yoctonear(),
},
),
Expand Down
Loading

0 comments on commit 5a6631a

Please sign in to comment.