Skip to content

Commit

Permalink
fix: update APY 30 day period
Browse files Browse the repository at this point in the history
  • Loading branch information
Majorfi committed Nov 22, 2023
1 parent f810579 commit 92cf8c3
Show file tree
Hide file tree
Showing 34 changed files with 1,181 additions and 542 deletions.
1 change: 1 addition & 0 deletions cmd/flags.chains.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func handleChainsInitialization(rawChains *string) []uint64 {
}
chains = append(chains, chainID)
ethereum.GetWSClient(chainID)
ethereum.InitBlockTimestamp(chainID)
}

if len(chains) == 0 {
Expand Down
12 changes: 7 additions & 5 deletions common/env/chain.arbitrum.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ import (
)

var ARBITRUM = TChain{
ID: 42161,
RpcURI: `https://arbitrum.public-rpc.com`,
SubgraphURI: `https://api.thegraph.com/subgraphs/name/yearn/yearn-vaults-v2-arbitrum`,
MaxBlockRange: 100_000_000,
MaxBatchSize: math.MaxInt64,
ID: 42161,
RpcURI: `https://arbitrum.public-rpc.com`,
SubgraphURI: `https://api.thegraph.com/subgraphs/name/yearn/yearn-vaults-v2-arbitrum`,
EtherscanURI: `https://api.arbiscan.io/api`,
MaxBlockRange: 100_000_000,
MaxBatchSize: math.MaxInt64,
AvgBlocksPerDay: 320_000,
LensContract: TContractData{
Address: common.HexToAddress(`0x043518AB266485dC085a1DB095B8d9C2Fc78E9b9`),
Block: 2396321,
Expand Down
12 changes: 7 additions & 5 deletions common/env/chain.base.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ import (
)

var BASE = TChain{
ID: 8453,
RpcURI: `https://developer-access-mainnet.base.org`,
SubgraphURI: `https://api.thegraph.com/subgraphs/name/rareweasel/yearn-vaults-v2-subgraph-base`,
MaxBlockRange: 100_000_000,
MaxBatchSize: math.MaxInt64,
ID: 8453,
RpcURI: `https://developer-access-mainnet.base.org`,
SubgraphURI: `https://api.thegraph.com/subgraphs/name/rareweasel/yearn-vaults-v2-subgraph-base`,
EtherscanURI: `https://api.basescan.org/api`,
MaxBlockRange: 100_000_000,
MaxBatchSize: math.MaxInt64,
AvgBlocksPerDay: 43_200,
LensContract: TContractData{
Address: common.HexToAddress(`0xE0F3D78DB7bC111996864A32d22AB0F59Ca5Fa86`),
Block: 3318817,
Expand Down
12 changes: 7 additions & 5 deletions common/env/chain.ethereum.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ import (
)

var ETHEREUM = TChain{
ID: 1,
RpcURI: `https://eth.public-rpc.com`,
SubgraphURI: `https://api.thegraph.com/subgraphs/name/rareweasel/yearn-vaults-v2-subgraph-mainnet`,
MaxBlockRange: 100_000_000,
MaxBatchSize: math.MaxInt64,
ID: 1,
RpcURI: `https://eth.public-rpc.com`,
SubgraphURI: `https://api.thegraph.com/subgraphs/name/rareweasel/yearn-vaults-v2-subgraph-mainnet`,
EtherscanURI: `https://api.etherscan.io/api`,
MaxBlockRange: 100_000_000,
MaxBatchSize: math.MaxInt64,
AvgBlocksPerDay: 7150,
YBribeV3Contract: TContractData{
Address: common.HexToAddress(`0x03dFdBcD4056E2F92251c7B07423E1a33a7D3F6d`),
Block: 15878262,
Expand Down
12 changes: 7 additions & 5 deletions common/env/chain.fantom.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ import (
)

var FANTOM = TChain{
ID: 250,
RpcURI: `https://rpc.ftm.tools`,
SubgraphURI: `https://api.thegraph.com/subgraphs/name/yearn/yearn-vaults-v2-fantom`,
MaxBlockRange: 100_000_000,
MaxBatchSize: math.MaxInt64,
ID: 250,
RpcURI: `https://rpc.ftm.tools`,
SubgraphURI: `https://api.thegraph.com/subgraphs/name/yearn/yearn-vaults-v2-fantom`,
EtherscanURI: `https://api.ftmscan.com/api`,
MaxBlockRange: 100_000_000,
MaxBatchSize: math.MaxInt64,
AvgBlocksPerDay: 45_000,
LensContract: TContractData{
Address: common.HexToAddress(`0x57AA88A0810dfe3f9b71a9b179Dd8bF5F956C46A`),
Block: 17091856,
Expand Down
12 changes: 7 additions & 5 deletions common/env/chain.optimism.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ import (
)

var OPTIMISM = TChain{
ID: 10,
RpcURI: `https://mainnet.optimism.io`,
SubgraphURI: `https://api.thegraph.com/subgraphs/name/yearn/yearn-vaults-v2-optimism`,
MaxBlockRange: 100_000_000,
MaxBatchSize: math.MaxInt64,
ID: 10,
RpcURI: `https://mainnet.optimism.io`,
SubgraphURI: `https://api.thegraph.com/subgraphs/name/yearn/yearn-vaults-v2-optimism`,
EtherscanURI: `https://api-optimistic.etherscan.io/api`,
MaxBlockRange: 100_000_000,
MaxBatchSize: math.MaxInt64,
AvgBlocksPerDay: 43_200,
LensContract: TContractData{
Address: common.HexToAddress(`0xB082d9f4734c535D9d80536F7E87a6f4F471bF65`),
Block: 18109291,
Expand Down
16 changes: 9 additions & 7 deletions common/env/chain.polygon.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,20 @@ import (
)

var POLYGON = TChain{
ID: 137,
RpcURI: `https://polygon.llamarpc.com`,
SubgraphURI: ``, //TODO: not deployed
MaxBlockRange: 100_000_000,
MaxBatchSize: math.MaxInt64,
LensContract: TContractData{}, //TODO: not deployed
ID: 137,
RpcURI: `https://polygon.llamarpc.com`,
SubgraphURI: ``, //TODO: not deployed
EtherscanURI: `https://api.polygonscan.com/api`,
MaxBlockRange: 100_000_000,
MaxBatchSize: math.MaxInt64,
AvgBlocksPerDay: 40_000,
LensContract: TContractData{}, //TODO: not deployed
MulticallContract: TContractData{
Address: common.HexToAddress(`0xca11bde05977b3631167028862be2a173976ca11`),
Block: 25770160,
},
APROracleContract: TContractData{
Address: common.HexToAddress(`0xf012fbb9283e03994a7829fce994a105cc066c14`),
Address: common.HexToAddress(`0xF012fBb9283e03994A7829fCE994a105cC066c14`),
Block: 48956840,
},
Coin: models.TERC20Token{
Expand Down
2 changes: 2 additions & 0 deletions common/env/chains.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ type TChain struct {
ID uint64
RpcURI string
SubgraphURI string
EtherscanURI string
MaxBlockRange uint64
MaxBatchSize int
AvgBlocksPerDay int
LensContract TContractData
MulticallContract TContractData
StackingRewardContract TContractData
Expand Down
53 changes: 53 additions & 0 deletions common/ethereum/blocktime.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,65 @@ package ethereum
import (
"context"
"math/big"
"os"
"strconv"
"time"

"github.com/yearn/ydaemon/common/env"
"github.com/yearn/ydaemon/common/helpers"
"github.com/yearn/ydaemon/common/logs"
"github.com/yearn/ydaemon/common/store"
)

var blockTimeMap = make(map[uint64]map[uint64]uint64)

func InitBlockTimestamp(chainID uint64) {
type TScanResult struct {
Status string `json:"status"`
Message string `json:"message"`
Result string `json:"result"`
}
var err error
now := time.Now()
noonUTC := time.Date(now.Year(), now.Month(), now.Day(), 12, 0, 0, 0, time.UTC)
lastWeekTimestamp := noonUTC.AddDate(0, 0, -7).Unix()
lastMonthTimestamp := noonUTC.AddDate(0, -1, 0).Unix()

APIKey := os.Getenv("SCAN_API_KEY_FOR_" + strconv.FormatUint(chainID, 10))
lastWeekBlock := helpers.FetchJSON[TScanResult](env.CHAINS[chainID].EtherscanURI + `?module=block&action=getblocknobytime&timestamp=` + strconv.FormatInt(lastWeekTimestamp, 10) + `&closest=before&apikey=` + APIKey)
lastMonthBlock := helpers.FetchJSON[TScanResult](env.CHAINS[chainID].EtherscanURI + `?module=block&action=getblocknobytime&timestamp=` + strconv.FormatInt(lastMonthTimestamp, 10) + `&closest=before&apikey=` + APIKey)

if blockTimeMap[chainID] == nil {
blockTimeMap[chainID] = make(map[uint64]uint64)
}
if lastWeekBlock.Status == "1" {
blockTimeMap[chainID][7], err = strconv.ParseUint(lastWeekBlock.Result, 10, 64)
if err != nil {
logs.Error(err)
blockTimeMap[chainID][7] = uint64(env.CHAINS[chainID].AvgBlocksPerDay * 7)
}
} else {
blockTimeMap[chainID][7] = uint64(env.CHAINS[chainID].AvgBlocksPerDay * 7)
}

if lastMonthBlock.Status == "1" {
blockTimeMap[chainID][30], err = strconv.ParseUint(lastMonthBlock.Result, 10, 64)
if err != nil {
logs.Error(err)
blockTimeMap[chainID][30] = uint64(env.CHAINS[chainID].AvgBlocksPerDay * 30)
}
} else {
blockTimeMap[chainID][30] = uint64(env.CHAINS[chainID].AvgBlocksPerDay * 30)
}
}

func GetBlockNumberXDaysAgo(chainID uint64, days uint64) uint64 {
if (days != 7 && days != 30) || blockTimeMap[chainID] == nil {
return 0
}
return blockTimeMap[chainID][days]
}

/**************************************************************************************************
** GetBlockTime will try, for a specific blockNumber on a specific chain, to find its execution
** timestamp. This timestamp should be available in the blockTimeSyncMap. If it is not, it will
Expand Down
31 changes: 2 additions & 29 deletions common/ethereum/pps.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package ethereum

import (
"math/big"
"time"

"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -37,8 +36,7 @@ func FetchPPSLastWeek(
logs.Error("Could not get vault contract for " + vaultAddress.Hex())
return bigNumber.NewFloat(0)
}
blocksPerDay := 7150
estBlockLastWeek := blocksPerDay * 7
estBlockLastWeek := GetBlockNumberXDaysAgo(chainID, 7)
opts := &bind.CallOpts{
BlockNumber: big.NewInt(int64(estBlockLastWeek)),
}
Expand All @@ -57,36 +55,11 @@ func FetchPPSLastMonth(
logs.Error("Could not get vault contract for " + vaultAddress.Hex())
return bigNumber.NewFloat(0)
}
blocksPerDay := 7150
estBlockLastWeek := blocksPerDay * 30
estBlockLastWeek := GetBlockNumberXDaysAgo(chainID, 30)
opts := &bind.CallOpts{
BlockNumber: big.NewInt(int64(estBlockLastWeek)),
}
pps, _ := vaultContract.PricePerShare(opts)
ppsToday := helpers.ToNormalizedAmount(bigNumber.SetInt(pps), decimals)
return ppsToday
}

func GetLastYear(ppsPerTime map[uint64]*bigNumber.Int, decimals uint64) *bigNumber.Float {
now := time.Now()
noonUTC := time.Date(now.Year(), now.Month(), now.Day(), 12, 0, 0, 0, time.UTC)
lastYear := noonUTC.AddDate(-1, 0, 0)
if now.Before(noonUTC) {
lastYear = noonUTC.AddDate(-1, 0, -1)
}
ppsYear := bigNumber.NewFloat(0)

if data, ok := ppsPerTime[uint64(lastYear.Unix())]; ok {
ppsYear = helpers.ToNormalizedAmount(data, decimals)
return ppsYear
}
for i := 1; i < 365; i++ {
dayToCheck := noonUTC.AddDate(0, 0, i-365)
if data, ok := ppsPerTime[uint64(dayToCheck.Unix())]; ok {
ppsYear = helpers.ToNormalizedAmount(data, decimals)
break
}
}

return ppsYear
}
30 changes: 3 additions & 27 deletions common/helpers/apyTime.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/yearn/ydaemon/common/bigNumber"
)

func GetToday(ppsPerTime map[uint64]*bigNumber.Int, decimals uint64) *bigNumber.Float {
func GetPPSToday(ppsPerTime map[uint64]*bigNumber.Int, decimals uint64) *bigNumber.Float {
now := time.Now()
noonUTC := time.Date(now.Year(), now.Month(), now.Day(), 12, 0, 0, 0, time.UTC)
ppsNow := bigNumber.NewFloat(0)
Expand All @@ -20,7 +20,7 @@ func GetToday(ppsPerTime map[uint64]*bigNumber.Int, decimals uint64) *bigNumber.
return ppsNow
}

func GetLastWeek(ppsPerTime map[uint64]*bigNumber.Int, decimals uint64) *bigNumber.Float {
func GetPPSLastWeek(ppsPerTime map[uint64]*bigNumber.Int, decimals uint64) *bigNumber.Float {
now := time.Now()
noonUTC := time.Date(now.Year(), now.Month(), now.Day(), 12, 0, 0, 0, time.UTC)
lastWeek := noonUTC.AddDate(0, 0, -7)
Expand All @@ -44,7 +44,7 @@ func GetLastWeek(ppsPerTime map[uint64]*bigNumber.Int, decimals uint64) *bigNumb
return ppsWeek
}

func GetLastMonth(ppsPerTime map[uint64]*bigNumber.Int, decimals uint64) *bigNumber.Float {
func GetPPSLastMonth(ppsPerTime map[uint64]*bigNumber.Int, decimals uint64) *bigNumber.Float {
now := time.Now()
noonUTC := time.Date(now.Year(), now.Month(), now.Day(), 12, 0, 0, 0, time.UTC)
lastMonth := noonUTC.AddDate(0, -1, 0)
Expand All @@ -68,30 +68,6 @@ func GetLastMonth(ppsPerTime map[uint64]*bigNumber.Int, decimals uint64) *bigNum
return ppsMonth
}

func GetLastYear(ppsPerTime map[uint64]*bigNumber.Int, decimals uint64) *bigNumber.Float {
now := time.Now()
noonUTC := time.Date(now.Year(), now.Month(), now.Day(), 12, 0, 0, 0, time.UTC)
lastYear := noonUTC.AddDate(-1, 0, 0)
if now.Before(noonUTC) {
lastYear = noonUTC.AddDate(-1, 0, -1)
}
ppsYear := bigNumber.NewFloat(0)

if data, ok := ppsPerTime[uint64(lastYear.Unix())]; ok {
ppsYear = ToNormalizedAmount(data, decimals)
return ppsYear
}
for i := 1; i < 365; i++ {
dayToCheck := noonUTC.AddDate(0, 0, i-365)
if data, ok := ppsPerTime[uint64(dayToCheck.Unix())]; ok {
ppsYear = ToNormalizedAmount(data, decimals)
break
}
}

return ppsYear
}

func GetAPR(vf *bigNumber.Float, vi *bigNumber.Float, days *bigNumber.Float) *bigNumber.Float {
apr := bigNumber.NewFloat(0).Sub(vf, vi)
apr = bigNumber.NewFloat(0).Div(apr, vi)
Expand Down
Loading

0 comments on commit 92cf8c3

Please sign in to comment.