From ccc19d635b8264ffbe993ddb43c3a8f2ecde6e9f Mon Sep 17 00:00:00 2001 From: Rene Vergara Date: Mon, 8 Jul 2024 15:17:31 -0500 Subject: [PATCH] Implement confirmed and unconfirmed balance functions --- src/Zenith/DB.hs | 133 ++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 121 insertions(+), 12 deletions(-) diff --git a/src/Zenith/DB.hs b/src/Zenith/DB.hs index 03cc451..f855f02 100644 --- a/src/Zenith/DB.hs +++ b/src/Zenith/DB.hs @@ -1392,6 +1392,19 @@ getBalance pool za = do let oBal = sum oAmts return . fromIntegral $ tBal + sBal + oBal +getUnconfirmedBalance :: ConnectionPool -> ZcashAccountId -> IO Integer +getUnconfirmedBalance pool za = do + trNotes <- getWalletUnspentUnconfirmedTrNotes pool za + let tAmts = map (walletTrNoteValue . entityVal) trNotes + let tBal = sum tAmts + sapNotes <- getWalletUnspentUnconfirmedSapNotes pool za + let sAmts = map (walletSapNoteValue . entityVal) sapNotes + let sBal = sum sAmts + orchNotes <- getWalletUnspentUnconfirmedOrchNotes pool za + let oAmts = map (walletOrchNoteValue . entityVal) orchNotes + let oBal = sum oAmts + return . fromIntegral $ tBal + sBal + oBal + clearWalletTransactions :: ConnectionPool -> IO () clearWalletTransactions pool = do runNoLoggingT $ @@ -1429,10 +1442,42 @@ getWalletUnspentTrNotes pool za = do PS.retryOnBusy $ flip PS.runSqlPool pool $ do select $ do - n <- from $ table @WalletTrNote - where_ (n ^. WalletTrNoteAccId ==. val za) - where_ (n ^. WalletTrNoteSpent ==. val False) - pure n + (txs :& tNotes) <- + from $ table @WalletTransaction `innerJoin` table @WalletTrNote `on` + (\(txs :& tNotes) -> + txs ^. WalletTransactionId ==. tNotes ^. WalletTrNoteTx) + where_ (tNotes ^. WalletTrNoteAccId ==. val za) + where_ (tNotes ^. WalletTrNoteSpent ==. val False) + where_ + ((tNotes ^. WalletTrNoteChange ==. val True &&. txs ^. + WalletTransactionConf >=. + val 3) ||. + (tNotes ^. WalletTrNoteChange ==. val False &&. txs ^. + WalletTransactionConf >=. + val 10)) + pure tNotes + +getWalletUnspentUnconfirmedTrNotes :: + ConnectionPool -> ZcashAccountId -> IO [Entity WalletTrNote] +getWalletUnspentUnconfirmedTrNotes pool za = do + runNoLoggingT $ + PS.retryOnBusy $ + flip PS.runSqlPool pool $ do + select $ do + (txs :& tNotes) <- + from $ table @WalletTransaction `innerJoin` table @WalletTrNote `on` + (\(txs :& tNotes) -> + txs ^. WalletTransactionId ==. tNotes ^. WalletTrNoteTx) + where_ (tNotes ^. WalletTrNoteAccId ==. val za) + where_ (tNotes ^. WalletTrNoteSpent ==. val False) + where_ + ((tNotes ^. WalletTrNoteChange ==. val True &&. txs ^. + WalletTransactionConf <. + val 3) ||. + (tNotes ^. WalletTrNoteChange ==. val False &&. txs ^. + WalletTransactionConf <. + val 10)) + pure tNotes getWalletUnspentSapNotes :: ConnectionPool -> ZcashAccountId -> IO [Entity WalletSapNote] @@ -1441,10 +1486,42 @@ getWalletUnspentSapNotes pool za = do PS.retryOnBusy $ flip PS.runSqlPool pool $ do select $ do - n1 <- from $ table @WalletSapNote - where_ (n1 ^. WalletSapNoteAccId ==. val za) - where_ (n1 ^. WalletSapNoteSpent ==. val False) - pure n1 + (txs :& sNotes) <- + from $ table @WalletTransaction `innerJoin` table @WalletSapNote `on` + (\(txs :& sNotes) -> + txs ^. WalletTransactionId ==. sNotes ^. WalletSapNoteTx) + where_ (sNotes ^. WalletSapNoteAccId ==. val za) + where_ (sNotes ^. WalletSapNoteSpent ==. val False) + where_ + ((sNotes ^. WalletSapNoteChange ==. val True &&. txs ^. + WalletTransactionConf >=. + val 3) ||. + (sNotes ^. WalletSapNoteChange ==. val False &&. txs ^. + WalletTransactionConf >=. + val 10)) + pure sNotes + +getWalletUnspentUnconfirmedSapNotes :: + ConnectionPool -> ZcashAccountId -> IO [Entity WalletSapNote] +getWalletUnspentUnconfirmedSapNotes pool za = do + runNoLoggingT $ + PS.retryOnBusy $ + flip PS.runSqlPool pool $ do + select $ do + (txs :& sNotes) <- + from $ table @WalletTransaction `innerJoin` table @WalletSapNote `on` + (\(txs :& sNotes) -> + txs ^. WalletTransactionId ==. sNotes ^. WalletSapNoteTx) + where_ (sNotes ^. WalletSapNoteAccId ==. val za) + where_ (sNotes ^. WalletSapNoteSpent ==. val False) + where_ + ((sNotes ^. WalletSapNoteChange ==. val True &&. txs ^. + WalletTransactionConf <. + val 3) ||. + (sNotes ^. WalletSapNoteChange ==. val False &&. txs ^. + WalletTransactionConf <. + val 10)) + pure sNotes getWalletUnspentOrchNotes :: ConnectionPool -> ZcashAccountId -> IO [Entity WalletOrchNote] @@ -1453,10 +1530,42 @@ getWalletUnspentOrchNotes pool za = do PS.retryOnBusy $ flip PS.runSqlPool pool $ do select $ do - n2 <- from $ table @WalletOrchNote - where_ (n2 ^. WalletOrchNoteAccId ==. val za) - where_ (n2 ^. WalletOrchNoteSpent ==. val False) - pure n2 + (txs :& oNotes) <- + from $ table @WalletTransaction `innerJoin` table @WalletOrchNote `on` + (\(txs :& oNotes) -> + txs ^. WalletTransactionId ==. oNotes ^. WalletOrchNoteTx) + where_ (oNotes ^. WalletOrchNoteAccId ==. val za) + where_ (oNotes ^. WalletOrchNoteSpent ==. val False) + where_ + ((oNotes ^. WalletOrchNoteChange ==. val True &&. txs ^. + WalletTransactionConf >=. + val 3) ||. + (oNotes ^. WalletOrchNoteChange ==. val False &&. txs ^. + WalletTransactionConf >=. + val 10)) + pure oNotes + +getWalletUnspentUnconfirmedOrchNotes :: + ConnectionPool -> ZcashAccountId -> IO [Entity WalletOrchNote] +getWalletUnspentUnconfirmedOrchNotes pool za = do + runNoLoggingT $ + PS.retryOnBusy $ + flip PS.runSqlPool pool $ do + select $ do + (txs :& oNotes) <- + from $ table @WalletTransaction `innerJoin` table @WalletOrchNote `on` + (\(txs :& oNotes) -> + txs ^. WalletTransactionId ==. oNotes ^. WalletOrchNoteTx) + where_ (oNotes ^. WalletOrchNoteAccId ==. val za) + where_ (oNotes ^. WalletOrchNoteSpent ==. val False) + where_ + ((oNotes ^. WalletOrchNoteChange ==. val True &&. txs ^. + WalletTransactionConf <. + val 3) ||. + (oNotes ^. WalletOrchNoteChange ==. val False &&. txs ^. + WalletTransactionConf <. + val 10)) + pure oNotes selectUnspentNotes :: ConnectionPool