github.com/ethersphere/bee/v2@v2.2.0/pkg/settlement/swap/chequebook/mock/chequebook.go (about)

     1  // Copyright 2020 The Swarm Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package mock
     6  
     7  import (
     8  	"context"
     9  	"errors"
    10  	"math/big"
    11  
    12  	"github.com/ethereum/go-ethereum/common"
    13  	"github.com/ethersphere/bee/v2/pkg/settlement/swap/chequebook"
    14  )
    15  
    16  // Service is the mock chequebook service.
    17  type Service struct {
    18  	chequebookBalanceFunc          func(context.Context) (*big.Int, error)
    19  	chequebookAvailableBalanceFunc func(context.Context) (*big.Int, error)
    20  	chequebookAddressFunc          func() common.Address
    21  	chequebookIssueFunc            func(ctx context.Context, beneficiary common.Address, amount *big.Int, sendChequeFunc chequebook.SendChequeFunc) (*big.Int, error)
    22  	chequebookWithdrawFunc         func(ctx context.Context, amount *big.Int) (hash common.Hash, err error)
    23  	chequebookDepositFunc          func(ctx context.Context, amount *big.Int) (hash common.Hash, err error)
    24  	lastChequeFunc                 func(common.Address) (*chequebook.SignedCheque, error)
    25  	lastChequesFunc                func() (map[common.Address]*chequebook.SignedCheque, error)
    26  }
    27  
    28  // WithChequebook*Functions set the mock chequebook functions
    29  func WithChequebookBalanceFunc(f func(ctx context.Context) (*big.Int, error)) Option {
    30  	return optionFunc(func(s *Service) {
    31  		s.chequebookBalanceFunc = f
    32  	})
    33  }
    34  
    35  func WithChequebookAvailableBalanceFunc(f func(ctx context.Context) (*big.Int, error)) Option {
    36  	return optionFunc(func(s *Service) {
    37  		s.chequebookAvailableBalanceFunc = f
    38  	})
    39  }
    40  
    41  func WithChequebookAddressFunc(f func() common.Address) Option {
    42  	return optionFunc(func(s *Service) {
    43  		s.chequebookAddressFunc = f
    44  	})
    45  }
    46  
    47  func WithChequebookDepositFunc(f func(ctx context.Context, amount *big.Int) (hash common.Hash, err error)) Option {
    48  	return optionFunc(func(s *Service) {
    49  		s.chequebookDepositFunc = f
    50  	})
    51  }
    52  
    53  func WithChequebookIssueFunc(f func(ctx context.Context, beneficiary common.Address, amount *big.Int, sendChequeFunc chequebook.SendChequeFunc) (*big.Int, error)) Option {
    54  	return optionFunc(func(s *Service) {
    55  		s.chequebookIssueFunc = f
    56  	})
    57  }
    58  
    59  func WithChequebookWithdrawFunc(f func(ctx context.Context, amount *big.Int) (hash common.Hash, err error)) Option {
    60  	return optionFunc(func(s *Service) {
    61  		s.chequebookWithdrawFunc = f
    62  	})
    63  }
    64  
    65  func WithLastChequeFunc(f func(beneficiary common.Address) (*chequebook.SignedCheque, error)) Option {
    66  	return optionFunc(func(s *Service) {
    67  		s.lastChequeFunc = f
    68  	})
    69  }
    70  
    71  func WithLastChequesFunc(f func() (map[common.Address]*chequebook.SignedCheque, error)) Option {
    72  	return optionFunc(func(s *Service) {
    73  		s.lastChequesFunc = f
    74  	})
    75  }
    76  
    77  // NewChequebook creates the mock chequebook implementation
    78  func NewChequebook(opts ...Option) chequebook.Service {
    79  	mock := new(Service)
    80  	for _, o := range opts {
    81  		o.apply(mock)
    82  	}
    83  	return mock
    84  }
    85  
    86  // Balance mocks the chequebook .Balance function
    87  func (s *Service) Balance(ctx context.Context) (bal *big.Int, err error) {
    88  	if s.chequebookBalanceFunc != nil {
    89  		return s.chequebookBalanceFunc(ctx)
    90  	}
    91  	return big.NewInt(0), errors.New("Error")
    92  }
    93  
    94  func (s *Service) AvailableBalance(ctx context.Context) (bal *big.Int, err error) {
    95  	if s.chequebookAvailableBalanceFunc != nil {
    96  		return s.chequebookAvailableBalanceFunc(ctx)
    97  	}
    98  	return big.NewInt(0), errors.New("Error")
    99  }
   100  
   101  // Deposit mocks the chequebook .Deposit function
   102  func (s *Service) Deposit(ctx context.Context, amount *big.Int) (hash common.Hash, err error) {
   103  	if s.chequebookDepositFunc != nil {
   104  		return s.chequebookDepositFunc(ctx, amount)
   105  	}
   106  	return common.Hash{}, errors.New("Error")
   107  }
   108  
   109  // WaitForDeposit mocks the chequebook .WaitForDeposit function
   110  func (s *Service) WaitForDeposit(ctx context.Context, txHash common.Hash) error {
   111  	return errors.New("Error")
   112  }
   113  
   114  // Address mocks the chequebook .Address function
   115  func (s *Service) Address() common.Address {
   116  	if s.chequebookAddressFunc != nil {
   117  		return s.chequebookAddressFunc()
   118  	}
   119  	return common.Address{}
   120  }
   121  
   122  func (s *Service) Issue(ctx context.Context, beneficiary common.Address, amount *big.Int, sendChequeFunc chequebook.SendChequeFunc) (*big.Int, error) {
   123  	if s.chequebookIssueFunc != nil {
   124  		return s.chequebookIssueFunc(ctx, beneficiary, amount, sendChequeFunc)
   125  	}
   126  	return big.NewInt(0), nil
   127  }
   128  
   129  func (s *Service) LastCheque(beneficiary common.Address) (*chequebook.SignedCheque, error) {
   130  	if s.lastChequeFunc != nil {
   131  		return s.lastChequeFunc(beneficiary)
   132  	}
   133  	return nil, errors.New("Error")
   134  }
   135  
   136  func (s *Service) LastCheques() (map[common.Address]*chequebook.SignedCheque, error) {
   137  	if s.lastChequesFunc != nil {
   138  		return s.lastChequesFunc()
   139  	}
   140  	return nil, errors.New("Error")
   141  }
   142  
   143  func (s *Service) Withdraw(ctx context.Context, amount *big.Int) (hash common.Hash, err error) {
   144  	return s.chequebookWithdrawFunc(ctx, amount)
   145  }
   146  
   147  // Option is the option passed to the mock Chequebook service
   148  type Option interface {
   149  	apply(*Service)
   150  }
   151  
   152  type optionFunc func(*Service)
   153  
   154  func (f optionFunc) apply(r *Service) { f(r) }