forked from pagu-project/pagu
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from akbariandev/feat/vocher
feat(voucher): update pactus repo
- Loading branch information
Showing
192 changed files
with
4,961 additions
and
3,856 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.