github.com/ethersphere/bee/v2@v2.2.0/pkg/settlement/swap/chequestore/mock/chequestore.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 "math/big" 10 11 "github.com/ethereum/go-ethereum/common" 12 "github.com/ethersphere/bee/v2/pkg/settlement/swap/chequebook" 13 ) 14 15 // Service is the mock chequeStore service. 16 type Service struct { 17 receiveCheque func(ctx context.Context, cheque *chequebook.SignedCheque, exchangeRate *big.Int, deduction *big.Int) (*big.Int, error) 18 lastCheque func(chequebook common.Address) (*chequebook.SignedCheque, error) 19 lastCheques func() (map[common.Address]*chequebook.SignedCheque, error) 20 } 21 22 func WithReceiveChequeFunc(f func(ctx context.Context, cheque *chequebook.SignedCheque, exchangeRate *big.Int, deduction *big.Int) (*big.Int, error)) Option { 23 return optionFunc(func(s *Service) { 24 s.receiveCheque = f 25 }) 26 } 27 28 func WithLastChequeFunc(f func(chequebook common.Address) (*chequebook.SignedCheque, error)) Option { 29 return optionFunc(func(s *Service) { 30 s.lastCheque = f 31 }) 32 } 33 34 func WithLastChequesFunc(f func() (map[common.Address]*chequebook.SignedCheque, error)) Option { 35 return optionFunc(func(s *Service) { 36 s.lastCheques = f 37 }) 38 } 39 40 // NewChequeStore creates the mock chequeStore implementation 41 func NewChequeStore(opts ...Option) chequebook.ChequeStore { 42 mock := new(Service) 43 for _, o := range opts { 44 o.apply(mock) 45 } 46 return mock 47 } 48 49 func (s *Service) ReceiveCheque(ctx context.Context, cheque *chequebook.SignedCheque, exchangeRate, deduction *big.Int) (*big.Int, error) { 50 return s.receiveCheque(ctx, cheque, exchangeRate, deduction) 51 } 52 53 func (s *Service) LastCheque(chequebook common.Address) (*chequebook.SignedCheque, error) { 54 return s.lastCheque(chequebook) 55 } 56 57 func (s *Service) LastCheques() (map[common.Address]*chequebook.SignedCheque, error) { 58 return s.lastCheques() 59 } 60 61 // Option is the option passed to the mock ChequeStore service 62 type Option interface { 63 apply(*Service) 64 } 65 66 type optionFunc func(*Service) 67 68 func (f optionFunc) apply(r *Service) { f(r) }