emission

package
v0.1.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 16, 2024 License: MIT Imports: 17 Imported by: 0

Documentation

Index

Constants

View Source
const (
	Name      = "emissionBalancer"
	Namespace = "emissionBalancer"
)

Variables

View Source
var (
	ErrValidatorAlreadyRegistered = errors.New("validator already registered")
	ErrStakedAmountInvalid        = errors.New("staked amount invalid")
	ErrValidatorNotFound          = errors.New("validator not found")
	ErrInvalidStakeDuration       = errors.New("invalid stake duration")
	ErrInsufficientRewards        = errors.New("insufficient rewards")
	ErrStakeExpired               = errors.New("stake expired")
	ErrDelegatorAlreadyStaked     = errors.New("delegator already staked")
	ErrDelegatorNotFound          = errors.New("delegator not found")
	ErrDelegatorAlreadyClaimed    = errors.New("delegator already claimed")
	ErrInvalidBlockHeight         = errors.New("invalid block height")
	ErrValidatorNotActive         = errors.New("validator not active")

	ErrInvalidNodeID      = errors.New("invalid node id")
	ErrStakeNotFound      = errors.New("stake not found")
	ErrNotAValidator      = errors.New("not a validator")
	ErrNotAValidatorOwner = errors.New("not a validator owner")
)

Functions

func NewEmissionSubscriptionFactory added in v0.1.3

func NewEmissionSubscriptionFactory(log logging.Logger, emission Tracker) event.SubscriptionFactory[*chain.ExecutedBlock]

Types

type Delegator

type Delegator struct {
	IsActive        bool   `json:"isActive"`        // Indicates if the delegator is currently active
	StakedAmount    uint64 `json:"stakedAmount"`    // Total amount staked by the delegator
	StakeStartBlock uint64 `json:"stakeStartBlock"` // Start block of the stake
	StakeEndBlock   uint64 `json:"stakeEndBlock"`   // End block of the stake
}

type DelegatorEvent

type DelegatorEvent struct {
	ValidatorNodeID ids.NodeID
	Delegator       codec.Address
}

type Emission

type Emission struct {
	TotalSupply     uint64          `json:"totalSupply"`     // Total supply of NAI
	MaxSupply       uint64          `json:"maxSupply"`       // Max supply of NAI
	EmissionAccount EmissionAccount `json:"emissionAccount"` // Emission Account Info

	TotalStaked uint64 `json:"totalStaked"` // Total staked NAI

	EpochTracker EpochTracker `json:"epochTracker"` // Epoch Tracker Info
	// contains filtered or unexported fields
}

func NewEmission added in v0.1.1

func NewEmission(log logging.Logger, vm *vm.VM) (*Emission, error)

NewEmission initializes the Emission struct with initial parameters and sets up the validators heap and indices map.

func (*Emission) AddToTotalSupply

func (e *Emission) AddToTotalSupply(amount uint64) uint64

AddToTotalSupply increases the total supply of NAI by a specified amount, ensuring it does not exceed the max supply.

func (*Emission) CalculateUserDelegationRewards

func (e *Emission) CalculateUserDelegationRewards(nodeID ids.NodeID, actor codec.Address) (uint64, error)

CalculateUserDelegationRewards computes the rewards for a user's delegated stake to a validator, factoring in the delegation duration and amount.

func (*Emission) ClaimStakingRewards

func (e *Emission) ClaimStakingRewards(nodeID ids.NodeID, actor codec.Address) (uint64, error)

ClaimStakingRewards lets validators and delegators claim their rewards

func (*Emission) DelegateUserStake

func (e *Emission) DelegateUserStake(nodeID ids.NodeID, delegatorAddress codec.Address, stakeStartBlock, stakeEndBlock, stakedAmount uint64) error

DelegateUserStake increases the delegated stake for a validator and rebalances the heap.

func (*Emission) DistributeFees

func (e *Emission) DistributeFees(fee uint64)

func (*Emission) GetAPRForValidators

func (e *Emission) GetAPRForValidators() float64

GetAPRForValidators calculates the Annual Percentage Rate (APR) for validators based on the number of validators.

func (*Emission) GetAllValidators

func (e *Emission) GetAllValidators(ctx context.Context) []*Validator

GetAllValidators fetches the current validators from the underlying VM

func (*Emission) GetDelegatorsForValidator

func (e *Emission) GetDelegatorsForValidator(nodeID ids.NodeID) ([]*Delegator, error)

GetDelegatorsForValidator retrieves all delegators for a specific validator by their NodeID.

func (*Emission) GetEmissionValidators added in v0.1.1

func (e *Emission) GetEmissionValidators() map[ids.NodeID]*Validator

func (*Emission) GetInfo added in v0.1.1

func (e *Emission) GetInfo() (emissionAccount EmissionAccount, totalSupply uint64, maxSupply uint64, totalStaked uint64, epochTracker EpochTracker)

func (*Emission) GetLastAcceptedBlockHeight

func (e *Emission) GetLastAcceptedBlockHeight() uint64

GetLastAcceptedBlockHeight retrieves the height of the last accepted block from the VM.

func (*Emission) GetLastAcceptedBlockTimestamp

func (e *Emission) GetLastAcceptedBlockTimestamp() time.Time

GetLastAcceptedBlockTimestamp retrieves the timestamp of the last accepted block from the VM.

func (*Emission) GetNumDelegators

func (e *Emission) GetNumDelegators(nodeID ids.NodeID) int

GetNumDelegators returns the total number of delegators across all validators.

func (*Emission) GetRewardsPerEpoch

func (e *Emission) GetRewardsPerEpoch() uint64

GetRewardsPerEpoch calculates the rewards per epoch based on the total staked amount and the APR for validators.

func (*Emission) GetStakedValidator

func (e *Emission) GetStakedValidator(nodeID ids.NodeID) []*Validator

GetStakedValidator retrieves the details of a specific validator by their NodeID.

func (*Emission) MintNewNAI

func (e *Emission) MintNewNAI() uint64

func (*Emission) RegisterValidatorStake

func (e *Emission) RegisterValidatorStake(nodeID ids.NodeID, nodePublicKey *bls.PublicKey, stakeStartBlock, stakeEndBlock, stakedAmount, delegationFeeRate uint64) error

RegisterValidatorStake adds a new validator to the heap with the specified staked amount and updates the total staked amount.

func (*Emission) UndelegateUserStake

func (e *Emission) UndelegateUserStake(nodeID ids.NodeID, actor codec.Address) (uint64, error)

UndelegateUserStake decreases the delegated stake for a validator and rebalances the heap.

func (*Emission) WithdrawValidatorStake

func (e *Emission) WithdrawValidatorStake(nodeID ids.NodeID) (uint64, error)

WithdrawValidatorStake removes a validator from the heap and updates the total staked amount accordingly.

type EmissionAccount

type EmissionAccount struct {
	Address           codec.Address `json:"address"`
	AccumulatedReward uint64        `json:"accumulatedReward"`
}

type EmissionSubscriptionFactory added in v0.1.3

type EmissionSubscriptionFactory struct {
	// contains filtered or unexported fields
}

func (*EmissionSubscriptionFactory) Accept added in v0.1.3

func (*EmissionSubscriptionFactory) Close added in v0.1.3

func (*EmissionSubscriptionFactory) New added in v0.1.3

type EpochTracker

type EpochTracker struct {
	BaseAPR        float64 `json:"baseAPR"`        // Base APR to use
	BaseValidators uint64  `json:"baseValidators"` // Base number of validators to use
	EpochLength    uint64  `json:"epochLength"`    // Number of blocks per reward epoch
}

type MockEmission added in v0.1.3

type MockEmission struct {
	TotalSupplyVal          uint64
	RewardsPerEpoch         uint64
	APRForValidators        float64
	NumDelegators           int
	StakeRewards            uint64
	LastAcceptedBlockHeight uint64
	Validator               *Validator
}

func MockNewEmission added in v0.1.3

func MockNewEmission(mockEmission *MockEmission) *MockEmission

func (*MockEmission) AddToTotalSupply added in v0.1.3

func (m *MockEmission) AddToTotalSupply(amount uint64) uint64

func (*MockEmission) CalculateUserDelegationRewards added in v0.1.3

func (m *MockEmission) CalculateUserDelegationRewards(nodeID ids.NodeID, actor codec.Address) (uint64, error)

func (*MockEmission) ClaimStakingRewards added in v0.1.3

func (m *MockEmission) ClaimStakingRewards(nodeID ids.NodeID, actor codec.Address) (uint64, error)

func (*MockEmission) DelegateUserStake added in v0.1.3

func (m *MockEmission) DelegateUserStake(nodeID ids.NodeID, delegatorAddress codec.Address, stakeStartBlock, stakeEndBlock, stakedAmount uint64) error

func (*MockEmission) DistributeFees added in v0.1.3

func (m *MockEmission) DistributeFees(fee uint64)

func (*MockEmission) GetAPRForValidators added in v0.1.3

func (m *MockEmission) GetAPRForValidators() float64

func (*MockEmission) GetAllValidators added in v0.1.3

func (m *MockEmission) GetAllValidators(ctx context.Context) []*Validator

func (*MockEmission) GetDelegatorsForValidator added in v0.1.3

func (m *MockEmission) GetDelegatorsForValidator(nodeID ids.NodeID) ([]*Delegator, error)

func (*MockEmission) GetEmissionValidators added in v0.1.3

func (m *MockEmission) GetEmissionValidators() map[ids.NodeID]*Validator

func (*MockEmission) GetInfo added in v0.1.3

func (m *MockEmission) GetInfo() (emissionAccount EmissionAccount, totalSupply uint64, maxSupply uint64, totalStaked uint64, epochTracker EpochTracker)

func (*MockEmission) GetLastAcceptedBlockHeight added in v0.1.3

func (m *MockEmission) GetLastAcceptedBlockHeight() uint64

func (*MockEmission) GetLastAcceptedBlockTimestamp added in v0.1.3

func (m *MockEmission) GetLastAcceptedBlockTimestamp() time.Time

func (*MockEmission) GetNumDelegators added in v0.1.3

func (m *MockEmission) GetNumDelegators(nodeID ids.NodeID) int

func (*MockEmission) GetRewardsPerEpoch added in v0.1.3

func (m *MockEmission) GetRewardsPerEpoch() uint64

func (*MockEmission) GetStakedValidator added in v0.1.3

func (m *MockEmission) GetStakedValidator(nodeID ids.NodeID) []*Validator

func (*MockEmission) MintNewNAI added in v0.1.3

func (m *MockEmission) MintNewNAI() uint64

func (*MockEmission) RegisterValidatorStake added in v0.1.3

func (m *MockEmission) RegisterValidatorStake(nodeID ids.NodeID, nodePublicKey *bls.PublicKey, stakeStartBlock, stakeEndBlock, stakedAmount, delegationFeeRate uint64) error

func (*MockEmission) UndelegateUserStake added in v0.1.3

func (m *MockEmission) UndelegateUserStake(nodeID ids.NodeID, actor codec.Address) (uint64, error)

func (*MockEmission) WithdrawValidatorStake added in v0.1.3

func (m *MockEmission) WithdrawValidatorStake(nodeID ids.NodeID) (uint64, error)

type RewardConfig

type RewardConfig struct {
	// MintingPeriod is period that the staking calculator runs on. It is
	// not valid for a validator's stake duration to be larger than this.
	MintingPeriod time.Duration `json:"mintingPeriod"`

	// SupplyCap is the target value that the reward calculation should be
	// asymptotic to.
	SupplyCap uint64 `json:"supplyCap"`

	// Emission Balancer Address
	EmissionAddress string `json:"emissionAddress"`
}

type StakingConfig

type StakingConfig struct {
	// Minimum stake, in NAI, required to validate the nuklai network
	MinValidatorStake uint64 `json:"minValidatorStake"`
	// Maximum stake, in NAI, allowed to be placed on a single validator in
	// the nuklai network
	MaxValidatorStake uint64 `json:"maxValidatorStake"`
	// Minimum stake, in NAI, that can be delegated on the nuklai network
	MinDelegatorStake uint64 `json:"minDelegatorStake"`
	// Minimum delegation fee, in the range [0, 100], that can be charged
	// for delegation on the nuklai network.
	MinDelegationFee uint64 `json:"minDelegationFee"`
	// MinValidatorStakeDuration is the minimum amount of blocks  a validator can validate
	// for in a single period.
	MinValidatorStakeDuration uint64 `json:"minValidatorStakeDuration"`
	// MaxStakeDuration is the maximum amount of blocks a validator can validate
	// for in a single period.
	MaxValidatorStakeDuration uint64 `json:"maxValidatorStakeDuration"`
	// RewardConfig is the config for the reward function.
	RewardConfig RewardConfig `json:"rewardConfig"`
}

func GetStakingConfig

func GetStakingConfig() StakingConfig

type Tracker added in v0.1.1

type Tracker interface {
	AddToTotalSupply(amount uint64) uint64
	GetRewardsPerEpoch() uint64
	GetAPRForValidators() float64
	GetNumDelegators(nodeID ids.NodeID) int
	CalculateUserDelegationRewards(nodeID ids.NodeID, actor codec.Address) (uint64, error)
	RegisterValidatorStake(nodeID ids.NodeID, nodePublicKey *bls.PublicKey, stakeStartBlock, stakeEndBlock, stakedAmount, delegationFeeRate uint64) error
	WithdrawValidatorStake(nodeID ids.NodeID) (uint64, error)
	DelegateUserStake(nodeID ids.NodeID, delegatorAddress codec.Address, stakeStartBlock, stakeEndBlock, stakedAmount uint64) error
	UndelegateUserStake(nodeID ids.NodeID, actor codec.Address) (uint64, error)
	ClaimStakingRewards(nodeID ids.NodeID, actor codec.Address) (uint64, error)
	MintNewNAI() uint64
	DistributeFees(fee uint64)
	GetStakedValidator(nodeID ids.NodeID) []*Validator
	GetAllValidators(ctx context.Context) []*Validator
	GetDelegatorsForValidator(nodeID ids.NodeID) ([]*Delegator, error)
	GetLastAcceptedBlockTimestamp() time.Time
	GetLastAcceptedBlockHeight() uint64
	GetEmissionValidators() map[ids.NodeID]*Validator
	GetInfo() (emissionAccount EmissionAccount, totalSupply uint64, maxSupply uint64, totalStaked uint64, epochTracker EpochTracker)
}

func GetEmission

func GetEmission() Tracker

GetEmission returns the singleton instance of Emission

type Validator

type Validator struct {
	IsActive                   bool       `json:"isActive"`                   // Indicates if the validator is currently active
	NodeID                     ids.NodeID `json:"nodeID"`                     // Node ID of the validator
	PublicKey                  []byte     `json:"publicKey"`                  // Public key of the validator
	StakedAmount               uint64     `json:"stakedAmount"`               // Total amount staked by the validator
	AccumulatedStakedReward    uint64     `json:"accumulatedStakedReward"`    // Total rewards accumulated by the validator
	DelegationFeeRate          float64    `json:"delegationFeeRate"`          // Fee rate for delegations
	DelegatedAmount            uint64     `json:"delegatedAmount"`            // Total amount delegated to the validator
	AccumulatedDelegatedReward uint64     `json:"accumulatedDelegatedReward"` // Total rewards accumulated by the delegators of the validator
	// contains filtered or unexported fields
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL