Skip to content

Commit

Permalink
wip: disbursal velocity checks
Browse files Browse the repository at this point in the history
  • Loading branch information
thevaibhav-dixit committed Jan 16, 2025
1 parent ce14dfc commit 55b213c
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lana/app/src/credit_facility/ledger/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ pub enum CreditLedgerError {
CalaBalance(#[from] cala_ledger::balance::error::BalanceError),
#[error("CreditLedgerError - ConversionError: {0}")]
ConversionError(#[from] core_money::ConversionError),
#[error("CreditLedgerError - CalaVelocityError: {0}")]
CalaVelocity(#[from] cala_ledger::velocity::error::VelocityError),
}
58 changes: 58 additions & 0 deletions lana/app/src/credit_facility/ledger/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
mod credit_facility_accounts;
pub mod error;
mod templates;
mod velocity;

use cala_ledger::{
account::{error::AccountError, NewAccount},
tx_template::Params,
velocity::{NewVelocityControl, VelocityControlId},
AccountId, CalaLedger, Currency, DebitOrCredit, JournalId, TransactionId,
};

Expand All @@ -14,6 +17,8 @@ use error::*;

pub(super) const BANK_COLLATERAL_ACCOUNT_CODE: &str = "BANK.COLLATERAL.OMNIBUS";
pub(super) const CREDIT_OMNIBUS_ACCOUNT_CODE: &str = "CREDIT.OMNIBUS";
pub(super) const CREDIT_FACILITY_VELOCITY_CONTROL_ID: uuid::Uuid =
uuid::uuid!("00000000-0000-0000-0000-000000000001");

#[derive(Debug, Clone)]
pub struct CreditFacilityCollateralUpdate {
Expand All @@ -29,6 +34,7 @@ pub struct CreditLedger {
journal_id: JournalId,
credit_omnibus_account: AccountId,
bank_collateral_account_id: AccountId,
credit_facility_control_id: VelocityControlId,
usd: Currency,
btc: Currency,
}
Expand All @@ -51,11 +57,26 @@ impl CreditLedger {
templates::CreditFacilityAccrueInterest::init(cala).await?;
templates::CreditFacilityDisbursal::init(cala).await?;

let disbursal_limit_id = velocity::DisbursalLimit::init(cala).await?;

let credit_facility_control_id = Self::create_credit_facility_control(cala).await?;

match cala
.velocities()
.add_limit_to_control(credit_facility_control_id, disbursal_limit_id)
.await
{
Ok(_)
| Err(cala_ledger::velocity::error::VelocityError::LimitAlreadyAddedToControl) => {}
Err(e) => return Err(e.into()),
}

Ok(Self {
cala: cala.clone(),
journal_id,
bank_collateral_account_id,
credit_omnibus_account,
credit_facility_control_id,
usd: "USD".parse().expect("Could not parse 'USD'"),
btc: "BTC".parse().expect("Could not parse 'BTC'"),
})
Expand Down Expand Up @@ -418,4 +439,41 @@ impl CreditLedger {
Ok(account) => Ok(account.id),
}
}

pub async fn create_credit_facility_control(
cala: &CalaLedger,
) -> Result<VelocityControlId, CreditLedgerError> {
let control = NewVelocityControl::builder()
.id(CREDIT_FACILITY_VELOCITY_CONTROL_ID)
.name("Credit Facility Control")
.description("Velocity Control for Deposits")
.build()
.expect("build control");

match cala.velocities().create_control(control).await {
Err(cala_ledger::velocity::error::VelocityError::ControlIdAlreadyExists) => {
Ok(CREDIT_FACILITY_VELOCITY_CONTROL_ID.into())
}
Err(e) => Err(e.into()),
Ok(control) => Ok(control.id()),
}
}

pub async fn add_credit_facility_control_to_account(
&self,
op: &mut cala_ledger::LedgerOperation<'_>,
account_id: impl Into<AccountId>,
) -> Result<(), CreditLedgerError> {
self.cala
.velocities()
.attach_control_to_account_in_op(
op,
self.credit_facility_control_id,
account_id.into(),
Params::default(),
)
.await?;

Ok(())
}
}
41 changes: 41 additions & 0 deletions lana/app/src/credit_facility/ledger/velocity/disbursal_limit.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use tracing::instrument;

use cala_ledger::{velocity::*, *};

pub struct DisbursalLimit;

const DISBURSAL_LIMIT_ID: uuid::Uuid = uuid::uuid!("00000000-0000-0000-0000-000000000002");

impl DisbursalLimit {
#[instrument(name = "ledger.overdraft_prevention.init", skip_all)]
pub async fn init(
ledger: &CalaLedger,
) -> Result<VelocityLimitId, crate::credit_facility::ledger::CreditLedgerError> {
let limit = NewVelocityLimit::builder()
.id(DISBURSAL_LIMIT_ID)
.name("Disbursal Limit")
.description("Limit for disbursals")
.window(vec![])
.limit(
NewLimit::builder()
.balance(vec![NewBalanceLimit::builder()
.layer("SETTLED")
.amount("decimal('0.0')")
.enforcement_direction("DEBIT")
.build()
.expect("balance limit")])
.build()
.expect("limit"),
)
.build()
.expect("velocity limit");

match ledger.velocities().create_limit(limit).await {
Err(cala_ledger::velocity::error::VelocityError::LimitIdAlreadyExists) => {
Ok(DISBURSAL_LIMIT_ID.into())
}
Err(e) => Err(e.into()),
Ok(limit) => Ok(limit.id()),
}
}
}
3 changes: 3 additions & 0 deletions lana/app/src/credit_facility/ledger/velocity/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
mod disbursal_limit;

pub use disbursal_limit::*;

0 comments on commit 55b213c

Please sign in to comment.