github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/x/staking/keeper/weight.go (about)

     1  package keeper
     2  
     3  import (
     4  	"fmt"
     5  	"math"
     6  
     7  	sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types"
     8  	"github.com/fibonacci-chain/fbc/x/staking/types"
     9  )
    10  
    11  const (
    12  	// UTC Time: 2000/1/1 00:00:00
    13  	blockTimestampEpoch = int64(946684800)
    14  	secondsPerWeek      = int64(60 * 60 * 24 * 7)
    15  	weeksPerYear        = float64(52)
    16  )
    17  
    18  func calculateWeight(nowTime int64, tokens sdk.Dec) (shares types.Shares, sdkErr error) {
    19  	nowWeek := (nowTime - blockTimestampEpoch) / secondsPerWeek
    20  	rate := float64(nowWeek) / weeksPerYear
    21  	weight := math.Pow(float64(2), rate)
    22  
    23  	precision := fmt.Sprintf("%d", sdk.Precision)
    24  
    25  	weightByDec, sdkErr := sdk.NewDecFromStr(fmt.Sprintf("%."+precision+"f", weight))
    26  	if sdkErr == nil {
    27  		shares = tokens.Mul(weightByDec)
    28  	}
    29  	return
    30  }
    31  
    32  func SimulateWeight(nowTime int64, tokens sdk.Dec) (votes types.Shares, sdkErr error) {
    33  	return calculateWeight(nowTime, tokens)
    34  }