-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
3,061 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package relayerclient | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strconv" | ||
|
||
"github.com/cosmos/cosmos-sdk/client" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
grpctypes "github.com/cosmos/cosmos-sdk/types/grpc" | ||
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" | ||
"google.golang.org/grpc" | ||
"google.golang.org/grpc/metadata" | ||
) | ||
|
||
var _ client.AccountRetriever = &CosmosProvider{} | ||
|
||
// GetAccount queries for an account given an address and a block height. An | ||
// error is returned if the query or decoding fails. | ||
func (cc *CosmosProvider) GetAccount(clientCtx client.Context, addr sdk.AccAddress) (client.Account, error) { | ||
account, _, err := cc.GetAccountWithHeight(clientCtx, addr) | ||
return account, err | ||
} | ||
|
||
// GetAccountWithHeight queries for an account given an address. Returns the | ||
// height of the query with the account. An error is returned if the query | ||
// or decoding fails. | ||
func (cc *CosmosProvider) GetAccountWithHeight(_ client.Context, addr sdk.AccAddress) (client.Account, int64, error) { | ||
var header metadata.MD | ||
address, err := cc.EncodeBech32AccAddr(addr) | ||
if err != nil { | ||
return nil, 0, err | ||
} | ||
|
||
queryClient := authtypes.NewQueryClient(cc) | ||
res, err := queryClient.Account(context.Background(), &authtypes.QueryAccountRequest{Address: address}, grpc.Header(&header)) | ||
if err != nil { | ||
return nil, 0, err | ||
} | ||
|
||
blockHeight := header.Get(grpctypes.GRPCBlockHeightHeader) | ||
if l := len(blockHeight); l != 1 { | ||
return nil, 0, fmt.Errorf("unexpected '%s' header length; got %d, expected: %d", grpctypes.GRPCBlockHeightHeader, l, 1) | ||
} | ||
|
||
nBlockHeight, err := strconv.Atoi(blockHeight[0]) | ||
if err != nil { | ||
return nil, 0, fmt.Errorf("failed to parse block height: %w", err) | ||
} | ||
|
||
var acc authtypes.AccountI | ||
if err := cc.Cdc.InterfaceRegistry.UnpackAny(res.Account, &acc); err != nil { | ||
return nil, 0, err | ||
} | ||
|
||
return acc, int64(nBlockHeight), nil | ||
} | ||
|
||
// EnsureExists returns an error if no account exists for the given address else nil. | ||
func (cc *CosmosProvider) EnsureExists(clientCtx client.Context, addr sdk.AccAddress) error { | ||
if _, err := cc.GetAccount(clientCtx, addr); err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
// GetAccountNumberSequence returns sequence and account number for the given address. | ||
// It returns an error if the account couldn't be retrieved from the state. | ||
func (cc *CosmosProvider) GetAccountNumberSequence(clientCtx client.Context, addr sdk.AccAddress) (uint64, uint64, error) { | ||
acc, err := cc.GetAccount(clientCtx, addr) | ||
if err != nil { | ||
return 0, 0, err | ||
} | ||
return acc.GetAccountNumber(), acc.GetSequence(), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package relayerclient | ||
|
||
import ( | ||
"sync" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
) | ||
|
||
// This file is cursed and this mutex is too | ||
// you don't want none of this dewey cox. | ||
var sdkConfigMutex sync.Mutex | ||
|
||
// SetSDKContext sets the SDK config to the proper bech32 prefixes. | ||
// Don't use this unless you know what you're doing. | ||
// TODO: :dagger: :knife: :chainsaw: remove this function | ||
func (cc *CosmosProvider) SetSDKContext() func() { | ||
return SetSDKConfigContext(cc.PCfg.AccountPrefix) | ||
} | ||
|
||
// SetSDKConfigContext sets the SDK config to the given bech32 prefixes | ||
func SetSDKConfigContext(prefix string) func() { | ||
sdkConfigMutex.Lock() | ||
sdkConf := sdk.GetConfig() | ||
sdkConf.SetBech32PrefixForAccount(prefix, prefix+"pub") | ||
sdkConf.SetBech32PrefixForValidator(prefix+"valoper", prefix+"valoperpub") | ||
sdkConf.SetBech32PrefixForConsensusNode(prefix+"valcons", prefix+"valconspub") | ||
return sdkConfigMutex.Unlock | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package relayerclient | ||
|
||
import ( | ||
codectypes "github.com/cosmos/cosmos-sdk/codec/types" | ||
) | ||
|
||
const ( | ||
ErrTimeoutAfterWaitingForTxBroadcast _err = "timed out after waiting for tx to get included in the block" | ||
) | ||
|
||
type _err string | ||
|
||
func (e _err) Error() string { return string(e) } | ||
|
||
// todo(lazar) remove this | ||
type intoAny interface { | ||
AsAny() *codectypes.Any | ||
} |
Oops, something went wrong.