@@ -82,3 +82,161 @@ impl DriveHighLevelOperationConverter for AddressCreditWithdrawalTransitionActio
8282 }
8383 }
8484}
85+
86+ #[ cfg( test) ]
87+ mod tests {
88+ use super :: * ;
89+ use crate :: state_transition_action:: address_funds:: address_credit_withdrawal:: v0:: AddressCreditWithdrawalTransitionActionV0 ;
90+ use dpp:: address_funds:: PlatformAddress ;
91+ use dpp:: block:: epoch:: Epoch ;
92+ use dpp:: document:: { Document , DocumentV0 } ;
93+ use dpp:: prelude:: Identifier ;
94+ use dpp:: version:: PlatformVersion ;
95+ use std:: collections:: BTreeMap ;
96+
97+ fn make_document ( ) -> Document {
98+ Document :: V0 ( DocumentV0 {
99+ id : Identifier :: from ( [ 0x11 ; 32 ] ) ,
100+ owner_id : Identifier :: from ( [ 0x22 ; 32 ] ) ,
101+ properties : Default :: default ( ) ,
102+ revision : Some ( 1 ) ,
103+ created_at : None ,
104+ updated_at : None ,
105+ transferred_at : None ,
106+ created_at_block_height : None ,
107+ updated_at_block_height : None ,
108+ transferred_at_block_height : None ,
109+ created_at_core_block_height : None ,
110+ updated_at_core_block_height : None ,
111+ transferred_at_core_block_height : None ,
112+ creator_id : None ,
113+ } )
114+ }
115+
116+ fn make_action_with_output ( ) -> AddressCreditWithdrawalTransitionAction {
117+ let addr_a = PlatformAddress :: P2pkh ( [ 0xAA ; 20 ] ) ;
118+ let addr_b = PlatformAddress :: P2sh ( [ 0xCC ; 20 ] ) ;
119+
120+ let mut inputs = BTreeMap :: new ( ) ;
121+ inputs. insert ( addr_a, ( 5_u32 , 10000_u64 ) ) ;
122+
123+ AddressCreditWithdrawalTransitionAction :: V0 ( AddressCreditWithdrawalTransitionActionV0 {
124+ inputs_with_remaining_balance : inputs,
125+ output : Some ( ( addr_b, 2000 ) ) ,
126+ fee_strategy : vec ! [ ] ,
127+ user_fee_increase : 0 ,
128+ prepared_withdrawal_document : make_document ( ) ,
129+ amount : 8000 ,
130+ } )
131+ }
132+
133+ fn make_action_no_output ( ) -> AddressCreditWithdrawalTransitionAction {
134+ let addr_a = PlatformAddress :: P2pkh ( [ 0xAA ; 20 ] ) ;
135+
136+ let mut inputs = BTreeMap :: new ( ) ;
137+ inputs. insert ( addr_a, ( 5_u32 , 2000_u64 ) ) ;
138+
139+ AddressCreditWithdrawalTransitionAction :: V0 ( AddressCreditWithdrawalTransitionActionV0 {
140+ inputs_with_remaining_balance : inputs,
141+ output : None ,
142+ fee_strategy : vec ! [ ] ,
143+ user_fee_increase : 0 ,
144+ prepared_withdrawal_document : make_document ( ) ,
145+ amount : 8000 ,
146+ } )
147+ }
148+
149+ #[ test]
150+ fn test_with_output_produces_correct_operation_count ( ) {
151+ let action = make_action_with_output ( ) ;
152+ let epoch = Epoch :: new ( 0 ) . unwrap ( ) ;
153+ let platform_version = PlatformVersion :: latest ( ) ;
154+
155+ let ops = action
156+ . into_high_level_drive_operations ( & epoch, platform_version)
157+ . expect ( "expected operations" ) ;
158+
159+ // SetBalanceToAddress (1 input) + AddBalanceToAddress (output)
160+ // + AddWithdrawalDocument + RemoveFromSystemCredits
161+ assert_eq ! ( ops. len( ) , 4 ) ;
162+ }
163+
164+ #[ test]
165+ fn test_without_output_produces_correct_operation_count ( ) {
166+ let action = make_action_no_output ( ) ;
167+ let epoch = Epoch :: new ( 0 ) . unwrap ( ) ;
168+ let platform_version = PlatformVersion :: latest ( ) ;
169+
170+ let ops = action
171+ . into_high_level_drive_operations ( & epoch, platform_version)
172+ . expect ( "expected operations" ) ;
173+
174+ // SetBalanceToAddress (1 input) + AddWithdrawalDocument + RemoveFromSystemCredits
175+ assert_eq ! ( ops. len( ) , 3 ) ;
176+ }
177+
178+ #[ test]
179+ fn test_input_balances_are_set ( ) {
180+ let action = make_action_with_output ( ) ;
181+ let epoch = Epoch :: new ( 0 ) . unwrap ( ) ;
182+ let platform_version = PlatformVersion :: latest ( ) ;
183+
184+ let ops = action
185+ . into_high_level_drive_operations ( & epoch, platform_version)
186+ . expect ( "expected operations" ) ;
187+
188+ match & ops[ 0 ] {
189+ AddressFundsOperation ( AddressFundsOperationType :: SetBalanceToAddress {
190+ address,
191+ nonce,
192+ balance,
193+ } ) => {
194+ assert_eq ! ( * address, PlatformAddress :: P2pkh ( [ 0xAA ; 20 ] ) ) ;
195+ assert_eq ! ( * nonce, 5 ) ;
196+ assert_eq ! ( * balance, 10000 ) ;
197+ }
198+ other => panic ! ( "expected SetBalanceToAddress, got {:?}" , other) ,
199+ }
200+ }
201+
202+ #[ test]
203+ fn test_system_credits_removal_matches_amount ( ) {
204+ let action = make_action_with_output ( ) ;
205+ let epoch = Epoch :: new ( 0 ) . unwrap ( ) ;
206+ let platform_version = PlatformVersion :: latest ( ) ;
207+
208+ let ops = action
209+ . into_high_level_drive_operations ( & epoch, platform_version)
210+ . expect ( "expected operations" ) ;
211+
212+ let last = ops. last ( ) . unwrap ( ) ;
213+ match last {
214+ SystemOperation ( SystemOperationType :: RemoveFromSystemCredits { amount } ) => {
215+ assert_eq ! ( * amount, 8000 ) ;
216+ }
217+ other => panic ! ( "expected RemoveFromSystemCredits, got {:?}" , other) ,
218+ }
219+ }
220+
221+ #[ test]
222+ fn test_has_withdrawal_document_operation ( ) {
223+ let action = make_action_with_output ( ) ;
224+ let epoch = Epoch :: new ( 0 ) . unwrap ( ) ;
225+ let platform_version = PlatformVersion :: latest ( ) ;
226+
227+ let ops = action
228+ . into_high_level_drive_operations ( & epoch, platform_version)
229+ . expect ( "expected operations" ) ;
230+
231+ let has_withdrawal_doc = ops. iter ( ) . any ( |op| {
232+ matches ! (
233+ op,
234+ DocumentOperation ( DocumentOperationType :: AddWithdrawalDocument { .. } )
235+ )
236+ } ) ;
237+ assert ! (
238+ has_withdrawal_doc,
239+ "expected AddWithdrawalDocument operation"
240+ ) ;
241+ }
242+ }
0 commit comments