-
Notifications
You must be signed in to change notification settings - Fork 2
/
broadcast.go
197 lines (169 loc) · 4.76 KB
/
broadcast.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package gosdk
import (
"context"
"github.com/NibiruChain/nibiru/x/common"
"github.com/NibiruChain/nibiru/x/common/denoms"
cmtrpc "github.com/cometbft/cometbft/rpc/client"
sdkclient "github.com/cosmos/cosmos-sdk/client"
sdkclienttx "github.com/cosmos/cosmos-sdk/client/tx"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
sdk "github.com/cosmos/cosmos-sdk/types"
sdktypestx "github.com/cosmos/cosmos-sdk/types/tx"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"google.golang.org/grpc"
)
func BroadcastMsgsWithSeq(
args BroadcastArgs,
from sdk.AccAddress,
seq uint64,
msgs ...sdk.Msg,
) (*sdk.TxResponse, error) {
broadcaster := args.Broadcaster
info, err := args.kring.KeyByAddress(from)
if err != nil {
return nil, err
}
txBuilder := args.txCfg.NewTxBuilder()
err = txBuilder.SetMsgs(msgs...)
if err != nil {
return nil, err
}
bondDenom := denoms.NIBI
txBuilder.SetFeeAmount(sdk.NewCoins(sdk.NewCoin(bondDenom, sdk.NewInt(1000))))
txBuilder.SetGasLimit(uint64(2 * common.TO_MICRO))
nums, err := args.gosdk.GetAccountNumbers(from.String())
if err != nil {
return nil, err
}
var accRetriever sdkclient.AccountRetriever = authtypes.AccountRetriever{}
txFactory := sdkclienttx.Factory{}.
WithChainID(args.chainID).
WithKeybase(args.kring).
WithTxConfig(args.txCfg).
WithAccountRetriever(accRetriever).
WithAccountNumber(nums.Number).
WithSequence(seq)
overwriteSig := true
err = sdkclienttx.Sign(txFactory, info.Name, txBuilder, overwriteSig)
if err != nil {
return nil, err
}
txBytes, err := args.txCfg.TxEncoder()(txBuilder.GetTx())
if err != nil {
return nil, err
}
return broadcaster.BroadcastTxSync(txBytes)
}
func BroadcastMsgs(
args BroadcastArgs,
from sdk.AccAddress,
msgs ...sdk.Msg,
) (*sdk.TxResponse, error) {
nums, err := args.gosdk.GetAccountNumbers(from.String())
if err != nil {
return nil, err
}
return BroadcastMsgsWithSeq(args, from, nums.Sequence, msgs...)
}
type Broadcaster interface {
BroadcastTxSync(txBytes []byte) (*sdk.TxResponse, error)
}
var (
_ Broadcaster = (*BroadcasterTmRpc)(nil)
_ Broadcaster = (*BroadcasterGrpc)(nil)
)
type BroadcasterTmRpc struct {
RPC cmtrpc.Client
}
func (b BroadcasterTmRpc) BroadcastTxSync(
txBytes []byte,
) (*sdk.TxResponse, error) {
respRaw, err := b.RPC.BroadcastTxSync(context.Background(), txBytes)
if err != nil {
return nil, err
}
return sdk.NewResponseFormatBroadcastTx(respRaw), err
}
type BroadcasterGrpc struct {
GRPC *grpc.ClientConn
}
func (b BroadcasterGrpc) BroadcastTx(
txBytes []byte, mode sdktypestx.BroadcastMode,
) (*sdk.TxResponse, error) {
txClient := sdktypestx.NewServiceClient(b.GRPC)
respRaw, err := txClient.BroadcastTx(
context.Background(), &sdktypestx.BroadcastTxRequest{
TxBytes: txBytes,
Mode: mode,
})
return respRaw.TxResponse, err
}
func (b BroadcasterGrpc) BroadcastTxSync(
txBytes []byte,
) (*sdk.TxResponse, error) {
return b.BroadcastTx(txBytes, sdktypestx.BroadcastMode_BROADCAST_MODE_SYNC)
}
func (b BroadcasterGrpc) BroadcastTxAsync(
txBytes []byte,
) (*sdk.TxResponse, error) {
return b.BroadcastTx(txBytes, sdktypestx.BroadcastMode_BROADCAST_MODE_ASYNC)
}
// func GetTxBytes() ([]byte, error) {
// return txBytes, err
// }
type BroadcastArgs struct {
kring keyring.Keyring
txCfg sdkclient.TxConfig
gosdk NibiruClient
// clientCtx sdkclient.Context // TODO: implement
Broadcaster Broadcaster
rpc cmtrpc.Client
chainID string
}
func initBroadcastArgs(
nc *NibiruClient, broadcaster Broadcaster,
) (args BroadcastArgs) {
txConfig := nc.EncCfg.TxConfig
return BroadcastArgs{
kring: nc.Keyring,
txCfg: txConfig,
gosdk: *nc,
Broadcaster: broadcaster,
rpc: nc.CometRPC,
chainID: nc.ChainId,
}
}
func (nc *NibiruClient) BroadcastMsgs(
from sdk.AccAddress,
msgs ...sdk.Msg,
) (*sdk.TxResponse, error) {
broadcaster := BroadcasterTmRpc{RPC: nc.CometRPC}
args := initBroadcastArgs(nc, broadcaster)
return BroadcastMsgs(args, from, msgs...)
}
func (nc *NibiruClient) BroadcastMsgsWithSeq(
from sdk.AccAddress,
seq uint64,
msgs ...sdk.Msg,
) (*sdk.TxResponse, error) {
broadcaster := BroadcasterTmRpc{RPC: nc.CometRPC}
args := initBroadcastArgs(nc, broadcaster)
return BroadcastMsgsWithSeq(args, from, seq, msgs...)
}
func (nc *NibiruClient) BroadcastMsgsGrpc(
from sdk.AccAddress,
msgs ...sdk.Msg,
) (*sdk.TxResponse, error) {
broadcaster := BroadcasterGrpc{GRPC: nc.Querier.ClientConn}
args := initBroadcastArgs(nc, broadcaster)
return BroadcastMsgs(args, from, msgs...)
}
func (nc *NibiruClient) BroadcastMsgsGrpcWithSeq(
from sdk.AccAddress,
seq uint64,
msgs ...sdk.Msg,
) (*sdk.TxResponse, error) {
broadcaster := BroadcasterGrpc{GRPC: nc.Querier.ClientConn}
args := initBroadcastArgs(nc, broadcaster)
return BroadcastMsgsWithSeq(args, from, seq, msgs...)
}