Skip to content

Commit

Permalink
chore(deposit): add DepositLedger
Browse files Browse the repository at this point in the history
  • Loading branch information
bodymindarts committed Dec 6, 2024
1 parent f91b5c5 commit 9b78c78
Show file tree
Hide file tree
Showing 9 changed files with 114 additions and 35 deletions.
48 changes: 20 additions & 28 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 6 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,12 @@ members = [
]

[workspace.dependencies]
es-entity = "0.3.1"
sim-time = "0.3.1"
cala-ledger = "0.3.1"
es-entity = { git = "https://github.com/galoymoney/cala.git", branch = "main" }
sim-time = { git = "https://github.com/galoymoney/cala.git", branch = "main" }
cala-ledger = { git = "https://github.com/galoymoney/cala.git", branch = "main" }
# es-entity = "0.3.1"
# sim-time = "0.3.1"
# cala-ledger = "0.3.1"

anyhow = "1.0.92"
async-graphql = { version = "7.0.11", default-features = false, features = ["dataloader", "tracing", "chrono", "playground"] }
Expand Down
2 changes: 2 additions & 0 deletions core/deposit/src/account/entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub enum DepositAccountEvent {
Initialized {
id: DepositAccountId,
account_holder_id: AccountHolderId,
ledger_account_id: LedgerAccountId,
audit_info: AuditInfo,
},
}
Expand Down Expand Up @@ -60,6 +61,7 @@ impl IntoEvents<DepositAccountEvent> for NewDepositAccount {
[DepositAccountEvent::Initialized {
id: self.id,
account_holder_id: self.account_holder_id,
ledger_account_id: self.id.into(),
audit_info: self.audit_info,
}],
)
Expand Down
2 changes: 2 additions & 0 deletions core/deposit/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ pub enum CoreDepositError {
DepositAccountError(#[from] crate::account::error::DepositAccountError),
#[error("CoreDepositError - DepositError: {0}")]
DepositError(#[from] crate::deposit::error::DepositError),
#[error("CoreDepositError - DepositLedgerError: {0}")]
DepositLedgerError(#[from] crate::ledger::error::DepositLedgerError),
}
11 changes: 11 additions & 0 deletions core/deposit/src/ledger/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use thiserror::Error;

#[derive(Error, Debug)]
pub enum DepositLedgerError {
#[error("DepositLedgerError - Sqlx: {0}")]
Sqlx(#[from] sqlx::Error),
#[error("DepositLedgerError - CalaLedger: {0}")]
CalaLedger(#[from] cala_ledger::error::LedgerError),
#[error("DepositLedgerError - CalaLedger: {0}")]
CalaAccount(#[from] cala_ledger::account::error::AccountError),
}
41 changes: 41 additions & 0 deletions core/deposit/src/ledger/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
pub mod error;

use cala_ledger::{account::*, CalaLedger};

use error::*;

#[derive(Clone)]
pub struct DepositLedger {
cala: CalaLedger,
}

impl DepositLedger {
pub async fn init(cala: &CalaLedger) -> Result<Self, DepositLedgerError> {
Ok(Self { cala: cala.clone() })
}

pub async fn create_account_for_deposit_account(
&self,
op: es_entity::DbOp<'_>,
id: impl Into<AccountId>,
code: String,
) -> Result<(), DepositLedgerError> {
let mut op = self.cala.ledger_operation_from_db_op(op);

let new_account = NewAccount::builder()
.id(id)
.name("Deposit Account")
.code(code)
.build()
.expect("Could not build new account");

self.cala
.accounts()
.create_in_op(&mut op, new_account)
.await?;

op.commit().await?;

Ok(())
}
}
17 changes: 15 additions & 2 deletions core/deposit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,21 @@ mod account;
mod deposit;
pub mod error;
mod event;
mod ledger;
mod primitives;

use tracing::instrument;

use audit::AuditSvc;
use authz::PermissionCheck;
use cala_ledger::CalaLedger;
use outbox::{Outbox, OutboxEventMarker};

use account::*;
use deposit::*;
use error::*;
pub use event::*;
use ledger::*;
pub use primitives::*;

pub struct CoreDeposit<Perms, E>
Expand All @@ -26,6 +29,7 @@ where
{
accounts: DepositAccountRepo,
deposits: DepositRepo,
ledger: DepositLedger,
authz: Perms,
outbox: Outbox<E>,
}
Expand All @@ -39,6 +43,7 @@ where
Self {
accounts: self.accounts.clone(),
deposits: self.deposits.clone(),
ledger: self.ledger.clone(),
authz: self.authz.clone(),
outbox: self.outbox.clone(),
}
Expand All @@ -56,14 +61,17 @@ where
pool: &sqlx::PgPool,
authz: &Perms,
outbox: &Outbox<E>,
cala: &CalaLedger,
) -> Result<Self, CoreDepositError> {
let accounts = DepositAccountRepo::new(pool);
let deposits = DepositRepo::new(pool);
let ledger = DepositLedger::init(cala).await?;
let res = Self {
accounts,
deposits,
authz: authz.clone(),
outbox: outbox.clone(),
ledger,
};
Ok(res)
}
Expand All @@ -83,14 +91,19 @@ where
)
.await?;

let account_id = DepositAccountId::new();
let new_account = NewDepositAccount::builder()
.id(DepositAccountId::new())
.id(account_id)
.account_holder_id(holder_id)
.audit_info(audit_info)
.build()
.expect("Could not build new committee");

let account = self.accounts.create(new_account).await?;
let mut op = self.accounts.begin_op().await?;
let account = self.accounts.create_in_op(&mut op, new_account).await?;
self.ledger
.create_account_for_deposit_account(op, account_id, account_id.to_string())
.await?;
Ok(account)
}

Expand Down
11 changes: 10 additions & 1 deletion core/deposit/src/primitives.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
use std::{fmt::Display, str::FromStr};

use authz::AllOrOne;
es_entity::entity_id! { AccountHolderId, DepositAccountId, DepositId }

pub use cala_ledger::primitives::AccountId as LedgerAccountId;

es_entity::entity_id! {
AccountHolderId,
DepositAccountId,
DepositId;

DepositAccountId => LedgerAccountId
}

pub type DepositAccountAllOrOne = AllOrOne<DepositAccountId>;
pub type DepositAllOrOne = AllOrOne<DepositId>;
Expand Down
8 changes: 7 additions & 1 deletion core/deposit/tests/deposit.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use authz::dummy::DummySubject;
use cala_ledger::{CalaLedger, CalaLedgerConfig};
use deposit::*;

pub async fn init_pool() -> anyhow::Result<sqlx::PgPool> {
Expand All @@ -13,7 +14,12 @@ async fn deposit() -> anyhow::Result<()> {
let pool = init_pool().await?;
let outbox = outbox::Outbox::<CoreDepositEvent>::init(&pool).await?;
let authz = authz::dummy::DummyPerms::<CoreDepositAction, CoreDepositObject>::new();
let deposit = CoreDeposit::init(&pool, &authz, &outbox).await?;
let cala_config = CalaLedgerConfig::builder()
.pool(pool.clone())
.exec_migrations(false)
.build()?;
let cala = CalaLedger::init(cala_config).await?;
let deposit = CoreDeposit::init(&pool, &authz, &outbox, &cala).await?;
let account_holder_id = AccountHolderId::new();
let account = deposit
.create_account(&DummySubject, account_holder_id)
Expand Down

0 comments on commit 9b78c78

Please sign in to comment.