Skip to content

Commit

Permalink
feat(engine): adding validator reward calculate method (discord added)
Browse files Browse the repository at this point in the history
  • Loading branch information
kehiy committed Feb 5, 2024
1 parent 3a8f5f5 commit 4296f2e
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 6 deletions.
25 changes: 22 additions & 3 deletions discord/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,24 @@ var commands = []*discordgo.ApplicationCommand{
},
},
},
{
Name: "reward-calc",
Description: "calculates how much PAC coins you will earn in a (day/month/year) based on your stake.",
Options: []*discordgo.ApplicationCommandOption{
{
Type: discordgo.ApplicationCommandOptionString,
Name: "stake",
Description: "your validator stake amount",
Required: true,
},
{
Type: discordgo.ApplicationCommandOptionString,
Name: "time",
Description: "in a day/month/year (default is a day)",
Required: false,
},
},
},
{
Name: "network-health",
Description: "network health status",
Expand All @@ -58,8 +76,8 @@ var commands = []*discordgo.ApplicationCommand{
Description: "status of The Pactus network",
},
{
Name: "bot-wallet",
Description: "The RoboPac wallet address and balance",
Name: "wallet",
Description: "The RoboPac wallet info",
},
{
Name: "claim-status",
Expand All @@ -78,6 +96,7 @@ var commandHandlers = map[string]func(*DiscordBot, *discordgo.Session, *discordg
"node-info": nodeInfoCommandHandler,
"network-health": networkHealthCommandHandler,
"network-status": networkStatusCommandHandler,
"bot-wallet": botWalletCommandHandler,
"wallet": walletCommandHandler,
"claim-status": claimStatusCommandHandler,
"reward-calc": rewardCalcCommandHandler,
}
8 changes: 8 additions & 0 deletions discord/embeds.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,14 @@ func claimStatusEmbed(s *discordgo.Session, i *discordgo.InteractionCreate, resu
}
}

func rewardCalcEmbed(s *discordgo.Session, i *discordgo.InteractionCreate, result string) *discordgo.MessageEmbed {
return &discordgo.MessageEmbed{
Title: "Vlidator reward calculation🧮",
Description: result,
Color: PACTUS,
}
}

func errorEmbedMessage(reason string) *discordgo.MessageEmbed {
return &discordgo.MessageEmbed{
Title: "Error",
Expand Down
37 changes: 36 additions & 1 deletion discord/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ func networkStatusCommandHandler(db *DiscordBot, s *discordgo.Session, i *discor
_ = s.InteractionRespond(i.Interaction, response)
}

func botWalletCommandHandler(db *DiscordBot, s *discordgo.Session, i *discordgo.InteractionCreate) {
func walletCommandHandler(db *DiscordBot, s *discordgo.Session, i *discordgo.InteractionCreate) {
if !checkMessage(i, s, db.GuildID, i.Member.User.ID) {
return
}
Expand Down Expand Up @@ -249,3 +249,38 @@ func claimStatusCommandHandler(db *DiscordBot, s *discordgo.Session, i *discordg

_ = s.InteractionRespond(i.Interaction, response)
}

func rewardCalcCommandHandler(db *DiscordBot, s *discordgo.Session, i *discordgo.InteractionCreate) {
if !checkMessage(i, s, db.GuildID, i.Member.User.ID) {
return
}

stake := i.ApplicationCommandData().Options[0].StringValue()
time := i.ApplicationCommandData().Options[1].StringValue()

result, err := db.BotEngine.Run(fmt.Sprintf("calc-reward %v %v", stake, time))
if err != nil {
errorEmbed := errorEmbedMessage(err.Error())

response := &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Embeds: []*discordgo.MessageEmbed{errorEmbed},
},
}

_ = s.InteractionRespond(i.Interaction, response)

return
}

embed := rewardCalcEmbed(s, i, result)
response := &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Embeds: []*discordgo.MessageEmbed{embed},
},
}

_ = s.InteractionRespond(i.Interaction, response)
}
25 changes: 25 additions & 0 deletions engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/kehiy/RoboPac/utils"
"github.com/kehiy/RoboPac/wallet"
"github.com/libp2p/go-libp2p/core/peer"
putils "github.com/pactus-project/pactus/util"
)

type BotEngine struct {
Expand Down Expand Up @@ -246,6 +247,30 @@ func (be *BotEngine) ClaimStatus() (int64, int64, int64, int64) {
return be.Store.Status()
}

func (be *BotEngine) RewardCalculate(stake int64, t string) (int64, string, int64, error) {
var blocks int64
time := t
switch t {
case "day":
blocks = 8640
case "month":
blocks = 259200
case "year":
blocks = 3110400
default:
blocks = 8640
time = "day"
}

bi, err := be.Cm.GetBlockchainInfo()
if err != nil {
return 0, "", 0, nil
}

reward := (stake * int64(blocks)) / int64(putils.ChangeToCoin(bi.TotalPower))
return reward, time, bi.TotalPower, nil
}

func (be *BotEngine) Stop() {
be.logger.Info("shutting bot engine down...")

Expand Down
1 change: 1 addition & 0 deletions engine/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ type IEngine interface {
ClaimerInfo(discordID string) (*store.Claimer, error)
Claim(discordID string, testnetAddr string, mainnetAddr string) (string, error)
BotWallet() (string, int64)
RewardCalculate(int64, string) (int64, string, int64, error)

Run(input string) (string, error)

Expand Down
25 changes: 23 additions & 2 deletions engine/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package engine

import (
"fmt"
"strconv"
"strings"

"github.com/pactus-project/pactus/util"
Expand All @@ -13,8 +14,9 @@ const (
CmdNodeInfo = "node-info" //!
CmdNetworkStatus = "network" //!
CmdNetworkHealth = "network-health" //!
CmdBotWallet = "bot-wallet" //!
CmdBotWallet = "wallet" //!
CmdClaimStatus = "claim-status" //!
CmdRewardCalc = "calc-reward" //!
)

// The input is always string.
Expand Down Expand Up @@ -98,7 +100,7 @@ func (be *BotEngine) Run(input string) (string, error) {

return fmt.Sprintf("Network Name: %s\nConnected Peers: %v\n"+
"Validators Count: %v\nAccounts Count: %v\nCurrent Block Height: %v\nTotal Power: %v PAC's\nTotal Committee Power: %v PAC's\n"+
"> Note📝: This info is from one random network node. Non-blockchain data may not be consistent.",
"\n> Note📝: This info is from one random network node. Non-blockchain data may not be consistent.",
net.NetworkName, net.ConnectedPeersCount, net.ValidatorsCount, net.TotalAccounts, net.CurrentBlockHeight, util.ChangeToString(net.TotalNetworkPower),
util.ChangeToString(net.TotalCommitteePower)), nil

Expand All @@ -111,6 +113,25 @@ func (be *BotEngine) Run(input string) (string, error) {
return fmt.Sprintf("Claimed rewards count: %v\nClaimed coins: %v PAC's\nNot-claimed rewards count: %v\nNot-claim coins: %v PAC's\n",
claimed, util.ChangeToString(claimedAmount), notClaimed, util.ChangeToString(notClaimedAmount)), nil

case CmdRewardCalc:
if len(args) != 2 {
return "", fmt.Errorf("expected to have 2 arguments, but it received %d", len(args))
}

stake, err := strconv.Atoi(args[0])
if err != nil {
return "", err
}

reward, time, totalPower, err := be.RewardCalculate(int64(stake), args[1])
if err != nil {
return "", err
}

return fmt.Sprintf("You will get: %v :pac: reward, with %v stake 🔒 on your validator in one %s ⏰ with %v total power ⚡ of committee."+
"\n\n> Note📝: This is an estimation not a 100 percent correct data and with increasing total power, validators and your stake amount this number will change.",
reward, stake, time, util.ChangeToString(totalPower)), nil

default:
return "", fmt.Errorf("unknown command: %s", cmd)
}
Expand Down

0 comments on commit 4296f2e

Please sign in to comment.