packages feed

morley-client-0.2.0: src/Morley/Client/Action/SizeCalculation.hs

-- 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 ct st = do
  pps <- getProtocolParameters
  let csProgram = toExpression ct
      csStorage = toExpression st
      csGas     = ppHardGasLimitPerOperation pps
      csLegacy  = False
  ScriptSize size <- getScriptSize CalcSize{..}
  return size

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