Skip to content
This repository has been archived by the owner on Dec 8, 2022. It is now read-only.

Commit

Permalink
Merge pull request #46 from input-output-hk/ksaric/CAD-1462
Browse files Browse the repository at this point in the history
[CAD-1462] Clarify insert ticker command name and report error if ticker already exists
  • Loading branch information
ksaric committed Aug 11, 2020
2 parents 341e349 + 627b21f commit 33c2c83
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 18 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ Currently, the SMASH service works by allowing superusers to insert the ticker n

There is a CLI utility for doing exactly that. If you want to reserve the ticker name "SALAD" for the specific metadata hash "2560993cf1b6f3f1ebde429f062ce48751ed6551c2629ce62e4e169f140a3524", then you would reserve it like this:
```
SMASHPGPASSFILE=config/pgpass stack run smash-exe -- insert-ticker-name --tickerName "SALAD" --poolhash "2560993cf1b6f3f1ebde429f062ce48751ed6551c2629ce62e4e169f140a3524"
SMASHPGPASSFILE=config/pgpass stack run smash-exe -- reserve-ticker-name --tickerName "SALAD" --poolhash "2560993cf1b6f3f1ebde429f062ce48751ed6551c2629ce62e4e169f140a3524"
```

If somebody adds the ticker name that exists there, it will not be returned, but it will return 404.
Expand Down
23 changes: 13 additions & 10 deletions app/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ data Command
| RunApplication
| RunApplicationWithDbSync SmashDbSyncNodeParams
| InsertPool FilePath PoolId PoolMetadataHash
| InsertTickerName Text PoolMetadataHash
| ReserveTickerName Text PoolMetadataHash

runCommand :: Command -> IO ()
runCommand cmd =
Expand All @@ -61,10 +61,13 @@ runCommand cmd =
(\err -> putTextLn $ "Error occured. " <> renderLookupFail err)
(\_ -> putTextLn "Insertion completed!")
result
InsertTickerName tickerName poolHash -> do
putTextLn "Inserting reserved ticker name!"
void $ runTickerNameInsertion tickerName poolHash

ReserveTickerName tickerName poolHash -> do
putTextLn "Reserving ticker name!"
result <- runTickerNameInsertion tickerName poolHash
either
(\err -> putTextLn $ "Reserved ticker name not inserted! " <> renderLookupFail err)
(\_ -> putTextLn "Ticker name inserted into the database reserved!")
result

doCreateMigration :: SmashMigrationDir -> IO ()
doCreateMigration mdir = do
Expand Down Expand Up @@ -149,8 +152,8 @@ pCommand =
( Opt.info pInsertPool
$ Opt.progDesc "Inserts the pool into the database (utility)."
)
<> Opt.command "insert-ticker-name"
( Opt.info pInsertTickerName
<> Opt.command "reserve-ticker-name"
( Opt.info pReserveTickerName
$ Opt.progDesc "Inserts the ticker name into the database (utility)."
)
)
Expand Down Expand Up @@ -179,9 +182,9 @@ pCommand =
InsertPool <$> pFilePath <*> pPoolId <*> pPoolHash

-- For inserting ticker names.
pInsertTickerName :: Parser Command
pInsertTickerName =
InsertTickerName <$> pTickerName <*> pPoolHash
pReserveTickerName :: Parser Command
pReserveTickerName =
ReserveTickerName <$> pTickerName <*> pPoolHash


pPoolId :: Parser PoolId
Expand Down
7 changes: 7 additions & 0 deletions src/Cardano/Db/Error.hs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ data DBFail
| PoolDelisted
| UnableToEncodePoolMetadataToJSON !Text
| UnknownError !Text
| ReservedTickerAlreadyInserted !Text
deriving (Eq, Show, Generic)

{-
Expand Down Expand Up @@ -80,6 +81,11 @@ instance ToJSON DBFail where
[ "code" .= String "UnknownError"
, "description" .= String (renderLookupFail failure)
]
toJSON failure@(ReservedTickerAlreadyInserted _tickerName) =
object
[ "code" .= String "ReservedTickerAlreadyInserted"
, "description" .= String (renderLookupFail failure)
]


renderLookupFail :: DBFail -> Text
Expand All @@ -93,4 +99,5 @@ renderLookupFail lf =
PoolDelisted -> "The pool has been delisted!"
UnableToEncodePoolMetadataToJSON err -> "Unable to encode the content to JSON. " <> err
UnknownError text -> "Unknown error. Context: " <> text
ReservedTickerAlreadyInserted tickerName -> "Ticker '" <> tickerName <> "' has already been inserted."

12 changes: 9 additions & 3 deletions src/Cardano/Db/Insert.hs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ import Control.Monad.IO.Class (MonadIO)
import Control.Monad.Trans.Reader (ReaderT)

import Database.Persist.Class (AtLeastOneUniqueKey, Key, PersistEntityBackend,
getByValue, insert)
getByValue, insert, checkUnique)
import Database.Persist.Sql (SqlBackend)
import Database.Persist.Types (entityKey)

import Cardano.Db.Schema
import Cardano.Db.Error

insertBlock :: (MonadIO m) => Block -> ReaderT SqlBackend m BlockId
insertBlock = insertByReturnKey
Expand All @@ -41,8 +42,13 @@ insertPoolMetadataReference
-> ReaderT SqlBackend m PoolMetadataReferenceId
insertPoolMetadataReference = insertByReturnKey

insertReservedTicker :: (MonadIO m) => ReservedTicker -> ReaderT SqlBackend m ReservedTickerId
insertReservedTicker = insertByReturnKey
insertReservedTicker :: (MonadIO m) => ReservedTicker -> ReaderT SqlBackend m (Either DBFail ReservedTickerId)
insertReservedTicker reservedTicker = do
isUnique <- checkUnique reservedTicker
-- If there is no unique constraint violated, insert, otherwise return error.
case isUnique of
Nothing -> Right <$> insertByReturnKey reservedTicker
Just _key -> return . Left . ReservedTickerAlreadyInserted $ reservedTickerName reservedTicker

insertDelistedPool :: (MonadIO m) => DelistedPool -> ReaderT SqlBackend m DelistedPoolId
insertDelistedPool = insertByReturnKey
Expand Down
7 changes: 3 additions & 4 deletions src/DB.hs
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,8 @@ postgresqlDataLayer = DataLayer
}
return poolMetadataRefId

, dlAddReservedTicker = \tickerName poolMetadataHash -> do
reservedTickerId <- runDbAction Nothing $ insertReservedTicker $ ReservedTicker tickerName poolMetadataHash
return $ Right reservedTickerId
, dlAddReservedTicker = \tickerName poolMetadataHash ->
runDbAction Nothing $ insertReservedTicker $ ReservedTicker tickerName poolMetadataHash

, dlCheckReservedTicker = \tickerName ->
runDbAction Nothing $ queryReservedTicker tickerName
Expand All @@ -142,7 +141,7 @@ postgresqlDataLayer = DataLayer
runDbAction Nothing $ queryDelistedPool poolId

, dlAddDelistedPool = \poolId -> do
_ <- runDbAction Nothing $ insertDelistedPool $ DelistedPool poolId
delistedPoolId <- runDbAction Nothing $ insertDelistedPool $ DelistedPool poolId
return $ Right poolId

, dlGetAdminUsers = do
Expand Down

0 comments on commit 33c2c83

Please sign in to comment.