Documentation
¶
Index ¶
- func GetRetryableHTTPClientForExchange(exchange string, maxRetries int) (*retryablehttp.Client, error)
- func Truncate(r *big.Rat, prec int) string
- type BinanceResponse
- type BitstampResponse
- type BybitListItem
- type BybitResponse
- type CoinbaseResponse
- type CryptoListItem
- type CryptoResponse
- type ExchangePrice
- type ExtractDataResult
- func ExtractDataFromHTML(ctx context.Context, attestationRequest attestation.AttestationRequest) (ExtractDataResult, *appErrors.AppError)
- func ExtractDataFromJSON(ctx context.Context, attestationRequest attestation.AttestationRequest) (ExtractDataResult, *appErrors.AppError)
- func ExtractDataFromTargetURL(ctx context.Context, attestationRequest attestation.AttestationRequest, ...) (ExtractDataResult, *appErrors.AppError)
- type GateResponse
- type GateResponseItem
- type GeminiResponse
- type GeminiVolumeInfo
- type KrakenResponse
- type KrakenResponseItem
- type MEXCResponse
- type PriceFeedClient
- func (c *PriceFeedClient) ExtractPriceFeedData(ctx context.Context, attestationRequest attestation.AttestationRequest, ...) (ExtractDataResult, *appErrors.AppError)
- func (c *PriceFeedClient) FetchPriceFromExchange(ctx context.Context, exchange, token, symbol string, timestamp int64) (*ExchangePrice, *appErrors.AppError)
- func (c *PriceFeedClient) GetPriceFeed(ctx context.Context, tokenName string, timestamp int64, precision uint) (*PriceFeedResult, *appErrors.AppError)
- type PriceFeedResult
- type XTResponse
- type XTResponseItem
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GetRetryableHTTPClientForExchange ¶
func GetRetryableHTTPClientForExchange(exchange string, maxRetries int) (*retryablehttp.Client, error)
Types ¶
type BinanceResponse ¶
type BitstampResponse ¶
type BybitListItem ¶
type BybitResponse ¶
type BybitResponse struct {
Timestamp int64 `json:"time"`
Result struct {
List []BybitListItem `json:"list"`
} `json:"result"`
}
type CoinbaseResponse ¶
type CryptoListItem ¶
type CryptoResponse ¶
type CryptoResponse struct {
Result struct {
Data []CryptoListItem `json:"data"`
} `json:"result"`
}
type ExchangePrice ¶
type ExchangePrice struct {
Exchange string `json:"exchange"` // Exchange name.
Price string `json:"price"` // Price.
Volume string `json:"volume"` // Volume.
Token string `json:"token"` // Token.
Symbol string `json:"symbol"` // Symbol.
}
ExchangePrice represents a price from a single exchange
func CalculateVolumeWeightedAverage ¶
func CalculateVolumeWeightedAverage(prices []ExchangePrice, precision uint, token string) (string, string, int, []ExchangePrice, *appErrors.AppError)
CalculateVolumeWeightedAverage calculates the volume-weighted average price
type ExtractDataResult ¶
type ExtractDataResult struct {
ResponseBody string // The response body.
AttestationData string // The attestation data.
StatusCode int // The status code.
}
ExtractDataResult represents the result of data extraction
func ExtractDataFromHTML ¶
func ExtractDataFromHTML(ctx context.Context, attestationRequest attestation.AttestationRequest) (ExtractDataResult, *appErrors.AppError)
ExtractDataFromHTML scrapes and extracts data from HTML responses for attestation purposes.
This function: 1. Makes an HTTP request to the specified URL 2. Reads and parses the HTML content using htmlquery 3. Extracts data using XPath selectors 4. Handles different result types (element vs value) 5. Applies encoding options for numeric values 6. Returns the full HTML content and extracted data
The selector uses XPath syntax for HTML element selection: - "//div[@class='price']" - finds div with class 'price' - "//span[@id='total']/text()" - gets text content of span with id 'total' - "//table//tr[1]/td[2]" - gets second cell of first table row
HTMLResultType controls what is extracted: - "value" (default): extracts text content only - "element": extracts the complete HTML element including tags
Example usage:
request := services.AttestationRequest{
Url: "https://example.com/price-page",
Selector: "//span[@class='price']/text()",
ResponseFormat: "html",
HTMLResultType: &[]string{"value"}[0],
EncodingOptions: encoding.EncodingOptions{Value: "float", Precision: 2}
}
result, err := ExtractDataFromHTML(request)
func ExtractDataFromJSON ¶
func ExtractDataFromJSON(ctx context.Context, attestationRequest attestation.AttestationRequest) (ExtractDataResult, *appErrors.AppError)
ExtractDataFromJSON fetches and extracts data from JSON responses for attestation purposes.
This function: 1. Makes an HTTP request to the specified URL 2. Parses the JSON response 3. Extracts a specific value using the provided selector (supports nested paths and array indexing) 4. Applies encoding options (e.g., float precision) 5. Returns the full response body and extracted data
The selector supports dot notation and array indexing: - "data.price" - extracts price from data object - "items[0].value" - extracts value from first item in items array - "results.data[2].metrics.total" - complex nested path with array access
Example usage:
request := services.AttestationRequest{
Url: "https://api.example.com/price",
Selector: "data.price",
ResponseFormat: "json",
EncodingOptions: encoding.EncodingOptions{Value: "float", Precision: 6}
}
result, err := ExtractDataFromJSON(request)
func ExtractDataFromTargetURL ¶
func ExtractDataFromTargetURL(ctx context.Context, attestationRequest attestation.AttestationRequest, timestamp int64) (ExtractDataResult, *appErrors.AppError)
ExtractDataFromTargetURL fetches the data from the attestation request target URL. This is the main entry point that routes to specific extractors based on the request type.
type GateResponse ¶
type GateResponse []GateResponseItem
type GateResponseItem ¶
type GeminiResponse ¶
type GeminiResponse struct {
Price string `json:"last"`
VolumeInfo GeminiVolumeInfo `json:"volume"`
}
type GeminiVolumeInfo ¶
type KrakenResponse ¶
type KrakenResponse struct {
Error []string `json:"error"`
Result map[string]KrakenResponseItem `json:"result"`
}
type KrakenResponseItem ¶
type MEXCResponse ¶
type PriceFeedClient ¶
type PriceFeedClient struct {
// contains filtered or unexported fields
}
PriceFeedClient is the client for the price feed.
func NewPriceFeedClient ¶
func NewPriceFeedClient() *PriceFeedClient
NewPriceFeedClient creates a new PriceFeedClient with default configurations
func (*PriceFeedClient) ExtractPriceFeedData ¶
func (c *PriceFeedClient) ExtractPriceFeedData(ctx context.Context, attestationRequest attestation.AttestationRequest, token string, timestamp int64) (ExtractDataResult, *appErrors.AppError)
ExtractPriceFeedData handles price feed requests and always returns the volume-weighted average price (VWAP) This ensures consistent and reliable price data for oracle attestations
func (*PriceFeedClient) FetchPriceFromExchange ¶
func (c *PriceFeedClient) FetchPriceFromExchange(ctx context.Context, exchange, token, symbol string, timestamp int64) (*ExchangePrice, *appErrors.AppError)
FetchPriceFromExchange fetches price and volume data from a specific exchange.
This function performs the following steps sequentially:
- Retrieves the exchange configuration for the given exchange.
- Replace the symbol in the endpoint template.
- Constructs the full URL for the API request, handling cases where the BaseURL may or may not include the protocol.
- Creates a retryable HTTP client for robust network requests.
- Builds an HTTP GET request with the provided context.
- Executes the HTTP request and handles any network errors.
- Checks the HTTP response status code for success.
- Reads the response body from the exchange API.
- Attempts to decode the response body as a JSON object. If decoding fails and the exchange is "gate.io", attempts to decode as a JSON array and adapts the data structure accordingly.
- Parses the price and volume from the decoded response using the appropriate exchange-specific parser.
- Returns an ExchangePrice struct with the parsed data, or an error if any step fails.
Parameters:
- ctx: The context for request cancellation and logging.
- exchange: The key identifying the exchange (e.g., "binance").
- token: The token (e.g., "BTC").
- symbol: The trading symbol (e.g., "BTCUSDT").
Returns:
- *ExchangePrice: The parsed price and volume data from the exchange.
- *appErrors.AppError: An application error if any step fails, otherwise nil.
func (*PriceFeedClient) GetPriceFeed ¶
func (c *PriceFeedClient) GetPriceFeed(ctx context.Context, tokenName string, timestamp int64, precision uint) (*PriceFeedResult, *appErrors.AppError)
GetPriceFeed fetches and calculates the volume-weighted average price for a given token
type PriceFeedResult ¶
type PriceFeedResult struct {
Token string `json:"token"` // Token.
VolumeWeightedAvg string `json:"volumeWeightedAvg"` // Volume-weighted average price.
TotalVolume string `json:"totalVolume"` // Total volume.
ExchangeCount int `json:"exchangeCount"` // Number of exchanges.
Timestamp int64 `json:"timestamp"` // Timestamp.
ExchangePricesRaw []ExchangePrice `json:"exchangePricesRaw"` // Exchange prices.
ExchangePricesUsed []ExchangePrice `json:"exchangePricesUsed"` // Exchange prices.
Success bool `json:"success"` // Success.
}
PriceFeedResult represents the result of a price feed calculation
type XTResponse ¶
type XTResponse struct {
Result []XTResponseItem `json:"result"`
}