Skip to content

Commit

Permalink
minor refactor for clarity
Browse files Browse the repository at this point in the history
  • Loading branch information
akremstudy committed Nov 21, 2024
1 parent 0bc17cf commit 430717c
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
8 changes: 6 additions & 2 deletions x/oracle/keeper/rewards.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,14 @@ func (k Keeper) GetTimeBasedRewardsAccount(ctx context.Context) sdk.ModuleAccoun
}

func CalculateRewardAmount(reporterPower, reportsCount, totalPower uint64, reward math.Int) math.LegacyDec {
power := math.LegacyNewDec(int64(reporterPower) * int64(reportsCount))
rPower := math.LegacyNewDec(int64(reporterPower))
rcount := math.LegacyNewDec(int64(reportsCount))
tPower := math.LegacyNewDec(int64(totalPower))

power := rPower.Mul(rcount)
// reward is in loya
// amount = (power/TotalPower) * reward
amount := power.Quo(math.LegacyNewDec(int64(totalPower))).Mul(math.LegacyNewDecFromInt(reward))
amount := power.Quo(tPower).Mul(reward.ToLegacyDec())
return amount
}

Expand Down
8 changes: 5 additions & 3 deletions x/reporter/keeper/distribution.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,10 @@ func (k Keeper) DivvyingTips(ctx context.Context, reporterAddr sdk.AccAddress, r

for _, del := range delAddrs.TokenOrigins {
// delegator share = netReward * selector's share / total shares
delegatorShare := netReward.Mul(math.LegacyNewDecFromInt(del.Amount)).Quo(math.LegacyNewDecFromInt(delAddrs.Total))
delegatorShare = delegatorShare.TruncateDec()
delAmountDec := del.Amount.ToLegacyDec()
delTotalDec := delAddrs.Total.ToLegacyDec()
delegatorShare := netReward.Mul(delAmountDec).Quo(delTotalDec)

if bytes.Equal(del.DelegatorAddress, reporterAddr.Bytes()) {
delegatorShare = delegatorShare.Add(commission)
}
Expand Down Expand Up @@ -108,7 +110,7 @@ func (k Keeper) ReturnSlashedTokens(ctx context.Context, amt math.Int, hashId []
shareAmt := math.LegacyNewDecFromInt(source.Amount)
if winningpurse.IsPositive() {
// convert args needed for calculations to legacy decimals
shareAmt = math.LegacyNewDecFromInt(source.Amount).Quo(math.LegacyNewDecFromInt(snapshot.Total)).Mul(math.LegacyNewDecFromInt(amt))
shareAmt = shareAmt.Quo(math.LegacyNewDecFromInt(snapshot.Total)).Mul(math.LegacyNewDecFromInt(amt))
}
// set token source to bonded if validator is bonded
// if not, set to unbonded
Expand Down
2 changes: 1 addition & 1 deletion x/reporter/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ func (k msgServer) WithdrawTip(goCtx context.Context, msg *types.MsgWithdrawTip)
if amtToDelegate.IsZero() {
return nil, errors.New("no tips to withdraw")
}
_, err = k.Keeper.stakingKeeper.Delegate(ctx, delAddr, math.NewInt(int64(amtToDelegate.Uint64())), val.Status, val, false)
_, err = k.Keeper.stakingKeeper.Delegate(ctx, delAddr, amtToDelegate, val.Status, val, false)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 430717c

Please sign in to comment.