-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
eth_key.go
132 lines (99 loc) · 2.56 KB
/
eth_key.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package resolver
import (
"context"
"github.com/ethereum/go-ethereum/common"
"github.com/graph-gophers/graphql-go"
"github.com/smartcontractkit/chainlink/core/chains/evm"
"github.com/smartcontractkit/chainlink/core/services/keystore/keys/ethkey"
"github.com/smartcontractkit/chainlink/core/web/loader"
)
type ETHKey struct {
state ethkey.State
addr ethkey.EIP55Address
chain evm.Chain
}
type ETHKeyResolver struct {
key ETHKey
}
func NewETHKey(key ETHKey) *ETHKeyResolver {
return ÐKeyResolver{key: key}
}
func NewETHKeys(keys []ETHKey) []*ETHKeyResolver {
var resolvers []*ETHKeyResolver
for _, k := range keys {
resolvers = append(resolvers, NewETHKey(k))
}
return resolvers
}
func (r *ETHKeyResolver) Chain(ctx context.Context) (*ChainResolver, error) {
chain, err := loader.GetChainByID(ctx, r.key.state.EVMChainID.String())
if err != nil {
return nil, err
}
return NewChain(*chain), nil
}
func (r *ETHKeyResolver) Address() string {
return r.key.addr.Hex()
}
func (r *ETHKeyResolver) IsFunding() bool {
return r.key.state.IsFunding
}
// ETHBalance returns the ETH balance available
func (r *ETHKeyResolver) ETHBalance(ctx context.Context) *string {
if r.key.chain == nil {
return nil
}
balanceMonitor := r.key.chain.BalanceMonitor()
if balanceMonitor == nil {
return nil
}
balance := balanceMonitor.GetEthBalance(r.key.state.Address.Address())
if balance != nil {
val := balance.String()
return &val
}
return nil
}
func (r *ETHKeyResolver) LINKBalance() *string {
if r.key.chain == nil {
return nil
}
client := r.key.chain.Client()
addr := common.HexToAddress(r.key.chain.Config().LinkContractAddress())
balance, err := client.GetLINKBalance(addr, r.key.state.Address.Address())
if err != nil {
return nil
}
if balance != nil {
val := balance.String()
return &val
}
return nil
}
func (r *ETHKeyResolver) MaxGasPriceWei() *string {
if r.key.chain == nil {
return nil
}
gasPrice := r.key.chain.Config().KeySpecificMaxGasPriceWei(r.key.addr.Address())
if gasPrice != nil {
val := gasPrice.String()
return &val
}
return nil
}
func (r *ETHKeyResolver) CreatedAt() graphql.Time {
return graphql.Time{Time: r.key.state.CreatedAt}
}
func (r *ETHKeyResolver) UpdatedAt() graphql.Time {
return graphql.Time{Time: r.key.state.UpdatedAt}
}
// -- EthKeys query --
type ETHKeysPayloadResolver struct {
keys []ETHKey
}
func NewETHKeysPayload(keys []ETHKey) *ETHKeysPayloadResolver {
return ÐKeysPayloadResolver{keys: keys}
}
func (r *ETHKeysPayloadResolver) Results() []*ETHKeyResolver {
return NewETHKeys(r.keys)
}