The backend for the project formerly known as signet, now known as beignet.
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

distributerewards.go 1.7 KiB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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/stellar/go/keypair"
  9. "github.com/stellar/go/txnbuild"
  10. )
  11. type RewardDistributionInfo struct {
  12. Destination string `json:"destination"`
  13. Amount float64 `json:"amount"`
  14. }
  15. type DistributeRewardsRequest struct {
  16. RewardFundID uint `json:"rewardFundID"`
  17. Payments []RewardDistributionInfo `json:"payments"`
  18. Distribute bool `json:"distribute"`
  19. }
  20. func DistributeRewards(w http.ResponseWriter, r *http.Request) {
  21. var req DistributeRewardsRequest
  22. err := json.NewDecoder(r.Body).Decode(&req)
  23. if err != nil {
  24. panic("Could not decode body")
  25. }
  26. var fund RewardFund
  27. Db.Table("reward_funds").Where("id = ?", req.RewardFundID).Scan(&fund)
  28. var resp SuccessResponse
  29. resp.Success = false
  30. if req.Distribute {
  31. source := keypair.MustParseFull(fund.FundSecret)
  32. resp.Success, err = utils.SendAsset(
  33. source,
  34. nil,
  35. constructOperations(
  36. source,
  37. txnbuild.CreditAsset{
  38. Code: fund.Asset,
  39. Issuer: fund.IssuerWallet,
  40. },
  41. req.Payments),
  42. )
  43. }
  44. err = json.NewEncoder(w).Encode(resp)
  45. if err != nil {
  46. panic("Could not deliver response")
  47. }
  48. }
  49. func constructOperations(sourceAccount *keypair.Full, asset txnbuild.CreditAsset, payments []RewardDistributionInfo) []txnbuild.Operation {
  50. var operations []txnbuild.Operation
  51. for _, payment := range payments {
  52. operations = append(operations, &txnbuild.Payment{
  53. Destination: payment.Destination,
  54. Amount: fmt.Sprintf("%f", payment.Amount),
  55. Asset: asset,
  56. SourceAccount: sourceAccount.Address(),
  57. })
  58. }
  59. return operations
  60. }