Skip to content

Commit

Permalink
Merge pull request #488 from near/austin/gas_update
Browse files Browse the repository at this point in the history
  • Loading branch information
austinabell authored Jul 21, 2021
2 parents 73a2ff0 + 261b43f commit 2743258
Show file tree
Hide file tree
Showing 13 changed files with 20 additions and 27 deletions.
2 changes: 1 addition & 1 deletion examples/cross-contract-low-level/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
2 changes: 1 addition & 1 deletion examples/fungible-token/test-contract-defi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion examples/non-fungible-token/test-token-receiver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
4 changes: 2 additions & 2 deletions near-contract-standards/src/fungible_token/core_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion near-sdk-sim/src/outcome.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions near-sdk/src/environment/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,12 +199,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() })
}

// ############
Expand Down
4 changes: 2 additions & 2 deletions near-sdk/src/promise.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
/// }
/// }
/// ```
Expand Down
3 changes: 1 addition & 2 deletions near-sdk/src/store/lazy/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
}
}

Expand Down
3 changes: 1 addition & 2 deletions near-sdk/src/store/lazy_option/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
}
}

Expand Down
13 changes: 4 additions & 9 deletions near-sdk/src/types/gas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
Expand All @@ -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)
}
}
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 2743258

Please sign in to comment.