use common_data::asset::Asset; use near_sdk::{ext_contract, json_types::U128, near, AccountId, PromiseOrValue}; // # Vault Types Module // This module defines the core data structures used by the vault contract. /// Vault balance tracking for each asset #[near(serializers=[json, borsh])] #[derive(Clone, Debug, PartialEq)] pub struct VaultBalance { /// Assets pending deposit (not yet converted to shares) pub pending_deposit: U128, /// Assets available for withdrawal pub available_amount: U128, } impl Default for VaultBalance { fn default() -> Self { Self { pending_deposit: U128(0), available_amount: U128(0), } } } /// Pending deposit request #[near(serializers=[json, borsh])] #[derive(Clone, Debug)] pub struct Deposit { /// Asset being deposited pub asset: Asset, /// Amount of assets to deposit pub deposit_amount: U128, /// Minimum shares expected (slippage protection) pub min_shares: U128, /// Account that initiated the deposit pub owner_id: AccountId, /// Account that should receive the shares pub receiver_id: AccountId, /// Optional memo for the deposit pub memo: Option, /// Timestamp when deposit was requested pub created_at: U128, } impl Deposit { pub fn new( asset: Asset, deposit_amount: U128, min_shares: U128, owner_id: AccountId, receiver_id: AccountId, memo: Option, ) -> Self { Self { asset, deposit_amount, min_shares, owner_id, receiver_id, memo, created_at: U128(near_sdk::env::block_timestamp() as u128), } } } /// Pending withdrawal request #[near(serializers=[json, borsh])] #[derive(Clone, Debug)] pub struct Withdraw { /// Asset to withdraw pub asset: Asset, /// Amount of shares to burn pub shares: U128, /// Minimum assets expected (slippage protection) pub min_asset_amount: U128, /// Account that owns the shares pub owner_id: AccountId, /// Account that should receive the assets pub receiver_id: AccountId, /// Optional memo for the withdrawal pub memo: Option, /// Timestamp when withdrawal can be activated pub activation_timestamp_nanosec: U128, /// Whether the withdrawal has been confirmed with locked share price pub confirmed: bool, /// Share price locked in at confirmation (if confirmed) pub confirmed_share_price: Option, /// Timestamp when withdrawal was confirmed pub confirmed_at_timestamp: Option, } pub type TellerOperationId = u64; #[near(serializers=[json, borsh])] #[derive(Clone, Debug)] pub enum TellerOperation { Deposit(Deposit), Withdraw(Withdraw), } #[near(serializers=[json, borsh])] #[derive(Clone, Debug)] pub struct TellerOperationWithId { pub operation: TellerOperation, pub operation_id: TellerOperationId, } impl Withdraw { pub fn new( asset: Asset, shares: U128, min_asset_amount: U128, owner_id: AccountId, receiver_id: AccountId, memo: Option, activation_timestamp_nanosec: U128, ) -> Self { Self { asset, shares, min_asset_amount, owner_id, receiver_id, memo, activation_timestamp_nanosec, confirmed: false, confirmed_share_price: None, confirmed_at_timestamp: None, } } } /// Message format for transfer callback deposits #[derive(serde::Deserialize, serde::Serialize)] #[serde(crate = "near_sdk::serde")] pub struct DepositMessage { /// Whether to process as async request (true) or immediate deposit (false) pub is_request: bool, /// Whether to process as async request (true) or immediate deposit (false) pub is_strategist_transfer: Option, /// Minimum shares expected (slippage protection) pub min_shares: U128, /// Account that should receive the shares (optional, defaults to sender) pub receiver_id: Option, /// Optional memo for the deposit pub memo: Option, /// Optional list of redeem operation IDs to process with transferred funds /// If provided, transferred funds will be used to process these redeems instead of depositing pub process_redeems: Option>, } #[allow(dead_code)] pub trait MultiTokenReceiver { /// Called when tokens are transferred to this contract via mt_transfer_call /// Returns the amount of tokens used (the rest is returned to sender) #[allow(dead_code)] fn mt_on_transfer( &mut self, sender_id: AccountId, previous_owner_ids: Vec, token_ids: Vec, amounts: Vec, msg: String, ) -> PromiseOrValue>; } /// Flow tracker for rate limiting deposits and withdrawals #[near(serializers=[json, borsh])] #[derive(Default, Clone, Debug)] pub struct FlowTracker { /// Accumulated amount in base token units within current window pub accumulated_amount: U128, /// Nanosecond timestamp when the current window started pub window_start_timestamp: u64, } #[ext_contract(ext_self)] pub trait _ExtSelf { fn redeem_transfer_callback( &mut self, operation_id: Option, owner_id: AccountId, receiver_id: AccountId, asset: Asset, shares: U128, net_redeem_amount: U128, vault_fee: U128, protocol_fee: U128, memo: Option, ); fn asset_transfer_callback( &mut self, operation_id: Option, asset: Asset, amount: U128, receiver_id: AccountId, memo: Option, ); fn async_redeem_transfer_callback( &mut self, operation_id: u64, owner_id: AccountId, receiver_id: AccountId, asset: Asset, shares: U128, net_redeem_amount: U128, vault_fee: U128, protocol_fee: U128, min_asset_amount: U128, memo: Option, activation_timestamp_nanosec: U128, confirmed_share_price: U128, ); fn deposit_rejection_transfer_callback( &mut self, operation_id: u64, owner_id: AccountId, asset: Asset, rejected_amount: U128, min_shares: U128, receiver_id: AccountId, memo: Option, reason: String, ); fn claim_assets_callback(&mut self, claimer: AccountId, asset: Asset, amount: U128); }