Skip to content

Commit 57a2ccc

Browse files
feat: use on chain staking params for locked token APR multiplier (#62)
* add gitleaks pre-commit hook * feat: use on chain staking params for locked token APR multiplier --------- Co-authored-by: Vinod Tiwari <vinod.tiwari@piplabs.xyz>
1 parent 2c8c4f6 commit 57a2ccc

File tree

1 file changed

+55
-4
lines changed

1 file changed

+55
-4
lines changed

pkg/server/handler.go

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,33 @@ func (s *Server) StakingValidatorsHandler() gin.HandlerFunc {
470470
return
471471
}
472472

473+
// Get staking params for locked token type multiplier
474+
stakingParamsResp, err := GetStakingParams(s.conf.Blockchain.StoryAPIEndpoint)
475+
if err != nil {
476+
logger.Error().Err(err).Msg("failed to get staking params")
477+
c.JSON(http.StatusOK, Response{
478+
Code: http.StatusInternalServerError,
479+
Error: ErrInternalAPIServiceError.Error(),
480+
})
481+
return
482+
}
483+
484+
var lockedTokenMultiplier decimal.Decimal
485+
for _, tt := range stakingParamsResp.Msg.Params.TokenTypes {
486+
if tt.TokenType == TokenTypeLocked {
487+
lockedTokenMultiplier, err = decimal.NewFromString(tt.RewardsMultiplier)
488+
if err != nil {
489+
logger.Error().Err(err).Msg("failed to parse locked token type multiplier")
490+
c.JSON(http.StatusOK, Response{
491+
Code: http.StatusInternalServerError,
492+
Error: ErrInternalAPIServiceError.Error(),
493+
})
494+
return
495+
}
496+
break
497+
}
498+
}
499+
473500
// Query from API and database
474501
stakingValidatorsResp, err := GetStakingValidators(s.conf.Blockchain.StoryAPIEndpoint, params)
475502
if err != nil {
@@ -517,9 +544,9 @@ func (s *Server) StakingValidatorsHandler() gin.HandlerFunc {
517544
}
518545

519546
valAPR := sysAPR.Mul(decimal.NewFromInt(1).Sub(commissionRate))
520-
// Locked token type has 0.5x APR
547+
// Apply locked token type multiplier from staking params
521548
if val.SupportTokenType == 0 {
522-
valAPR = valAPR.Div(decimal.NewFromInt(2))
549+
valAPR = valAPR.Mul(lockedTokenMultiplier)
523550
}
524551

525552
validators = append(validators, StakingValidatorData{
@@ -609,9 +636,33 @@ func (s *Server) StakingValidatorHandler() gin.HandlerFunc {
609636
}
610637

611638
valAPR := sysAPR.Mul(decimal.NewFromInt(1).Sub(commissionRate))
612-
// Locked token type has 0.5x APR
639+
// Apply locked token type multiplier from staking params
613640
if val.SupportTokenType == 0 {
614-
valAPR = valAPR.Div(decimal.NewFromInt(2))
641+
stakingParamsResp, err := GetStakingParams(s.conf.Blockchain.StoryAPIEndpoint)
642+
if err != nil {
643+
logger.Error().Err(err).Msg("failed to get staking params")
644+
c.JSON(http.StatusOK, Response{
645+
Code: http.StatusInternalServerError,
646+
Error: ErrInternalAPIServiceError.Error(),
647+
})
648+
return
649+
}
650+
651+
for _, tt := range stakingParamsResp.Msg.Params.TokenTypes {
652+
if tt.TokenType == TokenTypeLocked {
653+
lockedTokenMultiplier, err := decimal.NewFromString(tt.RewardsMultiplier)
654+
if err != nil {
655+
logger.Error().Err(err).Msg("failed to parse locked token type multiplier")
656+
c.JSON(http.StatusOK, Response{
657+
Code: http.StatusInternalServerError,
658+
Error: ErrInternalAPIServiceError.Error(),
659+
})
660+
return
661+
}
662+
valAPR = valAPR.Mul(lockedTokenMultiplier)
663+
break
664+
}
665+
}
615666
}
616667

617668
msg := StakingValidatorData{

0 commit comments

Comments
 (0)