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

fix: redeem PT&YT YT amount #717

Merged
merged 2 commits into from
Dec 19, 2023
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
13 changes: 13 additions & 0 deletions proto/ununifi/irs/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ service Query {
rpc EstimateMintPtYtPair(QueryEstimateMintPtYtPairRequest) returns (QueryEstimateMintPtYtPairResponse) {
option (google.api.http).get = "/ununifi/irs/estimate-mint-pt-yt-pair";
}
// Estimate redeem amount of PT&YT pair
rpc EstimateRedeemPtYtPair(QueryEstimateRedeemPtYtPairRequest) returns (QueryEstimateRedeemPtYtPairResponse) {
option (google.api.http).get = "/ununifi/irs/estimate-redeem-pt-yt-pair";
}
// Estimate redeem amount of YT after maturity
rpc EstimateRedeemYt(QueryEstimateRedeemYtRequest) returns (QueryEstimateRedeemYtResponse) {
option (google.api.http).get = "/ununifi/irs/estimate-redeem-yt";
Expand Down Expand Up @@ -150,6 +154,15 @@ message QueryEstimateMintPtYtPairResponse {
cosmos.base.v1beta1.Coin yt_amount = 2 [(gogoproto.nullable) = false];
}

message QueryEstimateRedeemPtYtPairRequest {
uint64 pool_id = 1;
string yt_amount = 2;
}

message QueryEstimateRedeemPtYtPairResponse {
cosmos.base.v1beta1.Coin pt_amount = 1 [(gogoproto.nullable) = false];
}

message QueryEstimateRedeemYtRequest {
uint64 pool_id = 1;
string denom = 2;
Expand Down
36 changes: 36 additions & 0 deletions x/irs/keeper/grpc_query_estimate_redeem_pt_yt_pair.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package keeper

import (
"context"

sdk "github.com/cosmos/cosmos-sdk/types"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

"github.com/UnUniFi/chain/x/irs/types"
)

func (k Keeper) EstimateRedeemPtYtPair(c context.Context, req *types.QueryEstimateRedeemPtYtPairRequest) (*types.QueryEstimateRedeemPtYtPairResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}

ctx := sdk.UnwrapSDKContext(c)
tranche, found := k.GetTranchePool(ctx, req.PoolId)
if !found {
return nil, types.ErrTrancheNotFound
}
redeemAmount, ok := sdk.NewIntFromString(req.YtAmount)
if !ok {
return nil, types.ErrInvalidAmount
}
ptAmount, err := k.CalculateRedeemRequiredPtAmount(ctx, tranche, redeemAmount)
if err != nil {
return nil, err
}

ptDenom := types.PtDenom(tranche)
return &types.QueryEstimateRedeemPtYtPairResponse{
PtAmount: sdk.NewCoin(ptDenom, ptAmount),
}, nil
}
25 changes: 21 additions & 4 deletions x/irs/keeper/stripping.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,12 @@ func (k Keeper) RedeemPtYtPair(ctx sdk.Context, sender sdk.AccAddress, pool type

ptDenom := types.PtDenom(pool)
ytDenom := types.YtDenom(pool)
ptSupply := k.bankKeeper.GetSupply(ctx, ptDenom)
ytSupply := k.bankKeeper.GetSupply(ctx, ytDenom)

requiredPtAmount := ptSupply.Amount.Mul(redeemUt).Quo(amountFromStrategy)
requiredYtAmount := ytSupply.Amount.Mul(redeemUt).Quo(amountFromStrategy)
requiredPtAmount, err := k.CalculateRedeemRequiredPtAmount(ctx, pool, redeemUt)
if err != nil {
return err
}
requiredYtAmount := redeemUt

coins := sdk.Coins{}
requiredPtYt := coins.Add(sdk.NewCoin(ptDenom, requiredPtAmount)).Add(sdk.NewCoin(ytDenom, requiredYtAmount))
Expand All @@ -137,6 +138,22 @@ func (k Keeper) RedeemPtYtPair(ctx sdk.Context, sender sdk.AccAddress, pool type
return k.UnstakeFromStrategy(ctx, moduleAddr, sender.String(), pool.StrategyContract, redeemUt)
}

func (k Keeper) CalculateRedeemRequiredPtAmount(ctx sdk.Context, pool types.TranchePool, redeemUt sdk.Int) (sdk.Int, error) {
moduleAddr := types.GetVaultModuleAddress(pool)

amountFromStrategy, err := k.GetAmountFromStrategy(ctx, moduleAddr, pool.StrategyContract)
if err != nil {
return sdk.ZeroInt(), err
}
if amountFromStrategy.IsZero() {
return sdk.ZeroInt(), types.ErrZeroAmount
}
ptDenom := types.PtDenom(pool)
ptSupply := k.bankKeeper.GetSupply(ctx, ptDenom)
requiredPtAmount := ptSupply.Amount.Mul(redeemUt).Quo(amountFromStrategy)
return requiredPtAmount, nil
}

func (k Keeper) RedeemPtAtMaturity(ctx sdk.Context, sender sdk.AccAddress, pool types.TranchePool, ptAmount sdk.Coin) error {
if uint64(ctx.BlockTime().Unix()) < pool.StartTime+pool.Maturity {
return types.ErrTrancheNotMatured
Expand Down
Loading
Loading