Skip to content

Commit 8894f6e

Browse files
committed
[cli] Move anti oracle token get to anti token
1 parent 1514e3a commit 8894f6e

File tree

4 files changed

+71
-44
lines changed

4 files changed

+71
-44
lines changed

cli/src/Cli.hs

Lines changed: 68 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,37 @@ module Cli
44
) where
55

66
import Control.Monad.IO.Class (MonadIO (..))
7-
import Core.Context (withContext)
7+
import Control.Monad.Trans.Class (MonadTrans (..))
8+
import Core.Context (askConfig, askMpfs, askValidation, withContext)
89
import Core.Types.Basic (RequestRefId, TokenId)
910
import Core.Types.MPFS (MPFSClient (..))
1011
import Core.Types.Tx (TxHash, WithTxHash (..))
1112
import Core.Types.Wallet (Wallet)
13+
import Data.Functor.Identity (Identity (..))
1214
import Lib.GitHub (getOAUth)
15+
import Lib.JSON.Canonical.Extra
1316
import MPFS.API
14-
( getTokenFacts
17+
( MPFS (..)
18+
, getTokenFacts
1519
, mpfsClient
1620
, retractChange
1721
)
1822
import Oracle.Cli (OracleCommand (..), oracleCmd)
23+
import Oracle.Types
24+
( RequestValidationFailure
25+
, Token (..)
26+
, TokenState (..)
27+
, fmapMToken
28+
)
29+
import Oracle.Validate.Request (validateRequest)
30+
import Oracle.Validate.Types
31+
( AValidationResult
32+
, ValidationResult
33+
, liftMaybe
34+
, runValidate
35+
)
1936
import Submitting (Submission (..))
20-
import Text.JSON.Canonical (JSValue)
37+
import Text.JSON.Canonical (FromJSON (..), JSValue, ToJSON (..))
2138
import User.Agent.Cli
2239
( AgentCommand (..)
2340
, IsReady (NotReady)
@@ -38,6 +55,11 @@ data Command a where
3855
:: MPFSClient -> Wallet -> RequestRefId -> Command TxHash
3956
GetFacts :: MPFSClient -> TokenId -> Command JSValue
4057
Wallet :: WalletCommand a -> Command a
58+
GetToken
59+
:: MPFSClient
60+
-> TokenId
61+
-> Command
62+
(AValidationResult TokenInfoFailure (Token WithValidation))
4163

4264
data SetupError = TokenNotSpecified
4365
deriving (Show, Eq)
@@ -87,3 +109,46 @@ cmd = \case
87109
GetFacts MPFSClient{runMPFS} tokenId -> do
88110
runMPFS $ getTokenFacts tokenId
89111
Wallet walletCommand -> liftIO $ walletCmd walletCommand
112+
GetToken
113+
MPFSClient{runMPFS, submitTx}
114+
tk -> do
115+
auth <- getOAUth
116+
runMPFS
117+
$ withContext
118+
mpfsClient
119+
(mkValidation auth)
120+
submitTx
121+
$ do
122+
validation <- askValidation $ Just tk
123+
mconfig <- askConfig tk
124+
mpfs <- askMpfs
125+
lift $ runValidate $ do
126+
mpendings <- lift $ fromJSON <$> mpfsGetToken mpfs tk
127+
token <- liftMaybe (TokenInfoTokenNotParsable tk) mpendings
128+
let oracle = tokenOwner $ tokenState token
129+
f (Identity req) = do
130+
r <-
131+
runValidate
132+
$ validateRequest oracle mconfig validation req
133+
pure $ WithValidation r req
134+
lift $ fmapMToken f token
135+
136+
newtype TokenInfoFailure = TokenInfoTokenNotParsable TokenId
137+
deriving (Show, Eq)
138+
139+
instance Monad m => ToJSON m TokenInfoFailure where
140+
toJSON (TokenInfoTokenNotParsable tk) =
141+
object ["tokenNotParsable" .= tk]
142+
143+
data WithValidation x = WithValidation
144+
{ validation :: ValidationResult RequestValidationFailure
145+
, request :: x
146+
}
147+
deriving (Show, Eq)
148+
149+
instance (Monad m, ToJSON m x) => ToJSON m (WithValidation x) where
150+
toJSON (WithValidation mvalidation request) =
151+
object
152+
[ "validation" .= mvalidation
153+
, "request" .= request
154+
]

cli/src/Options.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ commandParser =
6060
retractRequestOptions
6161
, command "facts" "Get token facts"
6262
$ fmap Box . GetFacts <$> mpfsClientOption <*> tokenIdOption
63+
, command "token" "Get the token content"
64+
$ fmap Box . GetToken <$> mpfsClientOption <*> tokenIdOption
6365
]
6466

6567
optionsParser :: Parser (Box Options)

cli/src/Oracle/Token/Cli.hs

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ import Oracle.Types
3131
, RequestZoo
3232
, Token (..)
3333
, TokenState (..)
34-
, fmapMToken
3534
, requestZooRefId
3635
)
3736
import Oracle.Validate.Request
@@ -40,7 +39,6 @@ import Oracle.Validate.Request
4039
import Oracle.Validate.Types
4140
( AValidationResult (..)
4241
, Validate
43-
, ValidationResult
4442
, liftMaybe
4543
, mapFailure
4644
, notValidated
@@ -103,18 +101,7 @@ instance Monad m => ToJSON m TokenUpdateFailure where
103101
TokenUpdateNotRequestedFromTokenOwner ->
104102
toJSON ("Token update not requested from token owner" :: String)
105103

106-
newtype TokenInfoFailure = TokenInfoTokenNotParsable TokenId
107-
deriving (Show, Eq)
108-
109-
instance Monad m => ToJSON m TokenInfoFailure where
110-
toJSON (TokenInfoTokenNotParsable tk) =
111-
object ["tokenNotParsable" .= tk]
112-
113104
data TokenCommand a where
114-
GetToken
115-
:: TokenId
116-
-> TokenCommand
117-
(AValidationResult TokenInfoFailure (Token WithValidation))
118105
BootToken :: Wallet -> TokenCommand (WithTxHash TokenId)
119106
UpdateToken
120107
:: TokenId
@@ -142,39 +129,13 @@ promoteFailure req =
142129
. TokenUpdateRequestValidationFailure
143130
)
144131

145-
data WithValidation x = WithValidation
146-
{ validation :: ValidationResult RequestValidationFailure
147-
, request :: x
148-
}
149-
deriving (Show, Eq)
150-
151-
instance (Monad m, ToJSON m x) => ToJSON m (WithValidation x) where
152-
toJSON (WithValidation mvalidation request) =
153-
object
154-
[ "validation" .= mvalidation
155-
, "request" .= request
156-
]
157-
158132
tokenCmdCore
159133
:: (MonadIO m, MonadMask m)
160134
=> TokenCommand a
161135
-> WithContext m a
162136
tokenCmdCore command = do
163137
mpfs <- askMpfs
164138
case command of
165-
GetToken tk -> do
166-
validation <- askValidation $ Just tk
167-
mconfig <- askConfig tk
168-
lift $ runValidate $ do
169-
mpendings <- lift $ fromJSON <$> mpfsGetToken mpfs tk
170-
token <- liftMaybe (TokenInfoTokenNotParsable tk) mpendings
171-
let oracle = tokenOwner $ tokenState token
172-
f (Identity req) = do
173-
r <-
174-
runValidate
175-
$ validateRequest oracle mconfig validation req
176-
pure $ WithValidation r req
177-
lift $ fmapMToken f token
178139
-- validateRequest oracle mconfig validation
179140
UpdateToken tk wallet wanted -> do
180141
Submission submit <- ($ wallet) <$> askSubmit

cli/src/Oracle/Token/Options.hs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ tokenCommandParser
2626
:: Parser (Box TokenCommand)
2727
tokenCommandParser =
2828
commands
29-
[ command "get" "Get the token" $ Box . GetToken <$> tokenIdOption
30-
, command "update" "Update the token"
29+
[ command "update" "Update the token"
3130
$ fmap (fmap Box) . UpdateToken
3231
<$> tokenIdOption
3332
<*> walletOption

0 commit comments

Comments
 (0)