From da86f5baeb04bdf8d131b0e25f8134e1bc521ed5 Mon Sep 17 00:00:00 2001 From: Kay Date: Thu, 7 Dec 2023 11:21:39 +0000 Subject: [PATCH] chore: change referral logic --- config/config.go | 1 - discord/discord.go | 15 +++++++++------ discord/referral.go | 16 ++++++++++++++++ wallet/wallet.go | 32 -------------------------------- 4 files changed, 25 insertions(+), 39 deletions(-) diff --git a/config/config.go b/config/config.go index d586b449..4d017cea 100644 --- a/config/config.go +++ b/config/config.go @@ -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"` diff --git a/discord/discord.go b/discord/discord.go index 9f18370c..8ea2918a 100644 --- a/discord/discord.go +++ b/discord/discord.go @@ -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()) - } } } diff --git a/discord/referral.go b/discord/referral.go index 176b145e..ace3e6ef 100644 --- a/discord/referral.go +++ b/discord/referral.go @@ -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 +} diff --git a/wallet/wallet.go b/wallet/wallet.go index 7fc18b69..c860d21d 100644 --- a/wallet/wallet.go +++ b/wallet/wallet.go @@ -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)