Skip to content
Open
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
47 changes: 47 additions & 0 deletions contracts/loan_manager/src/nonce.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use soroban_sdk::{contracterror, contracttype, Address, Env};

#[contracttype]
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum OpKind {
ProcessDefaults,
ApproveLoans,
GovernanceExecute,
}

#[contracttype]
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct OpNonceKey {
pub address: Address,
pub op_kind: OpKind,
}

#[contracterror]
#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
#[repr(u32)]
pub enum BatchError {
NonceReused = 1,
BatchWindowExpired = 2,
}

pub fn consume_nonce(env: &Env, address: Address, op_kind: OpKind, supplied_nonce: u64) -> Result<(), BatchError> {
let key = OpNonceKey { address, op_kind };
let current_nonce: u64 = env.storage().persistent().get(&key).unwrap_or(0);

if supplied_nonce != current_nonce + 1 {
return Err(BatchError::NonceReused);
}

env.storage().persistent().set(&key, &supplied_nonce);

// Bump TTL for the entry
env.storage().persistent().extend_ttl(&key, 1000, 100000);

Ok(())
}

pub fn validate_batch_window(env: &Env, valid_until_ledger: u32) -> Result<(), BatchError> {
if env.ledger().sequence() > valid_until_ledger {
return Err(BatchError::BatchWindowExpired);
}
Ok(())
}
Loading