-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.go
114 lines (89 loc) · 3.4 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
package main
import (
"fmt"
"context"
"log"
"crypto/ecdsa"
"math/big"
"time"
"os"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/params"
"github.com/joho/godotenv"
)
func etherToWei(val *big.Int) *big.Int {
return new(big.Int).Mul(val, big.NewInt(params.Ether))
}
func weiToEther(val *big.Int) *big.Int {
return new(big.Int).Div(val, big.NewInt(params.Ether))
}
func checkerr(err error) {
if err != nil {
log.Fatal(err)
}
}
func main() {
godotenv.Load(".envrc", ".env")
fmt.Println(os.Getenv("NODE_ENDPOINT"))
client, err := ethclient.Dial(os.Getenv("NODE_ENDPOINT"))
checkerr(err)
privateKey, err := crypto.HexToECDSA(os.Getenv("TARGET_PRIVATE_KEY"))
checkrate := time.Duration(15000000000)
for {
time.Sleep(checkrate)
// Get the balance of an account
publicKey := privateKey.Public()
publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey)
if !ok {
log.Fatal("cannot assert type: publicKey is not of type *ecdsa.PublicKey")
}
fromAddress := crypto.PubkeyToAddress(*publicKeyECDSA)
nonce, err := client.PendingNonceAt(context.Background(), fromAddress)
checkerr(err)
balance, err := client.BalanceAt(context.Background(), fromAddress, nil)
checkerr(err)
fmt.Println("Nonce: ", nonce)
fmt.Println("Account balance:", balance)
gasLimit := uint64(21000) // in units
gasPrice, err := client.SuggestGasPrice(context.Background())
checkerr(err)
gasLimitBigInt := new(big.Int).SetUint64(gasLimit)
gasExpense := new(big.Int)
gasExpense.Mul(gasLimitBigInt, gasPrice)
fmt.Println("gasExpense: ", gasExpense)
value := new(big.Int).Sub(balance, gasExpense)
fmt.Println("value: ", value)
if value.Int64() >= 0 {
fmt.Println("Valid balance: Initialize transaction")
toAddress := common.HexToAddress(os.Getenv("HQ_ADDRESS"))
var data []byte
tx := types.NewTransaction(nonce, toAddress, value, gasLimit, gasPrice, data)
chainID, err := client.NetworkID(context.Background())
checkerr(err)
signedTx, err := types.SignTx(tx, types.NewEIP155Signer(chainID), privateKey)
checkerr(err)
err = client.SendTransaction(context.Background(), signedTx)
checkerr(err)
balance, err = client.BalanceAt(context.Background(), fromAddress, nil)
checkerr(err)
fmt.Printf("tx sent: %s\n", signedTx.Hash().Hex())
txHash := signedTx.Hash()
sleeptime := time.Duration(30000000000)
for {
time.Sleep(sleeptime)
_, isPending, err := client.TransactionByHash(context.Background(), txHash)
checkerr(err)
fmt.Println("Waiting for transaction to finish...")
if !isPending {
fmt.Println("TX done.")
break
}
}
} else {
fmt.Println("Balance too low. Waiting...")
}
}
}