Skip to content

Commit

Permalink
chore: change referral logic
Browse files Browse the repository at this point in the history
  • Loading branch information
kehiy committed Dec 7, 2023
1 parent 285d8d5 commit da86f5b
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 39 deletions.
1 change: 0 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ type Config struct {
Servers []string `json:"servers"`
FaucetAddress string `json:"faucet_address"`
FaucetAmount float64 `json:"faucet_amount"`
ReferralRewardAmount float64 `json:"referral_reward_amount"` // who is a referral
ReferralerRewardAmount float64 `json:"referraler_reward_amount"` // who get faucet
ValidatorDataPath string `json:"validator_data_path"`
ReferralDataPath string `json:"referral_data_path"`
Expand Down
15 changes: 9 additions & 6 deletions discord/discord.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,21 +236,24 @@ func (b *Bot) messageHandler(s *discordgo.Session, m *discordgo.MessageCreate) {
}

amount := b.cfg.FaucetAmount + b.cfg.ReferralerRewardAmount
// send faucet
ok := b.referralStore.AddReferraler(referralCode)
if !ok {
_, _ = s.ChannelMessageSendReply(m.ChannelID, "Can't update referral data. please try again later.", m.Reference())
return
}

// send faucet
txHashFaucet := b.faucetWallet.BondTransaction(pubKey, trimmedAddress, amount)
txHashReferral := b.faucetWallet.TransferTransaction(referralAddr, b.cfg.ReferralRewardAmount)

if txHashFaucet != "" && txHashReferral != "" {
if txHashFaucet != "" {
err := b.store.SetData(peerID, trimmedAddress, m.Author.Username, m.Author.ID, amount)
if err != nil {
log.Printf("error saving faucet information: %v\n", err)
}

msg := p.Sprintf("%v %.4f test PACs is staked to %v successfully!\nReferral Data:\nTX ID:%v\nReferral Address:%v",
m.Author.Username, amount, trimmedAddress, txHashReferral, referralAddr)
msg := p.Sprintf("%v %.4f test PACs is staked to %v successfully!\nReferral Data:\nReferral Address:%v",
m.Author.Username, amount, trimmedAddress, referralAddr)
_, _ = s.ChannelMessageSendReply(m.ChannelID, msg, m.Reference())

}
}
}
Expand Down
16 changes: 16 additions & 0 deletions discord/referral.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,19 @@ func (rs *ReferralStore) GetData(code string) (*Referral, bool) {
referral := entry.(*Referral)
return referral, true
}

// AddReferraler add one new referraller.
func (rs *ReferralStore) AddReferraler(code string) bool {
entry, found := rs.syncMap.Load(code)
if !found {
return false
}

if found {
referral := entry.(*Referral)
referral.ReferralCounts++
rs.syncMap.Store(referral.ReferralCode, referral)
return true
}
return false
}
32 changes: 0 additions & 32 deletions wallet/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,38 +72,6 @@ func (w *Wallet) BondTransaction(pubKey, toAddress string, amount float64) strin
return res // return transaction hash
}

func (w *Wallet) TransferTransaction(toAddress string, amount float64) string {
opts := []pwallet.TxOption{
//nolint
pwallet.OptionFee(int64(1)), // TODO
pwallet.OptionMemo("Referral Reward from PactusBot"),
}
tx, err := w.wallet.MakeTransferTx(w.address, toAddress, util.CoinToChange(amount), opts...)
if err != nil {
log.Printf("error creating transfer transaction: %v", err)
return ""
}
// sign transaction
err = w.wallet.SignTransaction(w.password, tx)
if err != nil {
log.Printf("error signing transfer transaction: %v", err)
return ""
}

// broadcast transaction
res, err := w.wallet.BroadcastTransaction(tx)
if err != nil {
log.Printf("error broadcasting transfer transaction: %v", err)
return ""
}

err = w.wallet.Save()
if err != nil {
log.Printf("error saving wallet transaction history: %v", err)
}
return res // return transaction hash
}

func (w *Wallet) GetBalance() *Balance {
balance := &Balance{Available: 0, Staked: 0}
b, err := w.wallet.Balance(w.address)
Expand Down

0 comments on commit da86f5b

Please sign in to comment.