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

feat(accountplus): Add query for accountplus account state (backport #2659) #2661

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { LCDClient } from "@osmonauts/lcd";
import { QueryParamsRequest, QueryParamsResponseSDKType, GetAuthenticatorRequest, GetAuthenticatorResponseSDKType, GetAuthenticatorsRequest, GetAuthenticatorsResponseSDKType } from "./query";
import { QueryParamsRequest, QueryParamsResponseSDKType, GetAuthenticatorRequest, GetAuthenticatorResponseSDKType, GetAuthenticatorsRequest, GetAuthenticatorsResponseSDKType, AccountStateRequest, AccountStateResponseSDKType } from "./query";
export class LCDQueryClient {
req: LCDClient;

Expand All @@ -12,6 +12,7 @@ export class LCDQueryClient {
this.params = this.params.bind(this);
this.getAuthenticator = this.getAuthenticator.bind(this);
this.getAuthenticators = this.getAuthenticators.bind(this);
this.accountState = this.accountState.bind(this);
}
/* Parameters queries the parameters of the module. */

Expand All @@ -34,5 +35,12 @@ export class LCDQueryClient {
const endpoint = `dydxprotocol/accountplus/authenticators/${params.account}`;
return await this.req.get<GetAuthenticatorsResponseSDKType>(endpoint);
}
/* Queries for an account state (timestamp nonce). */


async accountState(params: AccountStateRequest): Promise<AccountStateResponseSDKType> {
const endpoint = `dydxprotocol/accountplus/account_state/${params.address}`;
return await this.req.get<AccountStateResponseSDKType>(endpoint);
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Rpc } from "../../helpers";
import * as _m0 from "protobufjs/minimal";
import { QueryClient, createProtobufRpcClient } from "@cosmjs/stargate";
import { QueryParamsRequest, QueryParamsResponse, GetAuthenticatorRequest, GetAuthenticatorResponse, GetAuthenticatorsRequest, GetAuthenticatorsResponse } from "./query";
import { QueryParamsRequest, QueryParamsResponse, GetAuthenticatorRequest, GetAuthenticatorResponse, GetAuthenticatorsRequest, GetAuthenticatorsResponse, AccountStateRequest, AccountStateResponse } from "./query";
/** Query defines the gRPC querier service. */

export interface Query {
Expand All @@ -13,6 +13,9 @@ export interface Query {
/** Queries all authenticators for a given account. */

getAuthenticators(request: GetAuthenticatorsRequest): Promise<GetAuthenticatorsResponse>;
/** Queries for an account state (timestamp nonce). */

accountState(request: AccountStateRequest): Promise<AccountStateResponse>;
}
export class QueryClientImpl implements Query {
private readonly rpc: Rpc;
Expand All @@ -22,6 +25,7 @@ export class QueryClientImpl implements Query {
this.params = this.params.bind(this);
this.getAuthenticator = this.getAuthenticator.bind(this);
this.getAuthenticators = this.getAuthenticators.bind(this);
this.accountState = this.accountState.bind(this);
}

params(request: QueryParamsRequest = {}): Promise<QueryParamsResponse> {
Expand All @@ -42,6 +46,12 @@ export class QueryClientImpl implements Query {
return promise.then(data => GetAuthenticatorsResponse.decode(new _m0.Reader(data)));
}

accountState(request: AccountStateRequest): Promise<AccountStateResponse> {
const data = AccountStateRequest.encode(request).finish();
const promise = this.rpc.request("dydxprotocol.accountplus.Query", "AccountState", data);
return promise.then(data => AccountStateResponse.decode(new _m0.Reader(data)));
}

}
export const createRpcQueryExtension = (base: QueryClient) => {
const rpc = createProtobufRpcClient(base);
Expand All @@ -57,6 +67,10 @@ export const createRpcQueryExtension = (base: QueryClient) => {

getAuthenticators(request: GetAuthenticatorsRequest): Promise<GetAuthenticatorsResponse> {
return queryService.getAuthenticators(request);
},

accountState(request: AccountStateRequest): Promise<AccountStateResponse> {
return queryService.accountState(request);
}

};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,44 @@
import { AccountState, AccountStateSDKType } from "./accountplus";
import { Params, ParamsSDKType } from "./params";
import { AccountAuthenticator, AccountAuthenticatorSDKType } from "./models";
import * as _m0 from "protobufjs/minimal";
import { DeepPartial, Long } from "../../helpers";
/** AccountStateRequest is request type for the Query/AccountState RPC method. */

export interface AccountStateRequest {
/** AccountStateRequest is request type for the Query/AccountState RPC method. */
address: string;
}
/** AccountStateRequest is request type for the Query/AccountState RPC method. */

export interface AccountStateRequestSDKType {
/** AccountStateRequest is request type for the Query/AccountState RPC method. */
address: string;
}
/**
* AccountStateResponse is response type for the Query/GetAccountState RPC
* method.
*/

export interface AccountStateResponse {
/**
* AccountStateResponse is response type for the Query/GetAccountState RPC
* method.
*/
accountState?: AccountState;
}
/**
* AccountStateResponse is response type for the Query/GetAccountState RPC
* method.
*/

export interface AccountStateResponseSDKType {
/**
* AccountStateResponse is response type for the Query/GetAccountState RPC
* method.
*/
account_state?: AccountStateSDKType;
}
/** QueryParamsRequest is request type for the Query/Params RPC method. */

export interface QueryParamsRequest {}
Expand Down Expand Up @@ -65,6 +102,96 @@ export interface GetAuthenticatorResponseSDKType {
account_authenticator?: AccountAuthenticatorSDKType;
}

function createBaseAccountStateRequest(): AccountStateRequest {
return {
address: ""
};
}

export const AccountStateRequest = {
encode(message: AccountStateRequest, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer {
if (message.address !== "") {
writer.uint32(10).string(message.address);
}

return writer;
},

decode(input: _m0.Reader | Uint8Array, length?: number): AccountStateRequest {
const reader = input instanceof _m0.Reader ? input : new _m0.Reader(input);
let end = length === undefined ? reader.len : reader.pos + length;
const message = createBaseAccountStateRequest();

while (reader.pos < end) {
const tag = reader.uint32();

switch (tag >>> 3) {
case 1:
message.address = reader.string();
break;

default:
reader.skipType(tag & 7);
break;
}
}

return message;
},

fromPartial(object: DeepPartial<AccountStateRequest>): AccountStateRequest {
const message = createBaseAccountStateRequest();
message.address = object.address ?? "";
return message;
}

};

function createBaseAccountStateResponse(): AccountStateResponse {
return {
accountState: undefined
};
}

export const AccountStateResponse = {
encode(message: AccountStateResponse, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer {
if (message.accountState !== undefined) {
AccountState.encode(message.accountState, writer.uint32(10).fork()).ldelim();
}

return writer;
},

decode(input: _m0.Reader | Uint8Array, length?: number): AccountStateResponse {
const reader = input instanceof _m0.Reader ? input : new _m0.Reader(input);
let end = length === undefined ? reader.len : reader.pos + length;
const message = createBaseAccountStateResponse();

while (reader.pos < end) {
const tag = reader.uint32();

switch (tag >>> 3) {
case 1:
message.accountState = AccountState.decode(reader, reader.uint32());
break;

default:
reader.skipType(tag & 7);
break;
}
}

return message;
},

fromPartial(object: DeepPartial<AccountStateResponse>): AccountStateResponse {
const message = createBaseAccountStateResponse();
message.accountState = object.accountState !== undefined && object.accountState !== null ? AccountState.fromPartial(object.accountState) : undefined;
return message;
}

};

function createBaseQueryParamsRequest(): QueryParamsRequest {
return {};
}
Expand Down
14 changes: 14 additions & 0 deletions proto/dydxprotocol/accountplus/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package dydxprotocol.accountplus;

import "gogoproto/gogo.proto";
import "google/api/annotations.proto";
import "dydxprotocol/accountplus/accountplus.proto";
import "dydxprotocol/accountplus/models.proto";
import "dydxprotocol/accountplus/params.proto";

Expand All @@ -28,8 +29,21 @@ service Query {
option (google.api.http).get =
"/dydxprotocol/accountplus/authenticators/{account}";
}

// Queries for an account state (timestamp nonce).
rpc AccountState(AccountStateRequest) returns (AccountStateResponse) {
option (google.api.http).get =
"/dydxprotocol/accountplus/account_state/{address}";
}
}

// AccountStateRequest is request type for the Query/AccountState RPC method.
message AccountStateRequest { string address = 1; }

// AccountStateResponse is response type for the Query/GetAccountState RPC
// method.
message AccountStateResponse { AccountState account_state = 1; }

// QueryParamsRequest is request type for the Query/Params RPC method.
message QueryParamsRequest {}

Expand Down
28 changes: 28 additions & 0 deletions protocol/x/accountplus/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func GetQueryCmd() *cobra.Command {
CmdQueryParam(),
CmdQueryGetAuthenticator(),
CmdQueryGetAllAuthenticators(),
CmdQueryAccountState(),
)
return cmd
}
Expand Down Expand Up @@ -114,3 +115,30 @@ func CmdQueryGetAllAuthenticators() *cobra.Command {
flags.AddQueryFlagsToCmd(cmd)
return cmd
}

func CmdQueryAccountState() *cobra.Command {
cmd := &cobra.Command{
Use: "account-state [address]",
Short: "Get account state for an address",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
queryClient := types.NewQueryClient(clientCtx)
res, err := queryClient.AccountState(
context.Background(),
&types.AccountStateRequest{
Address: args[0],
},
)
if err != nil {
return err
}
return clientCtx.PrintProto(res)
},
}
flags.AddQueryFlagsToCmd(cmd)
return cmd
}
23 changes: 23 additions & 0 deletions protocol/x/accountplus/keeper/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,26 @@ func (k Keeper) Params(goCtx context.Context, req *types.QueryParamsRequest) (*t

return &types.QueryParamsResponse{Params: k.GetParams(ctx)}, nil
}

// AccountState returns the x/accountplus account state for an address
func (k Keeper) AccountState(
ctx context.Context,
request *types.AccountStateRequest,
) (*types.AccountStateResponse, error) {
if request == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}

addr, err := sdk.AccAddressFromBech32(request.Address)
if err != nil {
return nil, status.Error(codes.InvalidArgument, "not valid bech32 address")
}

sdkCtx := sdk.UnwrapSDKContext(ctx)
// GetAccountState returns `empty, false` AccountState if the account does not exist.
accountState, _ := k.GetAccountState(sdkCtx, addr)

return &types.AccountStateResponse{
AccountState: &accountState,
}, nil
}
Loading
Loading