From cef1928fd9c1fefb1849e1d4daf652e105291f46 Mon Sep 17 00:00:00 2001 From: austinabell Date: Wed, 21 Jul 2021 11:22:08 -0400 Subject: [PATCH] refactor: remove Gas::new, aux cleanup --- examples/cross-contract-low-level/src/lib.rs | 2 +- .../fungible-token/test-contract-defi/src/lib.rs | 2 +- .../test-approval-receiver/src/lib.rs | 2 +- .../test-token-receiver/src/lib.rs | 2 +- .../src/fungible_token/core_impl.rs | 4 ++-- .../non_fungible_token/approval/approval_impl.rs | 2 +- .../src/non_fungible_token/core/core_impl.rs | 4 ++-- near-sdk-sim/src/outcome.rs | 2 +- near-sdk/src/environment/env.rs | 4 ++-- near-sdk/src/promise.rs | 4 ++-- near-sdk/src/store/lazy/mod.rs | 3 +-- near-sdk/src/store/lazy_option/mod.rs | 3 +-- near-sdk/src/types/gas.rs | 13 ++++--------- 13 files changed, 20 insertions(+), 27 deletions(-) diff --git a/examples/cross-contract-low-level/src/lib.rs b/examples/cross-contract-low-level/src/lib.rs index 40275c580..4c93ab28b 100644 --- a/examples/cross-contract-low-level/src/lib.rs +++ b/examples/cross-contract-low-level/src/lib.rs @@ -4,7 +4,7 @@ use near_sdk::serde_json::{self, json}; use near_sdk::{env, Gas, near_bindgen, AccountId, PromiseResult}; // Prepaid gas for making a single simple call. -const SINGLE_CALL_GAS: Gas = Gas::new(200000000000000); +const SINGLE_CALL_GAS: Gas = Gas(200000000000000); #[near_bindgen] #[derive(BorshDeserialize, BorshSerialize)] diff --git a/examples/fungible-token/test-contract-defi/src/lib.rs b/examples/fungible-token/test-contract-defi/src/lib.rs index 248a446cc..44f14e76c 100644 --- a/examples/fungible-token/test-contract-defi/src/lib.rs +++ b/examples/fungible-token/test-contract-defi/src/lib.rs @@ -11,7 +11,7 @@ use near_sdk::{ const BASE_GAS: u64 = 5_000_000_000_000; const PROMISE_CALL: u64 = 5_000_000_000_000; -const GAS_FOR_FT_ON_TRANSFER: Gas = Gas::new(BASE_GAS + PROMISE_CALL); +const GAS_FOR_FT_ON_TRANSFER: Gas = Gas(BASE_GAS + PROMISE_CALL); const NO_DEPOSIT: Balance = 0; diff --git a/examples/non-fungible-token/test-approval-receiver/src/lib.rs b/examples/non-fungible-token/test-approval-receiver/src/lib.rs index c5b4989a1..5da7a9595 100644 --- a/examples/non-fungible-token/test-approval-receiver/src/lib.rs +++ b/examples/non-fungible-token/test-approval-receiver/src/lib.rs @@ -11,7 +11,7 @@ use near_sdk::{ const BASE_GAS: u64 = 5_000_000_000_000; const PROMISE_CALL: u64 = 5_000_000_000_000; -const GAS_FOR_NFT_ON_APPROVE: Gas = Gas::new(BASE_GAS + PROMISE_CALL); +const GAS_FOR_NFT_ON_APPROVE: Gas = Gas(BASE_GAS + PROMISE_CALL); const NO_DEPOSIT: Balance = 0; diff --git a/examples/non-fungible-token/test-token-receiver/src/lib.rs b/examples/non-fungible-token/test-token-receiver/src/lib.rs index fc1b6e8b1..0d7cf2b10 100644 --- a/examples/non-fungible-token/test-token-receiver/src/lib.rs +++ b/examples/non-fungible-token/test-token-receiver/src/lib.rs @@ -11,7 +11,7 @@ use near_sdk::{ const BASE_GAS: u64 = 5_000_000_000_000; const PROMISE_CALL: u64 = 5_000_000_000_000; -const GAS_FOR_NFT_ON_TRANSFER: Gas = Gas::new(BASE_GAS + PROMISE_CALL); +const GAS_FOR_NFT_ON_TRANSFER: Gas = Gas(BASE_GAS + PROMISE_CALL); const NO_DEPOSIT: Balance = 0; diff --git a/near-contract-standards/src/fungible_token/core_impl.rs b/near-contract-standards/src/fungible_token/core_impl.rs index 1df8caaa7..b3530d448 100644 --- a/near-contract-standards/src/fungible_token/core_impl.rs +++ b/near-contract-standards/src/fungible_token/core_impl.rs @@ -8,8 +8,8 @@ use near_sdk::{ PromiseOrValue, PromiseResult, StorageUsage, }; -const GAS_FOR_RESOLVE_TRANSFER: Gas = Gas::new(5_000_000_000_000); -const GAS_FOR_FT_TRANSFER_CALL: Gas = Gas::new(25_000_000_000_000 + GAS_FOR_RESOLVE_TRANSFER.0); +const GAS_FOR_RESOLVE_TRANSFER: Gas = Gas(5_000_000_000_000); +const GAS_FOR_FT_TRANSFER_CALL: Gas = Gas(25_000_000_000_000 + GAS_FOR_RESOLVE_TRANSFER.0); const NO_DEPOSIT: Balance = 0; diff --git a/near-contract-standards/src/non_fungible_token/approval/approval_impl.rs b/near-contract-standards/src/non_fungible_token/approval/approval_impl.rs index a325e8cc7..68c7375ac 100644 --- a/near-contract-standards/src/non_fungible_token/approval/approval_impl.rs +++ b/near-contract-standards/src/non_fungible_token/approval/approval_impl.rs @@ -9,7 +9,7 @@ use crate::non_fungible_token::utils::{ use crate::non_fungible_token::NonFungibleToken; use near_sdk::{assert_one_yocto, env, ext_contract, AccountId, Balance, Gas, Promise}; -const GAS_FOR_NFT_APPROVE: Gas = Gas::new(10_000_000_000_000); +const GAS_FOR_NFT_APPROVE: Gas = Gas(10_000_000_000_000); const NO_DEPOSIT: Balance = 0; #[ext_contract(ext_approval_receiver)] diff --git a/near-contract-standards/src/non_fungible_token/core/core_impl.rs b/near-contract-standards/src/non_fungible_token/core/core_impl.rs index 4ba2b3e49..1251e014c 100644 --- a/near-contract-standards/src/non_fungible_token/core/core_impl.rs +++ b/near-contract-standards/src/non_fungible_token/core/core_impl.rs @@ -14,8 +14,8 @@ use near_sdk::{ }; use std::collections::HashMap; -const GAS_FOR_RESOLVE_TRANSFER: Gas = Gas::new(5_000_000_000_000); -const GAS_FOR_FT_TRANSFER_CALL: Gas = Gas::new(25_000_000_000_000 + GAS_FOR_RESOLVE_TRANSFER.0); +const GAS_FOR_RESOLVE_TRANSFER: Gas = Gas(5_000_000_000_000); +const GAS_FOR_FT_TRANSFER_CALL: Gas = Gas(25_000_000_000_000 + GAS_FOR_RESOLVE_TRANSFER.0); const NO_DEPOSIT: Balance = 0; diff --git a/near-sdk-sim/src/outcome.rs b/near-sdk-sim/src/outcome.rs index 02d115d6f..053a79945 100644 --- a/near-sdk-sim/src/outcome.rs +++ b/near-sdk-sim/src/outcome.rs @@ -139,7 +139,7 @@ impl ExecutionResult { /// The amount of the gas burnt by the given transaction or receipt. pub fn gas_burnt(&self) -> Gas { - Gas::new(self.outcome.gas_burnt) + Gas(self.outcome.gas_burnt) } /// The amount of tokens burnt corresponding to the burnt gas amount. diff --git a/near-sdk/src/environment/env.rs b/near-sdk/src/environment/env.rs index 86d7fa9f5..4dac7d0d0 100644 --- a/near-sdk/src/environment/env.rs +++ b/near-sdk/src/environment/env.rs @@ -223,12 +223,12 @@ pub fn attached_deposit() -> Balance { /// The amount of gas attached to the call that can be used to pay for the gas fees. pub fn prepaid_gas() -> Gas { - Gas::new(unsafe { sys::prepaid_gas() }) + Gas(unsafe { sys::prepaid_gas() }) } /// The gas that was already burnt during the contract execution (cannot exceed `prepaid_gas`) pub fn used_gas() -> Gas { - Gas::new(unsafe { sys::used_gas() }) + Gas(unsafe { sys::used_gas() }) } // ############ diff --git a/near-sdk/src/promise.rs b/near-sdk/src/promise.rs index de9d0f067..4035c0bc9 100644 --- a/near-sdk/src/promise.rs +++ b/near-sdk/src/promise.rs @@ -377,11 +377,11 @@ impl Promise { /// #[near_bindgen] /// impl ContractA { /// pub fn a1(&self) { - /// contract_b::b(&"bob_near".to_string(), 0, Gas::new(1_000)).as_return(); + /// contract_b::b(&"bob_near".to_string(), 0, Gas(1_000)).as_return(); /// } /// /// pub fn a2(&self) -> Promise { - /// contract_b::b(&"bob_near".to_string(), 0, Gas::new(1_000)) + /// contract_b::b(&"bob_near".to_string(), 0, Gas(1_000)) /// } /// } /// ``` diff --git a/near-sdk/src/store/lazy/mod.rs b/near-sdk/src/store/lazy/mod.rs index 75344a580..8fa190321 100644 --- a/near-sdk/src/store/lazy/mod.rs +++ b/near-sdk/src/store/lazy/mod.rs @@ -93,8 +93,7 @@ where } else { self.cache .set(CacheEntry::new_modified(Some(value))) - .ok() - .expect("cache is checked to not be filled above"); + .unwrap_or_else(|_| env::panic(b"cache is checked to not be filled above")) } } diff --git a/near-sdk/src/store/lazy_option/mod.rs b/near-sdk/src/store/lazy_option/mod.rs index 545f7da76..c00c8203c 100644 --- a/near-sdk/src/store/lazy_option/mod.rs +++ b/near-sdk/src/store/lazy_option/mod.rs @@ -72,8 +72,7 @@ where } else { self.cache .set(CacheEntry::new_modified(value)) - .ok() - .expect("cache is checked to not be filled above"); + .unwrap_or_else(|_| env::panic(b"cache is checked to not be filled above")); } } diff --git a/near-sdk/src/types/gas.rs b/near-sdk/src/types/gas.rs index f5cf4d973..e226db506 100644 --- a/near-sdk/src/types/gas.rs +++ b/near-sdk/src/types/gas.rs @@ -17,14 +17,9 @@ use serde::{de, Deserialize, Deserializer, Serialize, Serializer}; Hash, BorshSchema, )] +#[repr(transparent)] pub struct Gas(pub u64); -impl Gas { - pub const fn new(amount: u64) -> Self { - Self(amount) - } -} - impl Serialize for Gas { fn serialize(&self, serializer: S) -> Result where @@ -35,12 +30,12 @@ impl Serialize for Gas { use std::io::Write; let mut w: &mut [u8] = &mut buf; - write!(w, "{}", self.0).ok().unwrap(); + write!(w, "{}", self.0).unwrap_or_else(|_| unreachable!()); w.len() }; let len = buf.len() - remainder; - let s = std::str::from_utf8(&buf[..len]).ok().unwrap(); + let s = std::str::from_utf8(&buf[..len]).unwrap_or_else(|_| unreachable!()); serializer.serialize_str(s) } } @@ -124,7 +119,7 @@ mod tests { use super::*; fn test_json_ser(val: u64) { - let gas = Gas::new(val); + let gas = Gas(val); let ser = serde_json::to_string(&gas).unwrap(); assert_eq!(ser, format!("\"{}\"", val)); let de: Gas = serde_json::from_str(&ser).unwrap();