-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.go
97 lines (82 loc) · 2.75 KB
/
helper.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
package client
import (
"context"
"fmt"
"strconv"
"time"
band "github.com/bandprotocol/chain/v2/app"
oracletypes "github.com/bandprotocol/chain/v2/x/oracle/types"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/tx"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/tx/signing"
authtx "github.com/cosmos/cosmos-sdk/x/auth/tx"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
rpchttp "github.com/tendermint/tendermint/rpc/client/http"
libclient "github.com/tendermint/tendermint/rpc/jsonrpc/client"
)
func NewClientCtx(chainID string) client.Context {
cfg := band.MakeEncodingConfig()
return client.Context{}.
WithChainID(chainID).
WithAccountRetriever(authtypes.AccountRetriever{}).
WithBroadcastMode(flags.BroadcastSync).
WithCodec(cfg.Marshaler).
WithInterfaceRegistry(cfg.InterfaceRegistry).
WithTxConfig(cfg.TxConfig).
WithLegacyAmino(cfg.Amino).
WithViper("requester")
}
func newRPCClient(addr, timeout string) (*rpchttp.HTTP, error) {
to, err := time.ParseDuration(timeout)
if err != nil {
return nil, err
}
httpClient, err := libclient.DefaultHTTPClient(addr)
if err != nil {
return nil, err
}
httpClient.Timeout = to
rpcClient, err := rpchttp.NewWithClient(addr, "/websocket", httpClient)
if err != nil {
return nil, err
}
return rpcClient, nil
}
func createTxFactory(chainID, gasPrice string, keyring keyring.Keyring) tx.Factory {
return tx.Factory{}.
WithChainID(chainID).
WithTxConfig(band.MakeEncodingConfig().TxConfig).
WithGasAdjustment(1.1).
WithGasPrices(gasPrice).
WithKeybase(keyring).
WithSignMode(signing.SignMode_SIGN_MODE_DIRECT)
}
func getAccount(clientCtx client.Context, account sdk.AccAddress) (client.Account, error) {
return authtypes.AccountRetriever{}.GetAccount(clientCtx, account)
}
func getTx(clientCtx client.Context, txHash string) (*sdk.TxResponse, error) {
return authtx.QueryTx(clientCtx, txHash)
}
func getRequest(clientCtx client.Context, id uint64) (*oracletypes.QueryRequestResponse, error) {
queryClient := oracletypes.NewQueryClient(clientCtx)
return queryClient.Request(context.Background(), &oracletypes.QueryRequestRequest{RequestId: id})
}
func estimateGas(clientCtx client.Context, txf tx.Factory, msgs ...sdk.Msg) (uint64, error) {
_, gas, err := tx.CalculateGas(clientCtx, txf, msgs...)
return gas, err
}
func GetRequestID(events []sdk.StringEvent) (uint64, error) {
for _, event := range events {
if event.Type == oracletypes.EventTypeRequest {
rid, err := strconv.ParseUint(event.Attributes[0].Value, 10, 64)
if err != nil {
return 0, err
}
return rid, nil
}
}
return 0, fmt.Errorf("cannot find request id")
}