Skip to content

Commit

Permalink
chore: credit checking account on disbursal approval
Browse files Browse the repository at this point in the history
  • Loading branch information
thevaibhav-dixit committed Jan 20, 2025
1 parent ef5a216 commit a68c4ff
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 147 deletions.
8 changes: 0 additions & 8 deletions lana/app/src/credit_facility/ledger/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,6 @@ impl CreditLedger {
op: es_entity::DbOp<'_>,
amount: UsdCents,
credit_facility_account_ids: CreditFacilityAccountIds,
debit_account_id: impl Into<AccountId>,
) -> Result<(), CreditLedgerError> {
let mut op = self.cala.ledger_operation_from_db_op(op);

Expand All @@ -416,9 +415,6 @@ impl CreditLedger {
journal_id: self.journal_id,
credit_omnibus_account: self.credit_omnibus_account,
credit_facility_account: credit_facility_account_ids.facility_account_id,
facility_disbursed_receivable_account: credit_facility_account_ids
.disbursed_receivable_account_id,
checking_account: debit_account_id.into(),
disbursed_amount: amount.to_usd(),
},
)
Expand All @@ -433,7 +429,6 @@ impl CreditLedger {
tx_id: impl Into<TransactionId>,
amount: UsdCents,
credit_facility_account_ids: CreditFacilityAccountIds,
debit_account_id: impl Into<AccountId>,
) -> Result<(), CreditLedgerError> {
let mut op = self.cala.ledger_operation_from_db_op(op);
self.cala
Expand All @@ -445,9 +440,6 @@ impl CreditLedger {
journal_id: self.journal_id,
credit_omnibus_account: self.credit_omnibus_account,
credit_facility_account: credit_facility_account_ids.facility_account_id,
facility_disbursed_receivable_account: credit_facility_account_ids
.disbursed_receivable_account_id,
checking_account: debit_account_id.into(),
disbursed_amount: amount.to_usd(),
},
)
Expand Down
55 changes: 0 additions & 55 deletions lana/app/src/credit_facility/ledger/templates/cancel_disbursal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ pub struct CancelDisbursalParams {
pub journal_id: JournalId,
pub credit_omnibus_account: AccountId,
pub credit_facility_account: AccountId,
pub facility_disbursed_receivable_account: AccountId,
pub checking_account: AccountId,
pub disbursed_amount: Decimal,
}

Expand All @@ -36,16 +34,6 @@ impl CancelDisbursalParams {
.r#type(ParamDataType::Uuid)
.build()
.unwrap(),
NewParamDefinition::builder()
.name("facility_disbursed_receivable_account")
.r#type(ParamDataType::Uuid)
.build()
.unwrap(),
NewParamDefinition::builder()
.name("checking_account")
.r#type(ParamDataType::Uuid)
.build()
.unwrap(),
NewParamDefinition::builder()
.name("disbursed_amount")
.r#type(ParamDataType::Decimal)
Expand All @@ -66,20 +54,13 @@ impl From<CancelDisbursalParams> for Params {
journal_id,
credit_omnibus_account,
credit_facility_account,
facility_disbursed_receivable_account,
checking_account,
disbursed_amount,
}: CancelDisbursalParams,
) -> Self {
let mut params = Self::default();
params.insert("journal_id", journal_id);
params.insert("credit_omnibus_account", credit_omnibus_account);
params.insert("credit_facility_account", credit_facility_account);
params.insert(
"facility_disbursed_receivable_account",
facility_disbursed_receivable_account,
);
params.insert("checking_account", checking_account);
params.insert("disbursed_amount", disbursed_amount);
params.insert("effective", chrono::Utc::now().date_naive());
params
Expand Down Expand Up @@ -118,24 +99,6 @@ impl CancelDisbursal {
.units("params.disbursed_amount")
.build()
.expect("Couldn't build entry"),
NewTxTemplateEntry::builder()
.entry_type("'CANCEL_DISBURSAL_PENDING_DR'")
.currency("'USD'")
.account_id("params.facility_disbursed_receivable_account")
.direction("DEBIT")
.layer("PENDING")
.units("params.disbursed_amount")
.build()
.expect("Couldn't build entry"),
NewTxTemplateEntry::builder()
.entry_type("'CANCEL_DISBURSAL_PENDING_CR'")
.currency("'USD'")
.account_id("params.checking_account")
.direction("CREDIT")
.layer("PENDING")
.units("params.disbursed_amount")
.build()
.expect("Couldn't build entry"),
// Reverse settled entries
NewTxTemplateEntry::builder()
.entry_type("'CANCEL_DISBURSAL_DRAWDOWN_SETTLED_CR'")
Expand All @@ -155,24 +118,6 @@ impl CancelDisbursal {
.units("params.disbursed_amount")
.build()
.expect("Couldn't build entry"),
NewTxTemplateEntry::builder()
.entry_type("'CANCEL_DISBURSAL_SETTLED_CR'")
.currency("'USD'")
.account_id("params.facility_disbursed_receivable_account")
.direction("CREDIT")
.layer("SETTLED")
.units("params.disbursed_amount")
.build()
.expect("Couldn't build entry"),
NewTxTemplateEntry::builder()
.entry_type("'CANCEL_DISBURSAL_SETTLED_DR'")
.currency("'USD'")
.account_id("params.checking_account")
.direction("DEBIT")
.layer("SETTLED")
.units("params.disbursed_amount")
.build()
.expect("Couldn't build entry"),
];

let params = CancelDisbursalParams::defs();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,22 +126,23 @@ impl ConfirmDisbursal {
.units("params.disbursed_amount")
.build()
.expect("Couldn't build entry"),
// SETTLED LAYER
NewTxTemplateEntry::builder()
.entry_type("'CONFIRM_DISBURSAL_PENDING_DR'")
.currency("'USD'")
.account_id("params.facility_disbursed_receivable_account")
.direction("DEBIT")
.layer("PENDING")
.units("params.disbursed_amount")
.currency("'USD'")
.entry_type("'CONFIRM_DISBURSAL_SETTLED_DR'")
.direction("DEBIT")
.layer("SETTLED")
.build()
.expect("Couldn't build entry"),
NewTxTemplateEntry::builder()
.entry_type("'CONFIRM_DISBURSAL_PENDING_CR'")
.currency("'USD'")
.account_id("params.checking_account")
.direction("CREDIT")
.layer("PENDING")
.units("params.disbursed_amount")
.currency("'USD'")
.entry_type("'CONFIRM_DISBURSAL_SETTLED_CR'")
.direction("CREDIT")
.layer("SETTLED")
.build()
.expect("Couldn't build entry"),
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ pub struct InitiateDisbursalParams {
pub journal_id: JournalId,
pub credit_omnibus_account: AccountId,
pub credit_facility_account: AccountId,
pub facility_disbursed_receivable_account: AccountId,
pub checking_account: AccountId,
pub disbursed_amount: Decimal,
}

Expand All @@ -36,16 +34,6 @@ impl InitiateDisbursalParams {
.r#type(ParamDataType::Uuid)
.build()
.unwrap(),
NewParamDefinition::builder()
.name("facility_disbursed_receivable_account")
.r#type(ParamDataType::Uuid)
.build()
.unwrap(),
NewParamDefinition::builder()
.name("checking_account")
.r#type(ParamDataType::Uuid)
.build()
.unwrap(),
NewParamDefinition::builder()
.name("disbursed_amount")
.r#type(ParamDataType::Decimal)
Expand All @@ -66,8 +54,6 @@ impl From<InitiateDisbursalParams> for Params {
InitiateDisbursalParams {
journal_id,
credit_facility_account,
facility_disbursed_receivable_account,
checking_account,
disbursed_amount,
credit_omnibus_account,
}: InitiateDisbursalParams,
Expand All @@ -76,11 +62,6 @@ impl From<InitiateDisbursalParams> for Params {
params.insert("journal_id", journal_id);
params.insert("credit_omnibus_account", credit_omnibus_account);
params.insert("credit_facility_account", credit_facility_account);
params.insert(
"facility_disbursed_receivable_account",
facility_disbursed_receivable_account,
);
params.insert("checking_account", checking_account);
params.insert("disbursed_amount", disbursed_amount);
params.insert("effective", chrono::Utc::now().date_naive());
params
Expand Down Expand Up @@ -119,25 +100,6 @@ impl InitiateDisbursal {
.layer("SETTLED")
.build()
.expect("Couldn't build entry"),
NewTxTemplateEntry::builder()
.account_id("params.facility_disbursed_receivable_account")
.units("params.disbursed_amount")
.currency("'USD'")
.entry_type("'INITIATE_CREDIT_FACILITY_DISBURSAL_SETTLED_DR'")
.direction("DEBIT")
.layer("SETTLED")
.build()
.expect("Couldn't build entry"),
NewTxTemplateEntry::builder()
.account_id("params.checking_account")
.units("params.disbursed_amount")
.currency("'USD'")
.entry_type("'INITIATE_CREDIT_FACILITY_DISBURSAL_SETTLED_CR'")
.direction("CREDIT")
.layer("SETTLED")
.build()
.expect("Couldn't build entry"),
// PENDING layer entries
NewTxTemplateEntry::builder()
.account_id("params.credit_omnibus_account")
.units("params.disbursed_amount")
Expand All @@ -156,24 +118,6 @@ impl InitiateDisbursal {
.layer("PENDING")
.build()
.expect("Couldn't build entry"),
NewTxTemplateEntry::builder()
.account_id("params.facility_disbursed_receivable_account")
.units("params.disbursed_amount")
.currency("'USD'")
.entry_type("'INITIATE_CREDIT_FACILITY_DISBURSAL_PENDING_CR'")
.direction("CREDIT")
.layer("PENDING")
.build()
.expect("Couldn't build entry"),
NewTxTemplateEntry::builder()
.account_id("params.checking_account")
.units("params.disbursed_amount")
.currency("'USD'")
.entry_type("'INITIATE_CREDIT_FACILITY_DISBURSAL_PENDING_DR'")
.direction("DEBIT")
.layer("PENDING")
.build()
.expect("Couldn't build entry"),
];

let params = InitiateDisbursalParams::defs();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ impl DisbursalLimit {
.balance(vec![NewBalanceLimit::builder()
.layer("SETTLED")
.amount("params.disbursal_limit")
.enforcement_direction("DEBIT")
.enforcement_direction("CREDIT")
.build()
.expect("balance limit")])
.build()
Expand Down
20 changes: 7 additions & 13 deletions lana/app/src/credit_facility/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ impl CreditFacilities {
self.ledger
.add_credit_facility_control_to_account(
&mut op,
credit_facility.account_ids.disbursed_receivable_account_id,
credit_facility.account_ids.facility_account_id,
facility,
)
.await?;
Expand Down Expand Up @@ -344,11 +344,11 @@ impl CreditFacilities {
.find_by_id(credit_facility_id)
.await?;

let balances = self
.ledger
.get_credit_facility_balance(credit_facility.account_ids)
.await?;
credit_facility.balances().check_against_ledger(balances)?;
// let balances = self
// .ledger
// .get_credit_facility_balance(credit_facility.account_ids)
// .await?;
// credit_facility.balances().check_against_ledger(balances)?;

let price = self.price.usd_cents_per_btc().await?;

Expand All @@ -373,13 +373,7 @@ impl CreditFacilities {
.await?;

self.ledger
.initiate_disbursal(
db,
disbursal.id,
disbursal.amount,
disbursal.account_ids,
disbursal.deposit_account_id,
)
.initiate_disbursal(db, disbursal.id, disbursal.amount, disbursal.account_ids)
.await?;

Ok(disbursal)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,12 +156,7 @@ impl ApproveDisbursal {
es_entity::DbOp::new(tx.begin().await?, now)
};
self.ledger
.cancel_disbursal(
sub_op,
disbursal.amount,
disbursal.account_ids,
disbursal.deposit_account_id,
)
.cancel_disbursal(sub_op, disbursal.amount, disbursal.account_ids)
.await?;
let mut db = es_entity::DbOp::new(tx, now);
self.credit_facility_repo
Expand Down

0 comments on commit a68c4ff

Please sign in to comment.