From 0ab44208c3fbf837857d00d682902f8bdba8312a Mon Sep 17 00:00:00 2001 From: iho Date: Sun, 17 Sep 2023 15:19:20 +0300 Subject: [PATCH 1/5] chore: switch to near-gas where possible --- Cargo.lock | 132 ++++++++++++-- Cargo.toml | 6 + .../fund_myself_create_account/sign_as/mod.rs | 2 +- .../storage_management/storage_deposit.rs | 2 +- .../storage_management/storage_withdraw.rs | 2 +- .../call_function/as_transaction/mod.rs | 7 +- .../initialize_mode/call_function_type/mod.rs | 7 +- src/commands/tokens/send_ft/mod.rs | 7 +- src/commands/tokens/send_nft/mod.rs | 7 +- .../add_action/call_function/mod.rs | 7 +- .../add_action/call_function/mod.rs | 7 +- .../add_action/call_function/mod.rs | 7 +- src/common.rs | 162 +----------------- 13 files changed, 159 insertions(+), 196 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index fbe63c42e..efc5db29d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -74,6 +74,17 @@ dependencies = [ "opaque-debug", ] +[[package]] +name = "ahash" +version = "0.7.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fcb51a0695d8f838b1ee009b3fbf66bda078cd64590202a864a8f3e8c4315c47" +dependencies = [ + "getrandom 0.2.10", + "once_cell", + "version_check", +] + [[package]] name = "ahash" version = "0.8.3" @@ -464,29 +475,63 @@ dependencies = [ "log", ] +[[package]] +name = "borsh" +version = "0.9.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "15bf3650200d8bffa99015595e10f1fbd17de07abbc25bb067da79e769939bfa" +dependencies = [ + "borsh-derive 0.9.3", + "hashbrown 0.11.2", +] + [[package]] name = "borsh" version = "0.10.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4114279215a005bc675e386011e594e1d9b800918cea18fcadadcce864a2046b" dependencies = [ - "borsh-derive", + "borsh-derive 0.10.3", "hashbrown 0.13.2", ] +[[package]] +name = "borsh-derive" +version = "0.9.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6441c552f230375d18e3cc377677914d2ca2b0d36e52129fe15450a2dce46775" +dependencies = [ + "borsh-derive-internal 0.9.3", + "borsh-schema-derive-internal 0.9.3", + "proc-macro-crate 0.1.5", + "proc-macro2", + "syn 1.0.109", +] + [[package]] name = "borsh-derive" version = "0.10.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0754613691538d51f329cce9af41d7b7ca150bc973056f1156611489475f54f7" dependencies = [ - "borsh-derive-internal", - "borsh-schema-derive-internal", + "borsh-derive-internal 0.10.3", + "borsh-schema-derive-internal 0.10.3", "proc-macro-crate 0.1.5", "proc-macro2", "syn 1.0.109", ] +[[package]] +name = "borsh-derive-internal" +version = "0.9.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5449c28a7b352f2d1e592a8a28bf139bc71afb0764a14f3c02500935d8c44065" +dependencies = [ + "proc-macro2", + "quote", + "syn 1.0.109", +] + [[package]] name = "borsh-derive-internal" version = "0.10.3" @@ -498,6 +543,17 @@ dependencies = [ "syn 1.0.109", ] +[[package]] +name = "borsh-schema-derive-internal" +version = "0.9.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cdbd5696d8bfa21d53d9fe39a714a18538bad11492a42d066dbbc395fb1951c0" +dependencies = [ + "proc-macro2", + "quote", + "syn 1.0.109", +] + [[package]] name = "borsh-schema-derive-internal" version = "0.10.3" @@ -1441,6 +1497,15 @@ dependencies = [ "tracing", ] +[[package]] +name = "hashbrown" +version = "0.11.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ab5ef0d4909ef3724cc8cce6ccc8572c5c817592e9285f5464f8e86f8bd3726e" +dependencies = [ + "ahash 0.7.6", +] + [[package]] name = "hashbrown" version = "0.12.3" @@ -1453,7 +1518,7 @@ version = "0.13.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "43a3c133739dddd0d2990f9a4bdf8eb4b21ef50e4851ca85ab661199821d510e" dependencies = [ - "ahash", + "ahash 0.8.3", ] [[package]] @@ -2040,7 +2105,7 @@ version = "0.17.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dc0cb40869cab7f5232f934f45db35bffe0f2d2a7cb0cd0346202fbe4ebf2dd7" dependencies = [ - "borsh", + "borsh 0.10.3", "serde", ] @@ -2088,6 +2153,7 @@ dependencies = [ "keyring", "linked-hash-map", "near-crypto", + "near-gas", "near-jsonrpc-client", "near-jsonrpc-primitives", "near-ledger", @@ -2131,7 +2197,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ff6b382b626e7e0cd372d027c6672ac97b4b6ee6114288c9e58d8180b935d315" dependencies = [ "blake2", - "borsh", + "borsh 0.10.3", "bs58 0.4.0", "c2-chacha", "curve25519-dalek", @@ -2160,13 +2226,24 @@ dependencies = [ "near-primitives-core", ] +[[package]] +name = "near-gas" +version = "0.2.2" +source = "git+https://github.com/iho/near-gas.git?rev=b62074fe945c31a6a3807ac869f952784b9a3871#b62074fe945c31a6a3807ac869f952784b9a3871" +dependencies = [ + "borsh 0.9.3", + "interactive-clap", + "schemars", + "serde", +] + [[package]] name = "near-jsonrpc-client" version = "0.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "118f44c02ad211db805c1370ad3ff26576af6ff554093c9fece1b835d29d233a" dependencies = [ - "borsh", + "borsh 0.10.3", "lazy_static", "log", "near-chain-configs", @@ -2241,7 +2318,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "05f7051aaf199adc4d068620fca6d5f70f906a1540d03a8bb3701271f8881835" dependencies = [ "arbitrary", - "borsh", + "borsh 0.10.3", "bytesize", "cfg-if", "chrono", @@ -2279,7 +2356,7 @@ checksum = "775fec19ef51a341abdbf792a9dda5b4cb89f488f681b2fd689b9321d24db47b" dependencies = [ "arbitrary", "base64 0.21.2", - "borsh", + "borsh 0.10.3", "bs58 0.4.0", "derive_more", "enum-map", @@ -2328,7 +2405,7 @@ version = "0.17.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ec545d1bede0579e7c15dd2dce9b998dc975c52f2165702ff40bec7ff69728bb" dependencies = [ - "borsh", + "borsh 0.10.3", "near-account-id", "near-rpc-error-macro", "serde", @@ -3172,6 +3249,30 @@ dependencies = [ "windows-sys 0.42.0", ] +[[package]] +name = "schemars" +version = "0.8.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "763f8cd0d4c71ed8389c90cb8100cba87e763bd01a8e614d4f0af97bcd50a161" +dependencies = [ + "dyn-clone", + "schemars_derive", + "serde", + "serde_json", +] + +[[package]] +name = "schemars_derive" +version = "0.8.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec0f696e21e10fa546b7ffb1c9672c6de8fbc7a81acf59524386d8639bf12737" +dependencies = [ + "proc-macro2", + "quote", + "serde_derive_internals", + "syn 1.0.109", +] + [[package]] name = "scopeguard" version = "1.1.0" @@ -3286,6 +3387,17 @@ dependencies = [ "syn 2.0.18", ] +[[package]] +name = "serde_derive_internals" +version = "0.26.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85bf8229e7920a9f636479437026331ce11aa132b4dde37d121944a44d6e5f3c" +dependencies = [ + "proc-macro2", + "quote", + "syn 1.0.109", +] + [[package]] name = "serde_json" version = "1.0.96" diff --git a/Cargo.toml b/Cargo.toml index 8037acbb5..424be31ae 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -63,6 +63,12 @@ near-crypto = "0.17.0" near-primitives = "0.17.0" near-jsonrpc-client = "0.6.0" near-jsonrpc-primitives = "0.17.0" +near-gas = { git = "https://github.com/iho/near-gas.git", rev = "b62074fe945c31a6a3807ac869f952784b9a3871", features = [ + "serde", + "borsh", + "schemars", + "interactive-clap", +] } interactive-clap = "0.2.4" interactive-clap-derive = "0.2.4" diff --git a/src/commands/account/create_account/fund_myself_create_account/sign_as/mod.rs b/src/commands/account/create_account/fund_myself_create_account/sign_as/mod.rs index 30c070982..f26a46feb 100644 --- a/src/commands/account/create_account/fund_myself_create_account/sign_as/mod.rs +++ b/src/commands/account/create_account/fund_myself_create_account/sign_as/mod.rs @@ -101,7 +101,7 @@ impl From for crate::commands::ActionContext { args, gas: crate::common::NearGas::from_str("30 TeraGas") .unwrap() - .inner, + .as_gas(), deposit: item .account_properties .initial_balance diff --git a/src/commands/account/storage_management/storage_deposit.rs b/src/commands/account/storage_management/storage_deposit.rs index 28fb41c0e..b0fdf0b0e 100644 --- a/src/commands/account/storage_management/storage_deposit.rs +++ b/src/commands/account/storage_management/storage_deposit.rs @@ -124,7 +124,7 @@ impl SignerAccountIdContext { .into_bytes(), gas: crate::common::NearGas::from_str("50 TeraGas") .unwrap() - .inner, + .as_gas(), deposit: deposit.to_yoctonear(), }, )], diff --git a/src/commands/account/storage_management/storage_withdraw.rs b/src/commands/account/storage_management/storage_withdraw.rs index 0e3fbb74e..4ea03900f 100644 --- a/src/commands/account/storage_management/storage_withdraw.rs +++ b/src/commands/account/storage_management/storage_withdraw.rs @@ -72,7 +72,7 @@ impl SignerAccountIdContext { .into_bytes(), gas: crate::common::NearGas::from_str("50 TeraGas") .unwrap() - .inner, + .as_gas(), deposit: crate::common::NearBalance::from_yoctonear(1) .to_yoctonear(), }, diff --git a/src/commands/contract/call_function/as_transaction/mod.rs b/src/commands/contract/call_function/as_transaction/mod.rs index a12d9cd90..b7587511c 100644 --- a/src/commands/contract/call_function/as_transaction/mod.rs +++ b/src/commands/contract/call_function/as_transaction/mod.rs @@ -113,8 +113,7 @@ impl PrepaidGas { .prompt()?, ) { Ok(input_gas) => { - let crate::common::NearGas { inner: num } = input_gas; - let gas = num; + let gas = input_gas.as_gas(); if gas <= 300000000000000 { break gas; } else { @@ -124,7 +123,7 @@ impl PrepaidGas { Err(err) => return Err(color_eyre::Report::msg(err)), } }; - Ok(Some(gas.into())) + Ok(Some(near_gas::NearGas::from_gas(gas))) } } @@ -239,7 +238,7 @@ impl From 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(), }, )], diff --git a/src/commands/contract/deploy/initialize_mode/call_function_type/mod.rs b/src/commands/contract/deploy/initialize_mode/call_function_type/mod.rs index 02d3ba9fc..abe1efc2c 100644 --- a/src/commands/contract/deploy/initialize_mode/call_function_type/mod.rs +++ b/src/commands/contract/deploy/initialize_mode/call_function_type/mod.rs @@ -113,8 +113,7 @@ impl PrepaidGas { .prompt()?, ) { Ok(input_gas) => { - let crate::common::NearGas { inner: num } = input_gas; - let gas = num; + let gas = input_gas.as_gas(); if gas <= 300000000000000 { break gas; } else { @@ -124,7 +123,7 @@ impl PrepaidGas { Err(err) => return Err(color_eyre::Report::msg(err)), } }; - Ok(Some(gas.into())) + Ok(Some(near_gas::NearGas::from_gas(gas))) } } @@ -169,7 +168,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(), }, ), diff --git a/src/commands/tokens/send_ft/mod.rs b/src/commands/tokens/send_ft/mod.rs index c74477360..4ade6bd9e 100644 --- a/src/commands/tokens/send_ft/mod.rs +++ b/src/commands/tokens/send_ft/mod.rs @@ -77,7 +77,7 @@ impl From for crate::commands::ActionContext { }) .to_string() .into_bytes(), - gas: item.gas.inner, + gas: item.gas.as_gas(), deposit: item.deposit.to_yoctonear(), }, )], @@ -150,8 +150,7 @@ impl SendFtCommand { .prompt()?, ) { Ok(input_gas) => { - let crate::common::NearGas { inner: num } = input_gas; - let gas = num; + let gas = input_gas.as_gas(); if gas <= 300000000000000 { break gas; } else { @@ -161,7 +160,7 @@ impl SendFtCommand { Err(err) => return Err(color_eyre::Report::msg(err)), } }; - Ok(Some(gas.into())) + Ok(Some(near_gas::NearGas::from_gas(gas))) } fn input_deposit( diff --git a/src/commands/tokens/send_nft/mod.rs b/src/commands/tokens/send_nft/mod.rs index eaac90f71..4c18d13d1 100644 --- a/src/commands/tokens/send_nft/mod.rs +++ b/src/commands/tokens/send_nft/mod.rs @@ -78,7 +78,7 @@ impl From for crate::commands::ActionContext { }) .to_string() .into_bytes(), - gas: item.gas.inner, + gas: item.gas.as_gas(), deposit: item.deposit.to_yoctonear(), }, )], @@ -151,8 +151,7 @@ impl SendNftCommand { .prompt()?, ) { Ok(input_gas) => { - let crate::common::NearGas { inner: num } = input_gas; - let gas = num; + let gas = input_gas.as_gas(); if gas <= 300000000000000 { break gas; } else { @@ -162,7 +161,7 @@ impl SendNftCommand { Err(err) => return Err(color_eyre::Report::msg(err)), } }; - Ok(Some(gas.into())) + Ok(Some(near_gas::NearGas::from_gas(gas))) } fn input_deposit( diff --git a/src/commands/transaction/construct_transaction/add_action_1/add_action/call_function/mod.rs b/src/commands/transaction/construct_transaction/add_action_1/add_action/call_function/mod.rs index 270cc6207..01075e5ef 100644 --- a/src/commands/transaction/construct_transaction/add_action_1/add_action/call_function/mod.rs +++ b/src/commands/transaction/construct_transaction/add_action_1/add_action/call_function/mod.rs @@ -114,8 +114,7 @@ impl PrepaidGas { .prompt()?, ) { Ok(input_gas) => { - let crate::common::NearGas { inner: num } = input_gas; - let gas = num; + let gas = input_gas.as_gas(); if gas <= 300000000000000 { break gas; } else { @@ -125,7 +124,7 @@ impl PrepaidGas { Err(err) => return Err(color_eyre::Report::msg(err)), } }; - Ok(Some(gas.into())) + Ok(Some(near_gas::NearGas::from_gas(gas))) } } @@ -152,7 +151,7 @@ impl DepositContext { near_primitives::transaction::FunctionCallAction { method_name: previous_context.function_name, args: previous_context.function_args, - gas: previous_context.gas.inner, + gas: previous_context.gas.as_gas(), deposit: scope.deposit.clone().to_yoctonear(), }, ); diff --git a/src/commands/transaction/construct_transaction/add_action_2/add_action/call_function/mod.rs b/src/commands/transaction/construct_transaction/add_action_2/add_action/call_function/mod.rs index f08182b75..77dfa195b 100644 --- a/src/commands/transaction/construct_transaction/add_action_2/add_action/call_function/mod.rs +++ b/src/commands/transaction/construct_transaction/add_action_2/add_action/call_function/mod.rs @@ -114,8 +114,7 @@ impl PrepaidGas { .prompt()?, ) { Ok(input_gas) => { - let crate::common::NearGas { inner: num } = input_gas; - let gas = num; + let gas = input_gas.as_gas(); if gas <= 300000000000000 { break gas; } else { @@ -125,7 +124,7 @@ impl PrepaidGas { Err(err) => return Err(color_eyre::Report::msg(err)), } }; - Ok(Some(gas.into())) + Ok(Some(near_gas::NearGas::from_gas(gas))) } } @@ -152,7 +151,7 @@ impl DepositContext { near_primitives::transaction::FunctionCallAction { method_name: previous_context.function_name, args: previous_context.function_args, - gas: previous_context.gas.inner, + gas: previous_context.gas.as_gas(), deposit: scope.deposit.clone().to_yoctonear(), }, ); diff --git a/src/commands/transaction/construct_transaction/add_action_3/add_action/call_function/mod.rs b/src/commands/transaction/construct_transaction/add_action_3/add_action/call_function/mod.rs index f0126db45..56bf38604 100644 --- a/src/commands/transaction/construct_transaction/add_action_3/add_action/call_function/mod.rs +++ b/src/commands/transaction/construct_transaction/add_action_3/add_action/call_function/mod.rs @@ -114,8 +114,7 @@ impl PrepaidGas { .prompt()?, ) { Ok(input_gas) => { - let crate::common::NearGas { inner: num } = input_gas; - let gas = num; + let gas = input_gas.as_gas(); if gas <= 300000000000000 { break gas; } else { @@ -125,7 +124,7 @@ impl PrepaidGas { Err(err) => return Err(color_eyre::Report::msg(err)), } }; - Ok(Some(gas.into())) + Ok(Some(crate::common::NearGas::from_gas(gas))) } } @@ -152,7 +151,7 @@ impl DepositContext { near_primitives::transaction::FunctionCallAction { method_name: previous_context.function_name, args: previous_context.function_args, - gas: previous_context.gas.inner, + gas: previous_context.gas.as_gas(), deposit: scope.deposit.clone().to_yoctonear(), }, ); diff --git a/src/common.rs b/src/common.rs index f93f84bf6..ac63ada13 100644 --- a/src/common.rs +++ b/src/common.rs @@ -163,93 +163,7 @@ impl interactive_clap::ToCli for NearBalance { type CliVariant = NearBalance; } -const ONE_TERA_GAS: u64 = 10u64.pow(12); -const ONE_GIGA_GAS: u64 = 10u64.pow(9); - -#[derive(Debug, Clone, Default, PartialEq, Eq)] -pub struct NearGas { - pub inner: u64, -} - -impl std::fmt::Display for NearGas { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - if self.inner == 0 { - write!(f, "0 Gas") - } else if self.inner >= ONE_TERA_GAS { - write!( - f, - "{}.{:0>3} TeraGas", - self.inner / ONE_TERA_GAS, - self.inner / (ONE_TERA_GAS / 1000) % 1000 - ) - } else { - write!( - f, - "{}.{:0>3} GigaGas", - self.inner / ONE_GIGA_GAS, - self.inner / (ONE_GIGA_GAS / 1000) % 1000 - ) - } - } -} - -impl std::str::FromStr for NearGas { - type Err = String; - fn from_str(s: &str) -> Result { - let num = s.trim().trim_end_matches(char::is_alphabetic).trim(); - let currency = s.trim().trim_start_matches(num).trim().to_uppercase(); - let number = match currency.as_str() { - "T" | "TGAS" | "TERAGAS" => NearGas::into_tera_gas(num)?, - "GIGAGAS" | "GGAS" => NearGas::into_tera_gas(num)? / 1000, - _ => return Err("Near Gas: incorrect currency value entered".to_string()), - }; - Ok(NearGas { inner: number }) - } -} - -impl From for NearGas { - fn from(num: u64) -> Self { - Self { inner: num } - } -} - -impl NearGas { - fn into_tera_gas(num: &str) -> Result { - let res_split: Vec<&str> = num.split('.').collect(); - match res_split.len() { - 2 => { - let num_int_gas: u64 = res_split[0] - .parse::() - .map_err(|err| format!("Near Gas: {}", err))? - .checked_mul(10u64.pow(12)) - .ok_or("Near Gas: underflow or overflow happens")?; - let len_fract = res_split[1].len() as u32; - let num_fract_gas = if len_fract <= 12 { - res_split[1] - .parse::() - .map_err(|err| format!("Near Gas: {}", err))? - .checked_mul(10u64.pow(12 - res_split[1].len() as u32)) - .ok_or("Near Gas: underflow or overflow happens")? - } else { - return Err("Near Gas: too large fractional part of a number".to_string()); - }; - Ok(num_int_gas - .checked_add(num_fract_gas) - .ok_or("Near Gas: underflow or overflow happens")?) - } - 1 => Ok(res_split[0] - .parse::() - .map_err(|err| format!("Near Gas: {}", err))? - .checked_mul(10u64.pow(12)) - .ok_or("Near Gas: underflow or overflow happens")?), - _ => Err("Near Gas: incorrect number entered".to_string()), - } - } -} - -impl interactive_clap::ToCli for NearGas { - type CliVariant = NearGas; -} +pub use near_gas::NearGas; #[derive(Debug, Clone, Default, PartialEq, Eq, PartialOrd)] pub struct TransferAmount { @@ -754,9 +668,7 @@ pub fn print_unsigned_transaction(transaction: &crate::commands::PrepopulatedTra "{:>18} {:<13} {}", "", "gas:", - crate::common::NearGas { - inner: function_call_action.gas - } + crate::common::NearGas::from_gas(function_call_action.gas) ); eprintln!( "{:>18} {:<13} {}", @@ -2266,89 +2178,29 @@ mod tests { fn near_gas_from_str_currency_tgas() { assert_eq!( NearGas::from_str("10 tgas").unwrap(), - NearGas { - inner: 10000000000000 // 14 digits - } + NearGas::from_gas(10000000000000) // 14 digits ); assert_eq!( NearGas::from_str("10.055TERAGAS").unwrap(), - NearGas { - inner: 10055000000000 // 14 digits - } + NearGas::from_gas(10055000000000) // 14 digits ); } #[test] fn near_gas_from_str_currency_gigagas() { assert_eq!( NearGas::from_str("10 gigagas").unwrap(), - NearGas { inner: 10000000000 } // 11 digits + NearGas::from_gas(10000000000) // 11 digits ); assert_eq!( NearGas::from_str("10GGAS ").unwrap(), - NearGas { inner: 10000000000 } // 11 digits + NearGas::from_gas(10000000000) // 11 digits ); } #[test] fn near_gas_from_str_f64_tgas() { assert_eq!( NearGas::from_str("0.000001 tgas").unwrap(), - NearGas { inner: 1000000 } // 7 digits - ); - } - #[test] - fn near_gas_from_str_f64_gas_without_int() { - let near_gas = NearGas::from_str(".055ggas"); - assert_eq!( - near_gas, - Err("Near Gas: cannot parse integer from empty string".to_string()) - ); - } - #[test] - fn near_gas_from_str_without_currency() { - let near_gas = NearGas::from_str("100"); - assert_eq!( - near_gas, - Err("Near Gas: incorrect currency value entered".to_string()) - ); - } - #[test] - fn near_gas_from_str_incorrect_currency() { - let near_gas = NearGas::from_str("100 UAH"); - assert_eq!( - near_gas, - Err("Near Gas: incorrect currency value entered".to_string()) - ); - } - #[test] - fn near_gas_from_str_invalid_double_dot() { - let near_gas = NearGas::from_str("100.55."); - assert_eq!( - near_gas, - Err("Near Gas: incorrect currency value entered".to_string()) - ); - } - #[test] - fn near_gas_from_str_large_fractional_part() { - let near_gas = NearGas::from_str("100.1111122222333 ggas"); // 13 digits after "." - assert_eq!( - near_gas, - Err("Near Gas: too large fractional part of a number".to_string()) - ); - } - #[test] - fn near_gas_from_str_large_int_part() { - let near_gas = NearGas::from_str("200123456789123.0 tgas"); - assert_eq!( - near_gas, - Err("Near Gas: underflow or overflow happens".to_string()) - ); - } - #[test] - fn near_gas_from_str_negative_value() { - let near_gas = NearGas::from_str("-100 ggas"); - assert_eq!( - near_gas, - Err("Near Gas: invalid digit found in string".to_string()) + NearGas::from_gas(1000000) // 7 digits ); } } From 153624f742ffe19e556f291c9683315481b4bbb7 Mon Sep 17 00:00:00 2001 From: iho Date: Sun, 17 Sep 2023 15:31:15 +0300 Subject: [PATCH 2/5] chore: simplify code --- .../contract/call_function/as_transaction/mod.rs | 9 ++++----- .../deploy/initialize_mode/call_function_type/mod.rs | 9 ++++----- src/commands/tokens/send_ft/mod.rs | 9 ++++----- src/commands/tokens/send_nft/mod.rs | 9 ++++----- .../add_action_1/add_action/call_function/mod.rs | 9 ++++----- .../add_action_2/add_action/call_function/mod.rs | 9 ++++----- .../add_action_3/add_action/call_function/mod.rs | 9 ++++----- 7 files changed, 28 insertions(+), 35 deletions(-) diff --git a/src/commands/contract/call_function/as_transaction/mod.rs b/src/commands/contract/call_function/as_transaction/mod.rs index b7587511c..094d8d035 100644 --- a/src/commands/contract/call_function/as_transaction/mod.rs +++ b/src/commands/contract/call_function/as_transaction/mod.rs @@ -106,16 +106,15 @@ impl PrepaidGas { _context: &CallFunctionPropertiesContext, ) -> color_eyre::eyre::Result> { 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 gas = input_gas.as_gas(); - 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") } @@ -123,7 +122,7 @@ impl PrepaidGas { Err(err) => return Err(color_eyre::Report::msg(err)), } }; - Ok(Some(near_gas::NearGas::from_gas(gas))) + Ok(Some(gas)) } } diff --git a/src/commands/contract/deploy/initialize_mode/call_function_type/mod.rs b/src/commands/contract/deploy/initialize_mode/call_function_type/mod.rs index abe1efc2c..9722e371a 100644 --- a/src/commands/contract/deploy/initialize_mode/call_function_type/mod.rs +++ b/src/commands/contract/deploy/initialize_mode/call_function_type/mod.rs @@ -106,16 +106,15 @@ impl PrepaidGas { _context: &CallFunctionActionContext, ) -> color_eyre::eyre::Result> { 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 gas = input_gas.as_gas(); - 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") } @@ -123,7 +122,7 @@ impl PrepaidGas { Err(err) => return Err(color_eyre::Report::msg(err)), } }; - Ok(Some(near_gas::NearGas::from_gas(gas))) + Ok(Some(gas)) } } diff --git a/src/commands/tokens/send_ft/mod.rs b/src/commands/tokens/send_ft/mod.rs index 4ade6bd9e..d28aca196 100644 --- a/src/commands/tokens/send_ft/mod.rs +++ b/src/commands/tokens/send_ft/mod.rs @@ -143,16 +143,15 @@ impl SendFtCommand { _context: &super::TokensCommandsContext, ) -> color_eyre::eyre::Result> { 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 gas = input_gas.as_gas(); - 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") } @@ -160,7 +159,7 @@ impl SendFtCommand { Err(err) => return Err(color_eyre::Report::msg(err)), } }; - Ok(Some(near_gas::NearGas::from_gas(gas))) + Ok(Some(gas)) } fn input_deposit( diff --git a/src/commands/tokens/send_nft/mod.rs b/src/commands/tokens/send_nft/mod.rs index 4c18d13d1..649359cfa 100644 --- a/src/commands/tokens/send_nft/mod.rs +++ b/src/commands/tokens/send_nft/mod.rs @@ -144,16 +144,15 @@ impl SendNftCommand { _context: &super::TokensCommandsContext, ) -> color_eyre::eyre::Result> { 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 gas = input_gas.as_gas(); - 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") } @@ -161,7 +160,7 @@ impl SendNftCommand { Err(err) => return Err(color_eyre::Report::msg(err)), } }; - Ok(Some(near_gas::NearGas::from_gas(gas))) + Ok(Some(gas)) } fn input_deposit( diff --git a/src/commands/transaction/construct_transaction/add_action_1/add_action/call_function/mod.rs b/src/commands/transaction/construct_transaction/add_action_1/add_action/call_function/mod.rs index 01075e5ef..b82316552 100644 --- a/src/commands/transaction/construct_transaction/add_action_1/add_action/call_function/mod.rs +++ b/src/commands/transaction/construct_transaction/add_action_1/add_action/call_function/mod.rs @@ -107,16 +107,15 @@ impl PrepaidGas { _context: &FunctionCallActionContext, ) -> color_eyre::eyre::Result> { 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 gas = input_gas.as_gas(); - 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") } @@ -124,7 +123,7 @@ impl PrepaidGas { Err(err) => return Err(color_eyre::Report::msg(err)), } }; - Ok(Some(near_gas::NearGas::from_gas(gas))) + Ok(Some(gas)) } } diff --git a/src/commands/transaction/construct_transaction/add_action_2/add_action/call_function/mod.rs b/src/commands/transaction/construct_transaction/add_action_2/add_action/call_function/mod.rs index 77dfa195b..db1ffac52 100644 --- a/src/commands/transaction/construct_transaction/add_action_2/add_action/call_function/mod.rs +++ b/src/commands/transaction/construct_transaction/add_action_2/add_action/call_function/mod.rs @@ -107,16 +107,15 @@ impl PrepaidGas { _context: &FunctionCallActionContext, ) -> color_eyre::eyre::Result> { 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 gas = input_gas.as_gas(); - 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") } @@ -124,7 +123,7 @@ impl PrepaidGas { Err(err) => return Err(color_eyre::Report::msg(err)), } }; - Ok(Some(near_gas::NearGas::from_gas(gas))) + Ok(Some(gas)) } } diff --git a/src/commands/transaction/construct_transaction/add_action_3/add_action/call_function/mod.rs b/src/commands/transaction/construct_transaction/add_action_3/add_action/call_function/mod.rs index 56bf38604..14680e676 100644 --- a/src/commands/transaction/construct_transaction/add_action_3/add_action/call_function/mod.rs +++ b/src/commands/transaction/construct_transaction/add_action_3/add_action/call_function/mod.rs @@ -107,16 +107,15 @@ impl PrepaidGas { _context: &FunctionCallActionContext, ) -> color_eyre::eyre::Result> { 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 gas = input_gas.as_gas(); - 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") } @@ -124,7 +123,7 @@ impl PrepaidGas { Err(err) => return Err(color_eyre::Report::msg(err)), } }; - Ok(Some(crate::common::NearGas::from_gas(gas))) + Ok(Some(gas)) } } From 6b066f9c03e5a18a6a143ee8021c916276246cb8 Mon Sep 17 00:00:00 2001 From: iho Date: Sun, 17 Sep 2023 15:34:35 +0300 Subject: [PATCH 3/5] chore: move tests to near-gas --- src/common.rs | 30 ------------------------------ 1 file changed, 30 deletions(-) diff --git a/src/common.rs b/src/common.rs index ac63ada13..10bcb22f7 100644 --- a/src/common.rs +++ b/src/common.rs @@ -2173,34 +2173,4 @@ mod tests { Err("Near Balance: invalid digit found in string".to_string()) ); } - - #[test] - fn near_gas_from_str_currency_tgas() { - assert_eq!( - NearGas::from_str("10 tgas").unwrap(), - NearGas::from_gas(10000000000000) // 14 digits - ); - assert_eq!( - NearGas::from_str("10.055TERAGAS").unwrap(), - NearGas::from_gas(10055000000000) // 14 digits - ); - } - #[test] - fn near_gas_from_str_currency_gigagas() { - assert_eq!( - NearGas::from_str("10 gigagas").unwrap(), - NearGas::from_gas(10000000000) // 11 digits - ); - assert_eq!( - NearGas::from_str("10GGAS ").unwrap(), - NearGas::from_gas(10000000000) // 11 digits - ); - } - #[test] - fn near_gas_from_str_f64_tgas() { - assert_eq!( - NearGas::from_str("0.000001 tgas").unwrap(), - NearGas::from_gas(1000000) // 7 digits - ); - } } From a3735d2e5f5ed9ae9919c86571dfd95537cd1e02 Mon Sep 17 00:00:00 2001 From: iho Date: Sun, 24 Sep 2023 15:52:17 +0300 Subject: [PATCH 4/5] chore: bump near-gas version --- Cargo.lock | 2 +- Cargo.toml | 3 ++- .../fund_myself_create_account/sign_as/mod.rs | 6 +----- src/commands/account/storage_management/storage_deposit.rs | 6 +----- src/commands/account/storage_management/storage_withdraw.rs | 6 +----- 5 files changed, 6 insertions(+), 17 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index efc5db29d..cf298a1be 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2229,7 +2229,7 @@ dependencies = [ [[package]] name = "near-gas" version = "0.2.2" -source = "git+https://github.com/iho/near-gas.git?rev=b62074fe945c31a6a3807ac869f952784b9a3871#b62074fe945c31a6a3807ac869f952784b9a3871" +source = "git+https://github.com/Mr0melian/near-gas.git?rev=e6cb97e25064231f5e34c96ea4887f75169fe4cb#e6cb97e25064231f5e34c96ea4887f75169fe4cb" dependencies = [ "borsh 0.9.3", "interactive-clap", diff --git a/Cargo.toml b/Cargo.toml index 424be31ae..49995e90e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -63,7 +63,8 @@ near-crypto = "0.17.0" near-primitives = "0.17.0" near-jsonrpc-client = "0.6.0" near-jsonrpc-primitives = "0.17.0" -near-gas = { git = "https://github.com/iho/near-gas.git", rev = "b62074fe945c31a6a3807ac869f952784b9a3871", features = [ + +near-gas = { git = "https://github.com/Mr0melian/near-gas.git", rev = "e6cb97e25064231f5e34c96ea4887f75169fe4cb", features = [ "serde", "borsh", "schemars", diff --git a/src/commands/account/create_account/fund_myself_create_account/sign_as/mod.rs b/src/commands/account/create_account/fund_myself_create_account/sign_as/mod.rs index f26a46feb..293236aee 100644 --- a/src/commands/account/create_account/fund_myself_create_account/sign_as/mod.rs +++ b/src/commands/account/create_account/fund_myself_create_account/sign_as/mod.rs @@ -1,5 +1,3 @@ -use std::str::FromStr; - use serde_json::json; #[derive(Debug, Clone, interactive_clap::InteractiveClap)] @@ -99,9 +97,7 @@ impl From for crate::commands::ActionContext { near_primitives::transaction::FunctionCallAction { method_name: "create_account".to_string(), args, - gas: crate::common::NearGas::from_str("30 TeraGas") - .unwrap() - .as_gas(), + gas: crate::common::NearGas::from_tgas(30).as_gas(), deposit: item .account_properties .initial_balance diff --git a/src/commands/account/storage_management/storage_deposit.rs b/src/commands/account/storage_management/storage_deposit.rs index b0fdf0b0e..816ae34bb 100644 --- a/src/commands/account/storage_management/storage_deposit.rs +++ b/src/commands/account/storage_management/storage_deposit.rs @@ -1,5 +1,3 @@ -use std::str::FromStr; - use inquire::Select; #[derive(Debug, Clone, interactive_clap::InteractiveClap)] @@ -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() - .as_gas(), + gas: crate::common::NearGas::from_tgas(50).as_gas(), deposit: deposit.to_yoctonear(), }, )], diff --git a/src/commands/account/storage_management/storage_withdraw.rs b/src/commands/account/storage_management/storage_withdraw.rs index 4ea03900f..00d3d5f01 100644 --- a/src/commands/account/storage_management/storage_withdraw.rs +++ b/src/commands/account/storage_management/storage_withdraw.rs @@ -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)] @@ -70,9 +68,7 @@ impl SignerAccountIdContext { }) .to_string() .into_bytes(), - gas: crate::common::NearGas::from_str("50 TeraGas") - .unwrap() - .as_gas(), + gas: crate::common::NearGas::from_tgas(50).as_gas(), deposit: crate::common::NearBalance::from_yoctonear(1) .to_yoctonear(), }, From a1d98cab2695cb885303e4034f3f8e706f1dab9d Mon Sep 17 00:00:00 2001 From: iho Date: Sun, 24 Sep 2023 20:56:13 +0300 Subject: [PATCH 5/5] chore: bump near-gas version --- Cargo.lock | 5 +++-- Cargo.toml | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index cf298a1be..4d8d094f7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2228,8 +2228,9 @@ dependencies = [ [[package]] name = "near-gas" -version = "0.2.2" -source = "git+https://github.com/Mr0melian/near-gas.git?rev=e6cb97e25064231f5e34c96ea4887f75169fe4cb#e6cb97e25064231f5e34c96ea4887f75169fe4cb" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0dac82d80f9dba5d879876ccee0fc8f4d91ca82552bad545e9f9714c5da49e4c" dependencies = [ "borsh 0.9.3", "interactive-clap", diff --git a/Cargo.toml b/Cargo.toml index 49995e90e..605b9b7ff 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -64,7 +64,7 @@ near-primitives = "0.17.0" near-jsonrpc-client = "0.6.0" near-jsonrpc-primitives = "0.17.0" -near-gas = { git = "https://github.com/Mr0melian/near-gas.git", rev = "e6cb97e25064231f5e34c96ea4887f75169fe4cb", features = [ +near-gas = { version = "0.2.3", features = [ "serde", "borsh", "schemars",