-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtoken.go
262 lines (225 loc) · 6.99 KB
/
token.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
package ethcli
import (
"bytes"
"context"
"errors"
"fmt"
"log"
"math"
"math/big"
"os"
"strconv"
"strings"
"time"
abiRef "github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethclient"
hdwallet "github.com/miguelmota/go-ethereum-hdwallet"
"github.com/medeirosfalante/ethcli/abi"
"github.com/medeirosfalante/ethcli/util"
)
//sudo apt-get install libhidapi-dev
type TokenErc20 struct {
wethTokenAddress common.Address
TokenAddress string
client *ethclient.Client
instance *abi.Token
}
type TransferOpts struct {
Mnemonic string
Path string
Address string
Amount float64
}
const wethTokenAddress = "0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c"
func NewDefiToken(TokenAddress string, client *ethclient.Client) *TokenErc20 {
tokenAddress := common.HexToAddress(TokenAddress)
instance, _ := abi.NewToken(tokenAddress, client)
return &TokenErc20{
TokenAddress: TokenAddress,
client: client,
instance: instance,
wethTokenAddress: common.HexToAddress(wethTokenAddress),
}
}
func NewTokenErc20(TokenAddress string, client *ethclient.Client) *TokenErc20 {
tokenAddress := common.HexToAddress(TokenAddress)
instance, _ := abi.NewToken(tokenAddress, client)
return &TokenErc20{
TokenAddress: TokenAddress,
client: client,
instance: instance,
}
}
func etherToWei(eth *big.Float, parm int) *big.Int {
truncInt, _ := eth.Int(nil)
potencial := strings.Repeat("0", parm)
i, _ := strconv.ParseInt("1"+potencial, 10, 64)
truncInt = new(big.Int).Mul(truncInt, big.NewInt(i))
fracInt, _ := new(big.Int).SetString(potencial, parm)
wei := new(big.Int).Add(truncInt, fracInt)
return wei
}
func weiToEther(wei *big.Int, parm float64) *big.Float {
f := new(big.Float)
f.SetPrec(236)
f.SetMode(big.ToNearestEven)
fWei := new(big.Float)
fWei.SetPrec(236)
fWei.SetMode(big.ToNearestEven)
return f.Quo(fWei.SetInt(wei), big.NewFloat(parm))
}
func (t *TokenErc20) BuyToken(req *TransferOpts) (string, error) {
tokenWantedAddress := common.HexToAddress(t.TokenAddress)
wethTokenAddressRef := common.HexToAddress(wethTokenAddress)
pathRouter := []common.Address{wethTokenAddressRef, tokenWantedAddress}
wallet, err := hdwallet.NewFromMnemonic(req.Mnemonic)
if err != nil {
return "", err
}
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())
}
auth := &bind.TransactOpts{
Signer: func(address common.Address, tx *types.Transaction) (*types.Transaction, error) {
return wallet.SignTx(account, tx, nil)
},
}
deadline := &big.Int{}
deadline.SetInt64(time.Now().Add(10 * time.Minute).Unix())
ref, err := t.instance.Decimals(nil)
if err != nil {
return "", err
}
amountOutMin := util.ToWei(0.01, int(ref.Int64()))
auth.Nonce = big.NewInt(int64(nonce))
file, err := os.OpenFile("./pancakeswap/router.json", os.O_RDWR, 0644)
if err != nil {
return "", fmt.Errorf("file %s", err.Error())
}
defer file.Close()
buf := new(bytes.Buffer)
buf.ReadFrom(file)
contents := buf.String()
routerABI, err := abiRef.JSON(strings.NewReader(contents))
if err != nil {
log.Fatal(err)
}
data, err := routerABI.Pack("swapExactETHForTokens", amountOutMin, pathRouter, fromAddress, deadline)
if err != nil {
return "", fmt.Errorf("data %s", err.Error())
}
gasPrice, err := t.client.SuggestGasPrice(context.Background())
if err != nil {
return "", fmt.Errorf("gasPrice %s", err.Error())
}
gasLimit := uint64(91000)
tx := types.NewTransaction(nonce, fromAddress, big.NewInt(0), gasLimit, gasPrice, data)
chainID, err := t.client.NetworkID(context.Background())
if err != nil {
return "", fmt.Errorf("chainID %s", err.Error())
}
privRef, _ := wallet.PrivateKey(account)
signedTx, err := types.SignTx(tx, types.NewEIP155Signer(chainID), privRef)
if err != nil {
return "", fmt.Errorf("signedTx %s", err.Error())
}
err = t.client.SendTransaction(context.Background(), signedTx)
if err != nil {
return "", fmt.Errorf("SendTransaction %s", err.Error())
}
return signedTx.Hash().String(), nil
}
func (t *TokenErc20) BalanceOf(account string) (*big.Float, error) {
tokenAddress := common.HexToAddress(account)
amount, err := t.instance.BalanceOf(&bind.CallOpts{}, tokenAddress)
if err != nil {
return nil, err
}
instance, err := abi.NewToken(common.HexToAddress(t.TokenAddress), t.client)
if err != nil {
return nil, err
}
ref, err := instance.Decimals(nil)
if err != nil {
return nil, err
}
return weiToEther(amount, math.Pow10(int(ref.Int64()))), nil
}
func CalcGasCost(gasLimit uint64, gasPrice *big.Int) *big.Int {
gasLimitBig := big.NewInt(int64(gasLimit))
return gasLimitBig.Mul(gasLimitBig, gasPrice)
}
func (t *TokenErc20) Transfer(req *TransferOpts) (string, error) {
wallet, err := hdwallet.NewFromMnemonic(req.Mnemonic)
if err != nil {
return "", err
}
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())
}
auth := &bind.TransactOpts{
From: fromAddress,
Signer: func(address common.Address, tx *types.Transaction) (*types.Transaction, error) {
if address != fromAddress {
return nil, errors.New("not authorized to sign this account")
}
// if chainID.Int64() == 80001 {
// return wallet.SignTx(account, tx, chainID)
// }
return wallet.SignTxEIP155(account, tx, chainID)
},
}
gasPrice, err := t.GetGasPrice()
if err != nil {
return "", fmt.Errorf("GetGasPrice %s", err.Error())
}
auth.GasPrice = gasPrice
tokenAddress := common.HexToAddress(t.TokenAddress)
instance, err := abi.NewToken(tokenAddress, t.client)
if err != nil {
return "", fmt.Errorf("instance %s", err.Error())
}
ref, err := instance.Decimals(nil)
if err != nil {
return "", err
}
value := util.ToWei(req.Amount, int(ref.Int64()))
auth.Nonce = big.NewInt(int64(nonce))
addressRef := common.HexToAddress(req.Address)
tx, err := instance.Transfer(auth, addressRef, value)
if err != nil {
return "", fmt.Errorf("tx %s", err.Error())
}
return tx.Hash().Hex(), nil
}
func (t *TokenErc20) ChainID() (*big.Int, error) {
chainID, err := t.client.ChainID(context.Background())
if err != nil {
return nil, fmt.Errorf("chainID %s", err.Error())
}
return chainID, nil
}
func (t *TokenErc20) GetGasPrice() (*big.Int, error) {
return t.client.SuggestGasPrice(context.Background())
}