-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtransferer.go
More file actions
89 lines (81 loc) · 2.66 KB
/
Copy pathtransferer.go
File metadata and controls
89 lines (81 loc) · 2.66 KB
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
package ethereum
import (
"errors"
"fmt"
"math/big"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
)
// Transferer allows to make ethers transfer between accounts.
type Transferer struct {
ContractTransactor bind.ContractTransactor
}
// SuggestGasLimit returns suggested gas limit to make transfer.
func (t Transferer) SuggestGasLimit(opts *bind.TransactOpts, to common.Address, input []byte) (gasLimit *big.Int, err error) {
ct := t.ContractTransactor
// Ensure a valid value field and resolve the account nonce
value := opts.Value
if value == nil {
value = new(big.Int)
}
// estimate the transaction
msg := ethereum.CallMsg{From: opts.From, To: &to, Value: value, Data: input}
var gl uint64
gl, err = ct.EstimateGas(ensureContext(opts.Context), msg)
if err != nil {
return nil, fmt.Errorf("failed to estimate gas needed: %v", err)
}
gasLimit = new(big.Int).SetUint64(gl)
return
}
// Transfer transfers ethers to `to` account. `input` is optional and can be set to nil.
func (t Transferer) Transfer(opts *bind.TransactOpts, to common.Address, input []byte) (*types.Transaction, error) {
ct := t.ContractTransactor
var err error
// Ensure a valid value field and resolve the account nonce
value := opts.Value
if value == nil {
value = new(big.Int)
}
var nonce uint64
if opts.Nonce == nil {
nonce, err = ct.PendingNonceAt(ensureContext(opts.Context), opts.From)
if err != nil {
return nil, fmt.Errorf("failed to retrieve account nonce: %v", err)
}
} else {
nonce = opts.Nonce.Uint64()
}
// Figure out the gas allowance and gas price values
gasPrice := opts.GasPrice
if gasPrice == nil {
gasPrice, err = ct.SuggestGasPrice(ensureContext(opts.Context))
if err != nil {
return nil, fmt.Errorf("failed to suggest gas price: %v", err)
}
}
gasLimit := opts.GasLimit
if gasLimit == 0 {
// estimate the transaction
msg := ethereum.CallMsg{From: opts.From, To: &to, Value: value, Data: input}
gasLimit, err = ct.EstimateGas(ensureContext(opts.Context), msg)
if err != nil {
return nil, fmt.Errorf("failed to estimate gas needed: %v", err)
}
}
// Create the transaction, sign it and schedule it for execution
rawTx := types.NewTransaction(nonce, to, value, gasLimit, gasPrice, input)
if opts.Signer == nil {
return nil, errors.New("no signer to authorize the transaction with")
}
signedTx, err := opts.Signer(types.HomesteadSigner{}, opts.From, rawTx)
if err != nil {
return nil, err
}
if err := ct.SendTransaction(ensureContext(opts.Context), signedTx); err != nil {
return nil, err
}
return signedTx, nil
}