Skip to content

Commit

Permalink
calculate UsdAmount on Eden/Elys token amount queries (#457)
Browse files Browse the repository at this point in the history
return eden price multiplied on eden/elys staking/unstaking/vesting queries
  • Loading branch information
jelysn authored Apr 10, 2024
1 parent 7ed59a2 commit 4504ebb
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 8 deletions.
13 changes: 10 additions & 3 deletions x/commitment/client/wasm/query_delegator_validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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{
Expand All @@ -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
Expand All @@ -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))
Expand Down
10 changes: 9 additions & 1 deletion x/commitment/client/wasm/query_staked_balance.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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,
}

Expand Down
10 changes: 9 additions & 1 deletion x/commitment/client/wasm/query_staked_positions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down
10 changes: 9 additions & 1 deletion x/commitment/client/wasm/query_unstaked_positions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions x/commitment/client/wasm/query_vesting_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
}
Expand Down

0 comments on commit 4504ebb

Please sign in to comment.