diff --git a/Cargo.lock b/Cargo.lock index 1aeefe706..81cbe63a6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -711,11 +711,10 @@ dependencies = [ [[package]] name = "cala-cel-interpreter" -version = "0.3.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cb633ce00ca7246885b17091a95228169d2fa3be4d48d1392f87974f3d42f8c9" +version = "0.3.2-dev" +source = "git+https://github.com/galoymoney/cala.git?branch=main#528da8898a9b763c3a513c4eb46e31b03f6e0d4d" dependencies = [ - "cala-cel-parser 0.3.1", + "cala-cel-parser 0.3.2-dev", "chrono", "lazy_static", "rust_decimal", @@ -737,9 +736,8 @@ dependencies = [ [[package]] name = "cala-cel-parser" -version = "0.3.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c8fd69da0ccc13cc45cf6086f01792816562eb646d00e892a241d3c909ac959e" +version = "0.3.2-dev" +source = "git+https://github.com/galoymoney/cala.git?branch=main#528da8898a9b763c3a513c4eb46e31b03f6e0d4d" dependencies = [ "lalrpop", "lalrpop-util", @@ -747,13 +745,12 @@ dependencies = [ [[package]] name = "cala-ledger" -version = "0.3.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c0bdc7e8b6436920b49f0e3b0464d00ffcceb9ffc4405516e1e95d8938c9b993" +version = "0.3.2-dev" +source = "git+https://github.com/galoymoney/cala.git?branch=main#528da8898a9b763c3a513c4eb46e31b03f6e0d4d" dependencies = [ "cached 0.51.4", - "cala-cel-interpreter 0.3.1", - "cala-ledger-core-types 0.3.1", + "cala-cel-interpreter 0.3.2-dev", + "cala-ledger-core-types 0.3.2-dev", "cala-tracing", "chrono", "derive_builder", @@ -798,11 +795,10 @@ dependencies = [ [[package]] name = "cala-ledger-core-types" -version = "0.3.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cf8fb08fd15c491fb2ed2cab45c19ef85946ce7d89ecb5c0e510a4e20d2462f7" +version = "0.3.2-dev" +source = "git+https://github.com/galoymoney/cala.git?branch=main#528da8898a9b763c3a513c4eb46e31b03f6e0d4d" dependencies = [ - "cala-cel-interpreter 0.3.1", + "cala-cel-interpreter 0.3.2-dev", "chrono", "derive_builder", "rust_decimal", @@ -816,9 +812,8 @@ dependencies = [ [[package]] name = "cala-tracing" -version = "0.3.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "54925107e2dcf62b5ef688804c98eb0f459bf3f0e8e460548a61d42396394516" +version = "0.3.2-dev" +source = "git+https://github.com/galoymoney/cala.git?branch=main#528da8898a9b763c3a513c4eb46e31b03f6e0d4d" dependencies = [ "anyhow", "axum-extra", @@ -1384,9 +1379,8 @@ dependencies = [ [[package]] name = "es-entity" -version = "0.3.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "af9a48b924f08bdeba3e50bc6a14ca7ff8ca94abb42f6b35157a420c71551452" +version = "0.3.2-dev" +source = "git+https://github.com/galoymoney/cala.git?branch=main#528da8898a9b763c3a513c4eb46e31b03f6e0d4d" dependencies = [ "async-graphql", "async-trait", @@ -1404,9 +1398,8 @@ dependencies = [ [[package]] name = "es-entity-macros" -version = "0.3.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f06294b7c85dc3c31d390460fe17f8aa97723400186dff68fe43782e6df0f5c9" +version = "0.3.2-dev" +source = "git+https://github.com/galoymoney/cala.git?branch=main#528da8898a9b763c3a513c4eb46e31b03f6e0d4d" dependencies = [ "convert_case", "darling", @@ -4167,9 +4160,8 @@ dependencies = [ [[package]] name = "sim-time" -version = "0.3.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cb970973aa489a7bc2d6b81da17eef2e45a8d5abf2d185284782ee1bbbfd6bdf" +version = "0.3.2-dev" +source = "git+https://github.com/galoymoney/cala.git?branch=main#528da8898a9b763c3a513c4eb46e31b03f6e0d4d" dependencies = [ "chrono", "serde", diff --git a/Cargo.toml b/Cargo.toml index 8c3a838cf..6e4ec4722 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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"] } diff --git a/core/deposit/src/account/entity.rs b/core/deposit/src/account/entity.rs index b93241cdc..beded7b7a 100644 --- a/core/deposit/src/account/entity.rs +++ b/core/deposit/src/account/entity.rs @@ -14,6 +14,7 @@ pub enum DepositAccountEvent { Initialized { id: DepositAccountId, account_holder_id: AccountHolderId, + ledger_account_id: LedgerAccountId, audit_info: AuditInfo, }, } @@ -60,6 +61,7 @@ impl IntoEvents 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, }], ) diff --git a/core/deposit/src/error.rs b/core/deposit/src/error.rs index 69d05813a..dc989b594 100644 --- a/core/deposit/src/error.rs +++ b/core/deposit/src/error.rs @@ -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), } diff --git a/core/deposit/src/ledger/error.rs b/core/deposit/src/ledger/error.rs new file mode 100644 index 000000000..17334fdf1 --- /dev/null +++ b/core/deposit/src/ledger/error.rs @@ -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), +} diff --git a/core/deposit/src/ledger/mod.rs b/core/deposit/src/ledger/mod.rs new file mode 100644 index 000000000..cff71b7dc --- /dev/null +++ b/core/deposit/src/ledger/mod.rs @@ -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 { + Ok(Self { cala: cala.clone() }) + } + + pub async fn create_account_for_deposit_account( + &self, + op: es_entity::DbOp<'_>, + id: impl Into, + 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(()) + } +} diff --git a/core/deposit/src/lib.rs b/core/deposit/src/lib.rs index 734799009..d1b9dc7d5 100644 --- a/core/deposit/src/lib.rs +++ b/core/deposit/src/lib.rs @@ -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 @@ -26,6 +29,7 @@ where { accounts: DepositAccountRepo, deposits: DepositRepo, + ledger: DepositLedger, authz: Perms, outbox: Outbox, } @@ -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(), } @@ -56,14 +61,17 @@ where pool: &sqlx::PgPool, authz: &Perms, outbox: &Outbox, + cala: &CalaLedger, ) -> Result { 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) } @@ -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) } diff --git a/core/deposit/src/primitives.rs b/core/deposit/src/primitives.rs index 529007e59..adf85229c 100644 --- a/core/deposit/src/primitives.rs +++ b/core/deposit/src/primitives.rs @@ -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; pub type DepositAllOrOne = AllOrOne; diff --git a/core/deposit/tests/deposit.rs b/core/deposit/tests/deposit.rs index 2973c562f..88ea69627 100644 --- a/core/deposit/tests/deposit.rs +++ b/core/deposit/tests/deposit.rs @@ -1,4 +1,5 @@ use authz::dummy::DummySubject; +use cala_ledger::{CalaLedger, CalaLedgerConfig}; use deposit::*; pub async fn init_pool() -> anyhow::Result { @@ -13,7 +14,12 @@ async fn deposit() -> anyhow::Result<()> { let pool = init_pool().await?; let outbox = outbox::Outbox::::init(&pool).await?; let authz = authz::dummy::DummyPerms::::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)