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

refactoring ext rewards for vault #212

Merged
merged 1 commit into from
Jun 8, 2022
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
8 changes: 4 additions & 4 deletions x/market/keeper/oracle.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,16 +169,16 @@ func (k *Keeper) DeleteMarketForAsset(ctx sdk.Context, id uint64) {

func (k *Keeper) GetPriceForAsset(ctx sdk.Context, id uint64) (uint64, bool) {
if id == 1 {
return 200, true
return 300000, true
}
if id == 2 {
return 100, true
return 100000, true
}
if id == 3 {
return 2, true
return 2000000, true
}
if id == 4 {
return 1, true
return 1000000, true
}
return 0, false
//market, found := k.GetMarketForAsset(ctx, id)
Expand Down
2 changes: 0 additions & 2 deletions x/rewards/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ func BeginBlocker(ctx sdk.Context, req abci.RequestBeginBlock, k keeper.Keeper)
if err != nil {
return
}


}

// EndBlocker for incentives module.
Expand Down
73 changes: 39 additions & 34 deletions x/rewards/keeper/iter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ package keeper

import (
"fmt"
"math"
"strconv"

collectortypes "github.com/comdex-official/comdex/x/collector/types"
lockertypes "github.com/comdex-official/comdex/x/locker/types"
"github.com/comdex-official/comdex/x/rewards/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"math"
"strconv"
)

//IterateLocker does reward calculation for locker
Expand Down Expand Up @@ -37,10 +38,10 @@ func (k Keeper) IterateLocker(ctx sdk.Context) error {
if err != nil {
return nil
}

// update the lock position
returnsAcc := locker.ReturnsAccumulated

updatedReturnsAcc := rewards.Add(returnsAcc)
netBalance := locker.NetBalance.Add(rewards)
updatedLocker := lockertypes.Locker{
Expand Down Expand Up @@ -141,6 +142,7 @@ func (k Keeper) DistributeExtRewardLocker(ctx sdk.Context) error {
extRewards := k.GetExternalRewardsLockers(ctx)
for i, v := range extRewards {
epochTime, _ := k.GetEpochTime(ctx, v.EpochId)
fmt.Println("in abci epochTime DistributeExtRewardLocker ", epochTime)
et := epochTime.StartingTime
timeNow := ctx.BlockTime().Unix()

Expand Down Expand Up @@ -191,43 +193,46 @@ func (k Keeper) DistributeExtRewardLocker(ctx sdk.Context) error {
func (k Keeper) DistributeExtRewardVault(ctx sdk.Context) error {
extRewards := k.GetExternalRewardVaults(ctx)
for i, v := range extRewards {
epochTime, _ := k.GetEpochTime(ctx, v.EpochId)
et := epochTime.StartingTime
timeNow := ctx.BlockTime().Unix()
if v.IsActive {
epochTime, _ := k.GetEpochTime(ctx, v.EpochId)
et := epochTime.StartingTime

if et < timeNow {
if extRewards[i].IsActive == true {
epoch, _ := k.GetEpochTime(ctx, v.EpochId)
if epoch.Count < uint64(extRewards[i].DurationDays) {
appExtPairVaultData, _ := k.GetAppExtendedPairVaultMapping(ctx, v.AppMappingId)
for _, u := range appExtPairVaultData.ExtendedPairVaults {
for _, w := range u.VaultIds {
totalRewards := v.TotalRewards
userVault, _ := k.GetVault(ctx, w)
userShare := userVault.AmountOut.Quo(u.CollateralLockedAmount)
Duration := v.DurationDays
rewardsPerEpoch := (totalRewards.Amount).Quo(sdk.NewInt(Duration))
dailyRewards := userShare.Mul(rewardsPerEpoch)
user, _ := sdk.AccAddressFromBech32(userVault.Owner)
err := k.SendCoinFromModuleToAccount(ctx, types.ModuleName, user, sdk.NewCoin(totalRewards.Denom, dailyRewards))
if err != nil {
return err
timeNow := ctx.BlockTime().Unix()

if et < timeNow {
if extRewards[i].IsActive == true {

epoch, _ := k.GetEpochTime(ctx, v.EpochId)
if epoch.Count < uint64(extRewards[i].DurationDays) {
appExtPairVaultData, _ := k.GetAppExtendedPairVaultMapping(ctx, v.AppMappingId)
for _, u := range appExtPairVaultData.ExtendedPairVaults {
for _, w := range u.VaultIds {
totalRewards := v.TotalRewards
userVault, _ := k.GetVault(ctx, w)
var individualUserShare sdk.Dec
individualUserShare = sdk.NewDec(userVault.AmountOut.Int64()).Quo(sdk.NewDec(u.CollateralLockedAmount.Int64()))
Duration := v.DurationDays
rewardsPerEpoch := sdk.NewDec((totalRewards.Amount).Quo(sdk.NewInt(Duration)).Int64())
dailyRewards := individualUserShare.Mul(rewardsPerEpoch)
finalDailyRewards := sdk.NewInt(dailyRewards.TruncateInt64())

user, _ := sdk.AccAddressFromBech32(userVault.Owner)
err := k.SendCoinFromModuleToAccount(ctx, types.ModuleName, user, sdk.NewCoin(totalRewards.Denom, finalDailyRewards))
if err != nil {
return err
}
epoch.Count = epoch.Count + 1
epoch.StartingTime = timeNow + 84600
k.SetEpochTime(ctx, epoch)
}
epoch.Count = epoch.Count + 1
k.SetEpochTime(ctx, epoch)
}
} else {
extRewards[i].IsActive = false
k.SetExternalRewardVault(ctx, extRewards[i])
}
} else {
extRewards[i].IsActive = false
k.SetExternalRewardVault(ctx, extRewards[i])
}
}
epoch := types.EpochTime{
StartingTime: et + 84600,
}
k.SetEpochTime(ctx, epoch)
}
}

return nil
}
7 changes: 4 additions & 3 deletions x/rewards/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ func (k Keeper) ActExternalRewardsVaults(ctx sdk.Context, AppMappingId uint64, E

epochId := k.GetEpochTimeId(ctx)
epoch := types.EpochTime{
Id: epochId,
Id: epochId+1,
AppMappingId: AppMappingId,
StartingTime: ctx.BlockTime().Unix() + 84600,
}
Expand All @@ -243,13 +243,14 @@ func (k Keeper) ActExternalRewardsVaults(ctx sdk.Context, AppMappingId uint64, E
StartTimestamp: ctx.BlockTime(),
EndTimestamp: endTime,
MinLockupTimeSeconds: MinLockupTimeSeconds,
EpochId: epochId,
EpochId: epoch.Id,
}

if err := k.bank.SendCoinsFromAccountToModule(ctx, Depositor, types.ModuleName, sdk.NewCoins(sdk.Coin{Amount: TotalRewards.Amount, Denom: TotalRewards.Denom})); err != nil {
return err
}

k.SetEpochTimeId(ctx, epochId+1)
k.SetEpochTimeId(ctx, msg.EpochId)
k.SetExternalRewardVault(ctx, msg)
k.SetExternalRewardsVaultId(ctx, msg.Id)
k.SetEpochTime(ctx, epoch)
Expand Down
2 changes: 1 addition & 1 deletion x/rewards/keeper/rewards.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ func (k *Keeper) SetEpochTime(ctx sdk.Context, epoch types.EpochTime) {
func (k *Keeper) GetEpochTime(ctx sdk.Context, Id uint64) (epoch types.EpochTime, found bool) {
var (
store = k.Store(ctx)
key = types.AssetForDenomKey(Id)
key = types.EpochForLockerKey(Id)
value = store.Get(key)
)

Expand Down