package endpoints import ( "encoding/json" "fmt" "net/http" . "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 { Destination string `json:"destination"` Amount float64 `json:"amount"` } type DistributeRewardsRequest struct { RewardFundID uint `json:"rewardFundID"` Payments []RewardDistributionInfo `json:"payments"` Distribute bool `json:"distribute"` } func DistributeRewards(w http.ResponseWriter, r *http.Request) { var req DistributeRewardsRequest err := json.NewDecoder(r.Body).Decode(&req) if err != nil { panic("Could not decode body") } var fund RewardFund Db.Table("reward_funds").Where("id = ?", req.RewardFundID).Scan(&fund) var resp SuccessResponse resp.Success = false if req.Distribute { source := keypair.MustParseFull(fund.FundSecret) resp.Success, err = utils.SendAsset( source, nil, constructOperations( source, txnbuild.CreditAsset{ Code: fund.Asset, Issuer: fund.IssuerWallet, }, 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) if err != nil { panic("Could not deliver response") } } func constructOperations(sourceAccount *keypair.Full, asset txnbuild.CreditAsset, payments []RewardDistributionInfo) []txnbuild.Operation { var operations []txnbuild.Operation for _, payment := range payments { 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(), }) } return operations }