The backend for the project formerly known as signet, now known as beignet.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

distributerewards.go 2.3 KiB

пре 1 година
пре 1 година
пре 1 година
пре 1 година
пре 1 година
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. package endpoints
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net/http"
  6. . "github.com/imosed/signet/data"
  7. "github.com/imosed/signet/utils"
  8. "github.com/rs/zerolog/log"
  9. "github.com/stellar/go/keypair"
  10. "github.com/stellar/go/txnbuild"
  11. "github.com/stellar/go/xdr"
  12. )
  13. type RewardDistributionInfo struct {
  14. Destination string `json:"destination"`
  15. Amount float64 `json:"amount"`
  16. }
  17. type DistributeRewardsRequest struct {
  18. RewardFundID uint `json:"rewardFundID"`
  19. Payments []RewardDistributionInfo `json:"payments"`
  20. Distribute bool `json:"distribute"`
  21. }
  22. func DistributeRewards(w http.ResponseWriter, r *http.Request) {
  23. var req DistributeRewardsRequest
  24. err := json.NewDecoder(r.Body).Decode(&req)
  25. if err != nil {
  26. panic("Could not decode body")
  27. }
  28. var fund RewardFund
  29. Db.Table("reward_funds").Where("id = ?", req.RewardFundID).Scan(&fund)
  30. var resp SuccessResponse
  31. resp.Success = false
  32. if req.Distribute {
  33. source := keypair.MustParseFull(fund.FundSecret)
  34. resp.Success, err = utils.SendAsset(
  35. source,
  36. nil,
  37. constructOperations(
  38. source,
  39. txnbuild.CreditAsset{
  40. Code: fund.Asset,
  41. Issuer: fund.IssuerWallet,
  42. },
  43. req.Payments),
  44. )
  45. if err != nil {
  46. log.Error().Err(err).Msg("Could not distribute asset")
  47. }
  48. if resp.Success {
  49. var receivedWallets []string
  50. for _, payment := range req.Payments {
  51. receivedWallets = append(receivedWallets, payment.Destination)
  52. }
  53. Db.Table("contributions").Where("wallet in ? and reward_fund_id = ? and not submitted = true", receivedWallets, req.RewardFundID).Updates(Contribution{Received: true})
  54. }
  55. }
  56. err = json.NewEncoder(w).Encode(resp)
  57. if err != nil {
  58. panic("Could not deliver response")
  59. }
  60. }
  61. func constructOperations(sourceAccount *keypair.Full, asset txnbuild.CreditAsset, payments []RewardDistributionInfo) []txnbuild.Operation {
  62. var operations []txnbuild.Operation
  63. for _, payment := range payments {
  64. operations = append(operations, &txnbuild.CreateClaimableBalance{
  65. Destinations: []txnbuild.Claimant{
  66. {
  67. Destination: payment.Destination,
  68. Predicate: xdr.ClaimPredicate{},
  69. },
  70. },
  71. Amount: fmt.Sprintf("%f", payment.Amount),
  72. Asset: asset,
  73. SourceAccount: sourceAccount.Address(),
  74. })
  75. }
  76. return operations
  77. }