-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
115 lines (94 loc) 路 3.05 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
package main
import (
"context"
"fmt"
"os"
"time"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
sdk "github.com/cosmos/cosmos-sdk/types"
sdktx "github.com/cosmos/cosmos-sdk/types/tx"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
"github.com/jaybxyz/simd-tx/client"
"github.com/jaybxyz/simd-tx/codec"
"github.com/jaybxyz/simd-tx/config"
"github.com/jaybxyz/simd-tx/wallet"
)
/*
TODO
1. Remove config.toml as this project is just sample code and it increases line of code to write
2. Do we need rpcURL anymore? Can we use grpc to query network info?
3. Research Cosmos SDK's written interfaces to see if this code can be shortend in any way or create a simple library to make the process simpler
*/
const (
// rpcURL = "localhost:26657"
// grpcURL = "localhost:9090"
)
var (
timeout = 5 * time.Second
)
func init() {
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr}) // human-friendly output
}
func main() {
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
config, err := config.Read(config.DefaultConfigPath)
if err != nil {
panic(fmt.Errorf("failed to read config.toml file: %w", err))
}
// Connect Tendermint RPC client
rpcClient, err := client.ConnectRPCWithTimeout(config.RPC.Address, timeout)
if err != nil {
panic(fmt.Errorf("failed to connect RPC client: %w", err))
}
// Connect gRPC client
gRPCConn, err := client.ConnectGRPCWithTimeout(ctx, config.GRPC.Address, config.GRPC.UseTLS, timeout)
if err != nil {
panic(fmt.Errorf("failed to connect gRPC client: %w", err))
}
defer gRPCConn.Close()
// Recover private key from mnemonic phrases
privKey, err := wallet.RecoverPrivKeyFromMnemonic(config.WalletConfig.Mnemonic, config.WalletConfig.Password)
if err != nil {
panic(fmt.Errorf("recovering private key: %w", err))
}
chainID, _ := rpcClient.NetworkChainID(ctx)
creator := wallet.Address(privKey)
baseAccount, _ := gRPCConn.GetAccount(ctx, creator.String())
accNum := baseAccount.GetAccountNumber()
accSeq := baseAccount.GetSequence()
gasLimit := config.TxConfig.GasLimit
fees, err := sdk.ParseCoinsNormalized(config.TxConfig.Fees)
if err != nil {
panic(fmt.Errorf("failed to parse coins %w", err))
}
// Create new MsgSend for test
msg := banktypes.MsgSend{
FromAddress: creator.String(),
ToAddress: "cosmos1zaavvzxez0elundtn32qnk9lkm8kmcszzsv80v",
Amount: sdk.NewCoins(sdk.NewInt64Coin("stake", 999)),
}
msgs := []sdk.Msg{&msg}
tx := client.NewTx(
chainID,
accNum,
accSeq,
gasLimit,
fees,
msgs...,
)
txCfg := codec.MakeEncodingConfig().TxConfig
txBytes, err := client.SignTx(tx, txCfg, privKey)
if err != nil {
fmt.Printf("failed to sign transaction: %v", err)
return
}
resp, err := gRPCConn.BroadcastTx(ctx, txBytes, sdktx.BroadcastMode_BROADCAST_MODE_SYNC)
if err != nil {
fmt.Printf("failed to broadcast transaction: %v", err)
return
}
log.Info().Msg("Go to the following link to see if transaction is successfully included in a block")
log.Info().Msg("http://localhost:1317/cosmos/tx/v1beta1/txs/" + resp.TxResponse.TxHash)
}