The backend for the project formerly known as signet, now known as beignet.
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

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