|
| 1 | +package async |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "time" |
| 6 | + |
| 7 | + "github.com/newrelic/go-agent/v3/newrelic" |
| 8 | + "github.com/sirupsen/logrus" |
| 9 | + |
| 10 | + "github.com/code-payments/code-server/pkg/code/async" |
| 11 | + "github.com/code-payments/code-server/pkg/code/common" |
| 12 | + code_data "github.com/code-payments/code-server/pkg/code/data" |
| 13 | + "github.com/code-payments/code-server/pkg/code/data/currency" |
| 14 | + "github.com/code-payments/code-server/pkg/metrics" |
| 15 | + "github.com/code-payments/code-server/pkg/retry" |
| 16 | + "github.com/code-payments/code-server/pkg/retry/backoff" |
| 17 | + "github.com/code-payments/code-server/pkg/solana" |
| 18 | + "github.com/code-payments/code-server/pkg/solana/currencycreator" |
| 19 | + "github.com/code-payments/code-server/pkg/solana/token" |
| 20 | +) |
| 21 | + |
| 22 | +type reserveService struct { |
| 23 | + log *logrus.Entry |
| 24 | + data code_data.Provider |
| 25 | +} |
| 26 | + |
| 27 | +func NewReserveService(data code_data.Provider) async.Service { |
| 28 | + return &reserveService{ |
| 29 | + log: logrus.StandardLogger().WithField("service", "reserve"), |
| 30 | + data: data, |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +func (p *reserveService) Start(serviceCtx context.Context, interval time.Duration) error { |
| 35 | + for { |
| 36 | + _, err := retry.Retry( |
| 37 | + func() error { |
| 38 | + p.log.Trace("updating exchange rates") |
| 39 | + |
| 40 | + nr := serviceCtx.Value(metrics.NewRelicContextKey).(*newrelic.Application) |
| 41 | + m := nr.StartTransaction("async__currency_reserve_service") |
| 42 | + defer m.End() |
| 43 | + tracedCtx := newrelic.NewContext(serviceCtx, m) |
| 44 | + |
| 45 | + err := p.UpdateAllCurrencyReserves(tracedCtx) |
| 46 | + if err != nil { |
| 47 | + m.NoticeError(err) |
| 48 | + p.log.WithError(err).Warn("failed to process current rate data") |
| 49 | + } |
| 50 | + |
| 51 | + return err |
| 52 | + }, |
| 53 | + retry.NonRetriableErrors(context.Canceled), |
| 54 | + retry.BackoffWithJitter(backoff.BinaryExponential(time.Second), interval, 0.1), |
| 55 | + ) |
| 56 | + if err != nil { |
| 57 | + if err != context.Canceled { |
| 58 | + // Should not happen since only non-retriable error is context.Canceled |
| 59 | + p.log.WithError(err).Warn("unexpected error when processing current reserve data") |
| 60 | + } |
| 61 | + |
| 62 | + return err |
| 63 | + } |
| 64 | + |
| 65 | + select { |
| 66 | + case <-serviceCtx.Done(): |
| 67 | + return serviceCtx.Err() |
| 68 | + case <-time.After(interval): |
| 69 | + } |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +// todo: Don't hardcode Jeffy |
| 74 | +func (p *reserveService) UpdateAllCurrencyReserves(ctx context.Context) error { |
| 75 | + jeffyMintAccount, _ := common.NewAccountFromPublicKeyString("52MNGpgvydSwCtC2H4qeiZXZ1TxEuRVCRGa8LAfk2kSj") |
| 76 | + jeffyVaultAccount, _ := common.NewAccountFromPublicKeyString("BFDanLgELhpCCGTtaa7c8WGxTXcTxgwkf9DMQd4qheSK") |
| 77 | + coreMintVaultAccount, _ := common.NewAccountFromPublicKeyString("A9NVHVuorNL4y2YFxdwdU3Hqozxw1Y1YJ81ZPxJsRrT4") |
| 78 | + |
| 79 | + var tokenAccount token.Account |
| 80 | + ai, err := p.data.GetBlockchainAccountInfo(ctx, jeffyVaultAccount.PublicKey().ToBase58(), solana.CommitmentFinalized) |
| 81 | + if err != nil { |
| 82 | + return err |
| 83 | + } |
| 84 | + tokenAccount.Unmarshal(ai.Data) |
| 85 | + jeffyVaultBalance := tokenAccount.Amount |
| 86 | + |
| 87 | + ai, err = p.data.GetBlockchainAccountInfo(ctx, coreMintVaultAccount.PublicKey().ToBase58(), solana.CommitmentFinalized) |
| 88 | + if err != nil { |
| 89 | + return err |
| 90 | + } |
| 91 | + tokenAccount.Unmarshal(ai.Data) |
| 92 | + coreMintVaultBalance := tokenAccount.Amount |
| 93 | + |
| 94 | + return p.data.PutCurrencyReserveRecord(ctx, ¤cy.ReserveRecord{ |
| 95 | + Mint: jeffyMintAccount.PublicKey().ToBase58(), |
| 96 | + SupplyFromBonding: currencycreator.DefaultMintMaxQuarkSupply - jeffyVaultBalance, |
| 97 | + CoreMintLocked: coreMintVaultBalance, |
| 98 | + Time: time.Now(), |
| 99 | + }) |
| 100 | +} |
0 commit comments