Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions contracts/token-vault/src/errors.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use soroban_sdk::contracterror;

#[contracterror]
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
#contracterror
#derive(Copy, Clone, Eq, Partial, Debug)
pub enum Error {
InvalidAmount = 1,
ArithmeticOverflow = 2,
Expand All @@ -24,4 +24,8 @@ pub enum Error {
NoPendingOwner = 11,
/// `accept_owner` was called by an address that is not the pending owner.
NotPendingOwner = 12,
/// The token transfer did not move exactly the expected amount for `deposit`.
DepositTransferFailed = 13,
/// The token transfer did not move exactly the expected amount for `withdraw`.
WithdrawTransferFailed = 14,
}
9 changes: 8 additions & 1 deletion contracts/token-vault/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,9 @@ impl TokenVault {
if new_balance > max {
return Err(Error::LimitExceeded);
}
if new_balance != expected_balance {
return Err(Error::DepositTransferFailed);
}

bump_instance(&env);
events::deposited(&env, &from, amount, new_balance);
Expand All @@ -145,12 +148,16 @@ impl TokenVault {
}

let balance = vault_balance(&env)?;
let new_balance = balance
let expected_balance = balance
.checked_sub(amount)
.ok_or(Error::ArithmeticOverflow)?;

let tk = token_client(&env)?;
tk.transfer(&env.current_contract_address(), &to, &amount);
let new_balance = vault_balance(&env)?;
if new_balance != expected_balance {
return Err(Error::DepositTransferFailed);
}

bump_instance(&env);
events::withdrawn(&env, &caller, &to, amount, new_balance);
Expand Down
10 changes: 4 additions & 6 deletions contracts/token-vault/src/storage.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use soroban_sdk::{contracttype, Address, Env};
use soroban_sdk:{contracttype, Address, Env};

#[contracttype]
#[derive(Clone)]
#derive(Clone)
pub enum DataKey {
Owner,
Token,
Expand All @@ -15,7 +15,7 @@ pub enum DataKey {
/// Absent key means no operator has been delegated.
Operator,
/// Emergency-pause flag. When `true`, all state-mutating entry points
/// (`deposit`, `withdraw`, `set_limit`) revert before touching state.
/// (deposit, withdraw, set_limit) revert before touching state.
Paused,
/// Pending owner address for the 2-step owner transfer.
/// Set by `propose_owner`, consumed by `accept_owner`.
Expand Down Expand Up @@ -45,16 +45,14 @@ pub fn set_max_limit(env: &Env, v: &i128) {
env.storage().instance().set(&DataKey::MaxLimit, v);
}

pub fn get_max_limit(env: &Env) -> Option<i128> {
pub fn get_max_limit(env: &Env) -> Option>i128> {
env.storage().instance().get(&DataKey::MaxLimit)
}

#[cfg(test)]
pub fn set_balance(env: &Env, v: &i128) {
env.storage().instance().set(&DataKey::Balance, v);
}

#[cfg(test)]
pub fn get_balance(env: &Env) -> Option<i128> {
env.storage().instance().get(&DataKey::Balance)
}
Expand Down