I am building a PlutusTx V1 Smart Contract for NFT Royalty distribution. Part of that requires totaling up the amount of Lovelace sent to the contract from the most recent tx. I came up with the following function to try and accomplish this:
{-# LANGUAGE ImportQualifiedPost #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE NamedFieldPuns #-}
import Prelude qualified as P
import PlutusTx (makeIsDataIndexed)
import PlutusTx.Prelude as Tx hiding (Semigroup(..), unless)
import PlutusTx.Builtins
import Ledger hiding (singleton)
import Ledger.Constraints qualified as Constraints
import Plutus.Script.Utils.V1.Scripts qualified as Scripts
import Ledger.Typed.Scripts
import Ledger.Ada as Ada
import Ledger.Value
import Plutus.V1.Ledger.Tx
import Plutus.Contract
import Data.Aeson
import GHC.Generics (Generic)
{-# OPTIONS_GHC -fno-warn-unused-imports #-}
totalAdaAmnt :: TxInfo -> Integer
totalAdaAmnt TxInfo{txInfoOutputs} =
let valueSum = foldMap (Ledger.txOutValue . Ledger.TxOut) txInfoOutputs
in valueOf valueSum Ada.adaSymbol Ada.adaToken
But when I try to compile, I get the following error:
error:
• Couldn't match expected type ‘Ledger.Value.Value’
with actual type ‘cardano-api-1.35.4:Cardano.Api.Value.Lovelace’
• In the first argument of ‘valueOf’, namely ‘valueSum’
In the expression: valueOf valueSum Ada.adaSymbol Ada.adaToken
In the expression:
let valueSum = foldMap (Ledger.txOutValue . Ledger.TxOut) txInfoOutputs
in valueOf valueSum Ada.adaSymbol Ada.adaToken
|
42 | in valueOf valueSum Ada.adaSymbol Ada.adaToken
This is my first contract and I really don't know how to resolve this error. Ledger is key to many different parts of my code, whereas I'm not using the Cardano-api package anywhere except for in this function. Is there a Ledger equivalent for getting Ada amounts from previous script Txs? Or vice versa? I looked throughout the newly hosted Haddock docs for PlutusTx and couldn't find any work around, so any help is immensely appreciated!