{-# LANGUAGE OverloadedStrings #-}
module Cometa.Transaction.Building
( coinSize,
outputSize,
scriptSize,
transactionSize,
totalExecutionUnits,
minimumAda,
baseFee,
minimumFee,
executionFee,
referenceScriptFee,
scriptDataHash,
ImplicitCoin (..),
implicitCoin,
subTransactionImplicitCoin,
imbalance,
subTransactionImbalance,
batchImbalance,
isBalanced,
)
where
import qualified Codec.CBOR.Encoding as Encode
import qualified Codec.CBOR.Write as Write
import Cometa.Error (Error (InvalidBuildingRequest))
import qualified Cometa.Internal.Balance as Balance
import Cometa.Internal.Building (ImplicitCoin (..))
import qualified Cometa.Internal.Building as Internal
import Cometa.Protocol.CostModels (CostModels)
import qualified Cometa.Protocol.ExUnitPrices as Prices
import qualified Cometa.Protocol.ExUnits as ExUnits
import Cometa.Protocol.Parameters (Parameters)
import qualified Cometa.Protocol.Rational as Rational
import qualified Cometa.Script as Script
import qualified Cometa.Script.Native as NativeScript
import qualified Cometa.Script.Plutus as PlutusScript
import Cometa.SubTransaction (SubTransaction)
import Cometa.Transaction (Transaction)
import qualified Cometa.Transaction as Tx
import Cometa.Transaction.Datum (Hash32)
import Cometa.Transaction.Output (Output)
import qualified Cometa.Transaction.Output as Output
import qualified Cometa.Transaction.Redeemer as Redeemer
import Cometa.Transaction.Utxo (Utxo)
import Cometa.Transaction.Witnesses (EncodedWitnesses)
import Cometa.Value (Value)
import qualified Data.ByteString as ByteString
import Data.Word (Word64)
coinSize :: Word64 -> Int
coinSize :: Word64 -> Int
coinSize = ByteString -> Int
ByteString.length (ByteString -> Int) -> (Word64 -> ByteString) -> Word64 -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Encoding -> ByteString
Write.toStrictByteString (Encoding -> ByteString)
-> (Word64 -> Encoding) -> Word64 -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word64 -> Encoding
Encode.encodeWord64
outputSize :: Output -> Int
outputSize :: Output -> Int
outputSize = ByteString -> Int
ByteString.length (ByteString -> Int) -> (Output -> ByteString) -> Output -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Output -> ByteString
Output.encodeCbor
scriptSize :: Script.Script -> Int
scriptSize :: Script -> Int
scriptSize Script
script = case Script
script of
Script.Native NativeScript
native -> ByteString -> Int
ByteString.length (NativeScript -> ByteString
NativeScript.encodeCbor NativeScript
native)
Script.Plutus PlutusScript
plutus -> ByteString -> Int
ByteString.length (PlutusScript -> ByteString
PlutusScript.toBytes PlutusScript
plutus)
transactionSize :: Transaction -> Int
transactionSize :: Transaction -> Int
transactionSize = ByteString -> Int
ByteString.length (ByteString -> Int)
-> (Transaction -> ByteString) -> Transaction -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Transaction -> ByteString
Tx.encodeCbor
totalExecutionUnits :: [Redeemer.Redeemer] -> Either Error ExUnits.ExUnits
totalExecutionUnits :: [Redeemer] -> Either Error ExUnits
totalExecutionUnits [Redeemer]
redeemers =
Integer -> Integer -> Either Error ExUnits
ExUnits.mkExUnits
([Integer] -> Integer
forall a. Num a => [a] -> a
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum ((Redeemer -> Integer) -> [Redeemer] -> [Integer]
forall a b. (a -> b) -> [a] -> [b]
map (Word64 -> Integer
forall a. Integral a => a -> Integer
toInteger (Word64 -> Integer) -> (Redeemer -> Word64) -> Redeemer -> Integer
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ExUnits -> Word64
ExUnits.memory (ExUnits -> Word64) -> (Redeemer -> ExUnits) -> Redeemer -> Word64
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Redeemer -> ExUnits
Redeemer.executionUnits) [Redeemer]
redeemers))
([Integer] -> Integer
forall a. Num a => [a] -> a
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum ((Redeemer -> Integer) -> [Redeemer] -> [Integer]
forall a b. (a -> b) -> [a] -> [b]
map (Word64 -> Integer
forall a. Integral a => a -> Integer
toInteger (Word64 -> Integer) -> (Redeemer -> Word64) -> Redeemer -> Integer
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ExUnits -> Word64
ExUnits.steps (ExUnits -> Word64) -> (Redeemer -> ExUnits) -> Redeemer -> Word64
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Redeemer -> ExUnits
Redeemer.executionUnits) [Redeemer]
redeemers))
minimumAda :: Word64 -> Output -> IO (Either Error Word64)
minimumAda :: Word64 -> Output -> IO (Either Error Word64)
minimumAda = Word64 -> Output -> IO (Either Error Word64)
Internal.minimumAda
baseFee :: Word64 -> Word64 -> Transaction -> IO (Either Error Word64)
baseFee :: Word64 -> Word64 -> Transaction -> IO (Either Error Word64)
baseFee = Word64 -> Word64 -> Transaction -> IO (Either Error Word64)
Internal.baseFee
minimumFee :: Parameters -> [Utxo] -> Transaction -> IO (Either Error Word64)
minimumFee :: Parameters -> [Utxo] -> Transaction -> IO (Either Error Word64)
minimumFee = Parameters -> [Utxo] -> Transaction -> IO (Either Error Word64)
Internal.minimumFee
executionFee :: Prices.ExUnitPrices -> ExUnits.ExUnits -> Either Error Word64
executionFee :: ExUnitPrices -> ExUnits -> Either Error Word64
executionFee ExUnitPrices
prices ExUnits
units =
let charge :: Rational
charge =
Word64 -> Rational
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ExUnits -> Word64
ExUnits.memory ExUnits
units) Rational -> Rational -> Rational
forall a. Num a => a -> a -> a
* NonNegativeRational -> Rational
Rational.toRational (ExUnitPrices -> NonNegativeRational
Prices.memoryPrice ExUnitPrices
prices)
Rational -> Rational -> Rational
forall a. Num a => a -> a -> a
+ Word64 -> Rational
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ExUnits -> Word64
ExUnits.steps ExUnits
units) Rational -> Rational -> Rational
forall a. Num a => a -> a -> a
* NonNegativeRational -> Rational
Rational.toRational (ExUnitPrices -> NonNegativeRational
Prices.stepPrice ExUnitPrices
prices)
rounded :: Integer
rounded = Rational -> Integer
forall b. Integral b => Rational -> b
forall a b. (RealFrac a, Integral b) => a -> b
ceiling Rational
charge :: Integer
in if Integer
rounded Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
<= Word64 -> Integer
forall a. Integral a => a -> Integer
toInteger (Word64
forall a. Bounded a => a
maxBound :: Word64)
then Word64 -> Either Error Word64
forall a b. b -> Either a b
Right (Integer -> Word64
forall a. Num a => Integer -> a
fromInteger Integer
rounded)
else Error -> Either Error Word64
forall a b. a -> Either a b
Left (Text -> Error
InvalidBuildingRequest Text
"Execution fee exceeds the unsigned 64-bit range")
referenceScriptFee :: Parameters -> [Utxo] -> IO (Either Error Word64)
referenceScriptFee :: Parameters -> [Utxo] -> IO (Either Error Word64)
referenceScriptFee = Parameters -> [Utxo] -> IO (Either Error Word64)
Internal.referenceScriptFee
scriptDataHash :: CostModels -> EncodedWitnesses -> IO (Either Error (Maybe Hash32))
scriptDataHash :: CostModels -> EncodedWitnesses -> IO (Either Error (Maybe Hash32))
scriptDataHash = CostModels -> EncodedWitnesses -> IO (Either Error (Maybe Hash32))
Internal.scriptDataHash
implicitCoin :: Parameters -> Transaction -> IO (Either Error ImplicitCoin)
implicitCoin :: Parameters -> Transaction -> IO (Either Error ImplicitCoin)
implicitCoin = Parameters -> Transaction -> IO (Either Error ImplicitCoin)
Internal.implicitCoin
subTransactionImplicitCoin :: Parameters -> SubTransaction -> IO (Either Error ImplicitCoin)
subTransactionImplicitCoin :: Parameters -> SubTransaction -> IO (Either Error ImplicitCoin)
subTransactionImplicitCoin = Parameters -> SubTransaction -> IO (Either Error ImplicitCoin)
Internal.subTransactionImplicitCoin
imbalance :: Parameters -> [Utxo] -> Transaction -> IO (Either Error Value)
imbalance :: Parameters -> [Utxo] -> Transaction -> IO (Either Error Value)
imbalance = Parameters -> [Utxo] -> Transaction -> IO (Either Error Value)
Balance.imbalance
subTransactionImbalance :: Parameters -> [Utxo] -> SubTransaction -> IO (Either Error Value)
subTransactionImbalance :: Parameters -> [Utxo] -> SubTransaction -> IO (Either Error Value)
subTransactionImbalance = Parameters -> [Utxo] -> SubTransaction -> IO (Either Error Value)
Balance.subTransactionImbalance
batchImbalance :: Parameters -> [Utxo] -> Transaction -> IO (Either Error Value)
batchImbalance :: Parameters -> [Utxo] -> Transaction -> IO (Either Error Value)
batchImbalance = Parameters -> [Utxo] -> Transaction -> IO (Either Error Value)
Balance.batchImbalance
isBalanced :: Parameters -> [Utxo] -> Transaction -> IO (Either Error Bool)
isBalanced :: Parameters -> [Utxo] -> Transaction -> IO (Either Error Bool)
isBalanced = Parameters -> [Utxo] -> Transaction -> IO (Either Error Bool)
Balance.isBalanced