Skip to content

Commit

Permalink
feat(engine): adding CirculatingSupply to network status data
Browse files Browse the repository at this point in the history
  • Loading branch information
kehiy committed Feb 5, 2024
1 parent 93df3e3 commit 5a6b4d7
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 6 deletions.
11 changes: 11 additions & 0 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,17 @@ func (c *Client) GetTransactionData(txID string) (*pactus.GetTransactionResponse
})
}

func (c *Client) GetBalance(address string) (int64, error) {
account, err := c.blockchainClient.GetAccount(context.Background(), &pactus.GetAccountRequest{
Address: address,
})
if err != nil {
return 0, err
}

return account.Account.Balance, nil
}

func (c *Client) Close() error {
return c.conn.Close()
}
39 changes: 39 additions & 0 deletions client/client_mgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,45 @@ func (cm *Mgr) GetTransactionData(txID string) (*pactus.GetTransactionResponse,
return txData, nil
}

func (cm *Mgr) GetCirculatingSupply() (int64, error) {
localClient := cm.getLocalClient()

height, err := localClient.GetBlockchainInfo()
if err != nil {
return 0, err
}
minted := float64(height.LastBlockHeight) * 1e9
staked := height.TotalPower

var addr1Out int64 = 0
var addr2Out int64 = 0
var addr3Out int64 = 0
var addr4Out int64 = 0

balance1, err := localClient.GetBalance("pc1z2r0fmu8sg2ffa0tgrr08gnefcxl2kq7wvquf8z")
if err == nil {
addr1Out = 8_400_000_000_000_000 - balance1
}

balance2, err := localClient.GetBalance("pc1zprhnvcsy3pthekdcu28cw8muw4f432hkwgfasv")
if err == nil {
addr2Out = 6_300_000_000_000_000 - balance2
}

balance3, err := localClient.GetBalance("pc1znn2qxsugfrt7j4608zvtnxf8dnz8skrxguyf45")
if err == nil {
addr3Out = 4_200_000_000_000_000 - balance3
}

balance4, err := localClient.GetBalance("pc1zs64vdggjcshumjwzaskhfn0j9gfpkvche3kxd3")
if err == nil {
addr4Out = 2_100_000_000_000_000 - balance4
}

circulating := (addr1Out + addr2Out + addr3Out + addr4Out + int64(minted)) - staked
return circulating, nil
}

func (cm *Mgr) Stop() {
for addr, c := range cm.clients {
if err := c.Close(); err != nil {
Expand Down
1 change: 1 addition & 0 deletions client/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ type IClient interface {
GetValidatorInfo(string) (*pactus.GetValidatorResponse, error)
GetValidatorInfoByNumber(int32) (*pactus.GetValidatorResponse, error)
GetTransactionData(string) (*pactus.GetTransactionResponse, error)
GetBalance(string) (int64, error)
Close() error
}
15 changes: 15 additions & 0 deletions client/mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion discord/embeds.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ 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🧮",
Title: "Validator reward calculation🧮",
Description: result,
Color: PACTUS,
}
Expand Down
2 changes: 1 addition & 1 deletion discord/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ func walletCommandHandler(db *DiscordBot, s *discordgo.Session, i *discordgo.Int
return
}

result, _ := db.BotEngine.Run("bot-wallet")
result, _ := db.BotEngine.Run("wallet")

embed := botWalletEmbed(s, i, result)
response := &discordgo.InteractionResponse{
Expand Down
6 changes: 6 additions & 0 deletions engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,11 @@ func (be *BotEngine) NetworkStatus() (*NetStatus, error) {
return nil, err
}

cs, err := be.Cm.GetCirculatingSupply()
if err != nil {
cs = 0
}

return &NetStatus{
ConnectedPeersCount: netInfo.ConnectedPeersCount,
ValidatorsCount: chainInfo.TotalValidators,
Expand All @@ -125,6 +130,7 @@ func (be *BotEngine) NetworkStatus() (*NetStatus, error) {
TotalCommitteePower: chainInfo.CommitteePower,
NetworkName: netInfo.NetworkName,
TotalAccounts: chainInfo.TotalAccounts,
CirculatingSupply: cs,
}, nil
}

Expand Down
6 changes: 3 additions & 3 deletions engine/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,10 @@ 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"+
"Validators Count: %v\nAccounts Count: %v\nCurrent Block Height: %v\nTotal Power: %v PAC\nTotal Committee Power: %v PAC\nCirculating Supply: %v PAC\n"+
"\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
util.ChangeToString(net.TotalCommitteePower), util.ChangeToString(net.CirculatingSupply)), nil

case CmdBotWallet:
addr, blnc := be.BotWallet()
Expand All @@ -128,7 +128,7 @@ func (be *BotEngine) Run(input string) (string, error) {
return "", err
}

return fmt.Sprintf("Approximately you earn %v PAC's reward, with %v stake 🔒 on your validator in one %s ⏰ with %v PAC total power ⚡ of committee."+
return fmt.Sprintf("Approximately you earn %v PAC reward, with %v PAC stake 🔒 on your validator in one %s ⏰ with %v PAC total power ⚡ of committee."+
"\n\n> Note📝: This is an estimation and the number can get changed by changes of your stake amount, total power and ...",
reward, stake, time, totalPower), nil

Expand Down
3 changes: 2 additions & 1 deletion engine/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,16 @@ type NetHealthResponse struct {
}

type NetStatus struct {
NetworkName string
ConnectedPeersCount uint32
ValidatorsCount int32
TotalBytesSent uint32
TotalBytesReceived uint32
CurrentBlockHeight uint32
TotalNetworkPower int64
TotalCommitteePower int64
NetworkName string
TotalAccounts int32
CirculatingSupply int64
}

type NodeInfo struct {
Expand Down

0 comments on commit 5a6b4d7

Please sign in to comment.