-
Notifications
You must be signed in to change notification settings - Fork 0
/
earnings.go
32 lines (26 loc) · 1.07 KB
/
earnings.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package keeper
import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/fastock/fastock-chain/x/farm/types"
)
// GetEarnings gets the earnings info by a given user address and a specific pool name
func (k Keeper) GetEarnings(
ctx sdk.Context, poolName string, accAddr sdk.AccAddress,
) (types.Earnings, sdk.Error) {
var earnings types.Earnings
lockInfo, found := k.GetLockInfo(ctx, accAddr, poolName)
if !found {
return earnings, types.ErrNoLockInfoFound(accAddr.String(), poolName)
}
pool, found := k.GetFarmPool(ctx, poolName)
if !found {
return earnings, types.ErrNoFarmPoolFound(poolName)
}
// 1.1 Calculate how many provided token & native token have been yielded
// between start block height and current height
updatedPool, yieldedTokens := k.CalculateAmountYieldedBetween(ctx, pool)
endingPeriod := k.IncrementPoolPeriod(ctx, poolName, updatedPool.TotalValueLocked, yieldedTokens)
rewards := k.calculateRewards(ctx, poolName, accAddr, endingPeriod, lockInfo)
earnings = types.NewEarnings(ctx.BlockHeight(), lockInfo.Amount, rewards)
return earnings, nil
}