-
Notifications
You must be signed in to change notification settings - Fork 26
/
ethereum_wallet.go
125 lines (100 loc) · 3.3 KB
/
ethereum_wallet.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
package zcnbridge
import (
"context"
"crypto/ecdsa"
"math/big"
"path"
"time"
"github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/accounts/keystore"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/params"
"github.com/pkg/errors"
"github.com/ethereum/go-ethereum/common"
)
func (b *EthereumConfig) CreateEthClient() (*ethclient.Client, error) {
client, err := ethclient.Dial(b.EthereumNodeURL)
if err != nil {
Logger.Error(err)
}
return client, err
}
// _allowances[owner][spender] = amount;
// as a spender, ERC20 WZCN token must increase allowance for the bridge to make burn on behalf of WZCN owner
func CreateSignedTransaction(
chainID *big.Int,
client *ethclient.Client,
fromAddress common.Address,
privateKey *ecdsa.PrivateKey,
gasLimitUnits uint64,
) *bind.TransactOpts {
nonce, err := client.PendingNonceAt(context.Background(), fromAddress)
if err != nil {
Logger.Fatal(err)
}
// eth_estimateGas
// EstimateGas tries to estimate the gas needed to execute a specific transaction based on
// the current pending state of the backend blockchain. There is no guarantee that this is
// the true gas limit requirement as other transactions may be added or removed by miners,
// but it should provide a basis for setting a reasonable default.
// eth_gasPrice
// retrieves the currently suggested gas price to allow a timely
// execution of a transaction
gasPriceWei, err := client.SuggestGasPrice(context.Background())
if err != nil {
Logger.Fatal(err)
}
opts, err := bind.NewKeyedTransactorWithChainID(privateKey, chainID)
if err != nil {
Logger.Fatal(err)
}
valueWei := new(big.Int).Mul(big.NewInt(0), big.NewInt(params.Wei))
opts.Nonce = big.NewInt(int64(nonce))
opts.Value = valueWei // in wei
opts.GasLimit = gasLimitUnits // in units
opts.GasPrice = gasPriceWei // wei
return opts
}
func (b *BridgeClientConfig) CreateSignedTransactionFromKeyStore(client *ethclient.Client, gasLimitUnits uint64) *bind.TransactOpts {
var (
signerAddress = common.HexToAddress(b.EthereumAddress)
password = b.Password
value = b.Value
)
keyDir := path.Join(b.Homedir, EthereumWalletStorageDir)
ks := keystore.NewKeyStore(keyDir, keystore.StandardScryptN, keystore.StandardScryptP)
signer := accounts.Account{
Address: signerAddress,
}
signerAcc, err := ks.Find(signer)
if err != nil {
Logger.Fatal(errors.Wrapf(err, "signer: %s", signerAddress.Hex()))
}
chainID, err := client.ChainID(context.Background())
if err != nil {
Logger.Fatal(errors.Wrap(err, "failed to get chain ID"))
}
nonce, err := client.PendingNonceAt(context.Background(), signerAddress)
if err != nil {
Logger.Fatal(err)
}
gasPriceWei, err := client.SuggestGasPrice(context.Background())
if err != nil {
Logger.Fatal(err)
}
err = ks.TimedUnlock(signer, password, time.Second*2)
if err != nil {
Logger.Fatal(err)
}
opts, err := bind.NewKeyStoreTransactorWithChainID(ks, signerAcc, chainID)
if err != nil {
Logger.Fatal(err)
}
valueWei := new(big.Int).Mul(big.NewInt(value), big.NewInt(params.Wei))
opts.Nonce = big.NewInt(int64(nonce))
opts.Value = valueWei // in wei
opts.GasLimit = gasLimitUnits // in units
opts.GasPrice = gasPriceWei // wei
return opts
}