This repository has been archived by the owner on Apr 6, 2020. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #108 from input-output-hk/erikd/genesis-address
webapi: Implement /api/genesis/address endpoint
- Loading branch information
Showing
5 changed files
with
118 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
packages: | ||
cardano-explorer-db | ||
cardano-explorer-db/test | ||
cardano-explorer-node | ||
cardano-explorer | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
cardano-explorer/src/Explorer/Web/Server/GenesisAddress.hs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
{-# LANGUAGE OverloadedStrings #-} | ||
module Explorer.Web.Server.GenesisAddress | ||
( genesisAddressInfo | ||
) where | ||
|
||
import Control.Monad (when) | ||
import Control.Monad.IO.Class (MonadIO) | ||
import Control.Monad.Trans.Reader (ReaderT) | ||
|
||
import Data.Maybe (fromMaybe) | ||
import Data.Text (Text) | ||
import Data.Word (Word64) | ||
|
||
import Database.Esqueleto (InnerJoin (..), Value, (^.), (==.), | ||
desc, from, limit, offset, on, orderBy, select, unValue, val, where_) | ||
import Database.Persist.Sql (SqlBackend) | ||
|
||
import Explorer.DB (EntityField (..), txOutSpentB, txOutSpentP, txOutUnspentP) | ||
|
||
import Explorer.Web.ClientTypes (CAddress (..), CAddressesFilter (..), | ||
CGenesisAddressInfo (..), mkCCoin) | ||
import Explorer.Web.Error (ExplorerError (..)) | ||
import Explorer.Web.Server.Types (PageNo (..), PageSize (..)) | ||
import Explorer.Web.Server.Util (runQuery, toPageSize) | ||
|
||
import Servant (Handler) | ||
|
||
|
||
genesisAddressInfo | ||
:: SqlBackend -> Maybe PageNo -> Maybe PageSize -> Maybe CAddressesFilter | ||
-> Handler (Either ExplorerError [CGenesisAddressInfo]) | ||
-- filter redeemed addresses | ||
genesisAddressInfo backend mPage mPageSize mAddrFilter = | ||
if unPageSize pageSize < 1 | ||
then pure $ Left (Internal "Page size must be greater than 1.") | ||
else genesisAddrInfo backend page pageSize addrFilter | ||
where | ||
pageSize = toPageSize mPageSize | ||
page = fromMaybe (PageNo 0) mPage | ||
addrFilter = fromMaybe AllAddresses mAddrFilter | ||
|
||
genesisAddrInfo | ||
:: SqlBackend -> PageNo -> PageSize -> CAddressesFilter | ||
-> Handler (Either ExplorerError [CGenesisAddressInfo]) | ||
-- filter redeemed addresses | ||
genesisAddrInfo backend page pageSize addrFilter = | ||
runQuery backend $ | ||
case addrFilter of | ||
RedeemedAddresses -> Right <$> queryRedeemedGenesisAddresses page pageSize | ||
NonRedeemedAddresses -> Right <$> queryNonRedeemedGenesisAddresses page pageSize | ||
AllAddresses -> Right <$> queryAllGenesisAddresses page pageSize | ||
|
||
queryRedeemedGenesisAddresses | ||
:: MonadIO m | ||
=> PageNo -> PageSize | ||
-> ReaderT SqlBackend m [CGenesisAddressInfo] | ||
queryRedeemedGenesisAddresses (PageNo page) (PageSize pageSize) = do | ||
rows <- select . from $ \ (blk `InnerJoin` tx `InnerJoin` txOut) -> do | ||
on (tx ^. TxId ==. txOut ^. TxOutTxId) | ||
on (blk ^. BlockId ==. tx ^. TxBlock) | ||
-- Only the initial genesis block has a size of 0. | ||
where_ (blk ^. BlockSize ==. val 0) | ||
txOutSpentP txOut | ||
orderBy [desc (txOut ^. TxOutValue)] | ||
when (page > 0) $ | ||
offset (fromIntegral page) | ||
limit (fromIntegral pageSize) | ||
pure (txOut ^. TxOutAddress, txOut ^. TxOutValue, txOutSpentB txOut) | ||
pure $ map mkCGenesisAddressInfo rows | ||
|
||
queryNonRedeemedGenesisAddresses | ||
:: MonadIO m | ||
=> PageNo -> PageSize | ||
-> ReaderT SqlBackend m [CGenesisAddressInfo] | ||
queryNonRedeemedGenesisAddresses (PageNo page) (PageSize pageSize) = do | ||
rows <- select . from $ \ (blk `InnerJoin` tx `InnerJoin` txOut) -> do | ||
on (tx ^. TxId ==. txOut ^. TxOutTxId) | ||
on (blk ^. BlockId ==. tx ^. TxBlock) | ||
-- Only the initial genesis block has a size of 0. | ||
where_ (blk ^. BlockSize ==. val 0) | ||
txOutUnspentP txOut | ||
orderBy [desc (txOut ^. TxOutValue)] | ||
when (page > 0) $ | ||
offset (fromIntegral page) | ||
limit (fromIntegral pageSize) | ||
pure (txOut ^. TxOutAddress, txOut ^. TxOutValue, txOutSpentB txOut) | ||
pure $ map mkCGenesisAddressInfo rows | ||
|
||
queryAllGenesisAddresses | ||
:: MonadIO m | ||
=> PageNo -> PageSize | ||
-> ReaderT SqlBackend m [CGenesisAddressInfo] | ||
queryAllGenesisAddresses (PageNo page) (PageSize pageSize) = do | ||
rows <- select . from $ \ (blk `InnerJoin` tx `InnerJoin` txOut) -> do | ||
on (tx ^. TxId ==. txOut ^. TxOutTxId) | ||
on (blk ^. BlockId ==. tx ^. TxBlock) | ||
-- Only the initial genesis block has a size of 0. | ||
where_ (blk ^. BlockSize ==. val 0) | ||
orderBy [desc (txOut ^. TxOutValue)] | ||
when (page > 0) $ | ||
offset (fromIntegral page) | ||
limit (fromIntegral pageSize) | ||
pure (txOut ^. TxOutAddress, txOut ^. TxOutValue, txOutSpentB txOut) | ||
pure $ map mkCGenesisAddressInfo rows | ||
|
||
|
||
mkCGenesisAddressInfo :: (Value Text, Value Word64, Value Bool) -> CGenesisAddressInfo | ||
mkCGenesisAddressInfo (vaddr, vvalue, vRedeemed) = | ||
CGenesisAddressInfo | ||
{ cgaiCardanoAddress = CAddress (unValue vaddr) | ||
, cgaiGenesisAmount = mkCCoin (fromIntegral $ unValue vvalue) | ||
, cgaiIsRedeemed = unValue vRedeemed | ||
} |