Skip to content

Commit

Permalink
Merge pull request #13 from akbariandev/feat/vocher
Browse files Browse the repository at this point in the history
feat(voucher): update pactus repo
  • Loading branch information
akbariandev authored Jun 24, 2024
2 parents 39e1f3d + e7e1933 commit fff40ad
Show file tree
Hide file tree
Showing 192 changed files with 4,961 additions and 3,856 deletions.
3 changes: 1 addition & 2 deletions cmd/discord/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ func runCommand(parentCmd *cobra.Command) {
botEngine.RegisterAllCommands()
botEngine.Start()

discordBot, err := discord.NewDiscordBot(botEngine, configs.DiscordBot.Token,
configs.DiscordBot)
discordBot, err := discord.NewDiscordBot(botEngine, configs.DiscordBot.Token, configs.DiscordBot)
pCmd.ExitOnError(cmd, err)

err = discordBot.Start()
Expand Down
41 changes: 22 additions & 19 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,52 +6,55 @@ require (
github.com/PaulSonOfLars/gotgbot/v2 v2.0.0-rc.26
github.com/bwmarrin/discordgo v0.28.1
github.com/labstack/echo/v4 v4.12.0
github.com/pactus-project/pactus v1.1.4
github.com/spf13/cobra v1.8.0
github.com/pactus-project/pactus v1.2.0
github.com/spf13/cobra v1.8.1
github.com/stretchr/testify v1.9.0
google.golang.org/grpc v1.63.2
google.golang.org/grpc v1.64.0
gorm.io/driver/mysql v1.5.6
gorm.io/gorm v1.25.10
)

require (
github.com/go-sql-driver/mysql v1.7.0 // indirect
github.com/google/pprof v0.0.0-20240409012703-83162a5b38cd // 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 (
github.com/NathanBaulch/protoc-gen-cobra v1.2.1 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.3.0 // indirect
github.com/fxamacker/cbor/v2 v2.6.0 // indirect
github.com/fxamacker/cbor/v2 v2.7.0 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/gorilla/websocket v1.5.1 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.19.1 // 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/go-cid v0.4.1 // 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
github.com/klauspost/cpuid/v2 v2.2.7 // indirect
github.com/klauspost/cpuid/v2 v2.2.8 // indirect
github.com/labstack/gommon v0.4.2 // indirect
github.com/libp2p/go-buffer-pool v0.1.0 // indirect
github.com/libp2p/go-libp2p v0.33.2
github.com/libp2p/go-libp2p v0.35.1
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/minio/sha256-simd v1.0.1 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/mr-tron/base58 v1.2.0 // indirect
github.com/multiformats/go-base32 v0.1.0 // indirect
github.com/multiformats/go-base36 v0.2.0 // indirect
github.com/multiformats/go-multiaddr v0.12.3 // indirect
github.com/multiformats/go-multiaddr v0.12.4 // indirect
github.com/multiformats/go-multibase v0.2.0 // indirect
github.com/multiformats/go-multicodec v0.9.0 // indirect
github.com/multiformats/go-multihash v0.2.3 // indirect
github.com/multiformats/go-varint v0.0.7 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rogpeppe/go-internal v1.12.0 // indirect
github.com/rs/zerolog v1.32.0
github.com/rs/zerolog v1.33.0
github.com/spaolacci/murmur3 v1.1.0 // indirect
github.com/spf13/cast v1.6.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
Expand All @@ -60,14 +63,14 @@ require (
github.com/valyala/fasttemplate v1.2.2 // indirect
github.com/x448/float16 v0.8.4 // indirect
go.uber.org/mock v0.4.0
golang.org/x/crypto v0.23.0 // indirect
golang.org/x/exp v0.0.0-20240506185415-9bf2ced13842 // indirect
golang.org/x/net v0.25.0 // indirect
golang.org/x/sys v0.20.0 // indirect
golang.org/x/text v0.15.0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240506185236-b8a5c65736ae // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240506185236-b8a5c65736ae // indirect
google.golang.org/protobuf v1.34.1
golang.org/x/crypto v0.24.0 // indirect
golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8 // indirect
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/protobuf v1.34.2
gopkg.in/natefinch/lumberjack.v2 v2.2.1
gopkg.in/yaml.v3 v3.0.1
lukechampine.com/blake3 v1.3.0 // indirect
Expand Down
187 changes: 113 additions & 74 deletions go.sum

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions internal/engine/command/network/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ type NetStatus struct {
NetworkName string
ConnectedPeersCount uint32
ValidatorsCount int32
TotalBytesSent uint32
TotalBytesReceived uint32
TotalBytesSent int64
TotalBytesReceived int64
CurrentBlockHeight uint32
TotalNetworkPower int64
TotalCommitteePower int64
Expand Down
46 changes: 46 additions & 0 deletions internal/engine/command/voucher/claim.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package voucher

import (
"errors"

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

func (v *Voucher) claimHandler(cmd command.Command, _ entity.AppID, _ string, args ...string) command.CommandResult {
code := args[0]
if len(code) != 8 {
return cmd.ErrorResult(errors.New("voucher code is not valid"))
}

voucher, err := v.db.GetVoucherByCode(code)
if err != nil {
return cmd.ErrorResult(errors.New("voucher code is not valid"))
}

if len(voucher.TxHash) > 0 {
return cmd.ErrorResult(errors.New("voucher code claimed before"))
}

address := args[1]
validatorInfo, err := v.clientManager.GetValidatorInfo(address)
if err != nil {
return cmd.ErrorResult(errors.New("bond error"))
}

pubKey := validatorInfo.GetValidator().GetPublicKey()
txHash, err := v.wallet.BondTransaction(pubKey, address, "Voucher claim for bond in validator", int64(voucher.Amount))
if err != nil {
return cmd.ErrorResult(err)
}

if txHash == "" {
return cmd.ErrorResult(errors.New("can't send bond transaction"))
}

if err = v.db.UpdateVoucherTx(voucher.ID, txHash); err != nil {
return cmd.ErrorResult(err)
}

return cmd.SuccessfulResult("Voucher claimed successfully: https://pacviewer.com/transaction/%s", txHash)
}
65 changes: 65 additions & 0 deletions internal/engine/command/voucher/voucher.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package voucher

import (
"github.com/pagu-project/Pagu/internal/engine/command"
"github.com/pagu-project/Pagu/internal/entity"
"github.com/pagu-project/Pagu/internal/repository"
"github.com/pagu-project/Pagu/pkg/client"
"github.com/pagu-project/Pagu/pkg/wallet"
)

const (
CommandName = "voucher"
ClaimCommandName = "claim"
HelpCommandName = "help"
)

type Voucher struct {
db *repository.DB
wallet *wallet.Wallet
clientManager *client.Mgr
}

func NewVoucher(db *repository.DB, wallet *wallet.Wallet, cli *client.Mgr) Voucher {
return Voucher{
db: db,
wallet: wallet,
clientManager: cli,
}
}

func (v *Voucher) GetCommand() command.Command {
subCmdClaim := command.Command{
Name: ClaimCommandName,
Desc: "Claim your voucher coins adn bond to validator",
Help: "",
Args: []command.Args{
{
Name: "code",
Desc: "voucher code",
Optional: false,
},
{
Name: "address",
Desc: "your pactus validator address",
Optional: false,
},
},
SubCommands: nil,
AppIDs: entity.AllAppIDs(),
Handler: v.claimHandler,
}

cmdVoucher := command.Command{
Name: CommandName,
Desc: "Voucher Commands",
Help: "",
Args: nil,
AppIDs: entity.AllAppIDs(),
SubCommands: make([]command.Command, 0),
Handler: nil,
}

cmdVoucher.AddSubCommand(subCmdClaim)
return cmdVoucher
}
10 changes: 8 additions & 2 deletions internal/engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"errors"
"time"

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

"github.com/pagu-project/Pagu/internal/engine/command/market"
"github.com/pagu-project/Pagu/internal/entity"
"github.com/pagu-project/Pagu/internal/job"
Expand Down Expand Up @@ -36,6 +38,7 @@ type BotEngine struct {
networkCmd network.Network
phoenixCmd phoenixtestnet.Phoenix
zealyCmd zealy.Zealy
voucherCmd voucher.Voucher
marketCmd market.Market
}

Expand Down Expand Up @@ -140,7 +143,8 @@ func newBotEngine(cm, ptcm *client2.Mgr, wallet *wallet.Wallet, phoenixWal *wall
netCmd := network.NewNetwork(ctx, cm)
bcCmd := calculator.NewCalculator(cm)
ptCmd := phoenixtestnet.NewPhoenix(phoenixWal, phoenixFaucetAmount, ptcm, *db)
zCmd := zealy.NewZealy(db, wallet)
zealyCmd := zealy.NewZealy(db, wallet)
voucherCmd := voucher.NewVoucher(db, wallet, cm)
marketCmd := market.NewMarket(cm, priceCache)

return &BotEngine{
Expand All @@ -152,7 +156,8 @@ func newBotEngine(cm, ptcm *client2.Mgr, wallet *wallet.Wallet, phoenixWal *wall
blockchainCmd: bcCmd,
phoenixCmd: ptCmd,
phoenixClientMgr: ptcm,
zealyCmd: zCmd,
zealyCmd: zealyCmd,
voucherCmd: voucherCmd,
marketCmd: marketCmd,
}
}
Expand All @@ -165,6 +170,7 @@ func (be *BotEngine) RegisterAllCommands() {
be.rootCmd.AddSubCommand(be.blockchainCmd.GetCommand())
be.rootCmd.AddSubCommand(be.networkCmd.GetCommand())
be.rootCmd.AddSubCommand(be.zealyCmd.GetCommand())
be.rootCmd.AddSubCommand(be.voucherCmd.GetCommand())
be.rootCmd.AddSubCommand(be.marketCmd.GetCommand())
be.rootCmd.AddSubCommand(be.phoenixCmd.GetCommand())

Expand Down
19 changes: 19 additions & 0 deletions internal/entity/voucher.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package entity

import (
"gorm.io/gorm"
)

type Voucher struct {
ID uint `gorm:"primaryKey;unique"`
Creator uint `gorm:"size:255"`
Code string `gorm:"size:8"`
Amount uint
TxHash string

gorm.Model
}

func (Voucher) TableName() string {
return "voucher"
}
2 changes: 2 additions & 0 deletions internal/repository/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ func NewDB(path string) (*DB, error) {

if !db.Migrator().HasTable(&entity.User{}) ||
!db.Migrator().HasTable(&entity.PhoenixFaucet{}) ||
!db.Migrator().HasTable(&entity.Voucher{}) ||
!db.Migrator().HasTable(&entity.ZealyUser{}) {
if err := db.AutoMigrate(
&entity.User{},
&entity.PhoenixFaucet{},
&entity.ZealyUser{},
&entity.Voucher{},
); err != nil {
return nil, MigrationError{
Message: err.Error(),
Expand Down
26 changes: 26 additions & 0 deletions internal/repository/voucher.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package repository

import (
"github.com/pagu-project/Pagu/internal/entity"
)

func (db *DB) GetVoucherByCode(code string) (entity.Voucher, error) {
var voucher entity.Voucher
err := db.Model(&entity.Voucher{}).Where("code = ?", code).First(&voucher).Error
if err != nil {
return entity.Voucher{}, err
}

return voucher, nil
}

func (db *DB) UpdateVoucherTx(id uint, txHash string) error {
tx := db.Model(&entity.Voucher{}).Where("id = ?", id).Update("tx_hash", txHash)
if tx.Error != nil {
return WriteError{
Message: tx.Error.Error(),
}
}

return nil
}
3 changes: 1 addition & 2 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ type Client struct {
}

func NewClient(endpoint string) (*Client, error) {
conn, err := grpc.Dial(endpoint,
grpc.WithTransportCredentials(insecure.NewCredentials()))
conn, err := grpc.NewClient(endpoint, grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
return nil, err
}
Expand Down
Loading

0 comments on commit fff40ad

Please sign in to comment.