-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
eth_transaction.go
164 lines (123 loc) · 3.84 KB
/
eth_transaction.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package resolver
import (
"context"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/graph-gophers/graphql-go"
"github.com/smartcontractkit/chainlink/core/chains/evm/txmgr"
"github.com/smartcontractkit/chainlink/core/utils/stringutils"
"github.com/smartcontractkit/chainlink/core/web/loader"
)
type EthTransactionResolver struct {
tx txmgr.EthTx
}
func NewEthTransaction(tx txmgr.EthTx) *EthTransactionResolver {
return &EthTransactionResolver{tx: tx}
}
func NewEthTransactions(results []txmgr.EthTx) []*EthTransactionResolver {
var resolver []*EthTransactionResolver
for _, tx := range results {
resolver = append(resolver, NewEthTransaction(tx))
}
return resolver
}
func (r *EthTransactionResolver) State() string {
return string(r.tx.State)
}
func (r *EthTransactionResolver) Data() hexutil.Bytes {
return hexutil.Bytes(r.tx.EncodedPayload)
}
func (r *EthTransactionResolver) From() string {
return r.tx.FromAddress.String()
}
func (r *EthTransactionResolver) To() string {
return r.tx.ToAddress.String()
}
func (r *EthTransactionResolver) GasLimit() string {
return stringutils.FromInt64(int64(r.tx.GasLimit))
}
func (r *EthTransactionResolver) GasPrice(ctx context.Context) string {
attempts, err := r.Attempts(ctx)
if err != nil || len(attempts) == 0 {
return ""
}
return attempts[0].GasPrice()
}
func (r *EthTransactionResolver) Value() string {
return r.tx.Value.String()
}
func (r *EthTransactionResolver) EVMChainID() graphql.ID {
return graphql.ID(r.tx.EVMChainID.String())
}
func (r *EthTransactionResolver) Nonce() *string {
if r.tx.Nonce == nil {
return nil
}
value := stringutils.FromInt64(*r.tx.Nonce)
return &value
}
func (r *EthTransactionResolver) Hash(ctx context.Context) string {
attempts, err := r.Attempts(ctx)
if err != nil || len(attempts) == 0 {
return ""
}
return attempts[0].Hash()
}
func (r *EthTransactionResolver) Hex(ctx context.Context) string {
attempts, err := r.Attempts(ctx)
if err != nil || len(attempts) == 0 {
return ""
}
return attempts[0].Hex()
}
// Chain resolves the node's chain object field.
func (r *EthTransactionResolver) Chain(ctx context.Context) (*ChainResolver, error) {
chain, err := loader.GetChainByID(ctx, string(r.EVMChainID()))
if err != nil {
return nil, err
}
return NewChain(*chain), nil
}
func (r *EthTransactionResolver) Attempts(ctx context.Context) ([]*EthTransactionAttemptResolver, error) {
id := stringutils.FromInt64(r.tx.ID)
attempts, err := loader.GetEthTxAttemptsByEthTxID(ctx, id)
if err != nil {
return nil, err
}
return NewEthTransactionsAttempts(attempts), nil
}
func (r *EthTransactionResolver) SentAt(ctx context.Context) *string {
attempts, err := r.Attempts(ctx)
if err != nil || len(attempts) == 0 {
return nil
}
return attempts[0].SentAt()
}
// -- EthTransaction Query --
type EthTransactionPayloadResolver struct {
tx *txmgr.EthTx
NotFoundErrorUnionType
}
func NewEthTransactionPayload(tx *txmgr.EthTx, err error) *EthTransactionPayloadResolver {
e := NotFoundErrorUnionType{err: err, message: "transaction not found", isExpectedErrorFn: nil}
return &EthTransactionPayloadResolver{tx: tx, NotFoundErrorUnionType: e}
}
func (r *EthTransactionPayloadResolver) ToEthTransaction() (*EthTransactionResolver, bool) {
if r.err != nil {
return nil, false
}
return NewEthTransaction(*r.tx), true
}
// -- EthTransactions Query --
type EthTransactionsPayloadResolver struct {
results []txmgr.EthTx
total int32
}
func NewEthTransactionsPayload(results []txmgr.EthTx, total int32) *EthTransactionsPayloadResolver {
return &EthTransactionsPayloadResolver{results: results, total: total}
}
func (r *EthTransactionsPayloadResolver) Results() []*EthTransactionResolver {
return NewEthTransactions(r.results)
}
func (r *EthTransactionsPayloadResolver) Metadata() *PaginationMetadataResolver {
return NewPaginationMetadata(r.total)
}