-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
124 lines (110 loc) · 3.03 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
package main
import (
"context"
"crypto/ecdsa"
"fmt"
"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/joho/godotenv"
"github.com/spf13/cobra"
"github.com/vavilen84/geth_example/client"
"log"
"math/big"
"os"
"strings"
)
var AppCommands = []*cobra.Command{
&cobra.Command{
Use: "print-balance",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
cl := client.InitClient()
printBalance(cl, args[0], args[0])
},
},
&cobra.Command{
Use: "print-alice-balance",
Run: func(cmd *cobra.Command, args []string) {
cl := client.InitClient()
printBalance(cl, os.Getenv("ALICE_ADDRESS"), "ALICE")
},
},
&cobra.Command{
Use: "print-bob-balance",
Run: func(cmd *cobra.Command, args []string) {
cl := client.InitClient()
printBalance(cl, os.Getenv("BOB_ADDRESS"), "BOB")
},
},
&cobra.Command{
Use: "send-from-alice-to-bob",
Run: func(cmd *cobra.Command, args []string) {
cl := client.InitClient()
privateKey, err := crypto.HexToECDSA(
strings.TrimPrefix(os.Getenv("ALICE_PRIVATE_KEY"), "0x"),
)
if err != nil {
log.Fatal(err)
}
publicKey := privateKey.Public()
publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey)
if !ok {
log.Fatal("error casting public key to ECDSA")
}
fromAddress := crypto.PubkeyToAddress(*publicKeyECDSA)
nonce, err := cl.PendingNonceAt(context.Background(), fromAddress)
if err != nil {
log.Fatal(err)
}
value := big.NewInt(1000000000000000000) // in wei (1 eth)
gasLimit := uint64(21000) // in units
gasPrice, err := cl.SuggestGasPrice(context.Background())
if err != nil {
log.Fatal(err)
}
toAddress := common.HexToAddress(os.Getenv("BOB_ADDRESS"))
var data []byte
tx := types.NewTransaction(nonce, toAddress, value, gasLimit, gasPrice, data)
chainID, err := cl.NetworkID(context.Background())
if err != nil {
log.Fatal(err)
}
signedTx, err := types.SignTx(tx, types.NewEIP155Signer(chainID), privateKey)
if err != nil {
log.Fatal(err)
}
err = cl.SendTransaction(context.Background(), signedTx)
if err != nil {
log.Fatal(err)
}
fmt.Printf("tx sent: %s \r\n", signedTx.Hash().Hex())
printBalance(cl, os.Getenv("BOB_ADDRESS"), "BOB")
printBalance(cl, os.Getenv("ALICE_ADDRESS"), "ALICE")
},
},
}
func main() {
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
var rootCmd = &cobra.Command{}
for _, command := range AppCommands {
rootCmd.AddCommand(command)
}
rootCmd.Execute()
}
func printBalance(client *ethclient.Client, address, name string) {
account := common.HexToAddress(address)
balance, err := client.BalanceAt(context.Background(), account, nil)
if err != nil {
log.Fatal(err)
}
fmt.Println(name, convertWeiToETH(balance))
}
func convertWeiToETH(wei *big.Int) string {
fl := new(big.Float).Quo(new(big.Float).SetInt(wei), big.NewFloat(1e18))
return fl.String()
}