-- SPDX-FileCopyrightText: 2022 Oxhead Alpha
-- SPDX-License-Identifier: LicenseRef-MIT-OA

module Morley.Client.Action.SizeCalculation
  ( computeUntypedContractSize
  , computeContractSize
  ) where

import Morley.Client.RPC
import Morley.Micheline.Class (ToExpression(..))
import Morley.Michelson.Typed (Contract, Value)
import Morley.Michelson.Typed.Scope
import Morley.Michelson.Untyped qualified as U

computeUntypedContractSize :: HasTezosRpc m => U.Contract -> U.Value -> m Natural
computeUntypedContractSize :: forall (m :: * -> *).
HasTezosRpc m =>
Contract -> Value -> m Natural
computeUntypedContractSize Contract
ct Value
st = do
  ProtocolParameters
pps <- m ProtocolParameters
forall (m :: * -> *). HasTezosRpc m => m ProtocolParameters
getProtocolParameters
  let csProgram :: Expression
csProgram = Contract -> Expression
forall a. ToExpression a => a -> Expression
toExpression Contract
ct
      csStorage :: Expression
csStorage = Value -> Expression
forall a. ToExpression a => a -> Expression
toExpression Value
st
      csGas :: TezosInt64
csGas     = ProtocolParameters -> TezosInt64
ppHardGasLimitPerOperation ProtocolParameters
pps
      csLegacy :: Bool
csLegacy  = Bool
False
  ScriptSize Natural
size <- CalcSize -> m ScriptSize
forall (m :: * -> *). HasTezosRpc m => CalcSize -> m ScriptSize
getScriptSize CalcSize{Bool
Expression
TezosInt64
csProgram :: Expression
csStorage :: Expression
csGas :: TezosInt64
csLegacy :: Bool
csProgram :: Expression
csStorage :: Expression
csGas :: TezosInt64
csLegacy :: Bool
..}
  Natural -> m Natural
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return Natural
size

computeContractSize
  :: forall m cp st.
     ( HasTezosRpc m
     , StorageScope st
     )
  => Contract cp st
  -> Value st
  -> m Natural
computeContractSize :: forall (m :: * -> *) (cp :: T) (st :: T).
(HasTezosRpc m, StorageScope st) =>
Contract cp st -> Value st -> m Natural
computeContractSize Contract cp st
ct Value st
st = do
  ProtocolParameters
pps <- m ProtocolParameters
forall (m :: * -> *). HasTezosRpc m => m ProtocolParameters
getProtocolParameters
  let csProgram :: Expression
csProgram = Contract cp st -> Expression
forall a. ToExpression a => a -> Expression
toExpression Contract cp st
ct
      csStorage :: Expression
csStorage = Value st -> Expression
forall a. ToExpression a => a -> Expression
toExpression Value st
st
      csGas :: TezosInt64
csGas     = ProtocolParameters -> TezosInt64
ppHardGasLimitPerOperation ProtocolParameters
pps
      csLegacy :: Bool
csLegacy  = Bool
False
  ScriptSize Natural
size <- CalcSize -> m ScriptSize
forall (m :: * -> *). HasTezosRpc m => CalcSize -> m ScriptSize
getScriptSize CalcSize{Bool
Expression
TezosInt64
csProgram :: Expression
csStorage :: Expression
csGas :: TezosInt64
csLegacy :: Bool
csProgram :: Expression
csStorage :: Expression
csGas :: TezosInt64
csLegacy :: Bool
..}
  Natural -> m Natural
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return Natural
size