-- Copyright 2026 Biglup Labs
-- SPDX-License-Identifier: Apache-2.0
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
--     https://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
{-# LANGUAGE OverloadedStrings #-}
{-# OPTIONS_HADDOCK hide #-}

-- | Checked accounting before native signed arithmetic. Parsed native owners
-- remain scoped by the existing standalone building transport.
module Cometa.Internal.Balance (imbalance, subTransactionImbalance, batchImbalance, isBalanced) where

-- IMPORTS --------------------------------------------------------------------

import qualified Codec.CBOR.Decoding as D
import qualified Codec.CBOR.Encoding as E
import qualified Cometa.Certificate as Certificate
import qualified Cometa.Certificate.Collection as Certificates
import Cometa.Error (Error (..))
import qualified Cometa.Internal.Building as Building
import qualified Cometa.Internal.ParameterSnapshot as Snapshot
import qualified Cometa.Internal.TransactionCbor as Cbor
import qualified Cometa.Internal.TransactionValue as ValueCbor
import qualified Cometa.Pool.Parameters as Pool
import qualified Cometa.Protocol.Parameters as Parameters
import qualified Cometa.SubTransaction as Sub
import qualified Cometa.SubTransaction.Body as SubBody
import qualified Cometa.Transaction as Tx
import qualified Cometa.Transaction.Body as Body
import qualified Cometa.Transaction.Fields as Fields
import qualified Cometa.Transaction.Output as Output
import qualified Cometa.Transaction.Utxo as Utxo
import qualified Cometa.Value as Value
import Control.Monad (unless)
import Control.Monad.Trans.Except (ExceptT (..), except, runExceptT)
import Data.ByteString (ByteString)
import Data.Int (Int64)
import qualified Data.Map.Strict as Map
import Data.Maybe (fromMaybe)
import qualified Data.Set as Set
import Data.Word (Word64)
import Foreign.C.Types (CUInt)

-- DEFINITIONS ----------------------------------------------------------------

imbalance :: Parameters.Parameters -> [Utxo.Utxo] -> Tx.Transaction -> IO (Either Error Value.Value)
imbalance :: Parameters -> [Utxo] -> Transaction -> IO (Either Error Value)
imbalance Parameters
parameters [Utxo]
resolved Transaction
transaction = ExceptT Error IO Value -> IO (Either Error Value)
forall e (m :: * -> *) a. ExceptT e m a -> m (Either e a)
runExceptT (ExceptT Error IO Value -> IO (Either Error Value))
-> ExceptT Error IO Value -> IO (Either Error Value)
forall a b. (a -> b) -> a -> b
$ do
  Either Error () -> ExceptT Error IO ()
forall (m :: * -> *) e a. Monad m => Either e a -> ExceptT e m a
except ([Utxo] -> Either Error ()
validateResolved [Utxo]
resolved)
  ImplicitCoin
implicit <- IO (Either Error ImplicitCoin) -> ExceptT Error IO ImplicitCoin
forall e (m :: * -> *) a. m (Either e a) -> ExceptT e m a
ExceptT (Parameters -> Transaction -> IO (Either Error ImplicitCoin)
Building.implicitCoin Parameters
parameters Transaction
transaction)
  let body :: Body
body = EncodedBody -> Body
Body.toBody (Transaction -> EncodedBody
Tx.body Transaction
transaction)
  Value
_ <- Either Error Value -> ExceptT Error IO Value
forall (m :: * -> *) e a. Monad m => Either e a -> ExceptT e m a
except ([Utxo] -> Word64 -> Fields -> ImplicitCoin -> Either Error Value
bodyBalance [Utxo]
resolved (Body -> Word64
Body.fee Body
body) (Body -> Fields
Body.bodyFields Body
body) ImplicitCoin
implicit)
  IO (Either Error Value) -> ExceptT Error IO Value
forall e (m :: * -> *) a. m (Either e a) -> ExceptT e m a
ExceptT (CUInt
-> Parameters -> [Utxo] -> ByteString -> IO (Either Error Value)
compute CUInt
6 Parameters
parameters [Utxo]
resolved (Transaction -> ByteString
Tx.encodeCbor Transaction
transaction))

subTransactionImbalance :: Parameters.Parameters -> [Utxo.Utxo] -> Sub.SubTransaction -> IO (Either Error Value.Value)
subTransactionImbalance :: Parameters -> [Utxo] -> SubTransaction -> IO (Either Error Value)
subTransactionImbalance Parameters
parameters [Utxo]
resolved SubTransaction
transaction = ExceptT Error IO Value -> IO (Either Error Value)
forall e (m :: * -> *) a. ExceptT e m a -> m (Either e a)
runExceptT (ExceptT Error IO Value -> IO (Either Error Value))
-> ExceptT Error IO Value -> IO (Either Error Value)
forall a b. (a -> b) -> a -> b
$ do
  Either Error () -> ExceptT Error IO ()
forall (m :: * -> *) e a. Monad m => Either e a -> ExceptT e m a
except ([Utxo] -> Either Error ()
validateResolved [Utxo]
resolved)
  ImplicitCoin
implicit <- IO (Either Error ImplicitCoin) -> ExceptT Error IO ImplicitCoin
forall e (m :: * -> *) a. m (Either e a) -> ExceptT e m a
ExceptT (Parameters -> SubTransaction -> IO (Either Error ImplicitCoin)
Building.subTransactionImplicitCoin Parameters
parameters SubTransaction
transaction)
  Value
_ <- Either Error Value -> ExceptT Error IO Value
forall (m :: * -> *) e a. Monad m => Either e a -> ExceptT e m a
except ([Utxo] -> Word64 -> Fields -> ImplicitCoin -> Either Error Value
bodyBalance [Utxo]
resolved Word64
0 (Body -> Fields
SubBody.fields (EncodedBody -> Body
SubBody.toBody (SubTransaction -> EncodedBody
Sub.body SubTransaction
transaction))) ImplicitCoin
implicit)
  IO (Either Error Value) -> ExceptT Error IO Value
forall e (m :: * -> *) a. m (Either e a) -> ExceptT e m a
ExceptT (CUInt
-> Parameters -> [Utxo] -> ByteString -> IO (Either Error Value)
compute CUInt
7 Parameters
parameters [Utxo]
resolved (SubTransaction -> ByteString
Sub.encodeCbor SubTransaction
transaction))

batchImbalance :: Parameters.Parameters -> [Utxo.Utxo] -> Tx.Transaction -> IO (Either Error Value.Value)
batchImbalance :: Parameters -> [Utxo] -> Transaction -> IO (Either Error Value)
batchImbalance Parameters
parameters [Utxo]
resolved Transaction
transaction = ExceptT Error IO Value -> IO (Either Error Value)
forall e (m :: * -> *) a. ExceptT e m a -> m (Either e a)
runExceptT (ExceptT Error IO Value -> IO (Either Error Value))
-> ExceptT Error IO Value -> IO (Either Error Value)
forall a b. (a -> b) -> a -> b
$ do
  Either Error () -> ExceptT Error IO ()
forall (m :: * -> *) e a. Monad m => Either e a -> ExceptT e m a
except ([Utxo] -> Either Error ()
validateResolved [Utxo]
resolved)
  let body :: Body
body = EncodedBody -> Body
Body.toBody (Transaction -> EncodedBody
Tx.body Transaction
transaction)
      subs :: [SubTransaction]
subs = [SubTransaction] -> Maybe [SubTransaction] -> [SubTransaction]
forall a. a -> Maybe a -> a
fromMaybe [] (Body -> Maybe [SubTransaction]
Body.bodySubTransactions Body
body)
      fields :: [Fields]
fields = Body -> Fields
Body.bodyFields Body
body Fields -> [Fields] -> [Fields]
forall a. a -> [a] -> [a]
: (SubTransaction -> Fields) -> [SubTransaction] -> [Fields]
forall a b. (a -> b) -> [a] -> [b]
map (Body -> Fields
SubBody.fields (Body -> Fields)
-> (SubTransaction -> Body) -> SubTransaction -> Fields
forall b c a. (b -> c) -> (a -> b) -> a -> c
. EncodedBody -> Body
SubBody.toBody (EncodedBody -> Body)
-> (SubTransaction -> EncodedBody) -> SubTransaction -> Body
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SubTransaction -> EncodedBody
Sub.body) [SubTransaction]
subs
  ImplicitCoin
outer <- IO (Either Error ImplicitCoin) -> ExceptT Error IO ImplicitCoin
forall e (m :: * -> *) a. m (Either e a) -> ExceptT e m a
ExceptT (Parameters -> Transaction -> IO (Either Error ImplicitCoin)
Building.implicitCoin Parameters
parameters Transaction
transaction)
  [ImplicitCoin]
nested <- (SubTransaction -> ExceptT Error IO ImplicitCoin)
-> [SubTransaction] -> ExceptT Error IO [ImplicitCoin]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> [a] -> f [b]
traverse (IO (Either Error ImplicitCoin) -> ExceptT Error IO ImplicitCoin
forall e (m :: * -> *) a. m (Either e a) -> ExceptT e m a
ExceptT (IO (Either Error ImplicitCoin) -> ExceptT Error IO ImplicitCoin)
-> (SubTransaction -> IO (Either Error ImplicitCoin))
-> SubTransaction
-> ExceptT Error IO ImplicitCoin
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Parameters -> SubTransaction -> IO (Either Error ImplicitCoin)
Building.subTransactionImplicitCoin Parameters
parameters) [SubTransaction]
subs
  [Value]
values <- Either Error [Value] -> ExceptT Error IO [Value]
forall (m :: * -> *) e a. Monad m => Either e a -> ExceptT e m a
except ([Either Error Value] -> Either Error [Value]
forall (t :: * -> *) (m :: * -> *) a.
(Traversable t, Monad m) =>
t (m a) -> m (t a)
forall (m :: * -> *) a. Monad m => [m a] -> m [a]
sequence ((Word64 -> Fields -> ImplicitCoin -> Either Error Value)
-> [Word64] -> [Fields] -> [ImplicitCoin] -> [Either Error Value]
forall a b c d. (a -> b -> c -> d) -> [a] -> [b] -> [c] -> [d]
zipWith3 ([Utxo] -> Word64 -> Fields -> ImplicitCoin -> Either Error Value
bodyBalance [Utxo]
resolved) (Body -> Word64
Body.fee Body
body Word64 -> [Word64] -> [Word64]
forall a. a -> [a] -> [a]
: Word64 -> [Word64]
forall a. a -> [a]
repeat Word64
0) [Fields]
fields (ImplicitCoin
outer ImplicitCoin -> [ImplicitCoin] -> [ImplicitCoin]
forall a. a -> [a] -> [a]
: [ImplicitCoin]
nested)))
  -- Native set iteration order need not match imported envelopes. Bounding the
  -- positive and negative totals separately protects every possible prefix.
  let positives :: Value
positives = [(AssetId, Integer)] -> Value
Value.fromList [(AssetId
asset, Integer
amount) | Value
value <- [Value]
values, (AssetId
asset, Integer
amount) <- Value -> [(AssetId, Integer)]
Value.toList Value
value, Integer
amount Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
> Integer
0]
      negatives :: Value
negatives = [(AssetId, Integer)] -> Value
Value.fromList [(AssetId
asset, Integer
amount) | Value
value <- [Value]
values, (AssetId
asset, Integer
amount) <- Value -> [(AssetId, Integer)]
Value.toList Value
value, Integer
amount Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
< Integer
0]
      refund :: Value
refund = Integer -> Value
Value.lovelace (Parameters -> [Fields] -> Integer
repeatedPoolRefund Parameters
parameters [Fields]
fields)
  Either Error () -> ExceptT Error IO ()
forall (m :: * -> *) e a. Monad m => Either e a -> ExceptT e m a
except (Value -> Either Error ()
bounded (Value
positives Value -> Value -> Value
forall a. Semigroup a => a -> a -> a
<> Value
refund) Either Error () -> Either Error () -> Either Error ()
forall a b. Either Error a -> Either Error b -> Either Error b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Value -> Either Error ()
bounded Value
negatives)
  IO (Either Error Value) -> ExceptT Error IO Value
forall e (m :: * -> *) a. m (Either e a) -> ExceptT e m a
ExceptT (CUInt
-> Parameters -> [Utxo] -> ByteString -> IO (Either Error Value)
compute CUInt
8 Parameters
parameters [Utxo]
resolved (Transaction -> ByteString
Tx.encodeCbor Transaction
transaction))

isBalanced :: Parameters.Parameters -> [Utxo.Utxo] -> Tx.Transaction -> IO (Either Error Bool)
isBalanced :: Parameters -> [Utxo] -> Transaction -> IO (Either Error Bool)
isBalanced Parameters
parameters [Utxo]
resolved Transaction
transaction = (Either Error Value -> Either Error Bool)
-> IO (Either Error Value) -> IO (Either Error Bool)
forall a b. (a -> b) -> IO a -> IO b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((Value -> Bool) -> Either Error Value -> Either Error Bool
forall a b. (a -> b) -> Either Error a -> Either Error b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Value -> Value -> Bool
forall a. Eq a => a -> a -> Bool
== Value
forall a. Monoid a => a
mempty)) (Parameters -> [Utxo] -> Transaction -> IO (Either Error Value)
batchImbalance Parameters
parameters [Utxo]
resolved Transaction
transaction)

validateResolved :: [Utxo.Utxo] -> Either Error ()
validateResolved :: [Utxo] -> Either Error ()
validateResolved [Utxo]
resolved =
  Bool -> Either Error () -> Either Error ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless
    (Set Input -> Int
forall a. Set a -> Int
Set.size ([Input] -> Set Input
forall a. Ord a => [a] -> Set a
Set.fromList ((Utxo -> Input) -> [Utxo] -> [Input]
forall a b. (a -> b) -> [a] -> [b]
map Utxo -> Input
Utxo.input [Utxo]
resolved)) Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== [Utxo] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Utxo]
resolved)
    (Error -> Either Error ()
forall a b. a -> Either a b
Left (Text -> Error
InvalidBuildingRequest Text
"Duplicate resolved spending input"))

bodyBalance :: [Utxo.Utxo] -> Word64 -> Fields.Fields -> Building.ImplicitCoin -> Either Error Value.Value
bodyBalance :: [Utxo] -> Word64 -> Fields -> ImplicitCoin -> Either Error Value
bodyBalance [Utxo]
resolved Word64
fee Fields
fields ImplicitCoin
implicit = do
  [Utxo]
inputs <- (Input -> Either Error Utxo) -> [Input] -> Either Error [Utxo]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> [a] -> f [b]
traverse Input -> Either Error Utxo
resolve (Fields -> [Input]
Fields.fieldInputs Fields
fields)
  let inputValue :: Value
inputValue = (Utxo -> Value) -> [Utxo] -> Value
forall m a. Monoid m => (a -> m) -> [a] -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap (Output -> Value
Output.value (Output -> Value) -> (Utxo -> Output) -> Utxo -> Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Utxo -> Output
Utxo.output) [Utxo]
inputs
      outputValue :: Value
outputValue = (Output -> Value) -> [Output] -> Value
forall m a. Monoid m => (a -> m) -> [a] -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap Output -> Value
Output.value (Fields -> [Output]
Fields.fieldOutputs Fields
fields)
      mint :: Value
mint = Value -> (Mint -> Value) -> Maybe Mint -> Value
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Value
forall a. Monoid a => a
mempty Mint -> Value
Fields.mintValue (Fields -> Maybe Mint
Fields.fieldMint Fields
fields)
      consumedCoin :: Integer
consumedCoin = Word64 -> Integer
forall a. Integral a => a -> Integer
toInteger (ImplicitCoin -> Word64
Building.withdrawals ImplicitCoin
implicit) Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Word64 -> Integer
forall a. Integral a => a -> Integer
toInteger (ImplicitCoin -> Word64
Building.reclaimedDeposits ImplicitCoin
implicit)
      producedCoin :: Integer
producedCoin =
        Word64 -> Integer
forall a. Integral a => a -> Integer
toInteger Word64
fee
          Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Word64 -> Integer
forall a. Integral a => a -> Integer
toInteger (ImplicitCoin -> Word64
Building.deposits ImplicitCoin
implicit)
          Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Integer -> (Word64 -> Integer) -> Maybe Word64 -> Integer
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Integer
0 Word64 -> Integer
forall a. Integral a => a -> Integer
toInteger (Fields -> Maybe Word64
Fields.fieldDonation Fields
fields)
          Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ [Integer] -> Integer
forall a. Num a => [a] -> a
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum [Word64 -> Integer
forall a. Integral a => a -> Integer
toInteger Word64
amount | (RewardAddress
_, Word64
amount) <- [(RewardAddress, Word64)]
-> (Entries RewardAddress Word64 -> [(RewardAddress, Word64)])
-> Maybe (Entries RewardAddress Word64)
-> [(RewardAddress, Word64)]
forall b a. b -> (a -> b) -> Maybe a -> b
maybe [] Entries RewardAddress Word64 -> [(RewardAddress, Word64)]
forall key value. Entries key value -> [(key, value)]
Fields.entries (Fields -> Maybe (Entries RewardAddress Word64)
Fields.fieldDirectDeposits Fields
fields)]
      consumed :: Value
consumed = Value
inputValue Value -> Value -> Value
forall a. Semigroup a => a -> a -> a
<> Integer -> Value
Value.lovelace Integer
consumedCoin Value -> Value -> Value
forall a. Semigroup a => a -> a -> a
<> Value
mint
      difference :: Value
difference = Value
consumed Value -> Value -> Value
forall a. Semigroup a => a -> a -> a
<> Value -> Value
negateValue Value
outputValue
      result :: Value
result = Value
difference Value -> Value -> Value
forall a. Semigroup a => a -> a -> a
<> Integer -> Value
Value.lovelace (Integer -> Integer
forall a. Num a => a -> a
negate Integer
producedCoin)
  (Value -> Either Error ()) -> [Value] -> Either Error ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ Value -> Either Error ()
bounded [Value
inputValue, Value
outputValue, Integer -> Value
Value.lovelace Integer
consumedCoin, Integer -> Value
Value.lovelace Integer
producedCoin, Value
consumed, Value
difference, Value
result]
  Value -> Either Error Value
forall a. a -> Either Error a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Value
result
  where
    table :: Map Input Utxo
table = [(Input, Utxo)] -> Map Input Utxo
forall k a. Ord k => [(k, a)] -> Map k a
Map.fromList [(Utxo -> Input
Utxo.input Utxo
value, Utxo
value) | Utxo
value <- [Utxo]
resolved]
    resolve :: Input -> Either Error Utxo
resolve Input
reference = Either Error Utxo
-> (Utxo -> Either Error Utxo) -> Maybe Utxo -> Either Error Utxo
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (Error -> Either Error Utxo
forall a b. a -> Either a b
Left (Text -> Error
InvalidBuildingRequest Text
"A spending input has no resolved UTxO")) Utxo -> Either Error Utxo
forall a b. b -> Either a b
Right (Input -> Map Input Utxo -> Maybe Utxo
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup Input
reference Map Input Utxo
table)

bounded :: Value.Value -> Either Error ()
bounded :: Value -> Either Error ()
bounded Value
value = Bool -> Either Error () -> Either Error ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (((AssetId, Integer) -> Bool) -> [(AssetId, Integer)] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all (AssetId, Integer) -> Bool
forall {a}. (a, Integer) -> Bool
fits (Value -> [(AssetId, Integer)]
Value.toList Value
value)) (Error -> Either Error ()
forall a b. a -> Either a b
Left (Text -> Error
InvalidBuildingRequest Text
"Balance intermediate exceeds signed 64-bit bounds"))
  where
    fits :: (a, Integer) -> Bool
fits (a
_, Integer
amount) = Integer
amount Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
>= Int64 -> Integer
forall a. Integral a => a -> Integer
toInteger (Int64
forall a. Bounded a => a
minBound :: Int64) Bool -> Bool -> Bool
&& Integer
amount Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
<= Int64 -> Integer
forall a. Integral a => a -> Integer
toInteger (Int64
forall a. Bounded a => a
maxBound :: Int64)

negateValue :: Value.Value -> Value.Value
negateValue :: Value -> Value
negateValue = [(AssetId, Integer)] -> Value
Value.fromList ([(AssetId, Integer)] -> Value)
-> (Value -> [(AssetId, Integer)]) -> Value -> Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((AssetId, Integer) -> (AssetId, Integer))
-> [(AssetId, Integer)] -> [(AssetId, Integer)]
forall a b. (a -> b) -> [a] -> [b]
map ((Integer -> Integer) -> (AssetId, Integer) -> (AssetId, Integer)
forall a b. (a -> b) -> (AssetId, a) -> (AssetId, b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Integer -> Integer
forall a. Num a => a -> a
negate) ([(AssetId, Integer)] -> [(AssetId, Integer)])
-> (Value -> [(AssetId, Integer)]) -> Value -> [(AssetId, Integer)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Value -> [(AssetId, Integer)]
Value.toList

repeatedPoolRefund :: Parameters.Parameters -> [Fields.Fields] -> Integer
repeatedPoolRefund :: Parameters -> [Fields] -> Integer
repeatedPoolRefund Parameters
parameters [Fields]
fields =
  Int -> Integer
forall a. Integral a => a -> Integer
toInteger ([Int] -> Int
forall a. Num a => [a] -> a
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum ((Set (KeyHash StakePool) -> Int)
-> [Set (KeyHash StakePool)] -> [Int]
forall a b. (a -> b) -> [a] -> [b]
map Set (KeyHash StakePool) -> Int
forall a. Set a -> Int
Set.size [Set (KeyHash StakePool)]
pools) Int -> Int -> Int
forall a. Num a => a -> a -> a
- Set (KeyHash StakePool) -> Int
forall a. Set a -> Int
Set.size ([Set (KeyHash StakePool)] -> Set (KeyHash StakePool)
forall (f :: * -> *) a. (Foldable f, Ord a) => f (Set a) -> Set a
Set.unions [Set (KeyHash StakePool)]
pools)) Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
* Word64 -> Integer
forall a. Integral a => a -> Integer
toInteger (Parameters -> Word64
Parameters.parameterPoolDeposit Parameters
parameters)
  where
    pools :: [Set (KeyHash StakePool)]
pools = (Fields -> Set (KeyHash StakePool))
-> [Fields] -> [Set (KeyHash StakePool)]
forall a b. (a -> b) -> [a] -> [b]
map ([KeyHash StakePool] -> Set (KeyHash StakePool)
forall a. Ord a => [a] -> Set a
Set.fromList ([KeyHash StakePool] -> Set (KeyHash StakePool))
-> (Fields -> [KeyHash StakePool])
-> Fields
-> Set (KeyHash StakePool)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (PoolParameters -> KeyHash StakePool)
-> [PoolParameters] -> [KeyHash StakePool]
forall a b. (a -> b) -> [a] -> [b]
map PoolParameters -> KeyHash StakePool
Pool.operator ([PoolParameters] -> [KeyHash StakePool])
-> (Fields -> [PoolParameters]) -> Fields -> [KeyHash StakePool]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Fields -> [PoolParameters]
registrations) [Fields]
fields
    registrations :: Fields -> [PoolParameters]
registrations Fields
body = [PoolParameters
pool | Certificate.PoolRegistration PoolParameters
pool <- [Certificate]
-> (Certificates -> [Certificate])
-> Maybe Certificates
-> [Certificate]
forall b a. b -> (a -> b) -> Maybe a -> b
maybe [] Certificates -> [Certificate]
Certificates.toList (Fields -> Maybe Certificates
Fields.fieldCertificates Fields
body)]

compute :: CUInt -> Parameters.Parameters -> [Utxo.Utxo] -> ByteString -> IO (Either Error Value.Value)
compute :: CUInt
-> Parameters -> [Utxo] -> ByteString -> IO (Either Error Value)
compute CUInt
operation Parameters
parameters [Utxo]
resolved ByteString
transaction = do
  Either Error ByteString
result <- CUInt -> ByteString -> IO (Either Error ByteString)
Building.runUnchecked CUInt
operation (Encoding -> ByteString
Cbor.encode Encoding
request)
  Either Error Value -> IO (Either Error Value)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either Error ByteString
result Either Error ByteString
-> (ByteString -> Either Error Value) -> Either Error Value
forall a b.
Either Error a -> (a -> Either Error b) -> Either Error b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (Text -> Error)
-> (forall s. Decoder s Value) -> ByteString -> Either Error Value
forall a.
(Text -> Error)
-> (forall s. Decoder s a) -> ByteString -> Either Error a
Cbor.decodeAs Text -> Error
InvalidBuildingRequest Decoder s Value
forall s. Decoder s Value
decoder)
  where
    request :: Encoding
request =
      Word -> Encoding
E.encodeListLen Word
3
        Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> ByteString -> Encoding
Cbor.raw ByteString
transaction
        Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> ByteString -> Encoding
Cbor.raw (Parameters -> ByteString
Snapshot.encodeSnapshot Parameters
parameters)
        Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Word -> Encoding
E.encodeListLen (Int -> Word
forall a b. (Integral a, Num b) => a -> b
fromIntegral ([Utxo] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Utxo]
resolved))
        Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> (Utxo -> Encoding) -> [Utxo] -> Encoding
forall m a. Monoid m => (a -> m) -> [a] -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap (ByteString -> Encoding
Cbor.raw (ByteString -> Encoding)
-> (Utxo -> ByteString) -> Utxo -> Encoding
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Utxo -> ByteString
Utxo.encodeCbor) [Utxo]
resolved
    decoder :: Decoder s Value
decoder = Int -> Decoder s Value -> Decoder s Value
forall s a. Int -> Decoder s a -> Decoder s a
Cbor.array Int
2 ((Value -> Value -> Value
forall a. Semigroup a => a -> a -> a
(<>) (Value -> Value -> Value)
-> (Integer -> Value) -> Integer -> Value -> Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Integer -> Value
Value.lovelace (Integer -> Value -> Value)
-> Decoder s Integer -> Decoder s (Value -> Value)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s Integer
forall s. Decoder s Integer
D.decodeInteger) Decoder s (Value -> Value) -> Decoder s Value -> Decoder s Value
forall a b. Decoder s (a -> b) -> Decoder s a -> Decoder s b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Decoder s Value
forall s. Decoder s Value
ValueCbor.decodeAssets)