|
| 1 | +module Spec.TestContract.AdjustTx ( |
| 2 | + runAdjustTest, |
| 3 | +) where |
| 4 | + |
| 5 | +import BotPlutusInterface.Contract (runContract) |
| 6 | +import Control.Lens.Operators ((^.)) |
| 7 | +import Data.Text (Text) |
| 8 | +import Data.Text qualified as Text |
| 9 | +import Data.Void (Void) |
| 10 | +import Ledger ( |
| 11 | + PaymentPubKeyHash, |
| 12 | + Tx (..), |
| 13 | + TxOut (..), |
| 14 | + getCardanoTxId, |
| 15 | + ) |
| 16 | +import Ledger qualified |
| 17 | +import Ledger.Ada (adaValueOf) |
| 18 | +import Ledger.Ada qualified as Ada |
| 19 | + |
| 20 | +-- import Ledger.Constraints (mkTx) |
| 21 | +import Ledger.Constraints qualified as Constraints |
| 22 | +import Ledger.Constraints.OffChain qualified as OffChain |
| 23 | +import Ledger.Scripts qualified as Scripts |
| 24 | +import Ledger.Typed.Scripts (TypedValidator, Validator, ValidatorTypes, mkUntypedMintingPolicy) |
| 25 | +import Ledger.Typed.Scripts qualified as TypedScripts |
| 26 | +import Ledger.Value (Value, flattenValue, tokenName) |
| 27 | +import Ledger.Value qualified |
| 28 | +import Plutus.Contract ( |
| 29 | + Contract, |
| 30 | + ContractError (ConstraintResolutionContractError), |
| 31 | + adjustUnbalancedTx, |
| 32 | + awaitTxConfirmed, |
| 33 | + mkTxConstraints, |
| 34 | + submitTx, |
| 35 | + submitTxConstraintsWith, |
| 36 | + submitUnbalancedTx, |
| 37 | + utxosAt, |
| 38 | + ) |
| 39 | +import Plutus.Contract qualified as Contract |
| 40 | +import Plutus.PAB.Effects.Contract.Builtin (EmptySchema) |
| 41 | +import Plutus.Script.Utils.V1.Scripts qualified as ScriptUtils |
| 42 | +import Plutus.V1.Ledger.Value qualified as Value |
| 43 | +import PlutusTx qualified |
| 44 | +import PlutusTx.Prelude qualified as PP |
| 45 | +import Test.Plutip.Contract |
| 46 | +import Test.Plutip.Predicate ( |
| 47 | + shouldSucceed, |
| 48 | + yieldSatisfies, |
| 49 | + ) |
| 50 | +import Prelude |
| 51 | + |
| 52 | +adjustTx :: PaymentPubKeyHash -> Contract () EmptySchema Text [Value] |
| 53 | +adjustTx toPkh = do |
| 54 | + ownPkh <- Contract.ownPaymentPubKeyHash |
| 55 | + let ownAddr = Ledger.pubKeyHashAddress ownPkh Nothing |
| 56 | + utxos <- Contract.utxosAt ownAddr |
| 57 | + let consts = |
| 58 | + Constraints.mustPayToPubKey toPkh (Ada.lovelaceValueOf 50) |
| 59 | + lkups = |
| 60 | + Constraints.ownPaymentPubKeyHash ownPkh |
| 61 | + <> Constraints.unspentOutputs utxos |
| 62 | + unbalancedTx <- mkTxConstraints @Void lkups consts |
| 63 | + -- Adjust the Tx so that all UTxOs have the minimum ADA. |
| 64 | + adjustedTx <- adjustUnbalancedTx unbalancedTx |
| 65 | + let rawTx = adjustedTx ^. OffChain.tx |
| 66 | + vals = map txOutValue $ txOutputs rawTx |
| 67 | + -- crdTx <- submitUnbalancedTx adjustedTx |
| 68 | + balTx <- Contract.balanceTx adjustedTx |
| 69 | + crdTx <- Contract.submitBalancedTx balTx |
| 70 | + _ <- Contract.awaitTxConfirmed (getCardanoTxId crdTx) |
| 71 | + pure vals |
| 72 | + |
| 73 | +adjustTx' :: [PaymentPubKeyHash] -> Contract () EmptySchema Text [Value] |
| 74 | +adjustTx' [] = do |
| 75 | + pkh <- Contract.ownPaymentPubKeyHash |
| 76 | + adjustTx pkh |
| 77 | +adjustTx' (pkh : _) = adjustTx pkh |
| 78 | + |
| 79 | +-- The actual test |
| 80 | +runAdjustTest = |
| 81 | + assertExecution |
| 82 | + "Adjust Unbalanced Tx Contract" |
| 83 | + (initAda [1000] <> initAda [1000]) |
| 84 | + (withContract adjustTx') |
| 85 | + [ shouldSucceed |
| 86 | + , yieldSatisfies |
| 87 | + "All UTxOs have minimum(?) ADA." |
| 88 | + (all (\val -> 500_000 <= Ada.getLovelace (Ada.fromValue val))) |
| 89 | + ] |
0 commit comments