|
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- package endpoints
-
- import (
- "encoding/json"
- "fmt"
- "net/http"
-
- . "github.com/imosed/signet/data"
- "github.com/imosed/signet/utils"
- "github.com/stellar/go/keypair"
- "github.com/stellar/go/txnbuild"
- )
-
- 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),
- )
- }
-
- 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.Payment{
- Destination: payment.Destination,
- Amount: fmt.Sprintf("%f", payment.Amount),
- Asset: asset,
- SourceAccount: sourceAccount.Address(),
- })
- }
- return operations
- }
|