From 4504ebb25d8ebb9b8d34cda1b1f4b256f07b0f8d Mon Sep 17 00:00:00 2001 From: jelysn <129082781+jelysn@users.noreply.github.com> Date: Wed, 10 Apr 2024 21:08:03 +0800 Subject: [PATCH] calculate UsdAmount on Eden/Elys token amount queries (#457) return eden price multiplied on eden/elys staking/unstaking/vesting queries --- .../client/wasm/query_delegator_validators.go | 13 ++++++++++--- x/commitment/client/wasm/query_staked_balance.go | 10 +++++++++- x/commitment/client/wasm/query_staked_positions.go | 10 +++++++++- .../client/wasm/query_unstaked_positions.go | 10 +++++++++- x/commitment/client/wasm/query_vesting_info.go | 4 ++-- 5 files changed, 39 insertions(+), 8 deletions(-) diff --git a/x/commitment/client/wasm/query_delegator_validators.go b/x/commitment/client/wasm/query_delegator_validators.go index a7fd847d8..2ceab00ec 100644 --- a/x/commitment/client/wasm/query_delegator_validators.go +++ b/x/commitment/client/wasm/query_delegator_validators.go @@ -9,6 +9,7 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" commitmenttypes "github.com/elys-network/elys/x/commitment/types" + ptypes "github.com/elys-network/elys/x/parameter/types" ) func (oq *Querier) queryDelegatorValidators(ctx sdk.Context, query *commitmenttypes.QueryValidatorsRequest) ([]byte, error) { @@ -18,8 +19,7 @@ func (oq *Querier) queryDelegatorValidators(ctx sdk.Context, query *commitmentty return nil, errorsmod.Wrap(err, "invalid delegator address") } - validators := make([]stakingtypes.Validator, 0) - validators = oq.stakingKeeper.GetDelegatorValidators(ctx, delegatorAddr, math.MaxInt16) + validators := oq.stakingKeeper.GetDelegatorValidators(ctx, delegatorAddr, math.MaxInt16) validatorsCW := oq.BuildDelegatorValidatorsResponseCW(ctx, validators, totalBonded, query.DelegatorAddress) res := commitmenttypes.QueryDelegatorValidatorsResponse{ @@ -35,6 +35,13 @@ func (oq *Querier) queryDelegatorValidators(ctx sdk.Context, query *commitmentty } func (oq *Querier) BuildDelegatorValidatorsResponseCW(ctx sdk.Context, validators []stakingtypes.Validator, totalBonded cosmos_sdk_math.Int, delegatorAddress string) []commitmenttypes.ValidatorDetail { + edenDenomPrice := sdk.ZeroDec() + entry, found := oq.assetKeeper.GetEntry(ctx, ptypes.BaseCurrency) + if found { + baseCurrency := entry.Denom + edenDenomPrice = oq.ammKeeper.GetEdenDenomPrice(ctx, baseCurrency) + } + var validatorsCW []commitmenttypes.ValidatorDetail for _, validator := range validators { var validatorCW commitmenttypes.ValidatorDetail @@ -58,7 +65,7 @@ func (oq *Querier) BuildDelegatorValidatorsResponseCW(ctx sdk.Context, validator validatorCW.Staked = commitmenttypes.BalanceAvailable{ Amount: tokens.TruncateInt(), - UsdAmount: tokens, + UsdAmount: edenDenomPrice.Mul(tokens), } votingPower := sdk.NewDecFromInt(validator.Tokens).QuoInt(totalBonded).MulInt(sdk.NewInt(100)) diff --git a/x/commitment/client/wasm/query_staked_balance.go b/x/commitment/client/wasm/query_staked_balance.go index ab971ee95..bc16db5e5 100644 --- a/x/commitment/client/wasm/query_staked_balance.go +++ b/x/commitment/client/wasm/query_staked_balance.go @@ -8,10 +8,18 @@ import ( ammtypes "github.com/elys-network/elys/x/amm/types" commitmenttypes "github.com/elys-network/elys/x/commitment/types" paramtypes "github.com/elys-network/elys/x/parameter/types" + ptypes "github.com/elys-network/elys/x/parameter/types" stabletypes "github.com/elys-network/elys/x/stablestake/types" ) func (oq *Querier) queryStakedBalanceOfDenom(ctx sdk.Context, query *ammtypes.QueryBalanceRequest) ([]byte, error) { + edenDenomPrice := sdk.ZeroDec() + entry, found := oq.assetKeeper.GetEntry(ctx, ptypes.BaseCurrency) + if found { + baseCurrency := entry.Denom + edenDenomPrice = oq.ammKeeper.GetEdenDenomPrice(ctx, baseCurrency) + } + denom := query.Denom addr := query.Address address, err := sdk.AccAddressFromBech32(query.Address) @@ -36,7 +44,7 @@ func (oq *Querier) queryStakedBalanceOfDenom(ctx sdk.Context, query *ammtypes.Qu resp := commitmenttypes.StakedAvailable{ Amount: balance.Amount, - UsdAmount: sdk.NewDecFromInt(balance.Amount), + UsdAmount: edenDenomPrice.MulInt(balance.Amount), Lockups: lockups, } diff --git a/x/commitment/client/wasm/query_staked_positions.go b/x/commitment/client/wasm/query_staked_positions.go index 046b8787e..e3bba0f39 100644 --- a/x/commitment/client/wasm/query_staked_positions.go +++ b/x/commitment/client/wasm/query_staked_positions.go @@ -10,6 +10,7 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" commitmenttypes "github.com/elys-network/elys/x/commitment/types" + ptypes "github.com/elys-network/elys/x/parameter/types" ) func (oq *Querier) queryStakedPositions(ctx sdk.Context, query *commitmenttypes.QueryValidatorsRequest) ([]byte, error) { @@ -36,6 +37,13 @@ func (oq *Querier) queryStakedPositions(ctx sdk.Context, query *commitmenttypes. } func (oq *Querier) BuildStakedPositionResponseCW(ctx sdk.Context, validators []stakingtypes.Validator, totalBonded cosmos_sdk_math.Int, delegatorAddress string) []commitmenttypes.StakedPosition { + edenDenomPrice := sdk.ZeroDec() + entry, found := oq.assetKeeper.GetEntry(ctx, ptypes.BaseCurrency) + if found { + baseCurrency := entry.Denom + edenDenomPrice = oq.ammKeeper.GetEdenDenomPrice(ctx, baseCurrency) + } + var stakedPositions []commitmenttypes.StakedPosition for i, validator := range validators { var stakedPosition commitmenttypes.StakedPosition @@ -75,7 +83,7 @@ func (oq *Querier) BuildStakedPositionResponseCW(ctx sdk.Context, validators []s } stakedPosition.Staked = commitmenttypes.BalanceAvailable{ Amount: delAmount, - UsdAmount: tokens, + UsdAmount: edenDenomPrice.Mul(tokens), } stakedPositions = append(stakedPositions, stakedPosition) diff --git a/x/commitment/client/wasm/query_unstaked_positions.go b/x/commitment/client/wasm/query_unstaked_positions.go index 16d139d1e..56c45f454 100644 --- a/x/commitment/client/wasm/query_unstaked_positions.go +++ b/x/commitment/client/wasm/query_unstaked_positions.go @@ -10,6 +10,7 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" commitmenttypes "github.com/elys-network/elys/x/commitment/types" + ptypes "github.com/elys-network/elys/x/parameter/types" ) func (oq *Querier) queryUnStakedPositions(ctx sdk.Context, query *commitmenttypes.QueryValidatorsRequest) ([]byte, error) { @@ -35,6 +36,13 @@ func (oq *Querier) queryUnStakedPositions(ctx sdk.Context, query *commitmenttype } func (oq *Querier) BuildUnStakedPositionResponseCW(ctx sdk.Context, unbondingDelegations []stakingtypes.UnbondingDelegation, totalBonded cosmos_sdk_math.Int, delegatorAddress string) []commitmenttypes.UnstakedPosition { + edenDenomPrice := sdk.ZeroDec() + entry, found := oq.assetKeeper.GetEntry(ctx, ptypes.BaseCurrency) + if found { + baseCurrency := entry.Denom + edenDenomPrice = oq.ammKeeper.GetEdenDenomPrice(ctx, baseCurrency) + } + var unstakedPositions []commitmenttypes.UnstakedPosition i := 1 for _, undelegation := range unbondingDelegations { @@ -70,7 +78,7 @@ func (oq *Querier) BuildUnStakedPositionResponseCW(ctx sdk.Context, unbondingDel unstakedPosition.RemainingTime = uint64(entity.CompletionTime.Unix()) unstakedPosition.Unstaked = commitmenttypes.BalanceAvailable{ Amount: entity.Balance, - UsdAmount: sdk.NewDecFromInt(entity.Balance), + UsdAmount: edenDenomPrice.MulInt(entity.Balance), } unstakedPositions = append(unstakedPositions, unstakedPosition) diff --git a/x/commitment/client/wasm/query_vesting_info.go b/x/commitment/client/wasm/query_vesting_info.go index ec0c5c71d..1dc211b1a 100644 --- a/x/commitment/client/wasm/query_vesting_info.go +++ b/x/commitment/client/wasm/query_vesting_info.go @@ -32,7 +32,7 @@ func (oq *Querier) queryVestingInfo(ctx sdk.Context, query *commitmenttypes.Quer Id: fmt.Sprintf("%d", i), TotalVesting: commitmenttypes.BalanceAvailable{ Amount: vesting.TotalAmount, - UsdAmount: sdk.NewDecFromInt(vesting.TotalAmount), + UsdAmount: edenDenomPrice.MulInt(vesting.TotalAmount), }, Claimed: commitmenttypes.BalanceAvailable{ Amount: vesting.ClaimedAmount, @@ -52,7 +52,7 @@ func (oq *Querier) queryVestingInfo(ctx sdk.Context, query *commitmenttypes.Quer res := commitmenttypes.QueryVestingInfoResponse{ Vesting: commitmenttypes.BalanceAvailable{ Amount: totalVesting, - UsdAmount: sdk.NewDecFromInt(totalVesting), + UsdAmount: edenDenomPrice.MulInt(totalVesting), }, VestingDetails: vestingDetails, }