Skip to content

Commit

Permalink
feat: define prototype for market command (#278)
Browse files Browse the repository at this point in the history
  • Loading branch information
MHSaeedkia authored Jan 19, 2025
1 parent c0640ac commit d913e99
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 55 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ build-http:
### Generating commands
gen:
go run ./internal/generator/main.go \
"./internal/engine/command/crowdfund/crowdfund.yml"
"./internal/engine/command/crowdfund/crowdfund.yml" "./internal/engine/command/market/market.yml"
find . -name "*.gen.go" -exec gofumpt -l -w {} +

###
Expand Down
2 changes: 1 addition & 1 deletion config/config.sample.yml
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,4 @@ logger:
max_size: 10 # Maximum size (in MB) of the log file before rotation.
max_backups: 10 # Maximum number of backup log files to retain.
compress: true # Compress old log files.
targets: [file, console] # Logging targets: file, console, or both.
targets: [file, console] # Logging targets: file, console, or both.
32 changes: 15 additions & 17 deletions internal/engine/command/crowdfund/crowdfund.gen.go

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

38 changes: 38 additions & 0 deletions internal/engine/command/market/market.gen.go

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

29 changes: 8 additions & 21 deletions internal/engine/command/market/market.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
)

type MarketCmd struct {
*marketSubCmds

clientMgr client.IManager
priceCache cache.Cache[string, entity.Price]
}
Expand All @@ -20,27 +22,12 @@ func NewMarketCmd(clientMgr client.IManager, priceCache cache.Cache[string, enti
}

func (m *MarketCmd) GetCommand() *command.Command {
subCmdPrice := &command.Command{
Name: "price",
Help: "Shows the latest price of PAC coin across different markets",
Args: []*command.Args{},
SubCommands: nil,
AppIDs: entity.AllAppIDs(),
Handler: m.priceHandler,
TargetFlag: command.TargetMaskMainnet,
}

cmdMarket := &command.Command{
Name: "market",
Help: "Access market data and information for Pactus",
Args: nil,
AppIDs: entity.AllAppIDs(),
SubCommands: make([]*command.Command, 0),
Handler: nil,
TargetFlag: command.TargetMaskMainnet,
}
cmd := m.buildMarketCommand()
cmd.AppIDs = entity.AllAppIDs()
cmd.TargetFlag = command.TargetMaskMainnet

cmdMarket.AddSubCommand(subCmdPrice)
m.subCmdPrice.AppIDs = entity.AllAppIDs()
m.subCmdPrice.TargetFlag = command.TargetMaskMainnet

return cmdMarket
return cmd
}
12 changes: 12 additions & 0 deletions internal/engine/command/market/market.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
name: market
help: Commands for managing market
sub_commands:
- name: price
help: Shows the latest price of PAC coin across different markets
result_template: |
Xeggex Price: **{{.xeggexPrice}} USDT**
https://xeggex.com/market/PACTUS_USDT
Azbit Price: **{{.azbitPrice}} USDT**
https://azbit.com/exchange/PAC_USDT
18 changes: 5 additions & 13 deletions internal/engine/command/market/price.go
Original file line number Diff line number Diff line change
@@ -1,32 +1,24 @@
package market

import (
"fmt"
"strconv"
"strings"

"github.com/pagu-project/pagu/config"
"github.com/pagu-project/pagu/internal/engine/command"
"github.com/pagu-project/pagu/internal/entity"
"github.com/pagu-project/pagu/pkg/log"
)

func (m *MarketCmd) priceHandler(_ *entity.User, cmd *command.Command, _ map[string]string) command.CommandResult {
priceData, ok := m.priceCache.Get(config.PriceCacheKey)
if !ok {
return cmd.ErrorResult(fmt.Errorf("failed to get price from markets. please try again later"))
return cmd.RenderFailedTemplate("failed to get price from markets. please try again later")
}

bldr := strings.Builder{}
xeggexPrice, err := strconv.ParseFloat(priceData.XeggexPacToUSDT.LastPrice, 64)
if err == nil {
bldr.WriteString(fmt.Sprintf("Xeggex Price: %f USDT\n https://xeggex.com/market/PACTUS_USDT \n\n",
xeggexPrice))
if err != nil {
log.Error("unable to parse float", "error", err)
}

if priceData.AzbitPacToUSDT.Price > 0 {
bldr.WriteString(fmt.Sprintf("Azbit Price: %f USDT\n https://azbit.com/exchange/PAC_USDT \n\n",
priceData.AzbitPacToUSDT.Price))
}

return cmd.SuccessfulResult(bldr.String())
return cmd.RenderResultTemplate("xeggexPrice", xeggexPrice, "azbitPrice", priceData.AzbitPacToUSDT.Price)
}
4 changes: 2 additions & 2 deletions internal/generator/command.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type {{.Name}}SubCmds struct {
{{- end}}
}

func (c *{{.Name | title}}Cmd) buildSubCmds() *crowdfundSubCmds {
func (c *{{.Name | title}}Cmd) buildSubCmds() *{{.Name}}SubCmds {
{{- range .SubCommands }}
subCmd{{.Name | title}} := &command.Command{
Name: "{{.Name}}",
Expand Down Expand Up @@ -49,7 +49,7 @@ func (c *{{.Name | title}}Cmd) buildSubCmds() *crowdfundSubCmds {
}
{{- end }}

return &crowdfundSubCmds{
return &{{.Name}}SubCmds{
{{- $cmdName := .Name }}
{{- range .SubCommands }}
subCmd{{.Name | title}}: subCmd{{.Name | title}},
Expand Down

0 comments on commit d913e99

Please sign in to comment.