-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
75 lines (61 loc) · 1.97 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
package main
import (
"context"
"fmt"
"log"
// Importing the general purpose Cosmos blockchain client
"github.com/ignite/cli/ignite/pkg/cosmosclient"
// Importing the types package of your blog blockchain
"blog/x/blog/types"
)
func main() {
// Prefix to use for account addresses.
// The address prefix was assigned to the blog blockchain
// using the `--address-prefix` flag during scaffolding.
addressPrefix := "blog"
// Create a Cosmos client instance
cosmos, err := cosmosclient.New(
context.Background(),
cosmosclient.WithAddressPrefix(addressPrefix),
)
if err != nil {
log.Fatal(err)
}
// Account `alice` was initialized during `ignite chain serve`
accountName := "alice"
// Get account from the keyring
account, err := cosmos.Account(accountName)
if err != nil {
log.Fatal(err)
}
addr := account.Address(addressPrefix)
/*if err != nil {
log.Fatal(err)
}*/
// Define a message to create a post
msg := &types.MsgCreatePost{
Creator: addr,
Title: "Hello!",
Body: "This is the first post",
}
// Broadcast a transaction from account `alice` with the message
// to create a post store response in txResp
txResp, err := cosmos.BroadcastTx(accountName, msg)
if err != nil {
log.Fatal(err)
}
// Print response from broadcasting a transaction
fmt.Print("MsgCreatePost:\n\n")
fmt.Println(txResp)
// Instantiate a query client for your `blog` blockchain
queryClient := types.NewQueryClient(cosmos.Context())
// Query the blockchain using the client's `Posts` method
// to get all posts store all posts in queryResp
queryResp, err := queryClient.Posts(context.Background(), &types.QueryPostsRequest{})
if err != nil {
log.Fatal(err)
}
// Print response from querying all the posts
fmt.Print("\n\nAll posts:\n\n")
fmt.Println(queryResp)
}