-
Notifications
You must be signed in to change notification settings - Fork 39
/
example.go
127 lines (104 loc) · 3.03 KB
/
example.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
125
126
127
package main
import (
"fmt"
"os"
"time"
"github.com/InjectiveLabs/sdk-go/client/common"
"github.com/shopspring/decimal"
exchangetypes "github.com/InjectiveLabs/sdk-go/chain/exchange/types"
chainclient "github.com/InjectiveLabs/sdk-go/client/chain"
rpchttp "github.com/cometbft/cometbft/rpc/client/http"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
authztypes "github.com/cosmos/cosmos-sdk/x/authz"
)
func main() {
// network := common.LoadNetwork("mainnet", "k8s")
network := common.LoadNetwork("testnet", "k8s")
tmClient, err := rpchttp.New(network.TmEndpoint, "/websocket")
if err != nil {
panic(err)
}
granterAddress, _, err := chainclient.InitCosmosKeyring(
os.Getenv("HOME")+"/.injectived",
"injectived",
"file",
"inj-user",
"12345678",
"5d386fbdbf11f1141010f81a46b40f94887367562bd33b452bbaa6ce1cd1381e", // keyring will be used if pk not provided
false,
)
if err != nil {
panic(err)
}
senderAddress, cosmosKeyring, err := chainclient.InitCosmosKeyring(
os.Getenv("HOME")+"/.injectived",
"injectived",
"file",
"inj-user",
"12345678",
"f9db9bf330e23cb7839039e944adef6e9df447b90b503d5b4464c90bea9022f3", // keyring will be used if pk not provided
false,
)
if err != nil {
panic(err)
}
clientCtx, err := chainclient.NewClientContext(
network.ChainId,
senderAddress.String(),
cosmosKeyring,
)
if err != nil {
fmt.Println(err)
}
clientCtx = clientCtx.WithNodeURI(network.TmEndpoint).WithClient(tmClient)
chainClient, err := chainclient.NewChainClient(
clientCtx,
network.ChainGrpcEndpoint,
common.OptionTLSCert(network.ChainTlsCert),
common.OptionGasPrices("500000000inj"),
)
if err != nil {
fmt.Println(err)
}
// note that we use grantee keyring to send the msg on behalf of granter here
// sender, subaccount are from granter
granter := granterAddress.String()
grantee := "inj1hkhdaj2a2clmq5jq6mspsggqs32vynpk228q3r"
defaultSubaccountID := chainClient.DefaultSubaccount(granterAddress)
marketId := "0x0511ddc4e6586f3bfe1acb2dd905f8b8a82c97e1edaef654b12ca7e6031ca0fa"
amount := decimal.NewFromFloat(2)
price := decimal.NewFromFloat(22.55)
order := chainClient.SpotOrder(defaultSubaccountID, network, &chainclient.SpotOrderData{
OrderType: exchangetypes.OrderType_BUY,
Quantity: amount,
Price: price,
FeeRecipient: senderAddress.String(),
MarketId: marketId,
})
// manually pack msg into Any type
msg0 := exchangetypes.MsgCreateSpotLimitOrder{
Sender: granter,
Order: *order,
}
msg0Bytes, _ := msg0.Marshal()
msg0Any := &codectypes.Any{}
msg0Any.TypeUrl = sdk.MsgTypeURL(&msg0)
msg0Any.Value = msg0Bytes
msg := &authztypes.MsgExec{
Grantee: grantee,
Msgs: []*codectypes.Any{msg0Any},
}
//AsyncBroadcastMsg, SyncBroadcastMsg, QueueBroadcastMsg
err = chainClient.QueueBroadcastMsg(msg)
if err != nil {
fmt.Println(err)
}
time.Sleep(time.Second * 5)
gasFee, err := chainClient.GetGasFee()
if err != nil {
fmt.Println(err)
return
}
fmt.Println("gas fee:", gasFee, "INJ")
}