-
Notifications
You must be signed in to change notification settings - Fork 0
/
broadcast.go
67 lines (58 loc) · 2.29 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
package client
import (
mevm "github.com/xpladev/xpla.go/core/evm"
"github.com/xpladev/xpla.go/types"
"github.com/xpladev/xpla.go/types/errors"
"github.com/xpladev/xpla.go/util"
txtypes "github.com/cosmos/cosmos-sdk/types/tx"
)
var xplaTxRes types.TxRes
// Broadcast the transaction.
// Default broadcast mode is "sync" if not xpla client has broadcast mode option.
// The broadcast method is determined according to the broadcast mode option of the xpla client.
// For evm transaction broadcast, use a separate method in this function.
func (xplac *xplaClient) Broadcast(txBytes []byte) (*types.TxRes, error) {
if xplac.GetModule() == mevm.EvmModule {
return xplac.broadcastEvm(txBytes)
} else {
broadcastMode := xplac.GetBroadcastMode()
switch {
case broadcastMode == "block":
return xplac.BroadcastBlock(txBytes)
case broadcastMode == "async":
return xplac.BroadcastAsync(txBytes)
case broadcastMode == "sync":
return broadcastTx(xplac, txBytes, txtypes.BroadcastMode_BROADCAST_MODE_SYNC)
default:
return broadcastTx(xplac, txBytes, txtypes.BroadcastMode_BROADCAST_MODE_SYNC)
}
}
}
// Broadcast the transaction with mode "block".
// It takes precedence over the option of the xpla client.
func (xplac *xplaClient) BroadcastBlock(txBytes []byte) (*types.TxRes, error) {
if xplac.GetModule() == mevm.EvmModule {
return xplac.broadcastEvm(txBytes)
}
return broadcastTx(xplac, txBytes, txtypes.BroadcastMode_BROADCAST_MODE_BLOCK)
}
// Broadcast the transaction with mode "Async".
// It takes precedence over the option of the xpla client.
func (xplac *xplaClient) BroadcastAsync(txBytes []byte) (*types.TxRes, error) {
if xplac.GetModule() == mevm.EvmModule {
return xplac.broadcastEvm(txBytes)
}
return broadcastTx(xplac, txBytes, txtypes.BroadcastMode_BROADCAST_MODE_ASYNC)
}
// Broadcast the transaction which is evm transaction by using ethclient of go-ethereum.
func (xplac *xplaClient) broadcastEvm(txBytes []byte) (*types.TxRes, error) {
if xplac.GetEvmRpc() == "" {
return nil, util.LogErr(errors.ErrNotSatisfiedOptions, "evm JSON-RPC URL must exist")
}
evmClient, err := util.NewEvmClient(xplac.GetEvmRpc(), xplac.GetContext())
if err != nil {
return nil, err
}
broadcastMode := xplac.GetBroadcastMode()
return broadcastTxEvm(xplac, txBytes, broadcastMode, evmClient)
}