mod.rs 2.1 KB

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