This repository has been archived by the owner on Nov 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 51
/
provider.go
64 lines (55 loc) · 2.02 KB
/
provider.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
package bandlib
import (
"github.com/bandprotocol/d3n/chain/app"
"github.com/cosmos/cosmos-sdk/client/context"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth/client/utils"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/tendermint/tendermint/crypto"
)
// BandProvider contains context, txBuilder, private key, and address
type BandProvider struct {
cliCtx context.CLIContext
txBldr authtypes.TxBuilder
addr sdk.AccAddress
privKey crypto.PrivKey
}
func privKeyToBandAccAddress(privKey crypto.PrivKey) sdk.AccAddress {
config := sdk.GetConfig()
app.SetBech32AddressPrefixesAndBip44CoinType(config)
return sdk.AccAddress(privKey.PubKey().Address())
}
// NewBandProvider creates new BandProvider create new cliCtx and txBldr
func NewBandProvider(nodeURI string, privKey crypto.PrivKey) (BandProvider, error) {
cdc := NewCodec()
addr := privKeyToBandAccAddress(privKey)
cliCtx := NewCLIContext(nodeURI, addr).WithCodec(cdc)
num, _, err := authtypes.NewAccountRetriever(cliCtx).GetAccountNumberSequence(addr)
if err != nil {
return BandProvider{}, err
}
return BandProvider{
cliCtx: cliCtx,
txBldr: NewTxBuilder(utils.GetTxEncoder(cdc)).WithAccountNumber(num),
addr: addr,
privKey: privKey,
}, nil
}
func (provider *BandProvider) Sender() sdk.AccAddress {
return provider.addr
}
func (provider *BandProvider) SendTransaction(
msgs []sdk.Msg, seq, gas uint64,
memo, fees, broadcastMode string,
) (sdk.TxResponse, error) {
cliCtx := provider.cliCtx.WithBroadcastMode(broadcastMode)
txBldr := provider.txBldr.WithSequence(seq).WithGas(gas).WithMemo(memo).WithFees(fees)
return completeAndBroadcastTxCLI(cliCtx, txBldr, msgs, provider.privKey)
}
func (provider *BandProvider) QueryTx(hashHexStr string) (sdk.TxResponse, error) {
return utils.QueryTx(provider.cliCtx, hashHexStr)
}
func (provider *BandProvider) GetSequenceFromChain() (uint64, error) {
_, seq, err := authtypes.NewAccountRetriever(provider.cliCtx).GetAccountNumberSequence(provider.addr)
return seq, err
}