Skip to content

Commit 09909a3

Browse files
committed
feat: assert gas minimums on each fn
1 parent 26e41a9 commit 09909a3

File tree

4 files changed

+21
-21
lines changed

4 files changed

+21
-21
lines changed

common/src/vault.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use near_sdk::{json_types::U128, near, AccountId, Gas, Promise, PromiseOrValue};
1+
use near_sdk::{env, json_types::U128, near, require, AccountId, Gas, Promise, PromiseOrValue};
22

33
use crate::asset::{BorrowAsset, FungibleAsset};
44

@@ -154,6 +154,14 @@ pub const ALLOCATE_GAS: Gas = buffer(21);
154154
pub const WITHDRAW_GAS: Gas = buffer(4);
155155
pub const EXECUTE_WITHDRAW_GAS: Gas = buffer(9);
156156

157+
pub fn require_at_least(needed: Gas) {
158+
let gas = env::prepaid_gas();
159+
require!(
160+
gas >= needed,
161+
format!("Insufficient gas: {}, needed: {needed}", gas)
162+
);
163+
}
164+
157165
#[near_sdk::ext_contract(ext_self)]
158166
pub trait Callbacks {
159167
fn after_supply_1_check(&mut self, op_id: u64, market_index: u32, attempted: U128) -> bool;

contract/vault/examples/gas_report.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,7 @@ async fn main() {
3838

3939
(supply_gas, allocation_gas)
4040
});
41-
let results = futures::future::join_all(futures)
42-
.await
43-
.expect("All futures failed")
44-
.into_iter();
41+
let results = futures::future::join_all(futures).await.into_iter();
4542

4643
let mut supply_gas_average = 0f64;
4744
let mut allocation_gas_average = 0f64;

contract/vault/src/impl_token_receiver.rs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::{aux::ReturnStyle, Contract, ContractExt, OpState};
22
use near_contract_standards::fungible_token::receiver::FungibleTokenReceiver;
33
use near_sdk::{env, json_types::U128, near, require, AccountId, PromiseOrValue};
4-
use templar_common::vault::{AllocationMode, DepositMsg, Event, SUPPLY_GAS};
4+
use templar_common::vault::{require_at_least, AllocationMode, DepositMsg, Event, SUPPLY_GAS};
55

66
#[allow(clippy::wildcard_imports)]
77
use near_sdk_contract_tools::mt::*;
@@ -26,11 +26,7 @@ impl FungibleTokenReceiver for Contract {
2626

2727
match msg {
2828
DepositMsg::Supply => {
29-
require!(
30-
env::prepaid_gas() > SUPPLY_GAS,
31-
format!("Not enough gas, required {SUPPLY_GAS}")
32-
);
33-
29+
require_at_least(SUPPLY_GAS);
3430
let refund = self.execute_supply(sender_id, asset_id, amount.into());
3531
PromiseOrValue::Value(refund.into())
3632
}
@@ -71,11 +67,7 @@ impl Nep245Receiver for Contract {
7167

7268
match msg {
7369
DepositMsg::Supply => {
74-
require!(
75-
env::prepaid_gas() > SUPPLY_GAS,
76-
format!("Not enough gas, required {SUPPLY_GAS}")
77-
);
78-
70+
require_at_least(SUPPLY_GAS);
7971
let mt = env::predecessor_account_id();
8072

8173
if !self.underlying_asset.is_nep245(&mt, token_id) {

contract/vault/src/lib.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use near_contract_standards::fungible_token::core::ext_ft_core;
44
use near_sdk::{
55
env,
66
json_types::U128,
7-
near, serde_json,
7+
near, require, serde_json,
88
store::{IterableMap, LookupMap, Vector},
99
AccountId, BorshStorageKey, IntoStorageKey, PanicOnDefault, Promise, PromiseOrValue,
1010
};
@@ -21,11 +21,11 @@ use near_sdk_contract_tools::{owner::OwnerExternal, rbac::Rbac};
2121
use templar_common::{
2222
asset::{BorrowAsset, BorrowAssetAmount, FungibleAsset},
2323
vault::{
24-
ext_self, AllocationMode, AllocationPlan, AllocationWeights, Error, Event,
25-
MarketConfiguration, OpState, PendingValue, PendingWithdrawal, TimestampNs,
24+
ext_self, require_at_least, AllocationMode, AllocationPlan, AllocationWeights, Error,
25+
Event, MarketConfiguration, OpState, PendingValue, PendingWithdrawal, TimestampNs,
2626
VaultConfiguration, AFTER_CREATE_WITHDRAW_REQ_GAS, AFTER_SEND_TO_USER_GAS,
27-
AFTER_SUPPLY_ENSURE_GAS, CREATE_WITHDRAW_REQ_GAS, MAX_QUEUE_LEN, MAX_TIMELOCK_NS,
28-
MIN_TIMELOCK_NS,
27+
AFTER_SUPPLY_ENSURE_GAS, ALLOCATE_GAS, CREATE_WITHDRAW_REQ_GAS, EXECUTE_WITHDRAW_GAS,
28+
MAX_QUEUE_LEN, MAX_TIMELOCK_NS, MIN_TIMELOCK_NS, WITHDRAW_GAS,
2929
},
3030
};
3131
pub use wad::*;
@@ -731,6 +731,7 @@ impl Contract {
731731
/// Internally calls `redeem` after computing the share amount.
732732
#[payable]
733733
pub fn withdraw(&mut self, amount: U128, receiver: AccountId) -> PromiseOrValue<()> {
734+
require_at_least(WITHDRAW_GAS);
734735
let shares_needed = self.preview_withdraw(amount).0;
735736
self.redeem(U128(shares_needed), receiver)
736737
}
@@ -768,6 +769,7 @@ impl Contract {
768769
/// Executes the next pending withdrawal request, if any, using the existing withdraw pipeline.
769770
/// This defers creating market-side withdrawal requests until explicitly invoked.
770771
pub fn execute_next_withdrawal_request(&mut self) -> PromiseOrValue<()> {
772+
require_at_least(EXECUTE_WITHDRAW_GAS);
771773
self.ensure_idle();
772774
Self::assert_allocator();
773775

@@ -810,6 +812,7 @@ impl Contract {
810812
weights: AllocationWeights,
811813
amount: Option<U128>,
812814
) -> PromiseOrValue<()> {
815+
require_at_least(ALLOCATE_GAS);
813816
Self::assert_allocator();
814817
self.ensure_idle();
815818

0 commit comments

Comments
 (0)