/
transaction.go
123 lines (109 loc) · 3 KB
/
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
// Copyright © 2022 Weald Technology Trading
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package conn
import (
"context"
"fmt"
"github.com/ethereum/go-ethereum/core/types"
"github.com/pkg/errors"
)
// CreateSignedTransaction creates a signed transaction.
func (c *Conn) CreateSignedTransaction(ctx context.Context,
txData *TransactionData,
) (
*types.Transaction,
error,
) {
tx, err := c.CreateTransaction(ctx, txData)
if err != nil {
return nil, err
}
// Sign the transaction.
signedTx, err := c.SignTransaction(ctx, txData.From, tx)
if err != nil {
err = fmt.Errorf("failed to sign transaction: %v", err)
return nil, err
}
// Increment the nonce for the next transaction.
_, err = c.NextNonce(ctx, txData.From)
if err != nil {
return nil, err
}
return signedTx, nil
}
// CreateTransaction creates a transaction.
func (c *Conn) CreateTransaction(ctx context.Context,
txData *TransactionData,
) (
*types.Transaction,
error,
) {
if txData.Nonce == nil {
// Obtain the nonce for the transaction.
nonce, err := c.CurrentNonce(ctx, txData.From)
if err != nil {
return nil, err
}
txNonce := int64(nonce)
txData.Nonce = &txNonce
}
if txData.GasLimit == nil {
// Calculate gas limit for the transaction
gasLimit, err := c.EstimateGas(ctx, txData)
if err != nil {
return nil, err
}
txData.GasLimit = &gasLimit
}
// Calculate fees.
maxFeePerGas, maxPriorityFeePerGas, err := c.CalculateFees()
if err != nil {
return nil, err
}
if txData.MaxFeePerGas != nil {
// Manual override for fee per gas.
maxFeePerGas = txData.MaxFeePerGas
}
if txData.MaxPriorityFeePerGas != nil {
// Manual override for priority fee per gas.
maxPriorityFeePerGas = txData.MaxPriorityFeePerGas
}
// Create the transaction
return types.NewTx(&types.DynamicFeeTx{
ChainID: c.ChainID(),
Nonce: uint64(*txData.Nonce),
GasFeeCap: maxFeePerGas,
GasTipCap: maxPriorityFeePerGas,
Gas: *txData.GasLimit,
To: txData.To,
Value: txData.Value,
Data: txData.Data,
}), nil
}
// SendTransaction send the supplied transaction to the network.
func (c *Conn) SendTransaction(ctx context.Context,
tx *types.Transaction,
) error {
if c.client == nil {
return errors.New("cannot send transaction when offline")
}
if tx == nil {
return errors.New("transaction is nil")
}
ctx, cancel := context.WithTimeout(ctx, c.timeout)
defer cancel()
if err := c.Client().SendTransaction(ctx, tx); err != nil {
return errors.Wrap(err, "failed to send transaction")
}
return nil
}