github.com/ethersphere/bee/v2@v2.2.0/pkg/transaction/backendmock/backend.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 backendmock 6 7 import ( 8 "context" 9 "errors" 10 "math/big" 11 12 "github.com/ethereum/go-ethereum" 13 "github.com/ethereum/go-ethereum/common" 14 "github.com/ethereum/go-ethereum/core/types" 15 "github.com/ethersphere/bee/v2/pkg/transaction" 16 ) 17 18 type backendMock struct { 19 codeAt func(ctx context.Context, contract common.Address, blockNumber *big.Int) ([]byte, error) 20 callContract func(ctx context.Context, call ethereum.CallMsg, blockNumber *big.Int) ([]byte, error) 21 sendTransaction func(ctx context.Context, tx *types.Transaction) error 22 suggestGasPrice func(ctx context.Context) (*big.Int, error) 23 suggestGasTipCap func(ctx context.Context) (*big.Int, error) 24 estimateGas func(ctx context.Context, call ethereum.CallMsg) (gas uint64, err error) 25 transactionReceipt func(ctx context.Context, txHash common.Hash) (*types.Receipt, error) 26 pendingNonceAt func(ctx context.Context, account common.Address) (uint64, error) 27 transactionByHash func(ctx context.Context, hash common.Hash) (tx *types.Transaction, isPending bool, err error) 28 blockNumber func(ctx context.Context) (uint64, error) 29 blockByNumber func(ctx context.Context, number *big.Int) (*types.Block, error) 30 headerByNumber func(ctx context.Context, number *big.Int) (*types.Header, error) 31 balanceAt func(ctx context.Context, address common.Address, block *big.Int) (*big.Int, error) 32 nonceAt func(ctx context.Context, account common.Address, blockNumber *big.Int) (uint64, error) 33 } 34 35 func (m *backendMock) CodeAt(ctx context.Context, contract common.Address, blockNumber *big.Int) ([]byte, error) { 36 if m.codeAt != nil { 37 return m.codeAt(ctx, contract, blockNumber) 38 } 39 return nil, errors.New("not implemented") 40 } 41 42 func (m *backendMock) CallContract(ctx context.Context, call ethereum.CallMsg, blockNumber *big.Int) ([]byte, error) { 43 if m.callContract != nil { 44 return m.callContract(ctx, call, blockNumber) 45 } 46 return nil, errors.New("not implemented") 47 } 48 49 func (*backendMock) PendingCodeAt(ctx context.Context, account common.Address) ([]byte, error) { 50 return nil, errors.New("not implemented") 51 } 52 53 func (m *backendMock) PendingNonceAt(ctx context.Context, account common.Address) (uint64, error) { 54 if m.pendingNonceAt != nil { 55 return m.pendingNonceAt(ctx, account) 56 } 57 return 0, errors.New("not implemented") 58 } 59 60 func (m *backendMock) SuggestGasPrice(ctx context.Context) (*big.Int, error) { 61 if m.suggestGasPrice != nil { 62 return m.suggestGasPrice(ctx) 63 } 64 return nil, errors.New("not implemented") 65 } 66 67 func (m *backendMock) EstimateGas(ctx context.Context, call ethereum.CallMsg) (gas uint64, err error) { 68 if m.estimateGas != nil { 69 return m.estimateGas(ctx, call) 70 } 71 return 0, errors.New("not implemented") 72 } 73 74 func (m *backendMock) SendTransaction(ctx context.Context, tx *types.Transaction) error { 75 if m.sendTransaction != nil { 76 return m.sendTransaction(ctx, tx) 77 } 78 return errors.New("not implemented") 79 } 80 81 func (*backendMock) FilterLogs(ctx context.Context, query ethereum.FilterQuery) ([]types.Log, error) { 82 return nil, errors.New("not implemented") 83 } 84 85 func (*backendMock) SubscribeFilterLogs(ctx context.Context, query ethereum.FilterQuery, ch chan<- types.Log) (ethereum.Subscription, error) { 86 return nil, errors.New("not implemented") 87 } 88 89 func (m *backendMock) TransactionReceipt(ctx context.Context, txHash common.Hash) (*types.Receipt, error) { 90 if m.transactionReceipt != nil { 91 return m.transactionReceipt(ctx, txHash) 92 } 93 return nil, errors.New("not implemented") 94 } 95 96 func (m *backendMock) TransactionByHash(ctx context.Context, hash common.Hash) (tx *types.Transaction, isPending bool, err error) { 97 if m.transactionByHash != nil { 98 return m.transactionByHash(ctx, hash) 99 } 100 return nil, false, errors.New("not implemented") 101 } 102 103 func (m *backendMock) BlockNumber(ctx context.Context) (uint64, error) { 104 if m.blockNumber != nil { 105 return m.blockNumber(ctx) 106 } 107 return 0, errors.New("not implemented") 108 } 109 110 func (m *backendMock) BlockByNumber(ctx context.Context, number *big.Int) (*types.Block, error) { 111 if m.blockNumber != nil { 112 return m.blockByNumber(ctx, number) 113 } 114 return nil, errors.New("not implemented") 115 } 116 117 func (m *backendMock) HeaderByNumber(ctx context.Context, number *big.Int) (*types.Header, error) { 118 if m.headerByNumber != nil { 119 return m.headerByNumber(ctx, number) 120 } 121 return nil, errors.New("not implemented") 122 } 123 124 func (m *backendMock) BalanceAt(ctx context.Context, address common.Address, block *big.Int) (*big.Int, error) { 125 if m.balanceAt != nil { 126 return m.balanceAt(ctx, address, block) 127 } 128 return nil, errors.New("not implemented") 129 } 130 func (m *backendMock) NonceAt(ctx context.Context, account common.Address, blockNumber *big.Int) (uint64, error) { 131 if m.nonceAt != nil { 132 return m.nonceAt(ctx, account, blockNumber) 133 } 134 return 0, errors.New("not implemented") 135 } 136 137 func (m *backendMock) SuggestGasTipCap(ctx context.Context) (*big.Int, error) { 138 if m.suggestGasPrice != nil { 139 return m.suggestGasTipCap(ctx) 140 } 141 return nil, errors.New("not implemented") 142 } 143 144 func (m *backendMock) ChainID(ctx context.Context) (*big.Int, error) { 145 return nil, errors.New("not implemented") 146 } 147 148 func (m *backendMock) Close() { 149 } 150 151 func New(opts ...Option) transaction.Backend { 152 mock := new(backendMock) 153 for _, o := range opts { 154 o.apply(mock) 155 } 156 return mock 157 } 158 159 // Option is the option passed to the mock Chequebook service 160 type Option interface { 161 apply(*backendMock) 162 } 163 164 type optionFunc func(*backendMock) 165 166 func (f optionFunc) apply(r *backendMock) { f(r) } 167 168 func WithCallContractFunc(f func(ctx context.Context, call ethereum.CallMsg, blockNumber *big.Int) ([]byte, error)) Option { 169 return optionFunc(func(s *backendMock) { 170 s.callContract = f 171 }) 172 } 173 174 func WithCodeAtFunc(f func(ctx context.Context, contract common.Address, blockNumber *big.Int) ([]byte, error)) Option { 175 return optionFunc(func(s *backendMock) { 176 s.codeAt = f 177 }) 178 } 179 180 func WithBalanceAt(f func(ctx context.Context, address common.Address, block *big.Int) (*big.Int, error)) Option { 181 return optionFunc(func(s *backendMock) { 182 s.balanceAt = f 183 }) 184 } 185 186 func WithPendingNonceAtFunc(f func(ctx context.Context, account common.Address) (uint64, error)) Option { 187 return optionFunc(func(s *backendMock) { 188 s.pendingNonceAt = f 189 }) 190 } 191 192 func WithSuggestGasPriceFunc(f func(ctx context.Context) (*big.Int, error)) Option { 193 return optionFunc(func(s *backendMock) { 194 s.suggestGasPrice = f 195 }) 196 } 197 198 func WithSuggestGasTipCapFunc(f func(ctx context.Context) (*big.Int, error)) Option { 199 return optionFunc(func(s *backendMock) { 200 s.suggestGasTipCap = f 201 }) 202 } 203 204 func WithEstimateGasFunc(f func(ctx context.Context, call ethereum.CallMsg) (gas uint64, err error)) Option { 205 return optionFunc(func(s *backendMock) { 206 s.estimateGas = f 207 }) 208 } 209 210 func WithTransactionReceiptFunc(f func(ctx context.Context, txHash common.Hash) (*types.Receipt, error)) Option { 211 return optionFunc(func(s *backendMock) { 212 s.transactionReceipt = f 213 }) 214 } 215 216 func WithTransactionByHashFunc(f func(ctx context.Context, txHash common.Hash) (*types.Transaction, bool, error)) Option { 217 return optionFunc(func(s *backendMock) { 218 s.transactionByHash = f 219 }) 220 } 221 222 func WithBlockByNumberFunc(f func(ctx context.Context, number *big.Int) (*types.Block, error)) Option { 223 return optionFunc(func(s *backendMock) { 224 s.blockByNumber = f 225 }) 226 } 227 228 func WithSendTransactionFunc(f func(ctx context.Context, tx *types.Transaction) error) Option { 229 return optionFunc(func(s *backendMock) { 230 s.sendTransaction = f 231 }) 232 } 233 234 func WithBlockNumberFunc(f func(context.Context) (uint64, error)) Option { 235 return optionFunc(func(s *backendMock) { 236 s.blockNumber = f 237 }) 238 } 239 240 func WithHeaderbyNumberFunc(f func(ctx context.Context, number *big.Int) (*types.Header, error)) Option { 241 return optionFunc(func(s *backendMock) { 242 s.headerByNumber = f 243 }) 244 } 245 246 func WithNonceAtFunc(f func(ctx context.Context, account common.Address, blockNumber *big.Int) (uint64, error)) Option { 247 return optionFunc(func(s *backendMock) { 248 s.nonceAt = f 249 }) 250 }