Skip to content

Commit

Permalink
Merge pull request #182 from sunriselayer/query/proxy
Browse files Browse the repository at this point in the history
feat: lockup account & proxy account Query
  • Loading branch information
Senna46 authored Jan 29, 2025
2 parents da76007 + 3624e88 commit 0cb2d88
Show file tree
Hide file tree
Showing 16 changed files with 1,635 additions and 84 deletions.
2 changes: 1 addition & 1 deletion app/app_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ var (
authtypes.ModuleName,
banktypes.ModuleName,
stakingtypes.ModuleName,
tokenconvertermoduletypes.ModuleName,
selfdelegationmoduletypes.ModuleName,
// </sunrise>
accounts.ModuleName,
Expand All @@ -245,7 +246,6 @@ var (
ibcfeetypes.ModuleName,
// chain modules
damoduletypes.ModuleName,
tokenconvertermoduletypes.ModuleName,
liquiditypoolmoduletypes.ModuleName,
liquidityincentivemoduletypes.ModuleName,
swapmoduletypes.ModuleName,
Expand Down
2 changes: 1 addition & 1 deletion docs/static/openapi.json

Large diffs are not rendered by default.

34 changes: 32 additions & 2 deletions proto/sunrise/selfdelegation/v1/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,19 @@ option go_package = "github.com/sunriselayer/sunrise/x/selfdelegation/types";

// Query defines the gRPC querier service.
service Query {
// Parameters queries the parameters of the module.
// Params queries the parameters of the module.
rpc Params(QueryParamsRequest) returns (QueryParamsResponse) {
option (google.api.http).get = "/sunriselayer/sunrise/selfdelegation/v1/params";
option (google.api.http).get = "/sunrise/selfdelegation/v1/params";
}

// SelfDelegationProxyAccountByOwner queries the SelfDelegationProxyAccount by owner address.
rpc SelfDelegationProxyAccountByOwner(QuerySelfDelegationProxyAccountByOwnerRequest) returns (QuerySelfDelegationProxyAccountByOwnerResponse) {
option (google.api.http).get = "/sunrise/selfdelegation/v1/self_delegation_proxy_account_by_owner/{owner_address}";
}

// LockupAccountsByOwner queries the LockupAccounts by owner address.
rpc LockupAccountsByOwner(QueryLockupAccountsByOwnerRequest) returns (QueryLockupAccountsByOwnerResponse) {
option (google.api.http).get = "/sunrise/selfdelegation/v1/lockup_accounts_by_owner/{owner_address}";
}
}

Expand All @@ -23,3 +33,23 @@ message QueryParamsResponse {
// params holds all the parameters of this module.
Params params = 1 [(gogoproto.nullable) = false];
}

// QuerySelfDelegationProxyAccountByOwnerRequest is request type for the Query/SelfDelegationProxyAccountByOwner RPC method.
message QuerySelfDelegationProxyAccountByOwnerRequest {
string owner_address = 1;
}

// QuerySelfDelegationProxyAccountByOwnerResponse is response type for the Query/SelfDelegationProxyAccountByOwner RPC method.
message QuerySelfDelegationProxyAccountByOwnerResponse {
string self_delegation_proxy_account_address = 1;
}

// QueryLockupAccountsByOwnerRequest is request type for the Query/LockupAccountsByOwner RPC method.
message QueryLockupAccountsByOwnerRequest {
string owner_address = 1;
}

// QueryLockupAccountsByOwnerResponse is response type for the Query/LockupAccountsByOwner RPC method.
message QueryLockupAccountsByOwnerResponse {
repeated string lockup_account_addresses = 1;
}
13 changes: 13 additions & 0 deletions proto/sunrise/selfdelegation/v1/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ service Msg {
rpc SelfDelegate(MsgSelfDelegate) returns (MsgSelfDelegateResponse);
// WithdrawSelfDelegationUnbonded
rpc WithdrawSelfDelegationUnbonded(MsgWithdrawSelfDelegationUnbonded) returns (MsgWithdrawSelfDelegationUnbondedResponse);
// RegisterLockupAccount
rpc RegisterLockupAccount(MsgRegisterLockupAccount) returns (MsgRegisterLockupAccountResponse);
}

// MsgUpdateParams is the Msg/UpdateParams request type.
Expand Down Expand Up @@ -73,3 +75,14 @@ message MsgWithdrawSelfDelegationUnbonded {

// MsgWithdrawSelfDelegationUnbondedResponse
message MsgWithdrawSelfDelegationUnbondedResponse {}

// MsgRegisterLockupAccount
message MsgRegisterLockupAccount {
option (cosmos.msg.v1.signer) = "sender";
string sender = 1 [(cosmos_proto.scalar) = "cosmos.AddressString"];
// owner of the lockup account
string owner = 2 [(cosmos_proto.scalar) = "cosmos.AddressString"];
}

// MsgRegisterLockupAccountResponse
message MsgRegisterLockupAccountResponse {}
26 changes: 21 additions & 5 deletions x/accounts/self_delegatable_lockup/lockup.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,22 @@ func (bva *BaseLockup) Init(ctx context.Context, msg *v1.MsgInitSelfDelegatableL
return nil, err
}

// Set Lockup account to selfdelegation keeper's collection
whoami := accountstd.Whoami(ctx)
lockupAddress, err := bva.addressCodec.BytesToString(whoami)
if err != nil {
return nil, err
}

msgRegister := &selfdelegationtypes.MsgRegisterLockupAccount{
Sender: lockupAddress,
Owner: msg.Owner,
}
_, err = sendMessage(ctx, msgRegister)
if err != nil {
return nil, err
}

return &v1.MsgInitSelfDelegatableLockupAccountResponse{}, nil
}

Expand Down Expand Up @@ -338,7 +354,7 @@ func (bva *BaseLockup) SelfDelegate(ctx context.Context, msg *v1.MsgSelfDelegate
return nil, err
}
whoami := accountstd.Whoami(ctx)
delegatorAddress, err := bva.addressCodec.BytesToString(whoami)
lockupAddress, err := bva.addressCodec.BytesToString(whoami)
if err != nil {
return nil, err
}
Expand All @@ -351,7 +367,7 @@ func (bva *BaseLockup) SelfDelegate(ctx context.Context, msg *v1.MsgSelfDelegate
}
amount := sdk.NewCoin(res.Params.FeeDenom, msg.Amount)

balance, err := bva.getBalance(ctx, delegatorAddress, amount.Denom)
balance, err := bva.getBalance(ctx, lockupAddress, amount.Denom)
if err != nil {
return nil, err
}
Expand All @@ -377,7 +393,7 @@ func (bva *BaseLockup) SelfDelegate(ctx context.Context, msg *v1.MsgSelfDelegate
}

msgSelfDelegate := &selfdelegationtypes.MsgSelfDelegate{
Sender: delegatorAddress,
Sender: lockupAddress, // Must be lockup, not owner
Amount: msg.Amount,
}
_, err = sendMessage(ctx, msgSelfDelegate)
Expand All @@ -397,7 +413,7 @@ func (bva *BaseLockup) WithdrawSelfDelegationUnbonded(ctx context.Context, msg *
return nil, err
}
whoami := accountstd.Whoami(ctx)
delegatorAddress, err := bva.addressCodec.BytesToString(whoami)
lockupAddress, err := bva.addressCodec.BytesToString(whoami)
if err != nil {
return nil, err
}
Expand All @@ -423,7 +439,7 @@ func (bva *BaseLockup) WithdrawSelfDelegationUnbonded(ctx context.Context, msg *
}

msgWithdraw := &selfdelegationtypes.MsgWithdrawSelfDelegationUnbonded{
Sender: delegatorAddress,
Sender: lockupAddress, // Must be lockup, not owner
Amount: msg.Amount,
}
_, err = sendMessage(ctx, msgWithdraw)
Expand Down
7 changes: 4 additions & 3 deletions x/liquiditypool/module/autocli.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,18 +70,19 @@ func (am AppModule) AutoCLIOptions() *autocliv1.ModuleOptions {
},
{
RpcMethod: "CreatePool",
Use: "create-pool [lowerTick] [upperTick]",
Use: "create-pool",
Short: "Create pool",
PositionalArgs: []*autocliv1.PositionalArgDescriptor{
// {ProtoField: "denom_base"},
// {ProtoField: "denom_quote"},
// {ProtoField: "fee_rate"},
// {ProtoField: "tick_params"},
// {ProtoField: "price_ratio"},
// {ProtoField: "base_offset"},
},
},
{
RpcMethod: "CreatePosition",
Use: "create-position ",
Use: "create-position",
Short: "Create position",
PositionalArgs: []*autocliv1.PositionalArgDescriptor{
// {ProtoField: "pool_id"},
Expand Down
4 changes: 3 additions & 1 deletion x/selfdelegation/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ type Keeper struct {

Schema collections.Schema
Params collections.Item[types.Params]
SelfDelegationProxies collections.Map[[]byte, []byte]
LockupAccounts collections.Map[[]byte, []byte] // lockup account address -> owner address
SelfDelegationProxies collections.Map[[]byte, []byte] // owner address -> self-delegation proxy address

accountsKeeper types.AccountsKeeper
bankKeeper types.BankKeeper
Expand Down Expand Up @@ -54,6 +55,7 @@ func NewKeeper(
authority: authority,

Params: collections.NewItem(sb, types.ParamsKey, "params", codec.CollValue[types.Params](cdc)),
LockupAccounts: collections.NewMap(sb, types.LockupAccountsKeyPrefix, "lockup_accounts", types.LockupAccountsKeyCodec, collections.BytesValue),
SelfDelegationProxies: collections.NewMap(sb, types.SelfDelegationProxiesKeyPrefix, "self_delegation_proxies", types.SelfDelegationProxiesKeyCodec, collections.BytesValue),

accountsKeeper: accountsKeeper,
Expand Down
26 changes: 26 additions & 0 deletions x/selfdelegation/keeper/msg_server_register_lockup_account.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package keeper

import (
"context"

"cosmossdk.io/errors"

"github.com/sunriselayer/sunrise/x/selfdelegation/types"
)

func (k msgServer) RegisterLockupAccount(ctx context.Context, msg *types.MsgRegisterLockupAccount) (*types.MsgRegisterLockupAccountResponse, error) {
lockupBytes, err := k.addressCodec.StringToBytes(msg.Sender)
if err != nil {
return nil, errors.Wrap(err, "invalid lockup account address")
}
ownerBytes, err := k.addressCodec.StringToBytes(msg.Owner)
if err != nil {
return nil, errors.Wrap(err, "invalid owner address")
}
err = k.LockupAccounts.Set(ctx, lockupBytes, ownerBytes)
if err != nil {
return nil, err
}

return &types.MsgRegisterLockupAccountResponse{}, nil
}
12 changes: 6 additions & 6 deletions x/selfdelegation/keeper/msg_server_self_delegate.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
)

func (k msgServer) SelfDelegate(ctx context.Context, msg *types.MsgSelfDelegate) (*types.MsgSelfDelegateResponse, error) {
delegator, err := k.addressCodec.StringToBytes(msg.Sender)
delegatorBytes, err := k.addressCodec.StringToBytes(msg.Sender)
if err != nil {
return nil, err
}
Expand All @@ -24,13 +24,13 @@ func (k msgServer) SelfDelegate(ctx context.Context, msg *types.MsgSelfDelegate)
}

// Check proxy account existence
has, err := k.SelfDelegationProxies.Has(ctx, delegator)
has, err := k.SelfDelegationProxies.Has(ctx, delegatorBytes)
if err != nil {
return nil, err
}
var proxyAddrBytes []byte
if has {
proxyAddrBytes, err = k.SelfDelegationProxies.Get(ctx, delegator)
proxyAddrBytes, err = k.SelfDelegationProxies.Get(ctx, delegatorBytes)
if err != nil {
return nil, err
}
Expand All @@ -39,7 +39,7 @@ func (k msgServer) SelfDelegate(ctx context.Context, msg *types.MsgSelfDelegate)
_, proxyAddrBytes, err = k.accountsKeeper.Init(
ctx,
selfdelegationproxy.SELF_DELEGATION_PROXY_ACCOUNT,
delegator, // Must be delegator, not owner
delegatorBytes, // Must be delegator, not owner
&selfdelegationproxytypes.MsgInit{
Owner: msg.Sender,
RootOwner: rootOwnerAcc,
Expand All @@ -50,7 +50,7 @@ func (k msgServer) SelfDelegate(ctx context.Context, msg *types.MsgSelfDelegate)
if err != nil {
return nil, err
}
err = k.SelfDelegationProxies.Set(ctx, delegator, proxyAddrBytes)
err = k.SelfDelegationProxies.Set(ctx, delegatorBytes, proxyAddrBytes)
if err != nil {
return nil, err
}
Expand All @@ -61,7 +61,7 @@ func (k msgServer) SelfDelegate(ctx context.Context, msg *types.MsgSelfDelegate)
return nil, err
}

err = k.bankKeeper.SendCoins(ctx, delegator, proxyAddrBytes, sdk.NewCoins(sdk.NewCoin(params.FeeDenom, msg.Amount)))
err = k.bankKeeper.SendCoins(ctx, delegatorBytes, proxyAddrBytes, sdk.NewCoins(sdk.NewCoin(params.FeeDenom, msg.Amount)))
if err != nil {
return nil, err
}
Expand Down
40 changes: 40 additions & 0 deletions x/selfdelegation/keeper/query_lockup_account.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package keeper

import (
"bytes"
"context"

"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

"github.com/sunriselayer/sunrise/x/selfdelegation/types"
)

func (q queryServer) LockupAccountsByOwner(ctx context.Context, req *types.QueryLockupAccountsByOwnerRequest) (*types.QueryLockupAccountsByOwnerResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}

ownerAddress, err := q.k.addressCodec.StringToBytes(req.OwnerAddress)
if err != nil {
return nil, status.Error(codes.InvalidArgument, "invalid owner address")
}

var lockupAccountAddresses []string
err = q.k.LockupAccounts.Walk(ctx, nil, func(key []byte, val []byte) (stop bool, err error) {
if bytes.Equal(val, ownerAddress) { // if owner address matches
lockupAddressString, err := q.k.addressCodec.BytesToString(key)
if err != nil {
// continue walking and skip current lockup account
return true, nil
}
lockupAccountAddresses = append(lockupAccountAddresses, lockupAddressString)
}
return false, nil // continue walking to find all lockup accounts for the owner
})
if err != nil {
return nil, status.Error(codes.Internal, "failed to walk lockup accounts")
}

return &types.QueryLockupAccountsByOwnerResponse{LockupAccountAddresses: lockupAccountAddresses}, nil
}
33 changes: 33 additions & 0 deletions x/selfdelegation/keeper/query_proxy_account.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package keeper

import (
"context"

"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

"github.com/sunriselayer/sunrise/x/selfdelegation/types"
)

func (q queryServer) SelfDelegationProxyAccountByOwner(ctx context.Context, req *types.QuerySelfDelegationProxyAccountByOwnerRequest) (*types.QuerySelfDelegationProxyAccountByOwnerResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}

ownerAddress, err := q.k.addressCodec.StringToBytes(req.OwnerAddress)
if err != nil {
return nil, status.Error(codes.InvalidArgument, "invalid owner address")
}

proxyAccountAddress, err := q.k.SelfDelegationProxies.Get(ctx, ownerAddress)
if err != nil {
return nil, status.Error(codes.NotFound, "proxy account not found")
}

proxyAccountString, err := q.k.addressCodec.BytesToString(proxyAccountAddress)
if err != nil {
return nil, status.Error(codes.Internal, "failed to convert proxy account address to string")
}

return &types.QuerySelfDelegationProxyAccountByOwnerResponse{SelfDelegationProxyAccountAddress: proxyAccountString}, nil
}
16 changes: 16 additions & 0 deletions x/selfdelegation/module/autocli.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,18 @@ func (am AppModule) AutoCLIOptions() *autocliv1.ModuleOptions {
Use: "params",
Short: "Shows the parameters of the module",
},
{
RpcMethod: "SelfDelegationProxyAccountByOwner",
Use: "self-delegation-proxy-account-by-owner [owner_address]",
Short: "Shows the self-delegation proxy account by owner address",
PositionalArgs: []*autocliv1.PositionalArgDescriptor{{ProtoField: "owner_address"}},
},
{
RpcMethod: "LockupAccountsByOwner",
Use: "lockup-accounts-by-owner [owner_address]",
Short: "Shows the lockup accounts by owner address",
PositionalArgs: []*autocliv1.PositionalArgDescriptor{{ProtoField: "owner_address"}},
},
// this line is used by ignite scaffolding # autocli/query
},
},
Expand All @@ -40,6 +52,10 @@ func (am AppModule) AutoCLIOptions() *autocliv1.ModuleOptions {
Short: "Send a withdraw-self-delegation-unbonded tx",
PositionalArgs: []*autocliv1.PositionalArgDescriptor{},
},
{
RpcMethod: "RegisterLockupAccount",
Skip: true,
},
// this line is used by ignite scaffolding # autocli/tx
},
},
Expand Down
5 changes: 3 additions & 2 deletions x/selfdelegation/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ const (

var (
// ParamsKey is the prefix to retrieve all Params
ParamsKey = collections.NewPrefix("params/")

ParamsKey = collections.NewPrefix("params/")
LockupAccountsKeyPrefix = collections.NewPrefix("lockup_accounts/")
SelfDelegationProxiesKeyPrefix = collections.NewPrefix("self_delegation_proxies/")
)

var (
LockupAccountsKeyCodec = collections.BytesKey
SelfDelegationProxiesKeyCodec = collections.BytesKey
)
Loading

0 comments on commit 0cb2d88

Please sign in to comment.