Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(client): refactore account management #2397

Closed
wants to merge 10 commits into from
Closed
35 changes: 0 additions & 35 deletions client/auth.go

This file was deleted.

15 changes: 3 additions & 12 deletions client/client.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package client

import (
"context"

"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/types/module/testutil"
"github.com/umee-network/umee/v6/sdkclient"
Expand All @@ -17,6 +15,7 @@ type Client struct {
// NewClient constructs Client object.
// Accounts are generated using the list of mnemonics. Each string must be a sequence of words,
// eg: `["w11 w12 w13", "w21 w22 w23"]`. Keyring names for created accounts will be: val1, val2....
// TODO: mnemonics should be a single string (list of words)
func NewClient(
chainDataDir,
chainID,
Expand All @@ -37,17 +36,9 @@ func NewClient(
}

func (c Client) IncAccSeq() {
c.Client.Tx.IncAccSeq()
c.Client.IncAccSeq()
}

func (c Client) SetAccSeq(seq uint64) {
c.Client.Tx.SetAccSeq(seq)
}

func (c Client) NewQCtx() (context.Context, context.CancelFunc) {
return c.Query.NewCtx()
}

func (c Client) NewQCtxWithCancel() (context.Context, context.CancelFunc) {
return c.Query.NewCtxWithCancel()
c.Client.SetAccSeq(seq)
}
20 changes: 0 additions & 20 deletions client/gov.go

This file was deleted.

10 changes: 5 additions & 5 deletions client/metoken.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import (
)

func (c Client) MetokenQClient() metoken.QueryClient {
return metoken.NewQueryClient(c.Query.GrpcConn)
return metoken.NewQueryClient(c.GrpcConn)
}

func (c Client) QueryMetokenParams() (metoken.Params, error) {
ctx, cancel := c.NewQCtx()
ctx, cancel := c.NewCtxWithTimeout()
defer cancel()

resp, err := c.MetokenQClient().Params(ctx, &metoken.QueryParams{})
Expand All @@ -21,7 +21,7 @@ func (c Client) QueryMetokenParams() (metoken.Params, error) {
}

func (c Client) QueryMetokenIndexBalances(denom string) (*metoken.QueryIndexBalancesResponse, error) {
ctx, cancel := c.NewQCtx()
ctx, cancel := c.NewCtxWithTimeout()
defer cancel()

msg := &metoken.QueryIndexBalances{MetokenDenom: denom}
Expand All @@ -32,7 +32,7 @@ func (c Client) QueryMetokenIndexBalances(denom string) (*metoken.QueryIndexBala
}

func (c Client) QueryMetokenIndexes(denom string) (*metoken.QueryIndexesResponse, error) {
ctx, cancel := c.NewQCtx()
ctx, cancel := c.NewCtxWithTimeout()
defer cancel()

msg := &metoken.QueryIndexes{MetokenDenom: denom}
Expand All @@ -43,7 +43,7 @@ func (c Client) QueryMetokenIndexes(denom string) (*metoken.QueryIndexesResponse
}

func (c Client) QueryMetokenIndexPrices(denom string) (*metoken.QueryIndexPricesResponse, error) {
ctx, cancel := c.NewQCtx()
ctx, cancel := c.NewCtxWithTimeout()
defer cancel()

msg := &metoken.QueryIndexPrices{MetokenDenom: denom}
Expand Down
10 changes: 5 additions & 5 deletions client/oracle.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,35 @@ import (
)

func (c Client) OracleQueryClient() oracletypes.QueryClient {
return oracletypes.NewQueryClient(c.Query.GrpcConn)
return oracletypes.NewQueryClient(c.GrpcConn)
}

func (c Client) QueryOracleParams() (oracletypes.Params, error) {
ctx, cancel := c.NewQCtx()
ctx, cancel := c.NewCtxWithTimeout()
defer cancel()

queryResponse, err := c.OracleQueryClient().Params(ctx, &oracletypes.QueryParams{})
return queryResponse.Params, err
}

func (c Client) QueryExchangeRates() ([]sdk.DecCoin, error) {
ctx, cancel := c.NewQCtx()
ctx, cancel := c.NewCtxWithTimeout()
defer cancel()

queryResponse, err := c.OracleQueryClient().ExchangeRates(ctx, &oracletypes.QueryExchangeRates{})
return queryResponse.ExchangeRates, err
}

func (c Client) QueryMedians() ([]oracletypes.Price, error) {
ctx, cancel := c.NewQCtx()
ctx, cancel := c.NewCtxWithTimeout()
defer cancel()

resp, err := c.OracleQueryClient().Medians(ctx, &oracletypes.QueryMedians{})
return resp.Medians, err
}

func (c Client) QueryMedianDeviations() ([]oracletypes.Price, error) {
ctx, cancel := c.NewQCtx()
ctx, cancel := c.NewCtxWithTimeout()
defer cancel()

queryResponse, err := c.OracleQueryClient().MedianDeviations(ctx, &oracletypes.QueryMedianDeviations{})
Expand Down
4 changes: 2 additions & 2 deletions client/uibc.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import (
)

func (c Client) UIBCQueryClient() uibc.QueryClient {
return uibc.NewQueryClient(c.Query.GrpcConn)
return uibc.NewQueryClient(c.GrpcConn)
}

func (c Client) QueryUIBCParams() (uibc.Params, error) {
ctx, cancel := c.NewQCtx()
ctx, cancel := c.NewCtxWithTimeout()
defer cancel()

queryResponse, err := c.UIBCQueryClient().Params(ctx, &uibc.QueryParams{})
Expand Down
4 changes: 2 additions & 2 deletions client/wasm.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import (
)

func (c Client) WasmClient() wasmtypes.QueryClient {
return wasmtypes.NewQueryClient(c.Query.GrpcConn)
return wasmtypes.NewQueryClient(c.GrpcConn)
}

func (c Client) QueryContract(contractAddr string, query []byte) (*wasmtypes.QuerySmartContractStateResponse, error) {
ctx, cancel := c.NewQCtx()
ctx, cancel := c.NewCtxWithTimeout()
defer cancel()

return c.WasmClient().SmartContractState(ctx, &wasmtypes.QuerySmartContractStateRequest{
Expand Down
58 changes: 58 additions & 0 deletions sdkclient/auth.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package sdkclient

import (
"fmt"
"time"

sdk "github.com/cosmos/cosmos-sdk/types"
authtx "github.com/cosmos/cosmos-sdk/x/auth/tx"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
)

func (c Client) AuthQClient() authtypes.QueryClient {
return authtypes.NewQueryClient(c.GrpcConn)
}

func (c Client) QueryAuthSeq(accAddr string) (uint64, error) {
ctx, cancel := c.NewCtx()
defer cancel()

queryResponse, err := c.AuthQClient().Account(ctx, &authtypes.QueryAccountRequest{
Address: accAddr,
})
if err != nil {
return 0, err
}

var baseAccount authtypes.AccountI
err = c.encCfg.Codec.UnpackAny(queryResponse.Account, &baseAccount)
if err != nil {
return 0, err
}
accSeq := baseAccount.GetSequence()
return accSeq, nil
}

func (c Client) QueryTxHash(hash string) (*sdk.TxResponse, error) {
return authtx.QueryTx(*c.ClientContext, hash)
}

// GetTxResponse waits for tx response and checks if the receipt contains at least `minExpectedLogs`.
func (c Client) GetTxResponse(txHash string, minExpectedLogs int) (resp *sdk.TxResponse, err error) {
for i := 0; i < 6; i++ {
resp, err = c.QueryTxHash(txHash)
if err == nil {
break
}

// TODO: configure sleep time
// Ideally, we should subscribe to block websocket and query by tx hash
time.Sleep(500 * time.Millisecond)
}

if n := len(resp.Logs); n < minExpectedLogs {
return nil, fmt.Errorf("expecting at least %d logs in response, got: %d", minExpectedLogs, n)
}

return resp, err
}
2 changes: 1 addition & 1 deletion sdkclient/tx/bank.go → sdkclient/bank.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package tx
package sdkclient

import (
sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down
Loading
Loading