The backend for the project formerly known as signet, now known as beignet.
Você não pode selecionar mais de 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.

107 linhas
2.7 KiB

  1. package utils
  2. import (
  3. "fmt"
  4. "github.com/imosed/signet/client"
  5. . "github.com/imosed/signet/data"
  6. "github.com/stellar/go/clients/horizonclient"
  7. "github.com/stellar/go/keypair"
  8. "github.com/stellar/go/network"
  9. "github.com/stellar/go/protocols/horizon"
  10. "github.com/stellar/go/txnbuild"
  11. "github.com/stellar/go/xdr"
  12. "gorm.io/gorm/clause"
  13. )
  14. func SubmitGroupFund(fundID uint) (bool, error) {
  15. var fund RewardFund
  16. Db.Preload(clause.Associations).Find(&fund, fundID)
  17. var err error
  18. source := keypair.MustParseFull(fund.FundSecret)
  19. sourceReq := horizonclient.AccountRequest{AccountID: source.Address()}
  20. var sourceAcct horizon.Account
  21. sourceAcct, err = client.SignetClient.AccountDetail(sourceReq)
  22. offerReq := horizonclient.OfferRequest{
  23. Seller: fund.SellingWallet,
  24. Selling: fmt.Sprintf("%s:%s", fund.Asset, fund.IssuerWallet),
  25. Order: horizonclient.OrderDesc,
  26. }
  27. if err, ok := FindOffer(offerReq, &fund); !ok {
  28. return false, err
  29. }
  30. var currentContributions = fund.Contributions
  31. var submissionAmount = sumContributions(currentContributions)
  32. tr := Db.Begin()
  33. tr.Table("contributions").
  34. Where("reward_fund_id = ? and submitted is null or submitted = false", fundID).
  35. Updates(Contribution{Submitted: true})
  36. var tx *txnbuild.Transaction
  37. tx, err = txnbuild.NewTransaction(
  38. txnbuild.TransactionParams{
  39. SourceAccount: &sourceAcct,
  40. IncrementSequenceNum: true,
  41. Operations: []txnbuild.Operation{
  42. &txnbuild.ChangeTrust{
  43. Line: txnbuild.CreditAsset{
  44. Code: fund.Asset,
  45. Issuer: fund.IssuerWallet,
  46. }.MustToChangeTrustAsset(),
  47. SourceAccount: fund.FundWallet,
  48. },
  49. &txnbuild.ManageBuyOffer{
  50. Selling: txnbuild.NativeAsset{},
  51. Buying: txnbuild.CreditAsset{
  52. Code: fund.Asset,
  53. Issuer: fund.IssuerWallet,
  54. },
  55. Amount: fmt.Sprintf("%f", submissionAmount),
  56. Price: xdr.Price{N: 1, D: xdr.Int32(fund.Price)},
  57. OfferID: 0,
  58. SourceAccount: fund.FundWallet,
  59. },
  60. },
  61. BaseFee: txnbuild.MinBaseFee,
  62. Memo: txnbuild.Memo(nil),
  63. Preconditions: txnbuild.Preconditions{
  64. TimeBounds: txnbuild.NewInfiniteTimeout(), // TODO: change from infinite
  65. },
  66. })
  67. if err != nil {
  68. tr.Rollback()
  69. return false, err
  70. }
  71. tx, err = tx.Sign(network.TestNetworkPassphrase, source)
  72. if err != nil {
  73. tr.Rollback()
  74. return false, err
  75. }
  76. _, err = client.SignetClient.SubmitTransaction(tx)
  77. if err != nil {
  78. tr.Rollback()
  79. return false, err
  80. }
  81. tr.Commit()
  82. return true, nil
  83. }
  84. func sumContributions(contributions []Contribution) float64 {
  85. var total float64 = 0
  86. for _, contribution := range contributions {
  87. if !contribution.Submitted {
  88. total += contribution.Amount
  89. }
  90. }
  91. return total
  92. }