Skip to content

Commit

Permalink
call unSlotNo on slotNo to get raw integer value
Browse files Browse the repository at this point in the history
  • Loading branch information
disassembler committed Jan 16, 2021
1 parent 6f41822 commit f682f0a
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/Cardano/CLI/Query.hs
Expand Up @@ -28,6 +28,8 @@ import Database.Persist.Postgresql (Entity, IsolationLevel (Serializab
SqlPersistT, rawSql, runSqlConnWithIsolation)
import System.IO (hPutStr, hPutStrLn, stderr)

import Ouroboros.Network.Block (unSlotNo)

import Cardano.API (SlotNo)
import qualified Cardano.API as Api
import Cardano.API.Extended (VotingKeyPublic, serialiseToBech32')
Expand Down Expand Up @@ -94,14 +96,14 @@ queryStake mSlotRestriction stakeHash = do
-- Get first tx is slot after one we've asked to restrict the
-- query to, we don't want to get this Tx or any later Txs.
let
firstUnwantedTxIdSql = "SELECT tx.id FROM tx LEFT OUTER JOIN block ON block.id = tx.block_id WHERE block.slot_no > " <> T.pack (show slotNo) <> " LIMIT 1";
firstUnwantedTxIdSql = "SELECT tx.id FROM tx LEFT OUTER JOIN block ON block.id = tx.block_id WHERE block.slot_no > " <> T.pack (show $ unSlotNo slotNo) <> " LIMIT 1";

(txids :: [Single Word64]) <- (flip runReaderT) r $ rawSql firstUnwantedTxIdSql []
case txids of
[] -> error $ "No txs found in slot " <> show (slotNo + 1)-- TODO throwError (_NoTxsFoundInSlot # (slotNo + 1))
x1:(x2:xs) -> error $ "Too many txs found for slot " <> show slotNo <> ", add 'LIMIT 1' to query."
(Single txid):[] ->
pure $ "SELECT SUM(tx_out.value) FROM tx_out INNER JOIN tx ON tx.id = tx_out.tx_id LEFT OUTER JOIN tx_in ON tx_out.tx_id = tx_in.tx_out_id AND tx_out.index = tx_in.tx_out_index AND tx_in_id < " <> T.pack (show txid) <> " INNER JOIN block ON block.id = tx.block_id AND block.slot_no < " <> T.pack (show slotNo) <> " WHERE CAST(encode(address_raw, 'hex') AS text) LIKE '%" <> stkHashSql <> "' AND tx_in.tx_in_id is null;"
pure $ "SELECT SUM(tx_out.value) FROM tx_out INNER JOIN tx ON tx.id = tx_out.tx_id LEFT OUTER JOIN tx_in ON tx_out.tx_id = tx_in.tx_out_id AND tx_out.index = tx_in.tx_out_index AND tx_in_id < " <> T.pack (show txid) <> " INNER JOIN block ON block.id = tx.block_id AND block.slot_no < " <> T.pack (show $ unSlotNo slotNo) <> " WHERE CAST(encode(address_raw, 'hex') AS text) LIKE '%" <> stkHashSql <> "' AND tx_in.tx_in_id is null;"

(stakeValues :: [Single (Maybe DbLovelace)]) <- (flip runReaderT) r $ rawSql stakeQuerySql []
case stakeValues of
Expand Down Expand Up @@ -222,7 +224,7 @@ queryVoteRegistration mSlotNo =
in do
let
sql = case mSlotNo of
Just slot -> (sqlBase <> "INNER JOIN block ON block.id = tx.block_id WHERE block.slot_no < " <> T.pack (show slot) <> ";")
Just slot -> (sqlBase <> "INNER JOIN block ON block.id = tx.block_id WHERE block.slot_no < " <> T.pack (show $ unSlotNo slot) <> ";")
Nothing -> (sqlBase <> ";")
r <- ask
(results :: [(Single ByteString, Single TxId, Single (Maybe Text), Single (Maybe Text))]) <- (flip runReaderT) r $ rawSql sql []
Expand Down

0 comments on commit f682f0a

Please sign in to comment.