The core protocol of WoopChain
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
woop/internal/chain/supply.go

47 lines
1.7 KiB

Make token supply related fns more accurate (#3509) * [staking] Move reward values from Network pkg to its own * Refactor code for the move * Implement logic to accurately set total supply Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [staking] Add totalPreStakingNetworkRewards to reward values * Implement GetTotalTokens for use in other packages Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [core] Move getGenesisSpec to core pkg Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [core] Update gen spec docs Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [staking] Hook in updateInitialRewardValues on node init * Add some docs for clarification Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rpc] Fix GetCirculatingSupply & GetTotalSupply RPCs * Updated err msg in staking reward values.go Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rpc] Move GetCirculatingSupply logic into internal pkg Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [explorer] Update circulating supply & total supply vals Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [staking] Add Localnet rewards val & Errs * [internal] Make GetCirculatingSupply consistent with WhatPercentStakedNow Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [staking] Fix imports Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [consensus] Make PercentageForTimeStamp return 1 for non-mainnet chains Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [staking] Fix reward dec math + Testnet testnet vals Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [staking] Make all const reward vals ONE instead of ATTO Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [internal] Correct returned value to ONE instead of Atto Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [staking] Fix dec precision Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [staking] Fix TestGetPreStakingRewardsFromBlockNumber test Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [staking] Use TotalInitialTokens instead of TotalPreStakingTokens * Done so basis is off block 0 to account for phased mainnet release Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [internal] Fix GetCirculatingSupply Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu>
4 years ago
package chain
import (
"context"
"math/big"
"time"
"github.com/harmony-one/harmony/consensus/engine"
"github.com/harmony-one/harmony/consensus/reward"
"github.com/harmony-one/harmony/numeric"
"github.com/harmony-one/harmony/shard"
stakingReward "github.com/harmony-one/harmony/staking/reward"
)
// GetCirculatingSupply using the following formula:
// (TotalInitialTokens * percentReleased) + PreStakingBlockRewards + StakingBlockRewards
//
// Note that PreStakingBlockRewards is set to the amount of rewards given out by the
// LAST BLOCK of the pre-staking era regardless of what the current block height is
// if network is in the pre-staking era. This is for implementation reasons, reference
// stakingReward.GetTotalPreStakingTokens for more details.
//
// WARNING: only works on beaconchain if in staking era.
func GetCirculatingSupply(
ctx context.Context, chain engine.ChainReader,
) (ret numeric.Dec, err error) {
currHeader, timestamp := chain.CurrentHeader(), time.Now().Unix()
stakingBlockRewards := big.NewInt(0)
if chain.Config().IsStaking(currHeader.Epoch()) {
if chain.ShardID() != shard.BeaconChainShardID {
return numeric.Dec{}, stakingReward.ErrInvalidBeaconChain
}
if stakingBlockRewards, err = chain.ReadBlockRewardAccumulator(currHeader.Number().Uint64()); err != nil {
return numeric.Dec{}, err
}
}
releasedInitSupply := stakingReward.TotalInitialTokens.Mul(
reward.PercentageForTimeStamp(timestamp),
)
preStakingBlockRewards := stakingReward.GetTotalPreStakingTokens().Sub(stakingReward.TotalInitialTokens)
return releasedInitSupply.Add(preStakingBlockRewards).Add(
numeric.NewDecFromBigIntWithPrec(stakingBlockRewards, 18),
), nil
}