-
Notifications
You must be signed in to change notification settings - Fork 2
/
erc20.go
86 lines (71 loc) · 2.49 KB
/
erc20.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// Copyright 2021 The Swarm Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package mock
import (
"context"
"errors"
"math/big"
"github.com/ethereum/go-ethereum/common"
"github.com/ethsana/sana/pkg/settlement/swap/erc20"
)
type Service struct {
balanceOfFunc func(ctx context.Context, address common.Address) (*big.Int, error)
transferFunc func(ctx context.Context, address common.Address, value *big.Int) (common.Hash, error)
appproveFunc func(ctx context.Context, spender common.Address, value *big.Int) (common.Hash, error)
waitForApproveFunc func(ctx context.Context, hash common.Hash) error
allowanceFunc func(ctx context.Context, owner, spender common.Address) (*big.Int, error)
}
func WithBalanceOfFunc(f func(ctx context.Context, address common.Address) (*big.Int, error)) Option {
return optionFunc(func(s *Service) {
s.balanceOfFunc = f
})
}
func WithTransferFunc(f func(ctx context.Context, address common.Address, value *big.Int) (common.Hash, error)) Option {
return optionFunc(func(s *Service) {
s.transferFunc = f
})
}
func New(opts ...Option) erc20.Service {
mock := new(Service)
for _, o := range opts {
o.apply(mock)
}
return mock
}
func (s *Service) BalanceOf(ctx context.Context, address common.Address) (*big.Int, error) {
if s.balanceOfFunc != nil {
return s.balanceOfFunc(ctx, address)
}
return big.NewInt(0), errors.New("Error")
}
func (s *Service) Transfer(ctx context.Context, address common.Address, value *big.Int) (common.Hash, error) {
if s.transferFunc != nil {
return s.transferFunc(ctx, address, value)
}
return common.Hash{}, errors.New("Error")
}
func (s *Service) Approve(ctx context.Context, spender common.Address, value *big.Int) (common.Hash, error) {
if s.transferFunc != nil {
return s.appproveFunc(ctx, spender, value)
}
return common.Hash{}, errors.New("Error")
}
func (s *Service) WaitForApprove(ctx context.Context, hash common.Hash) error {
if s.transferFunc != nil {
return s.waitForApproveFunc(ctx, hash)
}
return errors.New("Error")
}
func (s *Service) Allowance(ctx context.Context, owner, spender common.Address) (*big.Int, error) {
if s.transferFunc != nil {
return s.allowanceFunc(ctx, owner, spender)
}
return nil, errors.New("Error")
}
// Option is the option passed to the mock Chequebook service
type Option interface {
apply(*Service)
}
type optionFunc func(*Service)
func (f optionFunc) apply(r *Service) { f(r) }