github.com/ethersphere/bee/v2@v2.2.0/pkg/transaction/monitormock/monitor.go (about) 1 // Copyright 2021 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 monitormock 6 7 import ( 8 "context" 9 "errors" 10 "math/big" 11 12 "github.com/ethereum/go-ethereum/common" 13 "github.com/ethereum/go-ethereum/core/types" 14 "github.com/ethersphere/bee/v2/pkg/transaction" 15 ) 16 17 type transactionMonitorMock struct { 18 watchTransaction func(txHash common.Hash, nonce uint64) (<-chan types.Receipt, <-chan error, error) 19 waitBlock func(ctx context.Context, block *big.Int) (*types.Block, error) 20 } 21 22 func (m *transactionMonitorMock) WatchTransaction(txHash common.Hash, nonce uint64) (<-chan types.Receipt, <-chan error, error) { 23 if m.watchTransaction != nil { 24 return m.watchTransaction(txHash, nonce) 25 } 26 return nil, nil, errors.New("not implemented") 27 } 28 29 func (m *transactionMonitorMock) WaitBlock(ctx context.Context, block *big.Int) (*types.Block, error) { 30 if m.watchTransaction != nil { 31 return m.waitBlock(ctx, block) 32 } 33 return nil, errors.New("not implemented") 34 } 35 36 func (m *transactionMonitorMock) Close() error { 37 return nil 38 } 39 40 // Option is the option passed to the mock Chequebook service 41 type Option interface { 42 apply(*transactionMonitorMock) 43 } 44 45 type optionFunc func(*transactionMonitorMock) 46 47 func (f optionFunc) apply(r *transactionMonitorMock) { f(r) } 48 49 func WithWatchTransactionFunc(f func(txHash common.Hash, nonce uint64) (<-chan types.Receipt, <-chan error, error)) Option { 50 return optionFunc(func(s *transactionMonitorMock) { 51 s.watchTransaction = f 52 }) 53 } 54 55 func WithWaitBlockFunc(f func(ctx context.Context, block *big.Int) (*types.Block, error)) Option { 56 return optionFunc(func(s *transactionMonitorMock) { 57 s.waitBlock = f 58 }) 59 } 60 61 func New(opts ...Option) transaction.Monitor { 62 mock := new(transactionMonitorMock) 63 for _, o := range opts { 64 o.apply(mock) 65 } 66 return mock 67 }