-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
499 lines (397 loc) · 14.8 KB
/
main.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
package main
import (
"buytokenspancakegolang/genericutils"
"buytokenspancakegolang/models"
"bytes"
"context"
"crypto/ecdsa"
"encoding/gob"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"math/big"
"os"
"strconv"
"strings"
"time"
ierc20 "buytokenspancakegolang/contracts/IERC20"
pancakeFactory "buytokenspancakegolang/contracts/IPancakeFactory"
pancakePair "buytokenspancakegolang/contracts/IPancakePair"
pancakeRouter "buytokenspancakegolang/contracts/IPancakeRouter02"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/fatih/color"
"github.com/hrharder/go-gas"
"github.com/mattn/go-colorable"
"github.com/nikola43/web3golanghelper/web3helper"
"github.com/samber/lo"
"github.com/sirupsen/logrus"
"golang.org/x/crypto/sha3"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)
// Create SprintXxx functions to mix strings with other non-colorized strings:
var yellow = color.New(color.FgYellow).SprintFunc()
var red = color.New(color.FgRed).SprintFunc()
var cyan = color.New(color.FgCyan).SprintFunc()
var green = color.New(color.FgGreen).SprintFunc()
type Wallet struct {
PublicKey string `json:"PublicKey"`
PrivateKey string `json:"PrivateKey"`
}
type Reserve struct {
Reserve0 *big.Int
Reserve1 *big.Int
BlockTimestampLast uint32
}
func main() {
//GenerateWallet()
fmt.Println(parseDateTime())
getWallets()
// Declarations
web3GolangHelper := initWeb3()
db := InitDatabase()
//migrate(db)
factoryAddress := "0xB7926C0430Afb07AA7DEfDE6DA862aE0Bde767bc"
factoryAbi, _ := abi.JSON(strings.NewReader(string(pancakeFactory.PancakeABI)))
// LOGIC -----------------------------------------------------------
checkTokens(db, web3GolangHelper)
proccessEvents(db, web3GolangHelper, factoryAddress, factoryAbi)
}
func proccessEvents(db *gorm.DB, web3GolangHelper *web3helper.Web3GolangHelper, contractAddress string, contractAbi abi.ABI) {
logs := make(chan types.Log)
sub := BuildContractEventSubscription(web3GolangHelper, contractAddress, logs)
for {
select {
case err := <-sub.Err():
fmt.Println(err)
//out <- err.Error()
case vLog := <-logs:
fmt.Println("vLog.TxHash: " + vLog.TxHash.Hex())
res, err := contractAbi.Unpack("PairCreated", vLog.Data)
if err != nil {
log.Fatal(err)
}
fmt.Println(res)
InsertNewEvent(db, res, vLog)
}
}
}
func initWeb3() *web3helper.Web3GolangHelper {
pk := "b366406bc0b4883b9b4b3b41117d6c62839174b7d21ec32a5ad0cc76cb3496bd"
rpcUrl := "https://speedy-nodes-nyc.moralis.io/84a2745d907034e6d388f8d6/bsc/testnet"
wsUrl := "wss://speedy-nodes-nyc.moralis.io/84a2745d907034e6d388f8d6/bsc/testnet/ws"
web3GolangHelper := web3helper.NewWeb3GolangHelper(rpcUrl, wsUrl, pk)
chainID, err := web3GolangHelper.HttpClient().NetworkID(context.Background())
if err != nil {
fmt.Println(err)
}
fmt.Println("Chain Id: " + chainID.String())
return web3GolangHelper
}
func BuildContractEventSubscription(web3GolangHelper *web3helper.Web3GolangHelper, contractAddress string, logs chan types.Log) ethereum.Subscription {
query := ethereum.FilterQuery{
Addresses: []common.Address{common.HexToAddress(contractAddress)},
}
sub, err := web3GolangHelper.WebSocketClient().SubscribeFilterLogs(context.Background(), query, logs)
if err != nil {
fmt.Println(sub)
}
return sub
}
func InitDatabase() *gorm.DB {
db, err := gorm.Open(sqlite.Open("test.db"), &gorm.Config{})
if err != nil {
panic("failed to connect database")
}
return db
}
func migrate(db *gorm.DB) {
db.Migrator().DropTable(&models.EventsCatched{})
db.Migrator().DropTable(&models.LpPair{})
db.Migrator().CreateTable(&models.LpPair{})
db.Migrator().CreateTable(&models.EventsCatched{})
}
func InsertNewEvent(db *gorm.DB, newEvent []interface{}, vLog types.Log) bool {
wBnbContractAddress := "0xae13d989daC2f0dEbFf460aC112a837C89BAa7cd"
tokenAddressA := vLog.Topics[0]
tokenAddressB := vLog.Topics[1]
event := new(models.EventsCatched)
lpPairs := make([]*models.LpPair, 0)
lpPairs = append(lpPairs, &models.LpPair{
LPAddress: newEvent[0].(common.Address).Hex(),
LPPairA: tokenAddressA.String(),
LPPairB: tokenAddressB.String(),
HasLiquidity: false,
})
event.TxHash = vLog.TxHash.Hex()
event.LPPairs = lpPairs
if tokenAddressA.Hex() != wBnbContractAddress {
event.TokenAddress = tokenAddressA.Hex()
} else {
event.TokenAddress = tokenAddressB.Hex()
}
db.Create(event)
return true
}
func UpdateLiquidity(db *gorm.DB, eventID uint) bool {
lpPair := new(models.LpPair)
db.Where(&lpPair, "events_catched_id = ?", eventID).Update("has_liquidity", 1)
return true
}
func UpdateName(db *gorm.DB, token string, name string) bool {
event := new(models.EventsCatched)
db.Where(&event, "token_address = ?", token).Update("token_name", 1)
return true
}
func checkTokens(db *gorm.DB, web3GolangHelper *web3helper.Web3GolangHelper) {
events := make([]*models.EventsCatched, 0)
db.Find(&events)
lo.ForEach(events, func(element *models.EventsCatched, _ int) {
printTokenStatus(element)
updateTokenStatus(db, web3GolangHelper, element)
})
}
func Buy(web3GolangHelper *web3helper.Web3GolangHelper, tokenAddress string) {
// contract addresses
pancakeContractAddress := common.HexToAddress("0x9Ac64Cc6e4415144C455BD8E4837Fea55603e5c3") // pancake router address
wBnbContractAddress := "0xae13d989daC2f0dEbFf460aC112a837C89BAa7cd" // wbnb token adddress
tokenContractAddress := common.HexToAddress(tokenAddress) // eth token adddress
// create pancakeRouter pancakeRouterInstance
pancakeRouterInstance, instanceErr := pancakeRouter.NewPancake(pancakeContractAddress, web3GolangHelper.HttpClient())
if instanceErr != nil {
fmt.Println(instanceErr)
}
fmt.Println("pancakeRouterInstance contract is loaded")
// calculate gas and gas limit
gasLimit := uint64(2100000) // in units
gasPrice, gasPriceErr := gas.SuggestGasPrice(gas.GasPriorityAverage)
if gasPriceErr != nil {
fmt.Println(gasPriceErr)
}
fmt.Println(
wBnbContractAddress,
tokenContractAddress,
pancakeRouterInstance,
gasLimit,
gasPrice,
)
// calculate fee and final value
gasFee := web3helper.CalcGasCost(gasLimit, gasPrice)
ethValue := web3helper.EtherToWei(big.NewFloat(0.1))
finalValue := big.NewInt(0).Sub(ethValue, gasFee)
// set transaction data
transactor := web3GolangHelper.BuildTransactor(finalValue, gasPrice, gasLimit)
amountOutMin := big.NewInt(1.0)
deadline := big.NewInt(time.Now().Unix() + 10000)
path := web3helper.GeneratePath(wBnbContractAddress, tokenContractAddress.Hex())
swapTx, SwapExactETHForTokensErr := pancakeRouterInstance.SwapExactETHForTokensSupportingFeeOnTransferTokens(
transactor,
amountOutMin,
path,
*web3GolangHelper.FromAddress,
deadline)
if SwapExactETHForTokensErr != nil {
fmt.Println("SwapExactETHForTokensErr")
fmt.Println(SwapExactETHForTokensErr)
}
fmt.Println(swapTx)
txHash := swapTx.Hash().Hex()
fmt.Println(txHash)
genericutils.OpenBrowser("https://testnet.bscscan.com/tx/" + txHash)
}
/*
function swapExactETHForTokensSupportingFeeOnTransferTokens(
uint amountOutMin,
address[] calldata path,
address to,
uint deadline
) external payable;
*/
func BuyV2(web3GolangHelper *web3helper.Web3GolangHelper, tokenAddress string, value *big.Int) {
toAddress := common.HexToAddress("0x9Ac64Cc6e4415144C455BD8E4837Fea55603e5c3")
wBnbContractAddress := "0xae13d989daC2f0dEbFf460aC112a837C89BAa7cd"
transferFnSignature := []byte("swapExactETHForTokensSupportingFeeOnTransferTokens(uint,address[],address,uint)")
hash := sha3.NewLegacyKeccak256()
hash.Write(transferFnSignature)
methodID := hash.Sum(nil)[:4]
path := web3helper.GeneratePath(wBnbContractAddress, tokenAddress)
pathString := []string{path[0].Hex(), path[1].Hex()}
deadline := big.NewInt(time.Now().Unix() + 10000)
buf := &bytes.Buffer{}
gob.NewEncoder(buf).Encode(pathString)
bs := buf.Bytes()
fmt.Printf("%q", bs)
paddedAmountOutMin := common.LeftPadBytes(value.Bytes(), 32)
paddedPathA := common.LeftPadBytes(path[0].Bytes(), 32)
paddedPathB := common.LeftPadBytes(path[1].Bytes(), 32)
paddedPath := common.LeftPadBytes(bs, 32)
paddedTo := common.LeftPadBytes(toAddress.Bytes(), 32)
paddedDeadline := common.LeftPadBytes(deadline.Bytes(), 32)
fmt.Println("paddedAmountOutMin", paddedAmountOutMin)
fmt.Println("paddedPathA", paddedPathA)
fmt.Println("paddedPathB", paddedPathB)
fmt.Println("paddedPath", paddedPath)
fmt.Println("paddedTo", paddedTo)
fmt.Println("paddedDeadline", paddedDeadline)
fmt.Println("paddedAmountOutMin", paddedAmountOutMin)
fmt.Println("paddedAmountOutMin", paddedAmountOutMin)
txData := web3helper.BuildTxData(methodID, paddedAmountOutMin, paddedPath, paddedTo, paddedDeadline)
fmt.Println("txData", txData)
estimateGas := web3GolangHelper.EstimateGas(toAddress.Hex(), txData)
fmt.Println("estimateGas", estimateGas)
txId, txNonce, err := web3GolangHelper.SignAndSendTransaction(toAddress.Hex(), web3helper.ToWei(value, 18), txData, web3GolangHelper.PendingNonce(), nil, estimateGas)
if err != nil {
fmt.Println(err)
}
fmt.Println(txId)
fmt.Println(txNonce)
}
/*
toAddress := common.HexToAddress(toAddressString)
transferFnSignature := []byte("transfer(address,uint256)")
hash := sha3.NewLegacyKeccak256()
hash.Write(transferFnSignature)
methodID := hash.Sum(nil)[:4]
paddedAddress := common.LeftPadBytes(toAddress.Bytes(), 32)
paddedAmount := common.LeftPadBytes(value.Bytes(), 32)
txData := BuildTxData(methodID, paddedAddress, paddedAmount)
estimateGas := w.EstimateGas(tokenAddressString, txData)
txId, txNonce, err := w.SignAndSendTransaction(toAddressString, ToWei(value, 18), txData, w.PendingNonce(), nil, estimateGas)
if err != nil {
return "", big.NewInt(0), err
}
return txId, txNonce, nil
*/
func updateTokenStatus(db *gorm.DB, web3GolangHelper *web3helper.Web3GolangHelper, token *models.EventsCatched) {
// create pancakeRouter pancakeRouterInstance
tokenContractInstance, instanceErr := ierc20.NewPancake(common.HexToAddress(token.TokenAddress), web3GolangHelper.HttpClient())
if instanceErr != nil {
fmt.Println(instanceErr)
}
tokenName, getNameErr := tokenContractInstance.Name(nil)
if getNameErr != nil {
UpdateName(db, token.TokenAddress, tokenName)
fmt.Println(getNameErr)
}
reserves := getReserves(web3GolangHelper, token.TokenAddress)
if reserves.Reserve0.Uint64() > web3helper.EtherToWei(big.NewFloat(0)).Uint64() {
UpdateLiquidity(db, token.ID)
}
}
func getTokenPairs(web3GolangHelper *web3helper.Web3GolangHelper, token *models.EventsCatched) string {
//lpPairs := make([]*models.LpPair, 0)
lpPairAddress := getPair(web3GolangHelper, token.TokenAddress)
//append(lpPairs, )
fmt.Println("lpPairAddress", lpPairAddress)
return lpPairAddress
}
func getReserves(web3GolangHelper *web3helper.Web3GolangHelper, tokenAddress string) Reserve {
pairInstance, instanceErr := pancakePair.NewPancake(common.HexToAddress("0xB7926C0430Afb07AA7DEfDE6DA862aE0Bde767bc"), web3GolangHelper.HttpClient())
if instanceErr != nil {
fmt.Println(instanceErr)
}
reserves, getReservesErr := pairInstance.GetReserves(nil)
if getReservesErr != nil {
fmt.Println(getReservesErr)
}
return reserves
}
func getPair(web3GolangHelper *web3helper.Web3GolangHelper, tokenAddress string) string {
factoryInstance, instanceErr := pancakeFactory.NewPancake(common.HexToAddress("0xB7926C0430Afb07AA7DEfDE6DA862aE0Bde767bc"), web3GolangHelper.HttpClient())
if instanceErr != nil {
fmt.Println(instanceErr)
}
wBnbContractAddress := "0xae13d989daC2f0dEbFf460aC112a837C89BAa7cd"
lpPairAddress, getPairErr := factoryInstance.GetPair(nil, common.HexToAddress(wBnbContractAddress), common.HexToAddress(tokenAddress))
if getPairErr != nil {
fmt.Println(getPairErr)
}
return lpPairAddress.Hex()
}
func printTokenStatus(token *models.EventsCatched) {
logrus.SetFormatter(&logrus.TextFormatter{ForceColors: true})
logrus.SetOutput(colorable.NewColorableStdout())
logrus.Info("TOKEN INFO")
fmt.Printf("%s: %s\n", cyan("Token Address"), yellow(token.TokenAddress))
fmt.Printf("%s:\n", cyan("LP Pairs"))
lo.ForEach(token.LPPairs, func(element *models.LpPair, _ int) {
fmt.Printf("\t%s: %s\n", cyan("LP Address"), yellow(element.LPAddress))
fmt.Printf("\t%s: %s\n", cyan("LP TokenA Address"), yellow(element.LPPairA))
fmt.Printf("\t%s: %s\n", cyan("LP TokenB Address"), yellow(element.LPPairB))
fmt.Printf("\t%s: %s\n\n", cyan("LP Has Liquidity"), getPairLiquidityIcon(element))
fmt.Printf("\t%s: %s\n\n", cyan("Trading Enabled"), getPairTradingIcon(element))
})
}
func getPairTradingIcon(pair *models.LpPair) string {
icon := "🔴"
if pair.TradingEnabled {
icon = "🟢"
}
return icon
}
func getPairLiquidityIcon(pair *models.LpPair) string {
icon := "🔴"
if pair.HasLiquidity {
icon = "🟢"
}
return icon
}
func GenerateWallet() {
privateKey, err := crypto.GenerateKey()
if err != nil {
log.Fatal(err)
}
privateKeyBytes := crypto.FromECDSA(privateKey)
fmt.Println(hexutil.Encode(privateKeyBytes)[2:]) // fad9c8855b740a0b7ed4c221dbad0f33a83a49cad6b3fe8d5817ac83d38b6a19
publicKey := privateKey.Public()
publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey)
if !ok {
log.Fatal("cannot assert type: publicKey is not of type *ecdsa.PublicKey")
}
publicKeyBytes := crypto.FromECDSAPub(publicKeyECDSA)
fmt.Println(hexutil.Encode(publicKeyBytes)[4:]) // 9a7df67f79246283fdc93af76d4f8cdd62c4886e8cd870944e817dd0b97934fdd7719d0810951e03418205868a5c1b40b192451367f28e0088dd75e15de40c05
address := crypto.PubkeyToAddress(*publicKeyECDSA).Hex()
fmt.Println(address) // 0x96216849c49358B10257cb55b28eA603c874b05E
hash := sha3.NewLegacyKeccak256()
hash.Write(publicKeyBytes[1:])
fmt.Println(hexutil.Encode(hash.Sum(nil)[12:])) // 0x96216849c49358b10257cb55b28ea603c874b05e
wallet := Wallet{
PublicKey: address,
PrivateKey: hexutil.Encode(privateKeyBytes)[2:],
}
file, _ := json.MarshalIndent(wallet, "", " ")
_ = ioutil.WriteFile("wallets/"+address+".json", file, 0644)
}
func getWallets() {
wallets := make([]Wallet, 0)
wPath := "./wallets"
files, err := ioutil.ReadDir(wPath)
if err != nil {
log.Fatal(err)
}
for _, file := range files {
fileName := file.Name()
fmt.Println("fileName", fileName)
wallet := Wallet{
PublicKey: "",
PrivateKey: "",
}
// Open our jsonFile
jsonFile, _ := os.Open(wPath + "/" + fileName)
byteValue, _ := ioutil.ReadAll(jsonFile)
json.Unmarshal(byteValue, &wallet)
fmt.Println(wallet)
wallets = append(wallets, wallet)
}
fmt.Println(wallets)
}
func parseDateTime() string {
now := time.Now()
return strconv.Itoa(now.Year()) + "/" + now.Month().String() + "/" + strconv.Itoa(now.Day()) + " " + strconv.Itoa(now.Hour()) + ":" + strconv.Itoa(now.Minute()) + ":" + strconv.Itoa(now.Second()) + ":" + strconv.Itoa(now.Nanosecond())
}