Skip to content

Commit

Permalink
feat(voucher): create command
Browse files Browse the repository at this point in the history
  • Loading branch information
kehiy committed Jul 1, 2024
1 parent 42d9f96 commit 718ed0c
Show file tree
Hide file tree
Showing 64 changed files with 5,847 additions and 697 deletions.
22 changes: 10 additions & 12 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,30 @@ module github.com/pagu-project/Pagu
go 1.22.2

require (
github.com/PaulSonOfLars/gotgbot/v2 v2.0.0-rc.26
github.com/PaulSonOfLars/gotgbot/v2 v2.0.0-rc.27
github.com/bwmarrin/discordgo v0.28.1
github.com/labstack/echo/v4 v4.12.0
github.com/pactus-project/pactus v1.3.0
github.com/spf13/cobra v1.8.1
github.com/stretchr/testify v1.9.0
google.golang.org/grpc v1.64.0
gorm.io/driver/mysql v1.5.6
gorm.io/driver/mysql v1.5.7
gorm.io/gorm v1.25.10
)

require (
github.com/go-sql-driver/mysql v1.7.0 // indirect
github.com/google/pprof v0.0.0-20240622144329-c177fd99eaa9 // indirect
github.com/ipfs/boxo v0.21.0 // indirect
github.com/prometheus/common v0.54.0 // indirect
github.com/quic-go/quic-go v0.45.1 // indirect
)

require (
filippo.io/edwards25519 v1.1.0 // indirect
github.com/NathanBaulch/protoc-gen-cobra v1.2.1 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fxamacker/cbor/v2 v2.7.0 // indirect
github.com/go-sql-driver/mysql v1.8.1 // indirect
github.com/google/pprof v0.0.0-20240622144329-c177fd99eaa9 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/gorilla/websocket v1.5.3 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.20.0 // indirect
github.com/iancoleman/strcase v0.3.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/ipfs/boxo v0.21.0 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/kilic/bls12-381 v0.1.0 // indirect
Expand All @@ -40,6 +36,8 @@ require (
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/common v0.54.0 // indirect
github.com/quic-go/quic-go v0.45.1 // indirect
github.com/rogpeppe/go-internal v1.12.0 // indirect
github.com/rs/zerolog v1.33.0
github.com/spf13/cast v1.6.0 // indirect
Expand All @@ -54,8 +52,8 @@ require (
golang.org/x/net v0.26.0 // indirect
golang.org/x/sys v0.21.0 // indirect
golang.org/x/text v0.16.0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240624140628-dc46fd24d27d // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240624140628-dc46fd24d27d // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240701130421-f6361c86f094 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240701130421-f6361c86f094 // indirect
google.golang.org/protobuf v1.34.2
gopkg.in/natefinch/lumberjack.v2 v2.2.1
gopkg.in/yaml.v3 v3.0.1
Expand Down
21 changes: 12 additions & 9 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA=
filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4=
github.com/NathanBaulch/protoc-gen-cobra v1.2.1 h1:BOqX9glwicbqDJDGndMnhHhx8psGTSjGdZzRDY1a7A8=
github.com/NathanBaulch/protoc-gen-cobra v1.2.1/go.mod h1:ZLPLEPQgV3jP3a7IEp+xxYPk8tF4lhY9ViV0hn6K3iA=
github.com/PaulSonOfLars/gotgbot/v2 v2.0.0-rc.26 h1:u1ZGYo3ml5ouOI9rCIknF0JO9REeKb69E6drCwZgZdc=
github.com/PaulSonOfLars/gotgbot/v2 v2.0.0-rc.26/go.mod h1:kL1v4iIjlalwm3gCYGvF4NLa3hs+aKEfRkNJvj4aoDU=
github.com/PaulSonOfLars/gotgbot/v2 v2.0.0-rc.27 h1:rOlGzmYC3jPVPLVLWKMiiYuePQ6MV8Cyw5qJYBoMnkY=
github.com/PaulSonOfLars/gotgbot/v2 v2.0.0-rc.27/go.mod h1:kL1v4iIjlalwm3gCYGvF4NLa3hs+aKEfRkNJvj4aoDU=
github.com/beevik/ntp v1.4.3 h1:PlbTvE5NNy4QHmA4Mg57n7mcFTmr1W1j3gcK7L1lqho=
github.com/beevik/ntp v1.4.3/go.mod h1:Unr8Zg+2dRn7d8bHFuehIMSvvUYssHMxW3Q5Nx4RW5Q=
github.com/benbjohnson/clock v1.3.5 h1:VvXlSJBzZpA/zum6Sj74hxwYI2DIxRWuNIoXAzHZz5o=
Expand Down Expand Up @@ -40,8 +42,9 @@ github.com/go-logr/logr v1.4.2 h1:6pFjapn8bFcIbiKo3XT4j/BhANplGihG6tvd+8rYgrY=
github.com/go-logr/logr v1.4.2/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
github.com/go-sql-driver/mysql v1.7.0 h1:ueSltNNllEqE3qcWBTD0iQd3IpL/6U+mJxLkazJ7YPc=
github.com/go-sql-driver/mysql v1.7.0/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI=
github.com/go-sql-driver/mysql v1.8.1 h1:LedoTUt/eveggdHS9qUFC1EFSa8bU2+1pZjSRpvNJ1Y=
github.com/go-sql-driver/mysql v1.8.1/go.mod h1:wEBSXgmK//2ZFJyE+qWnIsVGmvmEKlqwuVSjsCm7DZg=
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 h1:tfuBGBXKqDEevZMzYi5KSi8KkcZtzBcTgAUUtapy0OI=
github.com/go-task/slim-sprig/v3 v3.0.0 h1:sUs3vkvUymDpBKi3qH1YSqBQk9+9D/8M2mN1vB6EwHI=
github.com/go-task/slim-sprig/v3 v3.0.0/go.mod h1:W848ghGpv3Qj3dhTPRyJypKRiqCdHZiAzKg9hl15HA8=
Expand Down Expand Up @@ -337,10 +340,10 @@ golang.org/x/tools v0.22.0 h1:gqSGLZqv+AI9lIQzniJ0nZDRG5GBPsSi+DRNHWNz6yA=
golang.org/x/tools v0.22.0/go.mod h1:aCwcsjqvq7Yqt6TNyX7QMU2enbQ/Gt0bo6krSeEri+c=
gonum.org/v1/gonum v0.15.0 h1:2lYxjRbTYyxkJxlhC+LvJIx3SsANPdRybu1tGj9/OrQ=
gonum.org/v1/gonum v0.15.0/go.mod h1:xzZVBJBtS+Mz4q0Yl2LJTk+OxOg4jiXZ7qBoM0uISGo=
google.golang.org/genproto/googleapis/api v0.0.0-20240624140628-dc46fd24d27d h1:Aqf0fiIdUQEj0Gn9mKFFXoQfTTEaNopWpfVyYADxiSg=
google.golang.org/genproto/googleapis/api v0.0.0-20240624140628-dc46fd24d27d/go.mod h1:Od4k8V1LQSizPRUK4OzZ7TBE/20k+jPczUDAEyvn69Y=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240624140628-dc46fd24d27d h1:k3zyW3BYYR30e8v3x0bTDdE9vpYFjZHK+HcyqkrppWk=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240624140628-dc46fd24d27d/go.mod h1:Ue6ibwXGpU+dqIcODieyLOcgj7z8+IcskoNIgZxtrFY=
google.golang.org/genproto/googleapis/api v0.0.0-20240701130421-f6361c86f094 h1:0+ozOGcrp+Y8Aq8TLNN2Aliibms5LEzsq99ZZmAGYm0=
google.golang.org/genproto/googleapis/api v0.0.0-20240701130421-f6361c86f094/go.mod h1:fJ/e3If/Q67Mj99hin0hMhiNyCRmt6BQ2aWIJshUSJw=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240701130421-f6361c86f094 h1:BwIjyKYGsK9dMCBOorzRri8MQwmi7mT9rGHsCEinZkA=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240701130421-f6361c86f094/go.mod h1:Ue6ibwXGpU+dqIcODieyLOcgj7z8+IcskoNIgZxtrFY=
google.golang.org/grpc v1.64.0 h1:KH3VH9y/MgNQg1dE7b3XfVK0GsPSIzJwdF617gUSbvY=
google.golang.org/grpc v1.64.0/go.mod h1:oxjF8E3FBnjp+/gVFYdWacaLDx9na1aqy9oovLpxQYg=
google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg=
Expand All @@ -352,8 +355,8 @@ gopkg.in/natefinch/lumberjack.v2 v2.2.1 h1:bBRl1b0OH9s/DuPhuXpNl+VtCaJXFZ5/uEFST
gopkg.in/natefinch/lumberjack.v2 v2.2.1/go.mod h1:YD8tP3GAjkrDg1eZH7EGmyESg/lsYskCTPBJVb9jqSc=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gorm.io/driver/mysql v1.5.6 h1:Ld4mkIickM+EliaQZQx3uOJDJHtrd70MxAUqWqlx3Y8=
gorm.io/driver/mysql v1.5.6/go.mod h1:sEtPWMiqiN1N1cMXoXmBbd8C6/l+TESwriotuRRpkDM=
gorm.io/driver/mysql v1.5.7 h1:MndhOPYOfEp2rHKgkZIhJ16eVUIRf2HmzgoPmh7FCWo=
gorm.io/driver/mysql v1.5.7/go.mod h1:sEtPWMiqiN1N1cMXoXmBbd8C6/l+TESwriotuRRpkDM=
gorm.io/gorm v1.25.7/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8=
gorm.io/gorm v1.25.10 h1:dQpO+33KalOA+aFYGlK+EfxcI5MbO7EP2yYygwh9h+s=
gorm.io/gorm v1.25.10/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8=
Expand Down
9 changes: 3 additions & 6 deletions internal/engine/command/calculator/calculator.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ func NewCalculator(clientMgr *client.Mgr) Calculator {
func (bc *Calculator) GetCommand() command.Command {
subCmdCalcReward := command.Command{
Name: CalcRewardCommandName,
Desc: "Calculate how many PAC coins you will earn with your validator stake",
Help: "Provide a stake amount between 1 to 100, please avoid using float numbers like: 1.9 or PAC suffix",
Help: "Calculate how many PAC coins you will earn with your validator stake",
Args: []command.Args{
{
Name: "stake",
Expand All @@ -47,8 +46,7 @@ func (bc *Calculator) GetCommand() command.Command {

subCmdCalcFee := command.Command{
Name: CalcFeeCommandName,
Desc: "Calculate fee of a transaction with providing amount",
Help: "Provide your amount in PAC, please avoid using float numbers like: 1.9 or PAC suffix",
Help: "Calculate fee of a transaction with providing amount",
Args: []command.Args{
{
Name: "amount",
Expand All @@ -63,8 +61,7 @@ func (bc *Calculator) GetCommand() command.Command {

cmdBlockchain := command.Command{
Name: CommandName,
Desc: "Calculator information and tools",
Help: "",
Help: "Calculator information and tools",
Args: nil,
AppIDs: entity.AllAppIDs(),
SubCommands: make([]command.Command, 0),
Expand Down
11 changes: 5 additions & 6 deletions internal/engine/command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ type Command struct {
Emoji string
Color string
Name string
Desc string
Help string
Args []Args //! should be nil for commands.
AppIDs []entity.AppID
Expand All @@ -49,7 +48,7 @@ type CommandResult struct {
func (cmd *Command) SuccessfulResult(message string, a ...interface{}) CommandResult {
return CommandResult{
Color: cmd.Color,
Title: fmt.Sprintf("%v %v", cmd.Desc, cmd.Emoji),
Title: fmt.Sprintf("%v %v", cmd.Help, cmd.Emoji),
Message: fmt.Sprintf(message, a...),
Successful: true,
}
Expand All @@ -58,7 +57,7 @@ func (cmd *Command) SuccessfulResult(message string, a ...interface{}) CommandRe
func (cmd *Command) FailedResult(message string, a ...interface{}) CommandResult {
return CommandResult{
Color: cmd.Color,
Title: fmt.Sprintf("%v %v", cmd.Desc, cmd.Emoji),
Title: fmt.Sprintf("%v %v", cmd.Help, cmd.Emoji),
Message: fmt.Sprintf(message, a...),
Successful: false,
}
Expand All @@ -71,7 +70,7 @@ func (cmd *Command) ErrorResult(err error) CommandResult {
func (cmd *Command) HelpResult() CommandResult {
return CommandResult{
Color: cmd.Color,
Title: fmt.Sprintf("%v %v", cmd.Desc, cmd.Emoji),
Title: fmt.Sprintf("%v %v", cmd.Help, cmd.Emoji),
Message: cmd.HelpMessage(),
Successful: false,
}
Expand Down Expand Up @@ -106,7 +105,7 @@ func (cmd *Command) HelpMessage() string {
help := cmd.Help
help += "\n\nAvailable commands:\n"
for _, sc := range cmd.SubCommands {
help += fmt.Sprintf(" %-12s %s\n", sc.Name, sc.Desc)
help += fmt.Sprintf(" %-12s %s\n", sc.Name, sc.Help)
}
return help
}
Expand All @@ -122,7 +121,7 @@ func (cmd *Command) AddSubCommand(subCmd Command) {
func (cmd *Command) AddHelpSubCommand() {
helpCmd := Command{
Name: "help",
Desc: fmt.Sprintf("Help for %v command", cmd.Name),
Help: fmt.Sprintf("Help for %v command", cmd.Name),
AppIDs: entity.AllAppIDs(),
Handler: func(_ Command, _ entity.AppID, _ string, _ ...string) CommandResult {
return cmd.SuccessfulResult(cmd.HelpMessage())
Expand Down
6 changes: 2 additions & 4 deletions internal/engine/command/market/market.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ func NewMarket(clientMgr *client.Mgr, priceCache cache.Cache[string, entity.Pric
func (m *Market) GetCommand() command.Command {
subCmdPrice := command.Command{
Name: PriceCommandName,
Desc: "Shows the last price of PAC coin on the markets",
Help: "",
Help: "Shows the last price of PAC coin on the markets",
Args: []command.Args{},
SubCommands: nil,
AppIDs: entity.AllAppIDs(),
Expand All @@ -38,8 +37,7 @@ func (m *Market) GetCommand() command.Command {

cmdMarket := command.Command{
Name: CommandName,
Desc: "Blockchain data and information",
Help: "",
Help: "Blockchain data and information",
Args: nil,
AppIDs: entity.AllAppIDs(),
SubCommands: make([]command.Command, 0),
Expand Down
3 changes: 1 addition & 2 deletions internal/engine/command/market/price_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ func setup() (Market, command.Command) {

return m, command.Command{
Name: PriceCommandName,
Desc: "Shows the last price of PAC coin on the markets",
Help: "",
Help: "Shows the last price of PAC coin on the markets",
Args: []command.Args{},
SubCommands: nil,
AppIDs: entity.AllAppIDs(),
Expand Down
12 changes: 4 additions & 8 deletions internal/engine/command/network/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,7 @@ type NetStatus struct {
func (n *Network) GetCommand() command.Command {
subCmdNodeInfo := command.Command{
Name: NodeInfoCommandName,
Desc: "View the information of a node",
Help: "Provide your validator address on the specific node to get the validator and node info",
Help: "View the information of a node",
Args: []command.Args{
{
Name: "validator_address",
Expand All @@ -78,8 +77,7 @@ func (n *Network) GetCommand() command.Command {

subCmdHealth := command.Command{
Name: HealthCommandName,
Desc: "Checking network health status",
Help: "",
Help: "Checking network health status",
Args: []command.Args{},
SubCommands: nil,
AppIDs: entity.AllAppIDs(),
Expand All @@ -88,8 +86,7 @@ func (n *Network) GetCommand() command.Command {

subCmdStatus := command.Command{
Name: StatusCommandName,
Desc: "Network statistics",
Help: "",
Help: "Network statistics",
Args: []command.Args{},
SubCommands: nil,
AppIDs: entity.AllAppIDs(),
Expand All @@ -98,8 +95,7 @@ func (n *Network) GetCommand() command.Command {

cmdNetwork := command.Command{
Name: CommandName,
Desc: "Network related commands",
Help: "",
Help: "Network related commands",
Args: nil,
AppIDs: entity.AllAppIDs(),
SubCommands: make([]command.Command, 0),
Expand Down
2 changes: 1 addition & 1 deletion internal/engine/command/phoenix/faucet.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func (pt *Phoenix) faucetHandler(cmd command.Command, _ entity.AppID, _ string,
return cmd.FailedResult("Uh, you used your share of faucets today!")
}

txID, err := pt.wallet.TransferTransaction(toAddr, "Phoenix Testnet Pagu PhoenixFaucet", int64(pt.faucetAmount)) //! define me on config?
txID, err := pt.wallet.TransferTransaction(toAddr, "Phoenix Testnet Pagu PhoenixFaucet", int64(pt.faucetAmount))
if err != nil {
return cmd.ErrorResult(err)
}
Expand Down
1 change: 0 additions & 1 deletion internal/engine/command/phoenix/faucet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ func setup() (Phoenix, command.Command) {

return p, command.Command{
Name: FaucetCommandName,
Desc: "",
Help: "",
Args: []command.Args{},
SubCommands: nil,
Expand Down
9 changes: 3 additions & 6 deletions internal/engine/command/phoenix/phoenix.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ func (pt *Phoenix) GetCommand() command.Command {

subCmdStatus := command.Command{
Name: StatusCommandName,
Desc: "Phoenix test-network statistics",
Help: "",
Help: "Phoenix test-network statistics",
Args: []command.Args{},
SubCommands: nil,
AppIDs: entity.AllAppIDs(),
Expand All @@ -52,8 +51,7 @@ func (pt *Phoenix) GetCommand() command.Command {

subCmdFaucet := command.Command{
Name: FaucetCommandName,
Desc: fmt.Sprintf("Get %d tPAC Coins on Phoenix Testnet for Testing your code or project", pt.faucetAmount),
Help: "There is a limit that you can only get faucets 1 time per day with each user ID and address",
Help: fmt.Sprintf("Get %d tPAC Coins on Phoenix Testnet for Testing your code or project", pt.faucetAmount),
Args: []command.Args{
{
Name: "address",
Expand All @@ -69,8 +67,7 @@ func (pt *Phoenix) GetCommand() command.Command {

cmdPhoenix := command.Command{
Name: CommandName,
Desc: "Phoenix Testnet tools and utils for developers",
Help: "",
Help: "Phoenix Testnet tools and utils for developers",
Args: nil,
AppIDs: entity.AllAppIDs(),
SubCommands: make([]command.Command, 0),
Expand Down
6 changes: 6 additions & 0 deletions internal/engine/command/voucher/claim.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package voucher

import (
"errors"
"time"

"github.com/pagu-project/Pagu/internal/engine/command"
"github.com/pagu-project/Pagu/internal/entity"
Expand All @@ -18,6 +19,11 @@ func (v *Voucher) claimHandler(cmd command.Command, _ entity.AppID, _ string, ar
return cmd.ErrorResult(errors.New("voucher code is not valid"))
}

now := time.Now().Month()
if voucher.CreatedAt.Month() >= (now + time.Month(voucher.ValidMonths)) {
return cmd.ErrorResult(errors.New("voucher is expired"))
}

if len(voucher.TxHash) > 0 {
return cmd.ErrorResult(errors.New("voucher code claimed before"))
}
Expand Down
49 changes: 49 additions & 0 deletions internal/engine/command/voucher/create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package voucher

import (
"strconv"

"github.com/pagu-project/Pagu/internal/engine/command"
"github.com/pagu-project/Pagu/internal/entity"
)

func (v *Voucher) createHandler(cmd command.Command, _ entity.AppID, callerID string, args ...string) command.CommandResult {
//! Admin only check

cID, err := strconv.Atoi(callerID)
if err != nil {
return cmd.ErrorResult(err)
}

recipient := args[0]
description := args[1]
validMonths := args[2]
amount := args[3]
discordID := args[4]
code := args[5]

expireMonths, err := strconv.Atoi(validMonths)
if err != nil {
return cmd.ErrorResult(err)
}

intAmount, err := strconv.Atoi(amount)
if err != nil {
return cmd.ErrorResult(err)
}

err = v.db.AddVoucher(&entity.Voucher{
Creator: uint(cID),
Code: code,
Desc: description,
DiscordID: discordID,
Recipient: recipient,
ValidMonths: uint(expireMonths),
Amount: uint(intAmount),
})
if err != nil {
return cmd.ErrorResult(err)
}

return cmd.SuccessfulResult("Voucher crated successfully!")
}
Loading

0 comments on commit 718ed0c

Please sign in to comment.