Skip to content

Commit 8f00b93

Browse files
committed
bfx wip
1 parent a92a792 commit 8f00b93

File tree

8 files changed

+131
-102
lines changed

8 files changed

+131
-102
lines changed

pub/bfx/bfx.cabal

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ common pkg-lib
9090
, cryptonite
9191
, envparse
9292
, functora
93+
, functora:cfg
9394
, functora:money
9495
, functora:sql
9596
, http-client
@@ -207,6 +208,7 @@ test-suite bfx-test
207208
, cryptonite
208209
, envparse
209210
, functora
211+
, functora:cfg
210212
, functora:money
211213
, functora:sql
212214
, http-client

pub/bfx/src/Bfx/Data/Env.hs

Lines changed: 9 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
{-# OPTIONS_HADDOCK show-extensions #-}
22

33
module Bfx.Data.Env
4-
( RawEnv (..),
5-
Env (..),
4+
( Env (..),
65
newEnv,
76
sysEnv,
87
)
98
where
109

1110
import Bfx.Data.Web
1211
import Bfx.Import.External
13-
import qualified Data.Aeson as A
1412
import Env
1513
( Mod,
1614
Var,
@@ -22,26 +20,6 @@ import Env
2220
var,
2321
)
2422

25-
data RawEnv = RawEnv
26-
{ rawEnvApiKey :: ApiKey,
27-
rawEnvPrvKey :: PrvKey
28-
}
29-
deriving stock
30-
( Eq,
31-
-- | It's safe to derive 'Show' instance,
32-
-- because 'ApiKey' and 'PrvKey'
33-
-- instances are safe.
34-
Show,
35-
Generic
36-
)
37-
38-
instance FromJSON RawEnv where
39-
parseJSON =
40-
A.genericParseJSON
41-
A.defaultOptions
42-
{ A.fieldLabelModifier = A.camelTo2 '_' . drop 6
43-
}
44-
4523
data Env = Env
4624
{ envNonceGen :: NonceGen,
4725
envApiKey :: ApiKey,
@@ -52,14 +30,14 @@ data Env = Env
5230
Show
5331
)
5432

55-
newEnv :: (MonadIO m) => RawEnv -> m Env
56-
newEnv raw = do
33+
newEnv :: (MonadIO m) => ApiKey -> PrvKey -> m Env
34+
newEnv apiKey prvKey = do
5735
nonceGen <- newNonceGen
58-
pure $
59-
Env
36+
pure
37+
$ Env
6038
{ envNonceGen = nonceGen,
61-
envApiKey = rawEnvApiKey raw,
62-
envPrvKey = rawEnvPrvKey raw
39+
envApiKey = apiKey,
40+
envPrvKey = prvKey
6341
}
6442

6543
sysEnv ::
@@ -71,8 +49,8 @@ sysEnv = do
7149
liftIO
7250
. parse (header "Bfx")
7351
$ Env nonceGen
74-
<$> var (str <=< nonempty) "BITFINEX_API_KEY" op
75-
<*> var (str <=< nonempty) "BITFINEX_PRV_KEY" op
52+
<$> var (str <=< nonempty) "BITFINEX_API_KEY" op
53+
<*> var (str <=< nonempty) "BITFINEX_PRV_KEY" op
7654
where
7755
op :: Mod Var a
7856
op = help mempty

pub/bfx/src/Bfx/Data/Type.hs

Lines changed: 53 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
{-# LANGUAGE TypeApplications #-}
21
{-# OPTIONS_HADDOCK show-extensions #-}
32

43
module Bfx.Data.Type
@@ -59,12 +58,17 @@ newtype OrderId = OrderId
5958
( Eq,
6059
Ord,
6160
Show,
62-
Num,
61+
Read,
6362
ToJSON,
64-
FromJSON
63+
ToJSONKey,
64+
FromJSON,
65+
FromJSONKey,
66+
HasCodec,
67+
HasItemCodec
6568
)
6669
deriving stock
67-
( Generic
70+
( Data,
71+
Generic
6872
)
6973

7074
newtype OrderClientId = OrderClientId
@@ -74,12 +78,17 @@ newtype OrderClientId = OrderClientId
7478
( Eq,
7579
Ord,
7680
Show,
77-
Num,
81+
Read,
7882
ToJSON,
79-
FromJSON
83+
ToJSONKey,
84+
FromJSON,
85+
FromJSONKey,
86+
HasCodec,
87+
HasItemCodec
8088
)
8189
deriving stock
82-
( Generic
90+
( Data,
91+
Generic
8392
)
8493

8594
newtype OrderGroupId = OrderGroupId
@@ -89,12 +98,17 @@ newtype OrderGroupId = OrderGroupId
8998
( Eq,
9099
Ord,
91100
Show,
92-
Num,
101+
Read,
93102
ToJSON,
94-
FromJSON
103+
ToJSONKey,
104+
FromJSON,
105+
FromJSONKey,
106+
HasCodec,
107+
HasItemCodec
95108
)
96109
deriving stock
97-
( Generic
110+
( Data,
111+
Generic
98112
)
99113

100114
data Order (act :: BuyOrSell) (loc :: LocalOrRemote) = Order
@@ -110,7 +124,10 @@ data Order (act :: BuyOrSell) (loc :: LocalOrRemote) = Order
110124
}
111125
deriving stock
112126
( Eq,
127+
Ord,
113128
Show,
129+
Read,
130+
Data,
114131
Generic
115132
)
116133

@@ -143,6 +160,7 @@ data OrderFlag
143160
( Eq,
144161
Ord,
145162
Show,
163+
Data,
146164
Generic,
147165
Enum,
148166
Bounded
@@ -156,10 +174,13 @@ newtype OrderFlagAcc
156174
Show,
157175
Num,
158176
ToJSON,
159-
FromJSON
177+
ToJSONKey,
178+
FromJSON,
179+
FromJSONKey
160180
)
161181
deriving stock
162-
( Generic
182+
( Data,
183+
Generic
163184
)
164185

165186
unOrderFlag :: OrderFlag -> OrderFlagAcc
@@ -189,11 +210,24 @@ data OrderStatus
189210
( Eq,
190211
Ord,
191212
Show,
213+
Read,
192214
Data,
193215
Generic,
194216
Enum,
195217
Bounded
196218
)
219+
deriving
220+
( ToJSON,
221+
ToJSONKey,
222+
FromJSON,
223+
FromJSONKey
224+
)
225+
via GenericType OrderStatus
226+
deriving
227+
( HasCodec,
228+
HasItemCodec
229+
)
230+
via GenericEnum OrderStatus
197231

198232
newOrderStatus ::
199233
Text ->
@@ -393,8 +427,15 @@ data CurrencyPair = CurrencyPair
393427
( Eq,
394428
Ord,
395429
Show,
430+
Read,
431+
Data,
396432
Generic
397433
)
434+
deriving
435+
( HasCodec,
436+
HasItemCodec
437+
)
438+
via GenericType CurrencyPair
398439

399440
instance FromJSON CurrencyPair where
400441
parseJSON =

pub/bfx/src/Bfx/Data/Web.hs

Lines changed: 26 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -20,58 +20,35 @@ import Bfx.Import.External
2020
import qualified Data.Aeson as A
2121
import qualified Data.Aeson.Types as A
2222
import qualified Data.ByteString as BS
23-
import qualified Data.ByteString.Lazy as BL
24-
import qualified Data.Text as T
2523
import qualified Prelude
2624

2725
newtype PrvKey = PrvKey
28-
{ unPrvKey :: BS.ByteString
26+
{ unPrvKey :: Text
2927
}
3028
deriving newtype
3129
( Eq,
3230
Ord,
33-
IsString
31+
Read,
32+
IsString,
33+
HasCodec,
34+
HasItemCodec
3435
)
36+
deriving stock (Data, Generic)
37+
deriving (Show) via Redacted PrvKey
3538

36-
instance From BS.ByteString PrvKey
37-
38-
instance From PrvKey BS.ByteString
39-
40-
instance From (UTF_8 BS.ByteString) PrvKey where
41-
from =
42-
via @BS.ByteString
43-
44-
instance Prelude.Show PrvKey where
45-
show =
46-
const "SECRET"
47-
48-
instance FromJSON PrvKey where
49-
parseJSON =
50-
parseJsonBs
51-
52-
newtype ApiKey
53-
= ApiKey BS.ByteString
39+
newtype ApiKey = ApiKey
40+
{ unApiKey :: Text
41+
}
5442
deriving newtype
5543
( Eq,
5644
Ord,
57-
IsString
45+
Read,
46+
IsString,
47+
HasCodec,
48+
HasItemCodec
5849
)
59-
60-
instance From BS.ByteString ApiKey
61-
62-
instance From ApiKey BS.ByteString
63-
64-
instance From (UTF_8 BS.ByteString) ApiKey where
65-
from =
66-
via @BS.ByteString
67-
68-
instance Prelude.Show ApiKey where
69-
show =
70-
const "SECRET"
71-
72-
instance FromJSON ApiKey where
73-
parseJSON =
74-
parseJsonBs
50+
deriving stock (Data, Generic)
51+
deriving (Show) via Redacted ApiKey
7552

7653
data RequestMethod
7754
= GET
@@ -80,42 +57,35 @@ data RequestMethod
8057
( Eq,
8158
Ord,
8259
Show,
60+
Read,
8361
Data,
8462
Generic
8563
)
8664

87-
newtype BaseUrl
88-
= BaseUrl Text
65+
newtype BaseUrl = BaseUrl
66+
{ unBaseUrl :: Text
67+
}
8968
deriving newtype
9069
( Eq,
9170
Ord,
9271
Show,
72+
Read,
9373
IsString
9474
)
9575

96-
newtype RawResponse
97-
= RawResponse ByteString
76+
newtype RawResponse = RawResponse
77+
{ unRawResponse :: ByteString
78+
}
9879
deriving newtype
9980
( Eq,
100-
Ord
81+
Ord,
82+
Show
10183
)
10284
deriving stock
10385
( Data,
10486
Generic
10587
)
10688

107-
instance From ByteString RawResponse
108-
109-
instance From RawResponse ByteString
110-
111-
instance Show RawResponse where
112-
show x =
113-
case decodeUtf8Strict bs of
114-
Left {} -> "ByteString RawResponse" <> inspect (BS.unpack bs)
115-
Right res -> "Text RawResponse " <> T.unpack res
116-
where
117-
bs = BL.toStrict $ from x
118-
11989
newtype Nonce = Nonce
12090
{ unNonce :: Natural
12191
}

pub/bfx/src/Bfx/Import/External.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ where
77

88
import Control.Concurrent as X (threadDelay)
99
import Control.Monad.Trans.Except as X (catchE, except, throwE)
10-
import Data.Aeson as X (FromJSON (..), FromJSONKey (..), ToJSON (..))
1110
import Data.ByteString.Lazy as X (ByteString)
1211
import Data.Coerce as X (coerce)
1312
import Data.Fixed as X (Fixed, HasResolution (..), showFixed)
@@ -24,6 +23,7 @@ import Data.Metrology.Poly as X
2423
(|/|),
2524
)
2625
import Data.Ratio.Rounding as X (dpRound, sdRound)
26+
import Functora.Cfg as X
2727
import Functora.Money as X
2828
import Functora.Prelude as X hiding (ByteString, Exchange (..), at)
2929
import Functora.Sql as X

pub/bfx/src/Bfx/Orphan.hs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
{-# LANGUAGE TemplateHaskell #-}
2+
{-# OPTIONS_GHC -Wno-orphans #-}
23

34
module Bfx.Orphan () where
45

56
import Bfx.Data.Kind
67
import Bfx.Import.External
78

89
mkFgpt @Method
10+
11+
deriving newtype instance HasCodec CurrencyCode
12+
13+
deriving newtype instance HasItemCodec CurrencyCode

0 commit comments

Comments
 (0)