Skip to content

Commit

Permalink
Added support for stablecoin conversions.
Browse files Browse the repository at this point in the history
  • Loading branch information
mdunnio committed Mar 7, 2021
1 parent 8e174d1 commit fda2b6b
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 4 deletions.
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: 99c50583d7d248741138631c4b28753ea1333f29565c1567516c183489a6d05d
-- hash: add46fa3fa358a678cd7bcb5b32037fe01956ba49cae6d6385b9de0105063e50

name: coinbase-pro
version: 0.9.1.0
Expand Down Expand Up @@ -33,6 +33,7 @@ library
CoinbasePro.Authenticated.Accounts
CoinbasePro.Authenticated.API
CoinbasePro.Authenticated.CoinbaseAccounts
CoinbasePro.Authenticated.Conversion
CoinbasePro.Authenticated.Deposit
CoinbasePro.Authenticated.Fills
CoinbasePro.Authenticated.Headers
Expand Down
13 changes: 12 additions & 1 deletion src/lib/CoinbasePro/Authenticated.hs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ module CoinbasePro.Authenticated
, makeCoinbaseWithdrawal
, makeCryptoWithdrawal
, withdrawalFeeEstimate
, createStablecoinConversion
, paymentMethods
, coinbaseAccounts
, profiles
Expand Down Expand Up @@ -62,6 +63,8 @@ import CoinbasePro.Authenticated.Accounts (Account,
Fees, Hold,
TrailingVolume (..))
import CoinbasePro.Authenticated.CoinbaseAccounts (CoinbaseAccount)
import CoinbasePro.Authenticated.Conversion (StablecoinConversionRequest (..),
StablecoinConversionResponse)
import CoinbasePro.Authenticated.Deposit (CoinbaseDepositRequest (..),
CryptoDepositAddress,
DepositRequest (..),
Expand All @@ -85,7 +88,6 @@ import CoinbasePro.Authenticated.Request (CBAuthT (..),
authRequest)
import CoinbasePro.Authenticated.Transfer (Transfer,
TransferType (..))

import CoinbasePro.Authenticated.Withdrawal (CoinbaseWithdrawalRequest (..),
CryptoWithdrawalRequest (..),
CryptoWithdrawalResponse,
Expand Down Expand Up @@ -422,6 +424,15 @@ withdrawalFeeEstimate cur addr =
requestPath = encodeRequestPath [withdrawalsPath, "fee-estimate"] <> query


-- | https://docs.pro.coinbase.com/#stablecoin-conversions
createStablecoinConversion :: CurrencyType -> CurrencyType -> Double -> CBAuthT ClientM StablecoinConversionResponse
createStablecoinConversion fromCur toCur amt =
authRequest methodPost requestPath (encodeBody body) $ API.createStablecoinConversion body
where
requestPath = encodeRequestPath ["conversions"]
body = StablecoinConversionRequest fromCur toCur amt


-- | https://docs.pro.coinbase.com/#list-payment-methods
paymentMethods :: CBAuthT ClientM [PaymentMethod]
paymentMethods = authRequest methodGet requestPath emptyBody API.paymentMethods
Expand Down
9 changes: 7 additions & 2 deletions src/lib/CoinbasePro/Authenticated/API.hs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ module CoinbasePro.Authenticated.API
, makeCoinbaseWithdrawal
, makeCryptoWithdrawal
, withdrawalFeeEstimate
, createStablecoinConversion
, paymentMethods
, coinbaseAccounts
, profiles
Expand Down Expand Up @@ -54,6 +55,8 @@ import CoinbasePro.Authenticated.Accounts (Account,
Fees, Hold,
TrailingVolume)
import CoinbasePro.Authenticated.CoinbaseAccounts (CoinbaseAccount)
import CoinbasePro.Authenticated.Conversion (StablecoinConversionRequest,
StablecoinConversionResponse)
import CoinbasePro.Authenticated.Deposit (CoinbaseDepositRequest,
CryptoDepositAddress,
DepositRequest,
Expand Down Expand Up @@ -120,6 +123,7 @@ type API = "accounts" :> AuthGet [Account]
:> QueryParam' '[Required] "currency" CurrencyType
:> QueryParam' '[Required] "crypto_address" CryptoAddress
:> AuthGet WithdrawalFeeEstimateResponse
:<|> "conversions" :> ReqBody '[JSON] StablecoinConversionRequest :> AuthPost StablecoinConversionResponse
:<|> "payment-methods" :> AuthGet [PaymentMethod]
:<|> "coinbase-accounts" :> AuthGet [CoinbaseAccount]
:<|> "profiles" :> QueryParam "active" Bool :> AuthGet [Profile]
Expand Down Expand Up @@ -156,6 +160,7 @@ makeWithdrawal :: WithdrawalRequest -> AuthenticatedRequest (AuthProtect "CBAuth
makeCoinbaseWithdrawal :: CoinbaseWithdrawalRequest -> AuthenticatedRequest (AuthProtect "CBAuth") -> ClientM WithdrawalResponse
makeCryptoWithdrawal :: CryptoWithdrawalRequest -> AuthenticatedRequest (AuthProtect "CBAuth") -> ClientM CryptoWithdrawalResponse
withdrawalFeeEstimate :: CurrencyType -> CryptoAddress -> AuthenticatedRequest (AuthProtect "CBAuth") -> ClientM WithdrawalFeeEstimateResponse
createStablecoinConversion :: StablecoinConversionRequest -> AuthenticatedRequest (AuthProtect "CBAuth") -> ClientM StablecoinConversionResponse
paymentMethods :: AuthenticatedRequest (AuthProtect "CBAuth") -> ClientM [PaymentMethod]
coinbaseAccounts :: AuthenticatedRequest (AuthProtect "CBAuth") -> ClientM [CoinbaseAccount]
profiles :: Maybe Bool -> AuthenticatedRequest (AuthProtect "CBAuth") -> ClientM [Profile]
Expand All @@ -167,5 +172,5 @@ accounts
:<|> singleAccount :<|> accountHistory :<|> accountHolds :<|> listOrders :<|> getOrder :<|> getClientOrder :<|> placeOrder
:<|> cancelOrder :<|> cancelAll :<|> fills :<|> fees :<|> trailingVolume :<|> limits :<|> transfers :<|> transfer
:<|> makeDeposit :<|> makeCoinbaseDeposit :<|> cryptoDepositAddress :<|> makeWithdrawal :<|> makeCoinbaseWithdrawal
:<|> makeCryptoWithdrawal :<|> withdrawalFeeEstimate :<|> paymentMethods :<|> coinbaseAccounts :<|> profiles :<|> profile
:<|> profileTransfer :<|> createReport :<|> getReport = client api
:<|> makeCryptoWithdrawal :<|> withdrawalFeeEstimate :<|> createStablecoinConversion :<|> paymentMethods :<|>
coinbaseAccounts :<|> profiles :<|> profile :<|> profileTransfer :<|> createReport :<|> getReport = client api
64 changes: 64 additions & 0 deletions src/lib/CoinbasePro/Authenticated/Conversion.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TemplateHaskell #-}

module CoinbasePro.Authenticated.Conversion
( ConversionId
, StablecoinConversionRequest (..)
, StablecoinConversionResponse (..)
) where

import Data.Aeson (FromJSON, parseJSON,
withObject, (.:))
import Data.Aeson.Casing (snakeCase)
import Data.Aeson.TH (defaultOptions, deriveJSON,
fieldLabelModifier,
unwrapUnaryRecords)
import Data.UUID (UUID, toString)

import CoinbasePro.Authenticated.Accounts (AccountId)
import CoinbasePro.Types (CurrencyType)


data StablecoinConversionRequest = StablecoinConversionRequest
{ reqFrom :: CurrencyType
, reqTo :: CurrencyType
, reqAmount :: Double
} deriving Show


deriveJSON defaultOptions
{ fieldLabelModifier = snakeCase . drop 3
} ''StablecoinConversionRequest


newtype ConversionId = ConversionId UUID


instance Show ConversionId where
show (ConversionId u) = toString u


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


data StablecoinConversionResponse = StablecoinConversionResponse
{ resId :: ConversionId
, resAmount :: Double
, resFromAccountId :: AccountId
, resToAccountId :: AccountId
, resFrom :: CurrencyType
, restTo :: CurrencyType
} deriving Show


instance FromJSON StablecoinConversionResponse where
parseJSON = withObject "stablecoin conversion response" $ \o -> StablecoinConversionResponse
<$> o .: "id"
<*> (read <$> o .: "amount")
<*> o .: "from_account_id"
<*> o .: "to_account_id"
<*> o .: "from"
<*> o .: "to"

0 comments on commit fda2b6b

Please sign in to comment.