Skip to content

Commit 9d4da96

Browse files
committed
calcilationg ex-budget
- setting exact budget to each spending input and minting policy
1 parent c720514 commit 9d4da96

File tree

10 files changed

+216
-439
lines changed

10 files changed

+216
-439
lines changed

examples/ex-units/app/Main.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ main = do
3737
cEnv <- mkContractEnv nodeInfo clusterDir
3838
res <- BPI.runContract cEnv lockThenSpend
3939
putStrLn $ case res of
40-
Right _ -> "=== OK ==="
40+
Right r -> "=== OK ===\n" ++ show r
4141
Left e -> "=== FAILED ===\n" ++ show e
4242

4343
mkContractEnv :: Monoid w => BPI.NodeInfo -> FilePath -> IO (ContractEnvironment w)

examples/ex-units/ex-units.cabal

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ common common-lang
2424
ghc-options:
2525
-Wall -Wcompat -Wincomplete-record-updates
2626
-Wincomplete-uni-patterns -Wredundant-constraints
27-
-- -Werror
27+
-Werror
2828
-fobject-code -fno-ignore-interface-pragmas
2929
-fno-omit-interface-pragmas -Wunused-packages
3030
-fplugin-opt PlutusTx.Plugin:defer-errors
@@ -76,7 +76,6 @@ library
7676
import: common-lang
7777
exposed-modules:
7878
LockSpend
79-
LockSpendSingle
8079

8180
build-depends:
8281
plutus-ledger
@@ -86,10 +85,7 @@ library
8685
, plutus-ledger-constraints
8786
, plutus-pab
8887
, plutus-tx
89-
, plutus-tx-plugin
9088
, text
91-
, aeson
92-
, lens
9389

9490

9591

@@ -98,7 +94,7 @@ library
9894
executable ex-units-run
9995
import: common-lang
10096
build-depends:
101-
aeson ^>=1.5.0.0
97+
, aeson
10298
, bot-plutus-interface
10399
, cardano-api
104100
, data-default
@@ -108,7 +104,6 @@ executable ex-units-run
108104
, text
109105
, directory
110106
, plutus-ledger
111-
, plutus-tx
112107
, uuid
113108
, stm
114109
, plutus-pab

examples/ex-units/protocol.json

Lines changed: 0 additions & 208 deletions
This file was deleted.

examples/ex-units/src/LockSpend.hs

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import Ledger
1616
scriptCurrencySymbol,
1717
unitDatum,
1818
unitRedeemer,
19-
validatorHash, TxInfo (txInfoMint)
19+
validatorHash, TxInfo (txInfoMint), TxOutRef, ChainIndexTxOut, PaymentPubKeyHash (PaymentPubKeyHash), pubKeyHashAddress
2020
)
2121
import Ledger.Constraints qualified as Constraints
2222
import Ledger.Typed.Scripts (wrapMintingPolicy)
@@ -30,12 +30,19 @@ import Plutus.V1.Ledger.Value qualified as Value
3030
import PlutusTx qualified
3131
import PlutusTx.Prelude qualified as PP
3232
import Prelude
33+
import Control.Monad (void)
34+
import Plutus.V1.Ledger.Ada (adaValueOf)
3335

34-
lockThenSpend :: Contract () EmptySchema Text (TxId, CardanoTx)
36+
lockThenSpend :: Contract () EmptySchema Text [(TxOutRef, ChainIndexTxOut)]
3537
lockThenSpend = do
36-
_ <-lockAtScript
37-
_ <- Contract.waitNSlots 1
38-
spendFromScript
38+
_ <- lockAtScript
39+
wait 1
40+
_ <- spendFromScript
41+
wait 1
42+
pkh <- Contract.ownPaymentPubKeyHash
43+
Map.toList <$> Contract.utxosAt (pubKeyHashAddress pkh Nothing)
44+
where
45+
wait = void . Contract.waitNSlots
3946

4047
lockAtScript :: Contract () EmptySchema Text (TxId, CardanoTx)
4148
lockAtScript = do
@@ -63,8 +70,7 @@ spendFromScript = do
6370
((oref1, _) : _, (oref2, _) : _) -> spendUtxo oref1 utxos1 oref2 utxos2
6471
where
6572
spendUtxo oref1 utxos1 oref2 utxos2 = do
66-
let -- (JSON.Success tokenName) = JSON.fromJSON $ JSON.object ["unTokenName" .= ("ff" :: Text)]
67-
token = Value.singleton currencySymbol (tokenName "ff") 1
73+
let token = Value.singleton currencySymbol (tokenName "ff") 1
6874
txc1 =
6975
Constraints.mustSpendScriptOutput oref1 unitRedeemer
7076
<> Constraints.mustMintValueWithRedeemer unitRedeemer token
@@ -74,6 +80,9 @@ spendFromScript = do
7480
<> Constraints.mintingPolicy mintingPolicy
7581

7682
let txc2 = Constraints.mustSpendScriptOutput oref2 unitRedeemer
83+
<> Constraints.mustPayToPubKey
84+
(PaymentPubKeyHash "72cae61f85ed97fb0e7703d9fec382e4973bf47ea2ac9335cab1e3fe")
85+
(adaValueOf 200)
7786
lookups2 =
7887
Constraints.unspentOutputs (Map.fromList utxos2)
7988
<> Constraints.otherScript (validator2 2)
@@ -118,11 +127,9 @@ mkValidator2 i _ _ _ =
118127
then PP.traceIfFalse "looooooooooooong" check
119128
else PP.traceIfFalse "short" check
120129
where
121-
!someWork = PP.sort $ PP.reverse [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] :: [Integer]
130+
someWork = PP.sort $ PP.reverse [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] :: [Integer]
122131
check = PP.length someWork PP.== 10
123132

124-
-- info = scriptContextTxInfo ctx
125-
-- check = PP.length (txInfoOutputs info) PP.== 1
126133

127134
data TestLockSpend2
128135

@@ -150,17 +157,17 @@ mkPolicy :: () -> ScriptContext -> Bool
150157
mkPolicy _ !ctx =
151158
PP.traceIfFalse "Let me mint" check
152159
where
153-
!info = scriptContextTxInfo ctx
154-
!check =
160+
info = scriptContextTxInfo ctx
161+
check =
155162
PP.length (flattenValue PP.$ txInfoMint info) PP.== 1
156163
PP.&& PP.length someWork PP.== 10
157164

158-
!someWork = PP.sort [9, 8, 7, 6, 5, 4, 3, 2, 1, 0] :: [Integer]
165+
someWork = PP.sort [9, 8, 7, 6, 5, 4, 3, 2, 1, 0] :: [Integer]
159166

160167
mintingPolicy :: MintingPolicy
161168
mintingPolicy =
162169
mkMintingPolicyScript
163170
$$(PlutusTx.compile [||wrapMintingPolicy mkPolicy||])
164171

165172
currencySymbol :: CurrencySymbol
166-
currencySymbol = scriptCurrencySymbol mintingPolicy
173+
currencySymbol = scriptCurrencySymbol mintingPolicy

0 commit comments

Comments
 (0)