forked from bianjieai/opb-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
70 lines (60 loc) · 2.55 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
package main
import (
"fmt"
"github.com/bianjieai/irita-sdk-go/modules/nft"
"github.com/bianjieai/irita-sdk-go/types"
"github.com/bianjieai/irita-sdk-go/types/store"
opb "github.com/bianjieai/opb-sdk-go/pkg/app/sdk"
"github.com/bianjieai/opb-sdk-go/pkg/app/sdk/model"
"time"
)
func main() {
fee, _ := types.ParseCoin("100000uirita") // 设置文昌链主网的默认费用,10W不够就填20W,30W....
// 初始化 SDK 配置
options := []types.Option{
types.KeyDAOOption(store.NewMemory(nil)),
types.FeeOption(types.NewDecCoinsFromCoins(fee)),
}
cfg, err := types.NewClientConfig("http://47.100.192.234:26657", "ws://47.100.192.234:26657", "47.100.192.234:9090", "testing", options...)
if err != nil {
panic(err)
}
// 初始化 OPB 网关账号(测试网环境设置为 nil 即可)
authToken := model.NewAuthToken("TestProjectID", "TestProjectKey", "TestChainAccountAddress")
// 创建 OPB 客户端
client := opb.NewClient(cfg, &authToken)
// 导入私钥
client.Key.Recover("test_key_name", "test_password", "supreme zero ladder chaos blur lake dinner warm rely voyage scan dilemma future spin victory glance legend faculty join man mansion water mansion exotic")
// 初始化 Tx 基础参数
baseTx := types.BaseTx{
From: "test_key_name", // 对应上面导入的私钥名称
Password: "test_password", // 对应上面导入的私钥密码
Gas: 200000, // 单 Tx 消耗的 Gas 上限
Memo: "", // Tx 备注
Mode: types.Commit, // Tx 广播模式
}
// 使用 Client 选择对应的功能模块,构造、签名并发送交易;例:创建 NFT 类别
result, err := client.NFT.IssueDenom(nft.IssueDenomRequest{ID: "testdenom", Name: "TestDenom", Schema: "{}"}, baseTx)
if err != nil {
fmt.Println(fmt.Errorf("NFT 类别创建失败: %s", err.Error()))
} else {
fmt.Println("NFT 类别创建成功:", result.Hash)
}
// 使用 Client 选择对应的功能模块,查询链上状态;例:查询账户信息
acc, err := client.Bank.QueryAccount("iaa1lxvmp9h0v0dhzetmhstrmw3ecpplp5tljnr35f")
if err != nil {
fmt.Println(fmt.Errorf("账户查询失败: %s", err.Error()))
} else {
fmt.Println("账户信息查询成功:", acc)
}
// 使用 Client 订阅事件通知,例:订阅区块
subs, err := client.SubscribeNewBlock(types.NewEventQueryBuilder(), func(block types.EventDataNewBlock) {
fmt.Println(block)
})
if err != nil {
fmt.Println(fmt.Errorf("区块订阅失败: %s", err.Error()))
} else {
fmt.Println("区块订阅成功:", subs.ID)
}
time.Sleep(time.Second * 20)
}