{-# LANGUAGE GeneralizedNewtypeDeriving #-}
module Bitcoin.Fee where
import Bitcoin.Amount
import qualified Data.Aeson.Types as JSON
import qualified Data.Serialize as Bin
import PaymentChannel.Internal.Util
class HasFee a where
absoluteFee
:: BtcAmount
-> TxByteSize
-> a
-> BtcAmount
instance HasFee BtcAmount where
absoluteFee _ _ = id
newtype Constant = Constant BtcAmount
deriving (Eq, Generic, NFData)
instance HasFee Constant where
absoluteFee _ _ (Constant amt) = amt
type TxByteSize = Word
newtype SatoshisPerByte = SatoshisPerByte BtcAmount
deriving (Eq, Show, Ord, Num, Enum, Real, Integral, Bin.Serialize, JSON.ToJSON, JSON.FromJSON, NFData)
instance HasFee SatoshisPerByte where
absoluteFee _ txByteSize (SatoshisPerByte satoshisPerByte) =
fromIntegral txByteSize * satoshisPerByte
newtype MaxFee a b = MaxFee (a,b)
deriving (Eq, Show, Ord, Bin.Serialize, JSON.ToJSON, JSON.FromJSON, NFData)
instance (HasFee a, HasFee b) => HasFee (MaxFee a b) where
absoluteFee availVal txByteSize (MaxFee (f1,f2)) =
max (getAbsFee f1) (getAbsFee f2)
where
getAbsFee :: HasFee fee => fee -> BtcAmount
getAbsFee = absoluteFee availVal txByteSize
newtype Capped val = Capped val
instance HasFee fee => HasFee (Capped fee) where
absoluteFee availVal size (Capped fee) =
min availVal desiredFee
where
desiredFee = absoluteFee availVal size fee