@@ -425,3 +425,93 @@ func TestPeginContractImpl_RegisterPegin_ErrorHandling(t *testing.T) {
425425 assert .Empty (t , result )
426426 })
427427}
428+
429+ func TestPeginContractImpl_Withdraw (t * testing.T ) {
430+ contractBinding := & mocks.PeginContractAdapterMock {}
431+ signerMock := & mocks.TransactionSignerMock {}
432+ mockClient := & mocks.RpcClientBindingMock {}
433+ callerMock := & mocks.ContractCallerBindingMock {}
434+ peginContract := rootstock .NewPeginContractImpl (
435+ rootstock .NewRskClient (mockClient ),
436+ test .AnyAddress ,
437+ contractBinding ,
438+ signerMock ,
439+ rootstock.RetryParams {},
440+ time .Duration (1 ),
441+ Abis ,
442+ )
443+ withdrawAmount := entities .NewWei (5000000000000000000 )
444+ matchOptsFunc := func (opts * bind.TransactOpts ) bool {
445+ return opts .From .String () == parsedAddress .String ()
446+ }
447+ t .Run ("Success" , func (t * testing.T ) {
448+ contractBinding .On ("Caller" ).Return (callerMock ).Once ()
449+ callerMock .On ("Call" , mock .Anything , mock .Anything , "withdraw" , withdrawAmount .AsBigInt ()).Return (nil ).Once ()
450+ tx := prepareTxMocks (mockClient , signerMock , true )
451+ contractBinding .On ("Withdraw" , mock .MatchedBy (matchOptsFunc ), withdrawAmount .AsBigInt ()).Return (tx , nil ).Once ()
452+ err := peginContract .Withdraw (withdrawAmount )
453+ require .NoError (t , err )
454+ contractBinding .AssertExpectations (t )
455+ callerMock .AssertExpectations (t )
456+ })
457+ }
458+
459+ // nolint:funlen
460+ func TestPeginContractImpl_Withdraw_ErrorHandling (t * testing.T ) {
461+ contractBinding := & mocks.PeginContractAdapterMock {}
462+ signerMock := & mocks.TransactionSignerMock {}
463+ mockClient := & mocks.RpcClientBindingMock {}
464+ callerMock := & mocks.ContractCallerBindingMock {}
465+ peginContract := rootstock .NewPeginContractImpl (
466+ rootstock .NewRskClient (mockClient ),
467+ test .AnyAddress ,
468+ contractBinding ,
469+ signerMock ,
470+ rootstock.RetryParams {},
471+ time .Duration (1 ),
472+ Abis ,
473+ )
474+ withdrawAmount := entities .NewWei (5000000000000000000 )
475+ signerMock .On ("Address" ).Return (parsedAddress )
476+ matchOptsFunc := func (opts * bind.TransactOpts ) bool {
477+ return opts .From .String () == parsedAddress .String ()
478+ }
479+ t .Run ("Error handling (dry-run revert NoBalance)" , func (t * testing.T ) {
480+ e := NewRskRpcError ("transaction reverted" , "0x29226653" )
481+ contractBinding .On ("Caller" ).Return (callerMock ).Once ()
482+ callerMock .On ("Call" , mock .Anything , mock .Anything , "withdraw" , withdrawAmount .AsBigInt ()).Return (e ).Once ()
483+ err := peginContract .Withdraw (withdrawAmount )
484+ require .ErrorContains (t , err , "withdraw reverted with: NoBalance" )
485+ contractBinding .AssertNotCalled (t , "Withdraw" , mock .Anything , mock .Anything )
486+ contractBinding .AssertExpectations (t )
487+ callerMock .AssertExpectations (t )
488+ })
489+ t .Run ("Error handling (dry-run parse error)" , func (t * testing.T ) {
490+ contractBinding .On ("Caller" ).Return (callerMock ).Once ()
491+ callerMock .On ("Call" , mock .Anything , mock .Anything , "withdraw" , withdrawAmount .AsBigInt ()).Return (assert .AnError ).Once ()
492+ err := peginContract .Withdraw (withdrawAmount )
493+ require .ErrorContains (t , err , "error parsing withdraw result" )
494+ contractBinding .AssertExpectations (t )
495+ callerMock .AssertExpectations (t )
496+ })
497+ t .Run ("Error handling (transaction send error)" , func (t * testing.T ) {
498+ contractBinding .On ("Caller" ).Return (callerMock ).Once ()
499+ callerMock .On ("Call" , mock .Anything , mock .Anything , "withdraw" , withdrawAmount .AsBigInt ()).Return (nil ).Once ()
500+ _ = prepareTxMocks (mockClient , signerMock , true )
501+ contractBinding .On ("Withdraw" , mock .MatchedBy (matchOptsFunc ), withdrawAmount .AsBigInt ()).Return (nil , assert .AnError ).Once ()
502+ err := peginContract .Withdraw (withdrawAmount )
503+ require .ErrorContains (t , err , "withdraw error" )
504+ contractBinding .AssertExpectations (t )
505+ callerMock .AssertExpectations (t )
506+ })
507+ t .Run ("Error handling (transaction reverted)" , func (t * testing.T ) {
508+ contractBinding .On ("Caller" ).Return (callerMock ).Once ()
509+ callerMock .On ("Call" , mock .Anything , mock .Anything , "withdraw" , withdrawAmount .AsBigInt ()).Return (nil ).Once ()
510+ tx := prepareTxMocks (mockClient , signerMock , false )
511+ contractBinding .On ("Withdraw" , mock .MatchedBy (matchOptsFunc ), withdrawAmount .AsBigInt ()).Return (tx , nil ).Once ()
512+ err := peginContract .Withdraw (withdrawAmount )
513+ require .ErrorContains (t , err , "withdraw error: transaction failed" )
514+ contractBinding .AssertExpectations (t )
515+ callerMock .AssertExpectations (t )
516+ })
517+ }
0 commit comments