Skip to content

Commit

Permalink
Restore examples and tests to use the old balanceAndSignTx interface,…
Browse files Browse the repository at this point in the history
… add warnings
  • Loading branch information
jy14898 committed Aug 10, 2022
1 parent a266e06 commit 4939990
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 20 deletions.
4 changes: 3 additions & 1 deletion examples/AlwaysMints.purs
Expand Up @@ -13,6 +13,7 @@ import Contract.Monad
, liftContractAffM
, liftContractM
, liftedE
, liftedM
, runContract
)
import Contract.Prim.ByteArray (byteArrayFromAscii)
Expand Down Expand Up @@ -52,7 +53,8 @@ main = launchAff_ $ do
lookups = Lookups.mintingPolicy mp

ubTx <- liftedE $ Lookups.mkUnbalancedTx lookups constraints
bsTx <- balanceAndSignTx ubTx
bsTx <-
liftedM "Failed to balance/sign tx" $ balanceAndSignTx ubTx
txId <- submit bsTx
logInfo' $ "Tx ID: " <> show txId

Expand Down
4 changes: 2 additions & 2 deletions examples/AlwaysSucceeds.purs
Expand Up @@ -31,7 +31,7 @@ import Contract.Transaction
( TransactionHash
, TransactionInput(TransactionInput)
, awaitTxConfirmed
, balanceAndSignTx
, balanceAndSignTxE
, submit
)
import Contract.TxConstraints (TxConstraints)
Expand Down Expand Up @@ -110,7 +110,7 @@ buildBalanceSignAndSubmitTx
-> Contract () TransactionHash
buildBalanceSignAndSubmitTx lookups constraints = do
ubTx <- liftedE $ Lookups.mkUnbalancedTx lookups constraints
bsTx <- balanceAndSignTx ubTx
bsTx <- liftedE $ balanceAndSignTxE ubTx
txId <- submit bsTx
logInfo' $ "Tx ID: " <> show txId
pure txId
Expand Down
4 changes: 2 additions & 2 deletions examples/KeyWallet/MintsAndSendsToken.purs
Expand Up @@ -7,7 +7,7 @@ module Examples.KeyWallet.MintsAndSendsToken (main) where
import Contract.Prelude

import Contract.Log (logInfo')
import Contract.Monad (liftContractAffM, liftContractM, liftedE)
import Contract.Monad (liftContractAffM, liftContractM, liftedE, liftedM)
import Contract.Prim.ByteArray (byteArrayFromAscii)
import Contract.ScriptLookups as Lookups
import Contract.Transaction (balanceAndSignTx, submit)
Expand Down Expand Up @@ -38,7 +38,7 @@ main = runKeyWalletContract_ \pkh lovelace unlock -> do
lookups = Lookups.mintingPolicy mp

ubTx <- liftedE $ Lookups.mkUnbalancedTx lookups constraints
bsTx <- balanceAndSignTx ubTx
bsTx <- liftedM "Failed to balance/sign tx" $ balanceAndSignTx ubTx
txId <- submit bsTx
logInfo' $ "Tx ID: " <> show txId
liftEffect unlock
5 changes: 3 additions & 2 deletions examples/KeyWallet/Pkh2Pkh.purs
Expand Up @@ -6,7 +6,7 @@ module Examples.KeyWallet.Pkh2Pkh (main) where

import Contract.Prelude

import Contract.Monad (liftedE)
import Contract.Monad (liftedE, liftedM)
import Contract.Log (logInfo')
import Contract.ScriptLookups as Lookups
import Contract.Transaction
Expand All @@ -31,7 +31,8 @@ main = runKeyWalletContract_ \pkh lovelace unlock -> do
lookups = mempty

ubTx <- liftedE $ Lookups.mkUnbalancedTx lookups constraints
bsTx <- balanceAndSignTx ubTx
bsTx <-
liftedM "Failed to balance/sign tx" $ balanceAndSignTx ubTx
txId <- submit bsTx
logInfo' $ "Tx ID: " <> show txId
liftEffect unlock
Expand Down
4 changes: 3 additions & 1 deletion examples/MintsMultipleTokens.purs
Expand Up @@ -18,6 +18,7 @@ import Contract.Monad
, liftContractAffM
, liftContractM
, liftedE
, liftedM
, runContract
)
import Contract.PlutusData (PlutusData(Integer), Redeemer(Redeemer))
Expand Down Expand Up @@ -70,7 +71,8 @@ main = launchAff_ do
<> Lookups.mintingPolicy mp3

ubTx <- liftedE $ Lookups.mkUnbalancedTx lookups constraints
bsTx <- balanceAndSignTx ubTx
bsTx <-
liftedM "Failed to balance/sign tx" $ balanceAndSignTx ubTx
txId <- submit bsTx
logInfo' $ "Tx ID: " <> show txId

Expand Down
3 changes: 2 additions & 1 deletion examples/Pkh2Pkh.purs
Expand Up @@ -37,7 +37,8 @@ main = launchAff_ do
lookups = mempty

ubTx <- liftedE $ Lookups.mkUnbalancedTx lookups constraints
bsTx <- balanceAndSignTx ubTx
bsTx <-
liftedM "Failed to balance/sign tx" $ balanceAndSignTx ubTx
txId <- submit bsTx
logInfo' $ "Tx ID: " <> show txId
awaitTxConfirmedWithTimeout (wrap 100.0) txId
Expand Down
4 changes: 2 additions & 2 deletions examples/Pkh2PkhGero.purs
Expand Up @@ -11,7 +11,7 @@ import Contract.Log (logInfo')
import Contract.Monad (launchAff_, liftedE, liftedM, runContract)
import Contract.ScriptLookups as Lookups
import Contract.Test.E2E (publishTestFeedback)
import Contract.Transaction (balanceAndSignTx, submit)
import Contract.Transaction (balanceAndSignTxE, submit)
import Contract.TxConstraints as Constraints
import Contract.Value as Value
import Data.BigInt as BigInt
Expand All @@ -34,7 +34,7 @@ main = launchAff_ do
lookups = mempty

ubTx <- liftedE $ Lookups.mkUnbalancedTx lookups constraints
bsTx <- balanceAndSignTx ubTx
bsTx <- liftedE $ balanceAndSignTxE ubTx
txId <- submit bsTx
logInfo' $ "Tx ID: " <> show txId

Expand Down
24 changes: 20 additions & 4 deletions src/Contract/Transaction.purs
Expand Up @@ -6,6 +6,7 @@ module Contract.Transaction
, awaitTxConfirmedWithTimeout
, awaitTxConfirmedWithTimeoutSlots
, balanceAndSignTx
, balanceAndSignTxE
, balanceAndSignTxs
, balanceTx
, balanceTxWithAddress
Expand Down Expand Up @@ -34,6 +35,7 @@ module Contract.Transaction
) where

import Prelude
import Prim.TypeError (class Warn, Text)

import Aeson (class EncodeAeson, Aeson)
import BalanceTx (BalanceTxError) as BalanceTxError
Expand Down Expand Up @@ -124,7 +126,7 @@ import Data.Time.Duration (Seconds)
import Data.Traversable (class Traversable, for_, traverse)
import Data.Tuple.Nested (type (/\))
import Effect.Class (liftEffect)
import Effect.Exception (throw)
import Effect.Exception (Error, throw)
import Plutus.Conversion (toPlutusCoin, toPlutusTxOutput)
import Plutus.Conversion.Address (fromPlutusAddress)
import Plutus.Types.Address (Address)
Expand Down Expand Up @@ -376,7 +378,7 @@ withBalancedAndSignedTx
-> (BalancedSignedTransaction -> Contract r a)
-> Contract r a
withBalancedAndSignedTx = withSingleTransaction
balanceAndSignTx
internalBalanceAndSignTx
unwrap

-- | Like `balanceTxs`, but uses `balanceTxWithAddress` instead of `balanceTx`
Expand Down Expand Up @@ -482,16 +484,30 @@ balanceAndSignTxs txs = balanceTxs txs >>= traverse
-- | If you want to re-use them in the same 'QueryM' context, call
-- | `unlockTransactionInputs`.
balanceAndSignTx
:: forall (r :: Row Type)
. Warn ( Text "`balanceAndSignTx` no longer returns `Nothing` when failing, instead letting errors continue through the `Contract` monad. `Maybe` will be removed in a future release.")
=> UnattachedUnbalancedTx
-> Contract r (Maybe BalancedSignedTransaction)
balanceAndSignTx tx = pure <$> internalBalanceAndSignTx tx

internalBalanceAndSignTx
:: forall (r :: Row Type)
. UnattachedUnbalancedTx
-> Contract r BalancedSignedTransaction
balanceAndSignTx tx = balanceAndSignTxs [ tx ] >>=
internalBalanceAndSignTx tx = balanceAndSignTxs [ tx ] >>=
case _ of
[ x ] -> pure x
-- Which error should we throw here?
_ -> liftEffect $ throw $
"Unexpected internal error during transaction signing"

-- TODO Deprecate `balanceAndSignTxE` once `Maybe` is dropped from
-- `balanceAndSignTx`, like in `internalBalanceAndSignTx`.
balanceAndSignTxE
:: forall (r :: Row Type)
. UnattachedUnbalancedTx
-> Contract r (Either Error BalancedSignedTransaction)
balanceAndSignTxE = try <<< internalBalanceAndSignTx

scriptOutputToTransactionOutput
:: NetworkId
-> UnbalancedTx.ScriptOutput
Expand Down
16 changes: 11 additions & 5 deletions test/Plutip.purs
Expand Up @@ -36,6 +36,7 @@ import Contract.Transaction
, DataHash
, awaitTxConfirmed
, balanceAndSignTx
, balanceAndSignTxE
, getTxByHash
, submit
, withBalancedAndSignedTxs
Expand Down Expand Up @@ -181,7 +182,8 @@ suite = do
lookups :: Lookups.ScriptLookups Void
lookups = mempty
ubTx <- liftedE $ Lookups.mkUnbalancedTx lookups constraints
bsTx <- balanceAndSignTx ubTx
bsTx <-
liftedE $ balanceAndSignTxE ubTx
submitAndLog bsTx

test "runPlutipContract: parallel Pkh2Pkh" do
Expand Down Expand Up @@ -211,7 +213,8 @@ suite = do
lookups :: Lookups.ScriptLookups Void
lookups = mempty
ubTx <- liftedE $ Lookups.mkUnbalancedTx lookups constraints
bsTx <- balanceAndSignTx ubTx
bsTx <-
liftedE $ balanceAndSignTxE ubTx
submitAndLog bsTx
parallel $ runContractInEnv env $ withKeyWallet bob do
alicePkh <- liftedM "Failed to get PKH" $ withKeyWallet alice
Expand All @@ -228,7 +231,8 @@ suite = do
lookups :: Lookups.ScriptLookups Void
lookups = mempty
ubTx <- liftedE $ Lookups.mkUnbalancedTx lookups constraints
bsTx <- balanceAndSignTx ubTx
bsTx <-
liftedE $ balanceAndSignTxE ubTx
submitAndLog bsTx
in unit

Expand Down Expand Up @@ -257,7 +261,8 @@ suite = do
lookups = Lookups.mintingPolicy mp

ubTx <- liftedE $ Lookups.mkUnbalancedTx lookups constraints
bsTx <- balanceAndSignTx ubTx
bsTx <-
liftedM "Failed to balance/sign tx" $ balanceAndSignTx ubTx
submitAndLog bsTx

test "runPlutipContract: Datums" do
Expand Down Expand Up @@ -314,7 +319,8 @@ suite = do
<> Lookups.mintingPolicy mp3

ubTx <- liftedE $ Lookups.mkUnbalancedTx lookups constraints
bsTx <- balanceAndSignTx ubTx
bsTx <-
liftedM "Failed to balance/sign tx" $ balanceAndSignTx ubTx
submitAndLog bsTx

test "runPlutipContract: SignMultiple" do
Expand Down

0 comments on commit 4939990

Please sign in to comment.