Skip to content

Commit

Permalink
Merge pull request #720 from UnUniFi/fix/swap-yt-to-ut
Browse files Browse the repository at this point in the history
feat: impl Pool APY & Lp token price
  • Loading branch information
Senna46 authored Jan 4, 2024
2 parents 91be2d9 + 8d55935 commit 9434180
Show file tree
Hide file tree
Showing 3 changed files with 324 additions and 113 deletions.
19 changes: 17 additions & 2 deletions proto/ununifi/irs/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -255,12 +255,27 @@ message QueryTranchePoolAPYsRequest {
}

message QueryTranchePoolAPYsResponse {
string pool_apy = 1 [
string liquidity_apy = 1 [
(cosmos_proto.scalar) = "cosmos.Dec",
(gogoproto.customtype) = "cosmossdk.io/math.LegacyDec",
(gogoproto.nullable) = false
];
string liquidity_rate_per_ut = 2 [
string discount_pt_apy = 2 [
(cosmos_proto.scalar) = "cosmos.Dec",
(gogoproto.customtype) = "cosmossdk.io/math.LegacyDec",
(gogoproto.nullable) = false
];
string liquidity_rate_per_ut = 3 [
(cosmos_proto.scalar) = "cosmos.Dec",
(gogoproto.customtype) = "cosmossdk.io/math.LegacyDec",
(gogoproto.nullable) = false
];
string ut_percentage_in_pool = 4 [
(cosmos_proto.scalar) = "cosmos.Dec",
(gogoproto.customtype) = "cosmossdk.io/math.LegacyDec",
(gogoproto.nullable) = false
];
string pt_percentage_in_pool = 5 [
(cosmos_proto.scalar) = "cosmos.Dec",
(gogoproto.customtype) = "cosmossdk.io/math.LegacyDec",
(gogoproto.nullable) = false
Expand Down
55 changes: 53 additions & 2 deletions x/irs/keeper/grpc_query_tranche_apys.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,57 @@ func (k Keeper) TrancheYtAPYs(c context.Context, req *types.QueryTrancheYtAPYsRe
}

func (k Keeper) TranchePoolAPYs(c context.Context, req *types.QueryTranchePoolAPYsRequest) (*types.QueryTranchePoolAPYsResponse, error) {
// TODO: implement
return &types.QueryTranchePoolAPYsResponse{}, nil
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}
ctx := sdk.UnwrapSDKContext(c)
tranche, found := k.GetTranchePool(ctx, req.Id)
if !found {
return nil, types.ErrTrancheNotFound
}
depositInfo := k.GetStrategyDepositInfo(ctx, tranche.StrategyContract)
swapCoin := sdk.NewCoin(depositInfo.Denom, sdk.NewInt(1_000_000))
pt, err := k.SimulateSwapPoolTokens(ctx, tranche, swapCoin)
if err != nil {
return nil, err
}
restMaturity := tranche.StartTime + tranche.Maturity - uint64(ctx.BlockTime().Unix())
maturityPerYear := sdk.NewDecFromInt(sdk.NewIntFromUint64(restMaturity)).QuoInt(sdk.NewInt(365 * 24 * 60 * 60))
ptAPY := sdk.NewDecFromInt(pt.Amount.Sub(swapCoin.Amount)).QuoInt(swapCoin.Amount).Mul(maturityPerYear)
ptRate := sdk.NewDecFromInt(pt.Amount).QuoInt(swapCoin.Amount)

ptDenom := types.PtDenom(tranche)
var utPercentage sdk.Dec
var ptPercentage sdk.Dec
if ptDenom == tranche.PoolAssets[0].Denom {
total := sdk.NewDecFromInt(tranche.PoolAssets[1].Amount).Mul(ptRate).Add(sdk.NewDecFromInt(tranche.PoolAssets[0].Amount))
utPercentage = sdk.NewDecFromInt(tranche.PoolAssets[1].Amount).Quo(total)
ptPercentage = sdk.NewDecFromInt(tranche.PoolAssets[0].Amount).Quo(total)
} else {
total := sdk.NewDecFromInt(tranche.PoolAssets[0].Amount).Mul(ptRate).Add(sdk.NewDecFromInt(tranche.PoolAssets[1].Amount))
utPercentage = sdk.NewDecFromInt(tranche.PoolAssets[0].Amount).Quo(total)
ptPercentage = sdk.NewDecFromInt(tranche.PoolAssets[1].Amount).Quo(total)
}
discountPtAPY := ptAPY.Mul(ptPercentage)

lpAmount := sdk.NewInt(1_000_000)
requiredCoins, err := GetMaximalNoSwapLPAmount(ctx, tranche, lpAmount)
if err != nil {
return nil, err
}
var utAmount sdk.Dec
if ptDenom == requiredCoins[0].Denom {
utAmount = sdk.NewDecFromInt(requiredCoins[0].Amount).Quo(ptRate).Add(sdk.NewDecFromInt(requiredCoins[1].Amount))
} else {
utAmount = sdk.NewDecFromInt(requiredCoins[1].Amount).Quo(ptRate).Add(sdk.NewDecFromInt(requiredCoins[0].Amount))
}
lpRate := sdk.NewDecFromInt(lpAmount).Quo(utAmount)

return &types.QueryTranchePoolAPYsResponse{
LiquidityApy: sdk.ZeroDec(),
LiquidityRatePerUt: lpRate,
DiscountPtApy: discountPtAPY,
UtPercentageInPool: utPercentage,
PtPercentageInPool: ptPercentage,
}, nil
}
Loading

0 comments on commit 9434180

Please sign in to comment.