Skip to content

Commit 193d30d

Browse files
committed
chore: pr comments - docstrings, requires & panics
1 parent 16a64a4 commit 193d30d

File tree

4 files changed

+43
-45
lines changed

4 files changed

+43
-45
lines changed

common/src/vault.rs

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,28 +18,26 @@ pub type AllocationPlan = Vec<(AccountId, u128)>;
1818
#[derive(Clone, Debug, Default)]
1919
#[near(serializers = [json, borsh])]
2020
pub enum AllocationMode {
21-
// When eager makes sense
22-
//
23-
// • Retail/auto-pilot vaults: users expect deposits to “start earning” immediately without an active allocator.
24-
// • Small/simple vaults: stable caps/ordering, few markets; operational simplicity > fine-grained control.
25-
// • Integrations that assume quick deployment of idle assets.
26-
//
27-
// Risks/trade-offs of eager
28-
//
29-
// • Gas burden on depositors: ft_transfer_call into your vault must carry enough gas for multi-hop allocation.
30-
// Under-provisioned gas leads to partial allocations and extra callbacks.
31-
// • Timing control: depositors implicitly decide when allocation runs, which can fight the allocator’s planned rebalancing
32-
// cadence.
33-
// • Thrashing: many small deposits can trigger many allocation passes.
34-
// • Current code is “eager-ish but incomplete”: it only auto-starts when Idle, and does not auto-restart after the op. Deposits
35-
// that arrive during an allocation stay idle until someone triggers another pass.
36-
//
37-
// Behaviour
38-
// • On deposit: if Idle and idle_balance ≥ min_batch, start_allocation(idle_balance).
39-
// • Eager allocation can still honor a per-op plan if one is set (plan wins); otherwise fall back to supply_queue order.
40-
Eager {
41-
min_batch: u128,
42-
},
21+
/// When eager makes sense
22+
///
23+
/// • Retail/auto-pilot vaults: users expect deposits to “start earning” immediately without an active allocator.
24+
/// • Small/simple vaults: stable caps/ordering, few markets; operational simplicity > fine-grained control.
25+
/// • Integrations that assume quick deployment of idle assets.
26+
///
27+
/// Risks/trade-offs of eager
28+
///
29+
/// • Gas burden on depositors: ft_transfer_call into your vault must carry enough gas for multi-hop allocation.
30+
/// Under-provisioned gas leads to partial allocations and extra callbacks.
31+
/// • Timing control: depositors implicitly decide when allocation runs, which can fight the allocator’s planned rebalancing
32+
/// cadence.
33+
/// • Thrashing: many small deposits can trigger many allocation passes.
34+
/// • Current code is “eager-ish but incomplete”: it only auto-starts when Idle, and does not auto-restart after the op. Deposits
35+
/// that arrive during an allocation stay idle until someone triggers another pass.
36+
///
37+
/// Behaviour
38+
/// • On deposit: if Idle and idle_balance ≥ min_batch, start_allocation(idle_balance).
39+
/// • Eager allocation can still honor a per-op plan if one is set (plan wins); otherwise fall back to supply_queue order.
40+
Eager { min_batch: u128 },
4341
#[default]
4442
Lazy,
4543
}

contract/vault/src/aux.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::{env, near, serde_json, AccountId, Contract, Nep145Controller, Nep145ForceUnregister};
22

33
impl Contract {
4-
/* ----- Storage ----- */
4+
/// ----- Storage -----
55
fn charge_for_storage(&mut self, account_id: &AccountId, storage_consumption: u64) {
66
// Invariant: Storage charging saturates and panics on failure to avoid negative balances.
77
self.lock_storage(
@@ -31,7 +31,7 @@ pub enum ReturnStyle {
3131
Nep245MtTransferCall,
3232
}
3333

34-
// TODO: use this
34+
/// TODO: use this
3535
impl ReturnStyle {
3636
#[must_use]
3737
pub fn serialize(

contract/vault/src/impl_callbacks.rs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -393,20 +393,20 @@ impl Contract {
393393
Self::compute_escrow_settlement(escrow_shares, burn_shares);
394394
if to_burn > 0 {
395395
self.withdraw_unchecked(&env::current_account_id(), to_burn)
396-
.expect("Failed to burn escrowed shares");
396+
.unwrap_or_else(|e| env::panic_str(&e.to_string()));
397397
}
398398
if refund_shares > 0 {
399399
#[allow(clippy::expect_used, reason = "No side effects")]
400400
self.transfer_unchecked(&env::current_account_id(), &owner, refund_shares)
401-
.expect("Failed to refund remaining escrowed shares");
401+
.unwrap_or_else(|e| env::panic_str(&e.to_string()));
402402
}
403403
self.op_state = OpState::Idle;
404404
true
405405
} else {
406406
// Invariant: On payout failure, refund full escrow to owner and leave idle_balance unchanged
407407
#[allow(clippy::expect_used, reason = "No side effects")]
408408
self.transfer_unchecked(&env::current_account_id(), &owner, escrow_shares)
409-
.expect("Failed to release escrowed shares");
409+
.unwrap_or_else(|e| env::panic_str(&e.to_string()));
410410
self.op_state = OpState::Idle;
411411
false
412412
}
@@ -517,7 +517,7 @@ impl Contract {
517517
let self_id = env::current_account_id();
518518
#[allow(clippy::expect_used, reason = "No side effects")]
519519
self.transfer_unchecked(&self_id, &owner_acc, escrow)
520-
.expect("Failed to release escrowed shares");
520+
.unwrap_or_else(|e| env::panic_str(&e.to_string()));
521521
}
522522
}
523523
self.op_state = OpState::Idle;
@@ -555,7 +555,7 @@ impl Contract {
555555
let self_id = env::current_account_id();
556556
#[allow(clippy::expect_used, reason = "No side effects")]
557557
self.transfer_unchecked(&self_id, &owner_acc, escrow)
558-
.expect("Failed to release escrowed shares");
558+
.unwrap_or_else(|e| env::panic_str(&e.to_string()));
559559
}
560560
}
561561
self.op_state = OpState::Idle;
@@ -579,7 +579,7 @@ impl Contract {
579579
}
580580
PromiseOrValue::Value(())
581581
}
582-
// Validate current op is Allocating and return (index, remaining)
582+
/// Validate current op is Allocating and return (index, remaining)
583583
pub(crate) fn ctx_allocating(&self, op_id: u64) -> Result<(u32, u128), Error> {
584584
match &self.op_state {
585585
OpState::Allocating {
@@ -591,7 +591,7 @@ impl Contract {
591591
}
592592
}
593593

594-
// Validate current op is Withdrawing and return context tuple
594+
/// Validate current op is Withdrawing and return context tuple
595595
pub(crate) fn ctx_withdrawing(
596596
&self,
597597
op_id: u64,
@@ -617,7 +617,7 @@ impl Contract {
617617
}
618618
}
619619

620-
// Resolve a market for allocation by plan (if present) or supply_queue
620+
/// Resolve a market for allocation by plan (if present) or supply_queue
621621
pub(crate) fn resolve_supply_market(&self, market_index: u32) -> Result<AccountId, Error> {
622622
if let Some(plan) = &self.plan {
623623
if let Some((m, _)) = plan.get(market_index as usize) {
@@ -631,15 +631,15 @@ impl Contract {
631631
.ok_or(Error::MissingMarket(market_index))
632632
}
633633

634-
// Resolve a market for withdraw by withdraw_queue
634+
/// Resolve a market for withdraw by withdraw_queue
635635
pub(crate) fn resolve_withdraw_market(&self, market_index: u32) -> Result<AccountId, Error> {
636636
self.withdraw_queue
637637
.get(market_index)
638638
.cloned()
639639
.ok_or(Error::MissingMarket(market_index))
640640
}
641641

642-
// Pure reconciliation for withdraw read outcome to enable unit tests
642+
/// Pure reconciliation for withdraw read outcome to enable unit tests
643643
pub(crate) fn reconcile_withdraw_outcome(
644644
&self,
645645
before_principal: u128,
@@ -684,7 +684,7 @@ mod tests {
684684
&vault_id,
685685
&vault_id,
686686
vec![PromiseResult::Successful(
687-
near_sdk::serde_json::to_vec(&U128(u128::MAX)).unwrap(),
687+
near_sdk::serde_json::to_vec(&U128(u128::MAX)).unwrap_or_else(|e| near_sdk::env::panic_str(&e.to_string())),
688688
)],
689689
);
690690

@@ -707,7 +707,7 @@ mod tests {
707707
&vault_id,
708708
&vault_id,
709709
vec![PromiseResult::Successful(
710-
near_sdk::serde_json::to_vec(&U128(u128::MAX)).unwrap(),
710+
near_sdk::serde_json::to_vec(&U128(u128::MAX)).unwrap_or_else(|e| near_sdk::env::panic_str(&e.to_string())),
711711
)],
712712
);
713713

@@ -735,7 +735,7 @@ mod tests {
735735
&vault_id,
736736
&vault_id,
737737
vec![PromiseResult::Successful(
738-
near_sdk::serde_json::to_vec(&U128(u128::MAX)).unwrap(),
738+
near_sdk::serde_json::to_vec(&U128(u128::MAX)).unwrap_or_else(|e| near_sdk::env::panic_str(&e.to_string())),
739739
)],
740740
);
741741

@@ -865,7 +865,7 @@ mod tests {
865865
}
866866
}
867867

868-
// Property: Payout failure keeps idle_balance unchanged and does not burn escrow
868+
/// Property: Payout failure keeps idle_balance unchanged and does not burn escrow
869869
#[rstest(
870870
idle => [0u128, 1, 100],
871871
escrow => [0u128, 1, 50],
@@ -883,7 +883,7 @@ mod tests {
883883
use near_sdk_contract_tools::ft::Nep141Controller as _;
884884

885885
c.deposit_unchecked(&near_sdk::env::current_account_id(), escrow)
886-
.expect("seed escrow into vault");
886+
.unwrap_or_else(|e| near_sdk::env::panic_str(&e.to_string()));
887887
}
888888

889889
c.idle_balance = idle;
@@ -914,7 +914,7 @@ mod tests {
914914
);
915915
}
916916

917-
// Property: Create-withdraw failure skips to next market and if collected>0 ends in Payout
917+
/// Property: Create-withdraw failure skips to next market and if collected>0 ends in Payout
918918
#[rstest(
919919
collected => [1u128, 10u128],
920920
need => [1u128, 5u128]
@@ -954,7 +954,7 @@ mod tests {
954954
}
955955
}
956956

957-
// Property: Exec-withdraw read failure assumes unchanged principal and does not credit idle
957+
/// Property: Exec-withdraw read failure assumes unchanged principal and does not credit idle
958958
#[rstest(
959959
before => [0u128, 1u128, 100u128],
960960
need => [0u128, 1u128, 50u128],
@@ -1011,7 +1011,7 @@ mod tests {
10111011
}
10121012
}
10131013

1014-
// Property: Callbacks must match current op_id or index; otherwise stop and go Idle
1014+
/// Property: Callbacks must match current op_id or index; otherwise stop and go Idle
10151015
#[rstest(
10161016
pass_op => [false, true],
10171017
pass_index => [false, true]
@@ -1068,7 +1068,7 @@ mod tests {
10681068
// Seed escrowed shares into the vault's own account
10691069
let owner = accounts(1);
10701070
c.deposit_unchecked(&near_sdk::env::current_account_id(), 10)
1071-
.expect("seed escrow into vault");
1071+
.unwrap_or_else(|e| near_sdk::env::panic_str(&e.to_string()));
10721072

10731073
// Single-market withdraw queue (not used functionally here, just to satisfy path)
10741074
let market = mk(12);

contract/vault/src/storage_management.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use templar_common::vault::{storage_bytes_for_account_id, MarketConfiguration};
77
/// We do not implement refunds for storage management ops, to avoid any potential issues with
88
/// accounting.
99
10-
// Conservative per-entry overheads to cover collection metadata, prefixes, etc.
10+
/// Conservative per-entry overheads to cover collection metadata, prefixes, etc.
1111
pub const MAP_ENTRY_OVERHEAD: u64 = 64;
1212

1313
pub const VEC_ITEM_OVERHEAD: u64 = 16;
@@ -78,7 +78,7 @@ pub fn yocto_for_queue_additions(current: &HashSet<AccountId>, new: &[AccountId]
7878
#[must_use]
7979
pub fn require_attached_at_least(required_yocto: u128, ctx: &str) -> u128 {
8080
let attached = env::attached_deposit().as_yoctonear();
81-
assert!(
81+
near_sdk::require!(
8282
attached >= required_yocto,
8383
"Insufficient storage deposit for {ctx}: required {required_yocto}, attached {attached}"
8484
);

0 commit comments

Comments
 (0)