Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: define prototype for market command #278

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {} +
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel this line is not applied.


###
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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One new line is missed here.

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
Loading