-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Separate account getters from client/context
Closes: #4543
- Loading branch information
Showing
7 changed files
with
79 additions
and
107 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
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,59 @@ | ||
package types | ||
|
||
import ( | ||
"fmt" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
) | ||
|
||
// NodeQuerier is an interface that is satisfied by types that provide the QueryWithData method | ||
type NodeQuerier interface { | ||
// QueryWithData performs a query to a Tendermint node with the provided path | ||
// and a data payload. It returns the result and height of the query upon success | ||
// or an error if the query fails. | ||
QueryWithData(path string, data []byte) ([]byte, int64, error) | ||
} | ||
|
||
type AccountGetter struct { | ||
querier NodeQuerier | ||
} | ||
|
||
func NewAccountGetter(querier NodeQuerier) AccountGetter { | ||
return AccountGetter{querier: querier} | ||
} | ||
|
||
// GetAccount queries for an account given an address and a block height. An | ||
// error is returned if the query or decoding fails. | ||
func (ag AccountGetter) GetAccount(addr sdk.AccAddress) (Account, error) { | ||
bs, err := ModuleCdc.MarshalJSON(NewQueryAccountParams(addr)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
res, _, err := ag.querier.QueryWithData(fmt.Sprintf("custom/%s/%s", QuerierRoute, QueryAccount), bs) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var account Account | ||
if err := ModuleCdc.UnmarshalJSON(res, &account); err != nil { | ||
return nil, err | ||
} | ||
|
||
return account, nil | ||
} | ||
|
||
func (ag AccountGetter) EnsureExists(addr sdk.AccAddress) error { | ||
if _, err := ag.GetAccount(addr); err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func (ag AccountGetter) GetAccountNumberSequence(addr sdk.AccAddress) (uint64, uint64, error) { | ||
acc, err := ag.GetAccount(addr) | ||
if err != nil { | ||
return 0, 0, err | ||
} | ||
return acc.GetAccountNumber(), acc.GetSequence(), nil | ||
} |