@@ -4944,5 +4944,111 @@ mod tests {
49444944 assert_eq ! ( redeems. get( 0 ) . index( ) , to_bignum( 1 ) ) ;
49454945 assert_eq ! ( redeems. get( 1 ) . index( ) , to_bignum( 2 ) ) ;
49464946 }
4947+
4948+ #[ test]
4949+ fn test_native_and_plutus_scripts_together ( ) {
4950+ let mut tx_builder = create_reallistic_tx_builder ( ) ;
4951+ tx_builder. set_fee ( & to_bignum ( 42 ) ) ;
4952+ let ( pscript1, _) = plutus_script_and_hash ( 0 ) ;
4953+ let ( pscript2, phash2) = plutus_script_and_hash ( 1 ) ;
4954+ let ( nscript1, _) = mint_script_and_policy ( 0 ) ;
4955+ let ( nscript2, nhash2) = mint_script_and_policy ( 1 ) ;
4956+ let datum1 = PlutusData :: new_bytes ( fake_bytes ( 10 ) ) ;
4957+ let datum2 = PlutusData :: new_bytes ( fake_bytes ( 11 ) ) ;
4958+ // Creating redeemers with indexes ZERO
4959+ let redeemer1 = Redeemer :: new (
4960+ & RedeemerTag :: new_spend ( ) ,
4961+ & to_bignum ( 0 ) ,
4962+ & PlutusData :: new_bytes ( fake_bytes ( 20 ) ) ,
4963+ & ExUnits :: new ( & to_bignum ( 1 ) , & to_bignum ( 2 ) ) ,
4964+ ) ;
4965+ let redeemer2 = Redeemer :: new (
4966+ & RedeemerTag :: new_spend ( ) ,
4967+ & to_bignum ( 0 ) ,
4968+ & PlutusData :: new_bytes ( fake_bytes ( 21 ) ) ,
4969+ & ExUnits :: new ( & to_bignum ( 1 ) , & to_bignum ( 2 ) ) ,
4970+ ) ;
4971+
4972+ // Add one plutus input directly with witness
4973+ tx_builder. add_plutus_script_input (
4974+ & PlutusWitness :: new ( & pscript1, & datum1, & redeemer1) ,
4975+ & TransactionInput :: new ( & genesis_id ( ) , 0 ) ,
4976+ & Value :: new ( & to_bignum ( 1_000_000 ) ) ,
4977+ ) ;
4978+ // Add one native input directly with witness
4979+ tx_builder. add_native_script_input (
4980+ & nscript1,
4981+ & TransactionInput :: new ( & genesis_id ( ) , 0 ) ,
4982+ & Value :: new ( & to_bignum ( 1_000_000 ) ) ,
4983+ ) ;
4984+ // Add one plutus input generically without witness
4985+ tx_builder. add_input (
4986+ & create_base_address_from_script_hash ( & phash2) ,
4987+ & TransactionInput :: new ( & genesis_id ( ) , 0 ) ,
4988+ & Value :: new ( & to_bignum ( 1_000_000 ) ) ,
4989+ ) ;
4990+ // Add one native input generically without witness
4991+ tx_builder. add_input (
4992+ & create_base_address_from_script_hash ( & nhash2) ,
4993+ & TransactionInput :: new ( & genesis_id ( ) , 0 ) ,
4994+ & Value :: new ( & to_bignum ( 1_000_000 ) ) ,
4995+ ) ;
4996+
4997+ // There are two missing script witnesses
4998+ assert_eq ! ( tx_builder. count_missing_input_scripts( ) , 2 ) ;
4999+
5000+ let remaining1 = tx_builder. add_required_plutus_input_scripts (
5001+ & PlutusWitnesses :: from ( vec ! [
5002+ PlutusWitness :: new( & pscript2, & datum2, & redeemer2) ,
5003+ ] ) ,
5004+ ) ;
5005+
5006+ // There is one missing script witness now
5007+ assert_eq ! ( remaining1, 1 ) ;
5008+ assert_eq ! ( tx_builder. count_missing_input_scripts( ) , 1 ) ;
5009+
5010+ let remaining2 = tx_builder. add_required_native_input_scripts (
5011+ & NativeScripts :: from ( vec ! [ nscript2. clone( ) ] ) ,
5012+ ) ;
5013+
5014+ // There are no missing script witnesses now
5015+ assert_eq ! ( remaining2, 0 ) ;
5016+ assert_eq ! ( tx_builder. count_missing_input_scripts( ) , 0 ) ;
5017+
5018+ tx_builder. calc_script_data_hash (
5019+ & TxBuilderConstants :: plutus_default_cost_models ( ) ,
5020+ ) ;
5021+
5022+ let tx: Transaction = tx_builder. build_tx ( ) . unwrap ( ) ;
5023+
5024+ let wits = tx. witness_set ;
5025+ assert ! ( wits. native_scripts. is_some( ) ) ;
5026+ assert ! ( wits. plutus_scripts. is_some( ) ) ;
5027+ assert ! ( wits. plutus_data. is_some( ) ) ;
5028+ assert ! ( wits. redeemers. is_some( ) ) ;
5029+
5030+ let nscripts = wits. native_scripts . unwrap ( ) ;
5031+ assert_eq ! ( nscripts. len( ) , 2 ) ;
5032+ assert_eq ! ( nscripts. get( 0 ) , nscript1) ;
5033+ assert_eq ! ( nscripts. get( 1 ) , nscript2) ;
5034+
5035+ let pscripts = wits. plutus_scripts . unwrap ( ) ;
5036+ assert_eq ! ( pscripts. len( ) , 2 ) ;
5037+ assert_eq ! ( pscripts. get( 0 ) , pscript1) ;
5038+ assert_eq ! ( pscripts. get( 1 ) , pscript2) ;
5039+
5040+ let datums = wits. plutus_data . unwrap ( ) ;
5041+ assert_eq ! ( datums. len( ) , 2 ) ;
5042+ assert_eq ! ( datums. get( 0 ) , datum1) ;
5043+ assert_eq ! ( datums. get( 1 ) , datum2) ;
5044+
5045+ let redeems = wits. redeemers . unwrap ( ) ;
5046+ assert_eq ! ( redeems. len( ) , 2 ) ;
5047+ assert_eq ! ( redeems. get( 0 ) , redeemer1) ;
5048+
5049+ // The second plutus input redeemer index has automatically changed to 2
5050+ // because it was added on the third position
5051+ assert_eq ! ( redeems. get( 1 ) , redeemer2. clone_with_index( & to_bignum( 2 ) ) ) ;
5052+ }
49475053}
49485054
0 commit comments