Skip to content

Commit

Permalink
Locked tokens query (#626)
Browse files Browse the repository at this point in the history
* amm hook fix when executed through leveragelp

* add query for locked tokens in commitment and locked tokens in leveragelp position
  • Loading branch information
jelysn authored Jun 27, 2024
1 parent 21bce7e commit dd4e426
Show file tree
Hide file tree
Showing 13 changed files with 1,018 additions and 149 deletions.
108 changes: 108 additions & 0 deletions docs/static/openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39963,6 +39963,77 @@ paths:
additionalProperties: {}
tags:
- Query
/elys-network/elys/commitment/committed_tokens_locked/{address}:
get:
summary: Queries sum of committed tokens locked and not unlockable
operationId: ElysCommitmentCommittedTokensLocked
responses:
'200':
description: A successful response.
schema:
type: object
properties:
address:
type: string
locked_committed:
type: array
items:
type: object
properties:
denom:
type: string
amount:
type: string
description: >-
Coin defines a token with a denomination and an amount.


NOTE: The amount field is an Int which implements the custom
method

signatures required by gogoproto.
total_committed:
type: array
items:
type: object
properties:
denom:
type: string
amount:
type: string
description: >-
Coin defines a token with a denomination and an amount.


NOTE: The amount field is an Int which implements the custom
method

signatures required by gogoproto.
default:
description: An unexpected error response.
schema:
type: object
properties:
code:
type: integer
format: int32
message:
type: string
details:
type: array
items:
type: object
properties:
'@type':
type: string
additionalProperties: {}
parameters:
- name: address
in: path
required: true
type: string
tags:
- Query
/elys-network/elys/commitment/number_of_commitments:
get:
summary: Queries the total number of commitment items.
Expand Down Expand Up @@ -41244,6 +41315,8 @@ paths:
format: uint64
stop_loss_price:
type: string
locked_lp_token:
type: string
default:
description: An unexpected error response.
schema:
Expand Down Expand Up @@ -86377,6 +86450,39 @@ definitions:
type: string
format: uint64
description: Params defines the parameters for the module.
elys.commitment.QueryCommittedTokensLockedResponse:
type: object
properties:
address:
type: string
locked_committed:
type: array
items:
type: object
properties:
denom:
type: string
amount:
type: string
description: |-
Coin defines a token with a denomination and an amount.

NOTE: The amount field is an Int which implements the custom method
signatures required by gogoproto.
total_committed:
type: array
items:
type: object
properties:
denom:
type: string
amount:
type: string
description: |-
Coin defines a token with a denomination and an amount.

NOTE: The amount field is an Int which implements the custom method
signatures required by gogoproto.
elys.commitment.QueryNumberOfCommitmentsResponse:
type: object
properties:
Expand Down Expand Up @@ -87145,6 +87251,8 @@ definitions:
format: uint64
stop_loss_price:
type: string
locked_lp_token:
type: string
elys.leveragelp.PositionsByPoolResponse:
type: object
properties:
Expand Down
22 changes: 22 additions & 0 deletions proto/elys/commitment/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import "cosmos/base/query/v1beta1/pagination.proto";
import "elys/commitment/params.proto";
import "elys/commitment/commitments.proto";
import "amino/amino.proto";
import "cosmos/base/v1beta1/coin.proto";

option go_package = "github.com/elys-network/elys/x/commitment/types";

Expand All @@ -23,6 +24,11 @@ service Query {
option (google.api.http).get = "/elys-network/elys/commitment/show_commitments/{creator}";
}

// Queries sum of committed tokens locked and not unlockable
rpc CommittedTokensLocked (QueryCommittedTokensLockedRequest) returns (QueryCommittedTokensLockedResponse) {
option (google.api.http).get = "/elys-network/elys/commitment/committed_tokens_locked/{address}";
}

// Queries the total number of commitment items.
rpc NumberOfCommitments (QueryNumberOfCommitmentsRequest) returns (QueryNumberOfCommitmentsResponse) {
option (google.api.http).get = "/elys-network/elys/commitment/number_of_commitments";
Expand Down Expand Up @@ -50,3 +56,19 @@ message QueryNumberOfCommitmentsRequest {}
message QueryNumberOfCommitmentsResponse {
int64 number = 1;
}

message QueryCommittedTokensLockedRequest {
string address = 1;
}

message QueryCommittedTokensLockedResponse {
string address = 1;
repeated cosmos.base.v1beta1.Coin locked_committed = 2 [
(gogoproto.nullable) = false,
(gogoproto.castrepeated) = "github.com/cosmos/cosmos-sdk/types.Coins"
];
repeated cosmos.base.v1beta1.Coin total_committed = 3 [
(gogoproto.nullable) = false,
(gogoproto.castrepeated) = "github.com/cosmos/cosmos-sdk/types.Coins"
];
}
8 changes: 7 additions & 1 deletion proto/elys/leveragelp/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,13 @@ message PositionRequest {
uint64 id = 2;
}

message PositionResponse { Position position = 1; }
message PositionResponse {
Position position = 1;
string locked_lp_token = 2 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.nullable) = false
];
}

message QueryLiquidationPriceRequest {
string address = 1;
Expand Down
11 changes: 11 additions & 0 deletions scripts/examples/commitment/commitment.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,16 @@
elysd query commitment number-of-commitments
# number: "395166"

elysd query commitment committed-tokens-locked $(elysd keys show -a treasury --keyring-backend=test)
# address: elys12tzylat4udvjj56uuhu3vj2n4vgp7cf9fwna9w
# locked_committed:
# - amount: "110000000000000000000000"
# denom: amm/pool/1
# total_committed:
# - amount: "110000000000000000000000"
# denom: amm/pool/1
# - amount: "100000000"
# denom: stablestake/share

elysd tx commitment commit-claimed-rewards 503544 ueden --from=treasury --keyring-backend=test --chain-id=elystestnet-1 --yes --gas=1000000
elysd tx commitment commit-claimed-rewards 1678547 uedenb --from=treasury --keyring-backend=test --chain-id=elystestnet-1 --yes --gas=1000000
3 changes: 3 additions & 0 deletions scripts/examples/leveragelp/leveragelp.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

# list pool
elysd query leveragelp list-pool
elysd query leveragelp get-position $(elysd keys show -a treasury --keyring-backend=test) 1
elysd query leveragelp get-positions-for-address $(elysd keys show -a treasury --keyring-backend=test)
elysd query leveragelp get-positions
elysd query leveragelp params
Expand All @@ -25,6 +26,7 @@ elysd tx leveragelp open 5.0 uusdc 5000000 1 0.0 --from=treasury --keyring-backe
elysd tx leveragelp open [leverage] [collateral-asset] [collateral-amount] [amm-pool-id] [flags]

# Close position
elysd tx leveragelp close 2 25000000000000000000 --from=treasury --keyring-backend=test --chain-id=elystestnet-1 --yes --gas=1000000
elysd tx leveragelp close 1 500000000000000000 --from=treasury --keyring-backend=test --chain-id=elystestnet-1 --yes --gas=1000000
elysd tx leveragelp close [position-id] [flags]

Expand All @@ -36,6 +38,7 @@ elysd query leveragelp rewards $(elysd keys show -a treasury --keyring-backend=t
elysd tx leveragelp claim-rewards 1 --from=treasury --keyring-backend=test --chain-id=elystestnet-1 --yes --gas=1000000

elysd query commitment show-commitments $(elysd keys show -a treasury --keyring-backend=test)
elysd query commitment committed-tokens-locked $(elysd keys show -a treasury --keyring-backend=test)

# Testnet
elysd query oracle show-price ATOM --node=https://rpc.testnet.elys.network:443
Expand Down
1 change: 1 addition & 0 deletions x/commitment/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func GetQueryCmd(queryRoute string) *cobra.Command {
cmd.AddCommand(CmdQueryParams())
cmd.AddCommand(CmdShowCommitments())
cmd.AddCommand(CmdNumberOfCommitments())
cmd.AddCommand(CmdCommittedTokensLocked())

// this line is used by starport scaffolding # 1

Expand Down
29 changes: 29 additions & 0 deletions x/commitment/client/cli/query_show_commitments.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,35 @@ func CmdShowCommitments() *cobra.Command {
return cmd
}

func CmdCommittedTokensLocked() *cobra.Command {
cmd := &cobra.Command{
Use: "committed-tokens-locked [address]",
Short: "Show locked coins in commitment not unlockable",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) (err error) {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}

queryClient := types.NewQueryClient(clientCtx)
params := &types.QueryCommittedTokensLockedRequest{
Address: args[0],
}
res, err := queryClient.CommittedTokensLocked(cmd.Context(), params)
if err != nil {
return err
}

return clientCtx.PrintProto(res)
},
}

flags.AddQueryFlagsToCmd(cmd)

return cmd
}

func CmdNumberOfCommitments() *cobra.Command {
cmd := &cobra.Command{
Use: "number-of-commitments",
Expand Down
16 changes: 16 additions & 0 deletions x/commitment/keeper/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,19 @@ func (k Keeper) NumberOfCommitments(goCtx context.Context, req *types.QueryNumbe
ctx := sdk.UnwrapSDKContext(goCtx)
return &types.QueryNumberOfCommitmentsResponse{Number: k.TotalNumberOfCommitments(ctx)}, nil
}

func (k Keeper) CommittedTokensLocked(goCtx context.Context, req *types.QueryCommittedTokensLockedRequest) (*types.QueryCommittedTokensLockedResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}

ctx := sdk.UnwrapSDKContext(goCtx)

commitments := k.GetCommitments(ctx, req.Address)
totalLocked, totalCommitted := commitments.CommittedTokensLocked(ctx)
return &types.QueryCommittedTokensLockedResponse{
Address: req.Address,
LockedCommitted: totalLocked,
TotalCommitted: totalCommitted,
}, nil
}
16 changes: 16 additions & 0 deletions x/commitment/types/commitments.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,22 @@ func (c *Commitments) AddCommittedTokens(denom string, amount math.Int, unlockTi
c.CommittedTokens = append(c.CommittedTokens, committedToken)
}

func (c Commitments) CommittedTokensLocked(ctx sdk.Context) (sdk.Coins, sdk.Coins) {
totalLocked := sdk.Coins{}
totalCommitted := sdk.Coins{}
for _, token := range c.CommittedTokens {
lockedAmount := sdk.ZeroInt()
for _, lockup := range token.Lockups {
if lockup.UnlockTimestamp > uint64(ctx.BlockTime().Unix()) {
lockedAmount = lockedAmount.Add(lockup.Amount)
}
}
totalLocked = totalLocked.Add(sdk.NewCoin(token.Denom, lockedAmount))
totalCommitted = totalCommitted.Add(sdk.NewCoin(token.Denom, token.Amount))
}
return totalLocked, totalCommitted
}

func (c *Commitments) DeductFromCommitted(denom string, amount math.Int, currTime uint64) error {
for i, token := range c.CommittedTokens {
if token.Denom == denom {
Expand Down
Loading

0 comments on commit dd4e426

Please sign in to comment.