mod.rs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. use crate::{
  2. asset_manager::AssetDefinition, storage::sqlite::SQLite, storage::Storage, AccountId, Amount,
  3. AssetManager, Error, Ledger, Status, TransactionId,
  4. };
  5. use sqlx::sqlite::SqlitePoolOptions;
  6. #[allow(unused)]
  7. pub async fn get_file_asset_manager_and_ledger<'a>(name: &str) -> (AssetManager, Ledger<SQLite>) {
  8. let pool = SqlitePoolOptions::new()
  9. .max_connections(1)
  10. .idle_timeout(None)
  11. .max_lifetime(None)
  12. .connect(format!("sqlite:///tmp/{}.db", name).as_str())
  13. .await
  14. .expect("pool");
  15. let assets = AssetManager::new(vec![
  16. AssetDefinition::new(1, "BTC", 8),
  17. AssetDefinition::new(2, "USD", 4),
  18. ]);
  19. let db = SQLite::new(pool, assets.clone());
  20. db.setup().await.expect("setup");
  21. (assets.clone(), Ledger::new(db, assets))
  22. }
  23. pub async fn get_asset_manager_and_ledger() -> (AssetManager, Ledger<SQLite>) {
  24. let pool = SqlitePoolOptions::new()
  25. .max_connections(1)
  26. .idle_timeout(None)
  27. .max_lifetime(None)
  28. .connect(":memory:")
  29. .await
  30. .expect("pool");
  31. let assets = AssetManager::new(vec![
  32. AssetDefinition::new(1, "BTC", 8),
  33. AssetDefinition::new(2, "USD", 4),
  34. ]);
  35. let db = SQLite::new(pool, assets.clone());
  36. db.setup().await.expect("setup");
  37. (assets.clone(), Ledger::new(db, assets))
  38. }
  39. pub async fn withdrawal(
  40. ledger: &Ledger<SQLite>,
  41. account_id: &AccountId,
  42. status: Status,
  43. amount: Amount,
  44. ) -> Result<TransactionId, Error> {
  45. Ok(ledger
  46. .withdrawal(account_id, amount, status, "Test".to_owned())
  47. .await?
  48. .id()
  49. .clone())
  50. }
  51. pub async fn deposit<S>(ledger: &Ledger<S>, account_id: &AccountId, amount: Amount) -> TransactionId
  52. where
  53. S: Storage + Send + Sync,
  54. {
  55. ledger
  56. .deposit(account_id, amount, Status::Settled, "Test".to_owned())
  57. .await
  58. .expect("valid tx")
  59. .id()
  60. .clone()
  61. }
  62. mod deposit;
  63. mod negative_deposit;
  64. mod tx;
  65. mod withdrawal;