-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
flags.go
79 lines (63 loc) · 2.23 KB
/
flags.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
package fluxmonitorv2
import (
"reflect"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
evmclient "github.com/smartcontractkit/chainlink/core/chains/evm/client"
"github.com/smartcontractkit/chainlink/core/gethwrappers/generated"
"github.com/smartcontractkit/chainlink/core/gethwrappers/generated/flags_wrapper"
"github.com/smartcontractkit/chainlink/core/utils"
)
//go:generate mockery --quiet --name Flags --output ./mocks/ --case=underscore --structname Flags --filename flags.go
type Flags interface {
ContractExists() bool
IsLowered(contractAddr common.Address) (bool, error)
Address() common.Address
ParseLog(log types.Log) (generated.AbigenLog, error)
}
// ContractFlags wraps the a contract
type ContractFlags struct {
flags_wrapper.FlagsInterface
}
// NewFlags constructs a new Flags from a flags contract address
func NewFlags(addrHex string, ethClient evmclient.Client) (Flags, error) {
flags := &ContractFlags{}
if addrHex == "" {
return flags, nil
}
contractAddr := common.HexToAddress(addrHex)
contract, err := flags_wrapper.NewFlags(contractAddr, ethClient)
if err != nil {
return flags, err
}
// This is necessary due to the unfortunate fact that assigning `nil` to an
// interface variable causes `x == nil` checks to always return false. If we
// do this here, in the constructor, we can avoid using reflection when we
// check `p.flags == nil` later in the code.
if contract != nil && !reflect.ValueOf(contract).IsNil() {
flags.FlagsInterface = contract
}
return flags, nil
}
// Contract returns the flags contract
func (f *ContractFlags) Contract() flags_wrapper.FlagsInterface {
return f.FlagsInterface
}
// ContractExists returns whether a flag contract exists
func (f *ContractFlags) ContractExists() bool {
return f.FlagsInterface != nil
}
// IsLowered determines whether the flag is lowered for a given contract.
// If a contract does not exist, it is considered to be lowered
func (f *ContractFlags) IsLowered(contractAddr common.Address) (bool, error) {
if !f.ContractExists() {
return true, nil
}
flags, err := f.GetFlags(nil,
[]common.Address{utils.ZeroAddress, contractAddr},
)
if err != nil {
return true, err
}
return !flags[0] || !flags[1], nil
}