Skip to content

Commit

Permalink
Added support for oracle endpoint.
Browse files Browse the repository at this point in the history
  • Loading branch information
mdunnio committed Mar 7, 2021
1 parent e67d342 commit 86b2373
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 6 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ Here is a list of implemented/unimplemented features:
- [x] User Account
- [x] Trailing Volume
- ~~[ ] Margin~~ (Disabled)[https://blog.coinbase.com/coinbase-pro-disables-margin-trading-42f5862f8a66]
- [ ] Oracle
- [ ] Get Oracle
- [x] Oracle
- [x] Get Oracle
- Websocket Feed
- [x] Channels
- [x] The heartbeat channel
Expand Down
3 changes: 2 additions & 1 deletion coinbase-pro.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ cabal-version: 1.12
--
-- see: https://github.com/sol/hpack
--
-- hash: add46fa3fa358a678cd7bcb5b32037fe01956ba49cae6d6385b9de0105063e50
-- hash: 0482a3909f589fcef459af8d11acc45ae48f1918d4659454f25b36f471e2764d

name: coinbase-pro
version: 0.9.1.0
Expand Down Expand Up @@ -38,6 +38,7 @@ library
CoinbasePro.Authenticated.Fills
CoinbasePro.Authenticated.Headers
CoinbasePro.Authenticated.Limits
CoinbasePro.Authenticated.Oracle
CoinbasePro.Authenticated.Orders
CoinbasePro.Authenticated.Payment
CoinbasePro.Authenticated.Profile
Expand Down
9 changes: 9 additions & 0 deletions src/lib/CoinbasePro/Authenticated.hs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ module CoinbasePro.Authenticated
, profileTransfer
, createReport
, getReport
, getOracle
) where

import Control.Monad (void)
Expand Down Expand Up @@ -71,6 +72,7 @@ import CoinbasePro.Authenticated.Deposit (CoinbaseDepositRequ
DepositResponse)
import CoinbasePro.Authenticated.Fills (Fill)
import CoinbasePro.Authenticated.Limits (Limits)
import CoinbasePro.Authenticated.Oracle (OracleResponse)
import CoinbasePro.Authenticated.Orders (Order,
PlaceOrderBody (..),
STP, Status (..),
Expand Down Expand Up @@ -488,3 +490,10 @@ getReport :: ReportId -> CBAuthT ClientM ReportResponse
getReport rid = authRequest methodGet requestPath emptyBody $ API.getReport rid
where
requestPath = encodeRequestPath [reportsPath, toText $ unReportId rid]


-- | https://docs.pro.coinbase.com/#oracle
getOracle :: CBAuthT ClientM OracleResponse
getOracle = authRequest methodGet requestPath emptyBody API.getOracle
where
requestPath = encodeRequestPath ["oracle"]
8 changes: 6 additions & 2 deletions src/lib/CoinbasePro/Authenticated/API.hs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ module CoinbasePro.Authenticated.API
, profileTransfer
, createReport
, getReport
, getOracle
) where

import Data.Proxy (Proxy (..))
Expand Down Expand Up @@ -63,6 +64,7 @@ import CoinbasePro.Authenticated.Deposit (CoinbaseDepositRequ
DepositResponse)
import CoinbasePro.Authenticated.Fills (Fill)
import CoinbasePro.Authenticated.Limits (Limits)
import CoinbasePro.Authenticated.Oracle (OracleResponse)
import CoinbasePro.Authenticated.Orders (Order,
PlaceOrderBody (..),
Status (..))
Expand Down Expand Up @@ -131,6 +133,7 @@ type API = "accounts" :> AuthGet [Account]
:<|> "profiles" :> "transfer" :> ReqBody '[JSON] ProfileTransfer :> AuthPost NoContent
:<|> "reports" :> ReqBody '[JSON] ReportRequest :> AuthPost ReportResponse
:<|> "reports" :> Capture "report_id" ReportId :> AuthGet ReportResponse
:<|> "oracle" :> AuthGet OracleResponse


api :: Proxy API
Expand Down Expand Up @@ -168,9 +171,10 @@ profile :: ProfileId -> AuthenticatedRequest (AuthProtect "CBAuth") -> ClientM P
profileTransfer :: ProfileTransfer -> AuthenticatedRequest (AuthProtect "CBAuth") -> ClientM NoContent
createReport :: ReportRequest -> AuthenticatedRequest (AuthProtect "CBAuth") -> ClientM ReportResponse
getReport :: ReportId -> AuthenticatedRequest (AuthProtect "CBAuth") -> ClientM ReportResponse
getOracle :: AuthenticatedRequest (AuthProtect "CBAuth") -> ClientM OracleResponse
accounts
:<|> singleAccount :<|> accountHistory :<|> accountHolds :<|> listOrders :<|> getOrder :<|> getClientOrder :<|> placeOrder
:<|> cancelOrder :<|> cancelAll :<|> fills :<|> fees :<|> trailingVolume :<|> limits :<|> transfers :<|> transfer
:<|> makeDeposit :<|> makeCoinbaseDeposit :<|> cryptoDepositAddress :<|> makeWithdrawal :<|> makeCoinbaseWithdrawal
:<|> makeCryptoWithdrawal :<|> withdrawalFeeEstimate :<|> createStablecoinConversion :<|> paymentMethods :<|>
coinbaseAccounts :<|> profiles :<|> profile :<|> profileTransfer :<|> createReport :<|> getReport = client api
:<|> makeCryptoWithdrawal :<|> withdrawalFeeEstimate :<|> createStablecoinConversion :<|> paymentMethods :<|> coinbaseAccounts
:<|> profiles :<|> profile :<|> profileTransfer :<|> createReport :<|> getReport :<|> getOracle = client api
55 changes: 55 additions & 0 deletions src/lib/CoinbasePro/Authenticated/Oracle.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TemplateHaskell #-}
{-# OPTIONS_GHC -fno-warn-type-defaults #-}

module CoinbasePro.Authenticated.Oracle
( OracleResponse (..)
) where


import Data.Aeson (FromJSON, parseJSON, withObject, (.:))
import Data.Aeson.Casing (snakeCase)
import Data.Aeson.TH (defaultOptions, deriveJSON,
fieldLabelModifier, unwrapUnaryRecords)
import Data.Map.Strict (Map)
import Data.Text (Text)
import Data.Time.Clock (UTCTime)
import Data.Time.Clock.POSIX (posixSecondsToUTCTime)

import CoinbasePro.Types (CurrencyType, Price)


newtype Message = Message Text
deriving Show


deriveJSON defaultOptions
{ fieldLabelModifier = snakeCase
, unwrapUnaryRecords = True
} ''Message


newtype Signature = Signature Text
deriving Show


deriveJSON defaultOptions
{ fieldLabelModifier = snakeCase
, unwrapUnaryRecords = True
} ''Signature


data OracleResponse = OracleResponse
{ timestamp :: UTCTime
, messages :: [Message]
, signatures :: [Signature]
, prices :: Map CurrencyType Price
} deriving Show


instance FromJSON OracleResponse where
parseJSON = withObject "oracle response" $ \o -> OracleResponse
<$> (posixSecondsToUTCTime . realToFrac . read <$> o .: "timestamp")
<*> o .: "messages"
<*> o .: "signatures"
<*> o .: "prices"
2 changes: 1 addition & 1 deletion src/lib/CoinbasePro/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ instance FromJSON CurrencyDetails where


newtype CurrencyType = CurrencyType Text
deriving ToHttpApiData
deriving (Eq, Ord, ToHttpApiData, FromJSONKey)


instance Show CurrencyType where
Expand Down

0 comments on commit 86b2373

Please sign in to comment.