|
| 1 | +// Package nebyprices implements the Neby prices analyzer. |
| 2 | +package nebyprices |
| 3 | + |
| 4 | +import ( |
| 5 | + "bytes" |
| 6 | + "context" |
| 7 | + "encoding/json" |
| 8 | + "fmt" |
| 9 | + "io" |
| 10 | + "net/http" |
| 11 | + "time" |
| 12 | + |
| 13 | + ethCommon "github.com/ethereum/go-ethereum/common" |
| 14 | + |
| 15 | + "github.com/oasisprotocol/nexus/analyzer" |
| 16 | + "github.com/oasisprotocol/nexus/analyzer/httpmisc" |
| 17 | + "github.com/oasisprotocol/nexus/analyzer/item" |
| 18 | + "github.com/oasisprotocol/nexus/common" |
| 19 | + "github.com/oasisprotocol/nexus/config" |
| 20 | + "github.com/oasisprotocol/nexus/log" |
| 21 | + "github.com/oasisprotocol/nexus/storage" |
| 22 | +) |
| 23 | + |
| 24 | +const ( |
| 25 | + analyzerPrefix = "neby_prices_" |
| 26 | + |
| 27 | + defaultNebyEndpoint = "https://graph.api.neby.exchange/dex" |
| 28 | +) |
| 29 | + |
| 30 | +type processor struct { |
| 31 | + runtime common.Runtime |
| 32 | + target storage.TargetStorage |
| 33 | + logger *log.Logger |
| 34 | + |
| 35 | + client *http.Client |
| 36 | + |
| 37 | + endpoint string |
| 38 | +} |
| 39 | + |
| 40 | +var _ item.ItemProcessor[struct{}] = (*processor)(nil) |
| 41 | + |
| 42 | +func NewAnalyzer( |
| 43 | + runtime common.Runtime, |
| 44 | + cfg *config.NebyPricesConfig, |
| 45 | + target storage.TargetStorage, |
| 46 | + logger *log.Logger, |
| 47 | +) (analyzer.Analyzer, error) { |
| 48 | + logger = logger.With("analyzer", analyzerPrefix+runtime) |
| 49 | + |
| 50 | + logger.Info("Starting analyzer") |
| 51 | + |
| 52 | + endpoint := cfg.Endpoint |
| 53 | + if endpoint == "" { |
| 54 | + endpoint = defaultNebyEndpoint |
| 55 | + } |
| 56 | + |
| 57 | + if cfg.Interval == 0 { |
| 58 | + cfg.Interval = 15 * time.Minute |
| 59 | + } |
| 60 | + p := &processor{ |
| 61 | + runtime: runtime, |
| 62 | + target: target, |
| 63 | + logger: logger, |
| 64 | + client: &http.Client{Timeout: 10 * time.Second}, |
| 65 | + endpoint: endpoint, |
| 66 | + } |
| 67 | + |
| 68 | + return item.NewAnalyzer( |
| 69 | + analyzerPrefix+string(runtime), |
| 70 | + cfg.ItemBasedAnalyzerConfig, |
| 71 | + p, |
| 72 | + target, |
| 73 | + logger, |
| 74 | + ) |
| 75 | +} |
| 76 | + |
| 77 | +func (p *processor) GetItems(ctx context.Context, limit uint64) ([]struct{}, error) { |
| 78 | + return []struct{}{{}}, nil |
| 79 | +} |
| 80 | + |
| 81 | +func (p *processor) ProcessItem(ctx context.Context, batch *storage.QueryBatch, item struct{}) error { |
| 82 | + p.logger.Debug("fetching Neby token prices", "endpoint", p.endpoint) |
| 83 | + result, err := p.fetchNebyTokenPrices(ctx, p.endpoint) |
| 84 | + if err != nil { |
| 85 | + return fmt.Errorf("failed to fetch Neby token prices: %w", err) |
| 86 | + } |
| 87 | + p.logger.Debug("fetched Neby token prices", "count", len(result.Data.Tokens), "result", result) |
| 88 | + |
| 89 | + // Store the prices in the database. |
| 90 | + for _, token := range result.Data.Tokens { |
| 91 | + ethAddress := ethCommon.HexToAddress(token.ID) |
| 92 | + batch.Queue( |
| 93 | + tokenNebyDerivedPriceUpsert, |
| 94 | + p.runtime, |
| 95 | + ethAddress[:], |
| 96 | + token.DerivedETH, |
| 97 | + ) |
| 98 | + } |
| 99 | + |
| 100 | + return nil |
| 101 | +} |
| 102 | + |
| 103 | +type graphTokensResponse struct { |
| 104 | + Data struct { |
| 105 | + Tokens []struct { |
| 106 | + ID string `json:"id"` |
| 107 | + DerivedETH string `json:"derivedETH"` |
| 108 | + Name string `json:"name"` |
| 109 | + } `json:"tokens"` |
| 110 | + } `json:"data"` |
| 111 | +} |
| 112 | + |
| 113 | +func (p *processor) fetchNebyTokenPrices(ctx context.Context, endpoint string) (*graphTokensResponse, error) { |
| 114 | + query := ` |
| 115 | + { |
| 116 | + tokens(first: 1000) { |
| 117 | + id |
| 118 | + derivedETH |
| 119 | + name |
| 120 | + } |
| 121 | + }` |
| 122 | + body, _ := json.Marshal(map[string]string{ |
| 123 | + "query": query, |
| 124 | + }) |
| 125 | + |
| 126 | + req, err := http.NewRequestWithContext(ctx, http.MethodPost, endpoint, bytes.NewReader(body)) |
| 127 | + if err != nil { |
| 128 | + return nil, fmt.Errorf("failed to create request: %w", err) |
| 129 | + } |
| 130 | + req.Header.Set("Content-Type", "application/json") |
| 131 | + resp, err := p.client.Do(req) |
| 132 | + if err != nil { |
| 133 | + return nil, fmt.Errorf("failed to send request: %w", err) |
| 134 | + } |
| 135 | + if err := httpmisc.ResponseOK(resp); err != nil { |
| 136 | + return nil, fmt.Errorf("response error: %w", err) |
| 137 | + } |
| 138 | + defer resp.Body.Close() |
| 139 | + |
| 140 | + respBody, err := io.ReadAll(resp.Body) |
| 141 | + if err != nil { |
| 142 | + return nil, fmt.Errorf("failed to read response body: %w", err) |
| 143 | + } |
| 144 | + |
| 145 | + var result graphTokensResponse |
| 146 | + if err := json.Unmarshal(respBody, &result); err != nil { |
| 147 | + return nil, fmt.Errorf("failed to unmarshal response: %w", err) |
| 148 | + } |
| 149 | + |
| 150 | + return &result, nil |
| 151 | +} |
| 152 | + |
| 153 | +func (p *processor) QueueLength(ctx context.Context) (int, error) { |
| 154 | + return 0, nil |
| 155 | +} |
0 commit comments