-
Notifications
You must be signed in to change notification settings - Fork 1
/
native.go
121 lines (90 loc) · 2.69 KB
/
native.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
package ethcli
import (
"context"
"fmt"
"log"
"math/big"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/params"
"github.com/medeirosfalante/ethcli/util"
hdwallet "github.com/miguelmota/go-ethereum-hdwallet"
)
const (
params_ether int = 18
)
//sudo apt-get install libhidapi-dev
type Native struct {
client *ethclient.Client
}
func NewNative(client *ethclient.Client) *Native {
return &Native{
client: client,
}
}
func (t *Native) GetAddress(mnemonic string, pathDerivation string) (*common.Address, error) {
wallet, err := hdwallet.NewFromMnemonic(mnemonic)
if err != nil {
return nil, err
}
path := hdwallet.MustParseDerivationPath(pathDerivation)
account, err := wallet.Derive(path, true)
if err != nil {
return nil, fmt.Errorf("account %s", err.Error())
}
return &account.Address, nil
}
func (t *Native) BalanceOf(address string) (*big.Float, error) {
account := common.HexToAddress(address)
balance, err := t.client.BalanceAt(context.Background(), account, nil)
if err != nil {
log.Fatal(err)
}
return weiToEther(balance, params.Ether), nil
}
func (t *Native) Transfer(req *TransferOpts) (string, error) {
wallet, err := hdwallet.NewFromMnemonic(req.Mnemonic)
if err != nil {
return "", fmt.Errorf("mnemonic %s", err.Error())
}
path := hdwallet.MustParseDerivationPath(req.Path)
account, err := wallet.Derive(path, true)
if err != nil {
return "", fmt.Errorf("account %s", err.Error())
}
fromAddress := account.Address
nonce, err := t.client.PendingNonceAt(context.Background(), fromAddress)
if err != nil {
return "", fmt.Errorf("nonce %s", err.Error())
}
chainID, err := t.client.ChainID(context.Background())
if err != nil {
return "", fmt.Errorf("chainID %s", err.Error())
}
value := util.ToWei(req.Amount, params_ether)
var data []byte
gasLimit := uint64(21000) // in units
gasPrice, err := t.client.SuggestGasPrice(context.Background())
if err != nil {
return "", fmt.Errorf("gasPrice %s", err.Error())
}
toAddress := common.HexToAddress(req.Address)
tx := types.NewTransaction(nonce, toAddress, value, gasLimit, gasPrice, data)
sign, err := wallet.SignTxEIP155(account, tx, chainID)
if err != nil {
return "", fmt.Errorf("sign %s", err.Error())
}
err = t.client.SendTransaction(context.Background(), sign)
if err != nil {
return "", fmt.Errorf("tx %s", err.Error())
}
return sign.Hash().Hex(), nil
}
func (t *Native) SuggestGasPrice() (*big.Int, error) {
gasPrice, err := t.client.SuggestGasPrice(context.Background())
if err != nil {
return nil, fmt.Errorf("gasPrice %s", err.Error())
}
return gasPrice, nil
}