commit inicial do projeto
This commit is contained in:
59
internal/oracle/fetcher.go
Normal file
59
internal/oracle/fetcher.go
Normal file
@ -0,0 +1,59 @@
|
||||
package oracle
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
// OracleData representa um dado externo coletado via oráculo
|
||||
type OracleData struct {
|
||||
Source string `json:"source"`
|
||||
Key string `json:"key"`
|
||||
Value float64 `json:"value"`
|
||||
Time time.Time `json:"timestamp"`
|
||||
}
|
||||
|
||||
// FetchFromMock simula obtenção de dados de um oráculo externo (mock)
|
||||
func FetchFromMock(key string) (*OracleData, error) {
|
||||
mockPrices := map[string]float64{
|
||||
"USD-BRL": 5.06,
|
||||
"ETH-BRL": 18732.42,
|
||||
"IMOVEL-ABC123": 950000.00,
|
||||
}
|
||||
val, ok := mockPrices[key]
|
||||
if !ok {
|
||||
return nil, errors.New("dado não encontrado no mock")
|
||||
}
|
||||
return &OracleData{
|
||||
Source: "mock",
|
||||
Key: key,
|
||||
Value: val,
|
||||
Time: time.Now(),
|
||||
}, nil
|
||||
}
|
||||
|
||||
// FetchFromCoinGecko busca o preço de uma moeda em relação ao BRL
|
||||
func FetchFromCoinGecko(coin string) (*OracleData, error) {
|
||||
url := fmt.Sprintf("https://api.coingecko.com/api/v3/simple/price?ids=%s&vs_currencies=brl", coin)
|
||||
resp, err := http.Get(url)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
var parsed map[string]map[string]float64
|
||||
if err := json.NewDecoder(resp.Body).Decode(&parsed); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
price := parsed[coin]["brl"]
|
||||
|
||||
return &OracleData{
|
||||
Source: "coingecko",
|
||||
Key: coin + "-BRL",
|
||||
Value: price,
|
||||
Time: time.Now(),
|
||||
}, nil
|
||||
}
|
||||
Reference in New Issue
Block a user