|
| 1 | +package sources |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "crypto/tls" |
| 6 | + "encoding/json" |
| 7 | + "fmt" |
| 8 | + "strconv" |
| 9 | + |
| 10 | + wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types" |
| 11 | + "github.com/NibiruChain/nibiru/x/common/set" |
| 12 | + "github.com/NibiruChain/pricefeeder/metrics" |
| 13 | + "github.com/NibiruChain/pricefeeder/types" |
| 14 | + "github.com/rs/zerolog" |
| 15 | + "google.golang.org/grpc" |
| 16 | + "google.golang.org/grpc/credentials" |
| 17 | +) |
| 18 | + |
| 19 | +const ( |
| 20 | + ErisProtocol = "eris_protocol" |
| 21 | + // Configuration constants |
| 22 | + grpcEndpoint = "grpc.nibiru.fi:443" |
| 23 | + contractAddr = "nibi1udqqx30cw8nwjxtl4l28ym9hhrp933zlq8dqxfjzcdhvl8y24zcqpzmh8m" |
| 24 | + stateQuery = `{"state": {}}` |
| 25 | +) |
| 26 | + |
| 27 | +var _ types.FetchPricesFunc = ErisProtocolPriceUpdate |
| 28 | + |
| 29 | +// newGRPCConnection creates a new gRPC connection with TLS |
| 30 | +func newGRPCConnection() (*grpc.ClientConn, error) { |
| 31 | + transportDialOpt := grpc.WithTransportCredentials( |
| 32 | + credentials.NewTLS( |
| 33 | + &tls.Config{ |
| 34 | + InsecureSkipVerify: false, |
| 35 | + }, |
| 36 | + ), |
| 37 | + ) |
| 38 | + return grpc.Dial(grpcEndpoint, transportDialOpt) |
| 39 | +} |
| 40 | + |
| 41 | +// ErisProtocolPriceUpdate retrieves the exchange rate for stNIBI to NIBI (ustnibi:unibi) from the Eris Protocol smart contract. |
| 42 | +// Note: This function ignores the input symbols and always returns the exchange rate for "ustnibi:unibi". |
| 43 | +func ErisProtocolPriceUpdate(symbols set.Set[types.Symbol], logger zerolog.Logger) (rawPrices map[types.Symbol]float64, err error) { |
| 44 | + conn, err := newGRPCConnection() |
| 45 | + if err != nil { |
| 46 | + logger.Err(err).Msgf("failed to connect to gRPC endpoint %s", grpcEndpoint) |
| 47 | + metrics.PriceSourceCounter.WithLabelValues(ErisProtocol, "false").Inc() |
| 48 | + return nil, fmt.Errorf("failed to connect to gRPC endpoint %s: %w", grpcEndpoint, err) |
| 49 | + } |
| 50 | + defer func() { |
| 51 | + if closeErr := conn.Close(); closeErr != nil { |
| 52 | + logger.Err(closeErr).Msg("failed to close gRPC connection") |
| 53 | + } |
| 54 | + }() |
| 55 | + |
| 56 | + wasmClient := wasmtypes.NewQueryClient(conn) |
| 57 | + query := wasmtypes.QuerySmartContractStateRequest{ |
| 58 | + Address: contractAddr, |
| 59 | + QueryData: []byte(stateQuery), |
| 60 | + } |
| 61 | + |
| 62 | + resp, err := wasmClient.SmartContractState(context.Background(), &query) |
| 63 | + if err != nil { |
| 64 | + logger.Err(err).Msg("failed to query SmartContractState") |
| 65 | + metrics.PriceSourceCounter.WithLabelValues(ErisProtocol, "false").Inc() |
| 66 | + return nil, fmt.Errorf("failed to query SmartContractState: %w", err) |
| 67 | + } |
| 68 | + |
| 69 | + if resp == nil || len(resp.Data) == 0 { |
| 70 | + logger.Error().Msg("received nil or empty response from SmartContractState") |
| 71 | + metrics.PriceSourceCounter.WithLabelValues(ErisProtocol, "false").Inc() |
| 72 | + return nil, fmt.Errorf("nil response from SmartContractState") |
| 73 | + } |
| 74 | + |
| 75 | + var responseObj struct { |
| 76 | + ExchangeRate string `json:"exchange_rate"` |
| 77 | + } |
| 78 | + if err := json.Unmarshal(resp.Data, &responseObj); err != nil { |
| 79 | + logger.Err(err).Msg("failed to unmarshal SmartContractState response data") |
| 80 | + metrics.PriceSourceCounter.WithLabelValues(ErisProtocol, "false").Inc() |
| 81 | + return nil, fmt.Errorf("failed to unmarshal SmartContractState response data: %w", err) |
| 82 | + } |
| 83 | + |
| 84 | + exchangeRate, err := strconv.ParseFloat(responseObj.ExchangeRate, 64) |
| 85 | + if err != nil { |
| 86 | + logger.Err(err).Msg("failed to convert exchange_rate to float") |
| 87 | + metrics.PriceSourceCounter.WithLabelValues(ErisProtocol, "false").Inc() |
| 88 | + return nil, fmt.Errorf("failed to convert exchange_rate to float: %w", err) |
| 89 | + } |
| 90 | + |
| 91 | + // Validate the exchange rate |
| 92 | + if exchangeRate <= 0 { |
| 93 | + errMsg := "received invalid exchange rate: value must be positive" |
| 94 | + logger.Error().Float64("exchange_rate", exchangeRate).Msg(errMsg) |
| 95 | + metrics.PriceSourceCounter.WithLabelValues(ErisProtocol, "false").Inc() |
| 96 | + return nil, fmt.Errorf(errMsg) |
| 97 | + } |
| 98 | + |
| 99 | + rawPrices = make(map[types.Symbol]float64) |
| 100 | + rawPrices[types.Symbol("ustnibi:unibi")] = exchangeRate |
| 101 | + |
| 102 | + logger.Debug(). |
| 103 | + Str("symbols", fmt.Sprint(symbols)). |
| 104 | + Str("source", ErisProtocol). |
| 105 | + Float64("exchange_rate", exchangeRate). |
| 106 | + Msg("fetched prices") |
| 107 | + |
| 108 | + metrics.PriceSourceCounter.WithLabelValues(ErisProtocol, "true").Inc() |
| 109 | + return rawPrices, nil |
| 110 | +} |
0 commit comments