Skip to content

Commit

Permalink
WIP: change generation
Browse files Browse the repository at this point in the history
  • Loading branch information
klntsky committed Sep 19, 2023
1 parent faeb88e commit bc82c39
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 13 deletions.
6 changes: 3 additions & 3 deletions examples/ByUrl.purs
Original file line number Diff line number Diff line change
Expand Up @@ -216,9 +216,9 @@ examples = Map.fromFoldable
, "PaysWithDatum" /\ PaysWithDatum.contract
, "DropTokens" /\ DropTokens.contract
, "ChangeGeneration1-1" /\
ChangeGeneration.checkChangeOutputsDistribution 1 1 3
ChangeGeneration.checkChangeOutputsDistribution 1 1 3
, "ChangeGeneration3-1" /\
ChangeGeneration.checkChangeOutputsDistribution 3 1 5
ChangeGeneration.checkChangeOutputsDistribution 3 1 5
, "ChangeGeneration1-3" /\
ChangeGeneration.checkChangeOutputsDistribution 1 3 7
ChangeGeneration.checkChangeOutputsDistribution 1 3 7
]
23 changes: 16 additions & 7 deletions examples/ChangeGeneration.purs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,21 @@ module Ctl.Examples.ChangeGeneration (checkChangeOutputsDistribution) where

import Prelude

import Contract.BalanceTxConstraints (mustSendChangeWithDatum)
import Contract.Monad (Contract, liftedE)
import Contract.PlutusData (PlutusData, unitDatum)
import Contract.PlutusData
( Datum(Datum)
, OutputDatum(OutputDatum)
, PlutusData(Integer)
, unitDatum
)
import Contract.ScriptLookups as Lookups
import Contract.Scripts (validatorHash)
import Contract.Transaction
( _body
, _outputs
, awaitTxConfirmed
, balanceTx
, balanceTxWithConstraints
, signTransaction
, submit
)
Expand All @@ -20,8 +26,7 @@ import Contract.UnbalancedTx (mkUnbalancedTx)
import Contract.Value as Value
import Contract.Wallet (ownPaymentPubKeyHashes, ownStakePubKeyHashes)
import Ctl.Examples.AlwaysSucceeds as AlwaysSucceeds
import Data.Array (fold, replicate, zip)
import Data.Array (length) as Array
import Data.Array (fold, length, replicate, zip)
import Data.BigInt (fromInt) as BigInt
import Data.Lens (to, (^.))
import Data.Maybe (Maybe(Just, Nothing))
Expand Down Expand Up @@ -59,9 +64,13 @@ checkChangeOutputsDistribution outputsToScript outputsToSelf expectedOutputs =
lookups :: Lookups.ScriptLookups PlutusData
lookups = mempty
unbalancedTx <- liftedE $ mkUnbalancedTx lookups constraints
balancedTx <- liftedE $ balanceTx unbalancedTx
let outputs = balancedTx ^. to unwrap <<< _body <<< _outputs
Array.length outputs `shouldEqual` expectedOutputs
balancedTx <- liftedE $ balanceTxWithConstraints unbalancedTx
-- just to check that attaching datums works
( mustSendChangeWithDatum $ OutputDatum $ Datum $ Integer $ BigInt.fromInt
1000
)
balancedSignedTx <- signTransaction balancedTx
let outputs = balancedTx ^. to unwrap <<< _body <<< _outputs
length outputs `shouldEqual` expectedOutputs
txHash <- submit balancedSignedTx
awaitTxConfirmed txHash
5 changes: 4 additions & 1 deletion src/Internal/BalanceTx/BalanceTx.purs
Original file line number Diff line number Diff line change
Expand Up @@ -740,7 +740,10 @@ assignCoinsToChangeValues changeAddress adaAvailable pairsAtStart =

changeValuesForOutputCoins :: NonEmptyArray Value
changeValuesForOutputCoins =
makeChangeForCoin (_.outputAda <$> changeValues) adaRemaining
let
weights = _.outputAda <$> changeValues
in
makeChangeForCoin weights adaRemaining

changeValuesWithMinCoins :: NonEmptyArray Value
changeValuesWithMinCoins = assignMinCoin <$> changeValues
Expand Down
4 changes: 2 additions & 2 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ module.exports = {
},
},
client: {
overlay: false
}
overlay: false,
},
},

// we can add more entrypoints as needed
Expand Down

0 comments on commit bc82c39

Please sign in to comment.