Skip to content

Commit

Permalink
more fixes for staking APY
Browse files Browse the repository at this point in the history
  • Loading branch information
mr-tron committed Aug 31, 2023
1 parent e86f898 commit 633fa63
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
11 changes: 3 additions & 8 deletions pkg/api/staking_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"math"
"net/http"

"github.com/tonkeeper/opentonapi/internal/g"
Expand Down Expand Up @@ -269,12 +270,7 @@ func (h Handler) GetStakingPoolHistory(ctx context.Context, params oas.GetStakin
return nil, toError(http.StatusInternalServerError, err)
}
var result oas.GetStakingPoolHistoryOK
var prevTime uint32
for i, l := range logs {
if i == 0 {
prevTime = l.CreatedAt
continue
}
for _, l := range logs {
cells, err := boc.DeserializeBoc(l.Body)
if err != nil {
return nil, toError(http.StatusInternalServerError, err)
Expand All @@ -292,10 +288,9 @@ func (h Handler) GetStakingPoolHistory(ctx context.Context, params oas.GetStakin
return nil, toError(http.StatusInternalServerError, err)
}
result.Apy = append(result.Apy, oas.ApyHistory{
Apy: float64(round.Profit) / float64(round.TotalBalance) / float64(l.CreatedAt-prevTime) * 3600 * 24 * 365 * 100,
Apy: (math.Pow(float64(round.Returned-round.Borrowed)/float64(round.TotalBalance)+1, 365*24*60*60/float64(65536)) - 1) * 100,
Time: int(l.CreatedAt),
})
prevTime = l.CreatedAt
}
return &result, nil
}
15 changes: 15 additions & 0 deletions pkg/core/staking.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package core

import (
"github.com/tonkeeper/tongo"
"math"
)

type Nominator struct {
Expand Down Expand Up @@ -35,3 +36,17 @@ type LiquidPool struct {
JettonMaster tongo.AccountID
APY float64
}

func CalculateAPY(roundExpected, roundBorrowed int64, governanceFee int32) float64 {
const secondsPerRound = 1 << 16
const secondsPerYear = 3600 * 24 * 365
roundsPerYear := float64(secondsPerYear) / float64(secondsPerRound)
effectiveRounds := roundsPerYear / 2 // Because each coin may participate only in odd/even rounds
profitPrevRound := float64(roundExpected-roundBorrowed) * (1 - float64(governanceFee)/float64(1<<24))
percentPerPrevRound := profitPrevRound / float64(roundBorrowed)
apy := (math.Pow(1+percentPerPrevRound, effectiveRounds) - 1) * 100
if math.IsNaN(apy) {
return 0
}
return apy
}
3 changes: 1 addition & 2 deletions pkg/litestorage/stacking.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"context"
"fmt"
"math"
"math/big"

"github.com/tonkeeper/tongo/tlb"
Expand Down Expand Up @@ -202,7 +201,7 @@ func (s *LiteStorage) GetLiquidPool(ctx context.Context, pool tongo.AccountID) (
TotalAmount: p.TotalBalance,
VerifiedSources: bytes.Equal(hash, references.TFLiquidPoolCodeHash[:]),
JettonMaster: *jettonMaster,
APY: (math.Pow(1+float64(p.InterestRate)/float64(1<<24)*(1-float64(p.GovernanceFee)/float64(1<<24)), 3600*24*366/(1<<16)) - 1) * 100,
APY: core.CalculateAPY(p.PrevRoundBorrowers.Expected, p.PrevRoundBorrowers.Borrowed, p.InterestRate),
}, err
}

Expand Down

0 comments on commit 633fa63

Please sign in to comment.