-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Modify change generation algorithm to only consider outputs going back
to the wallet when deciding on change UTxO distribution (#1530)
- Loading branch information
Showing
6 changed files
with
138 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
module Ctl.Examples.ChangeGeneration (checkChangeOutputsDistribution) where | ||
|
||
import Prelude | ||
|
||
import Contract.Monad (Contract, liftedE) | ||
import Contract.PlutusData (PlutusData, unitDatum) | ||
import Contract.ScriptLookups as Lookups | ||
import Contract.Scripts (validatorHash) | ||
import Contract.Transaction (_body, _outputs, balanceTx) | ||
import Contract.TxConstraints (TxConstraints) | ||
import Contract.TxConstraints as Constraints | ||
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.BigInt (fromInt) as BigInt | ||
import Data.Lens (to, (^.)) | ||
import Data.Maybe (Maybe(Just, Nothing)) | ||
import Data.Newtype (unwrap) | ||
import Data.Tuple (Tuple(Tuple)) | ||
import Test.Spec.Assertions (shouldEqual) | ||
|
||
-- | A contract that creates `outputsToScript` number of outputs at a script address, | ||
-- | `outputsToSelf` outputs going to own address, and asserts that the number of change | ||
-- | outputs is equal to `expectedOutputs`. | ||
checkChangeOutputsDistribution :: Int -> Int -> Int -> Contract Unit | ||
checkChangeOutputsDistribution outputsToScript outputsToSelf expectedOutputs = | ||
do | ||
pkhs <- ownPaymentPubKeyHashes | ||
skhs <- ownStakePubKeyHashes | ||
validator <- AlwaysSucceeds.alwaysSucceedsScript | ||
let | ||
vhash = validatorHash validator | ||
value = Value.lovelaceValueOf $ BigInt.fromInt 10000000 | ||
|
||
constraintsToSelf :: TxConstraints Unit Unit | ||
constraintsToSelf = fold <<< fold $ replicate outputsToSelf | ||
$ zip pkhs skhs <#> \(Tuple pkh mbSkh) -> case mbSkh of | ||
Nothing -> Constraints.mustPayToPubKey pkh value | ||
Just skh -> Constraints.mustPayToPubKeyAddress pkh skh value | ||
|
||
constraintsToScripts :: TxConstraints Unit Unit | ||
constraintsToScripts = fold $ replicate outputsToScript | ||
$ Constraints.mustPayToScript vhash unitDatum | ||
Constraints.DatumWitness | ||
value | ||
|
||
constraints = constraintsToSelf <> constraintsToScripts | ||
|
||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
module Test.Ctl.BalanceTx.ChangeGeneration (suite) where | ||
|
||
import Prelude | ||
|
||
import Contract.Test (ContractTest, InitialUTxOs, withKeyWallet, withWallets) | ||
import Ctl.Examples.ChangeGeneration (checkChangeOutputsDistribution) | ||
import Ctl.Internal.Test.TestPlanM (TestPlanM) | ||
import Data.BigInt (fromInt) as BigInt | ||
import Mote (group, test) | ||
|
||
suite :: TestPlanM ContractTest Unit | ||
suite = do | ||
group "BalanceTx.ChangeGeneration" do | ||
group | ||
"The number of change outputs must equal the number of normal outputs going to our own address" | ||
do | ||
test "no outputs to own address" do | ||
mkChangeOutputs 10 0 11 | ||
test "1 output to own address" do | ||
mkChangeOutputs 10 1 12 | ||
test "2 outputs to own address" do | ||
mkChangeOutputs 10 2 14 | ||
test "2 outputs to own address" do | ||
mkChangeOutputs 10 3 16 | ||
test "0 outputs to script address, 10 outputs to own address" do | ||
mkChangeOutputs 0 10 20 | ||
|
||
mkChangeOutputs :: Int -> Int -> Int -> ContractTest | ||
mkChangeOutputs outputsToScript outputsToSelf expectedOutputs = do | ||
let | ||
distribution :: InitialUTxOs | ||
distribution = | ||
[ BigInt.fromInt 1000_000_000 | ||
, BigInt.fromInt 2000_000_000 | ||
] | ||
withWallets distribution \alice -> do | ||
withKeyWallet alice do | ||
checkChangeOutputsDistribution outputsToScript outputsToSelf | ||
expectedOutputs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters