From f31060c7e57f8ea973ed72e5e18a727dc9f712ee Mon Sep 17 00:00:00 2001 From: Jared Date: Mon, 30 Jan 2023 00:34:55 -0500 Subject: [PATCH] Fix a couple bugs and send claimable balance instead --- data/context.go | 1 + endpoints/distributerewards.go | 21 +++++++++++++++++++-- endpoints/getrewardfund.go | 2 +- main.go | 1 + 4 files changed, 22 insertions(+), 3 deletions(-) diff --git a/data/context.go b/data/context.go index 0b160eb..f694c91 100644 --- a/data/context.go +++ b/data/context.go @@ -59,6 +59,7 @@ type Contribution struct { Wallet string `json:"wallet"` Amount float64 `gorm:"type:decimal(19,7)" json:"amount"` Submitted bool `gorm:"type:boolean" json:"submitted"` + Received bool `gorm:"type:boolean" json:"received"` TransactionID string `json:"transactionID"` RewardFundID uint `json:"rewardFundID"` } diff --git a/endpoints/distributerewards.go b/endpoints/distributerewards.go index bc6da61..8236a9a 100644 --- a/endpoints/distributerewards.go +++ b/endpoints/distributerewards.go @@ -7,8 +7,10 @@ import ( . "github.com/imosed/signet/data" "github.com/imosed/signet/utils" + "github.com/rs/zerolog/log" "github.com/stellar/go/keypair" "github.com/stellar/go/txnbuild" + "github.com/stellar/go/xdr" ) type RewardDistributionInfo struct { @@ -48,6 +50,16 @@ func DistributeRewards(w http.ResponseWriter, r *http.Request) { }, req.Payments), ) + if err != nil { + log.Error().Err(err).Msg("Could not distribute asset") + } + if resp.Success { + var receivedWallets []string + for _, payment := range req.Payments { + receivedWallets = append(receivedWallets, payment.Destination) + } + Db.Table("contributions").Where("wallet in ? and reward_fund_id = ? and not submitted = true", receivedWallets, req.RewardFundID).Updates(Contribution{Received: true}) + } } err = json.NewEncoder(w).Encode(resp) @@ -59,8 +71,13 @@ func DistributeRewards(w http.ResponseWriter, r *http.Request) { func constructOperations(sourceAccount *keypair.Full, asset txnbuild.CreditAsset, payments []RewardDistributionInfo) []txnbuild.Operation { var operations []txnbuild.Operation for _, payment := range payments { - operations = append(operations, &txnbuild.Payment{ - Destination: payment.Destination, + operations = append(operations, &txnbuild.CreateClaimableBalance{ + Destinations: []txnbuild.Claimant{ + { + Destination: payment.Destination, + Predicate: xdr.ClaimPredicate{}, + }, + }, Amount: fmt.Sprintf("%f", payment.Amount), Asset: asset, SourceAccount: sourceAccount.Address(), diff --git a/endpoints/getrewardfund.go b/endpoints/getrewardfund.go index 1507c92..2f6ea2d 100644 --- a/endpoints/getrewardfund.go +++ b/endpoints/getrewardfund.go @@ -51,7 +51,7 @@ func GetRewardFund(resp http.ResponseWriter, req *http.Request) { baseQuery.Select("wallet, sum(amount) amount").Group("wallet").Order("wallet").Scan(&contribs.List) baseCount.Group("wallet").Count(&contribs.Total) } else { - baseQuery.Select("wallet, amount, created_at").Order("created_at desc").Scan(&contribs.List) + baseQuery.Select("wallet, amount, created_at").Order("created_at desc").Debug().Scan(&contribs.List) baseCount.Count(&contribs.Total) } diff --git a/main.go b/main.go index a7bbcd6..4a827eb 100644 --- a/main.go +++ b/main.go @@ -41,6 +41,7 @@ func main() { router.HandleFunc("/CreateRewardFund", endpoints.CreateRewardFund) router.HandleFunc("/CloseRewardFund", endpoints.CloseRewardFund) router.HandleFunc("/SubmitRewardFund", endpoints.SubmitFund) + router.HandleFunc("/DistributeRewardFund", endpoints.DistributeRewards) // router.HandleFunc("/SubmitFund", endpoints.SubmitFund) router.HandleFunc("/GetBalance", endpoints.GetBalance) router.HandleFunc("/Contribute", endpoints.Contribute)