github.com/diadata-org/diadata@v1.4.593/pkg/utils/diasource.go (about) 1 package utils 2 3 import ( 4 "context" 5 "errors" 6 "strconv" 7 "time" 8 9 gql "github.com/machinebox/graphql" 10 ) 11 12 func GetGraphqlAssetQuotationFromDia(blockchain, address string, blockDuration int) (float64, error) { 13 currentTime := time.Now() 14 starttime := currentTime.Add(time.Duration(-blockDuration*2) * time.Second) 15 type Response struct { 16 GetFeed []struct { 17 Name string `json:"Name"` 18 Time time.Time `json:"Time"` 19 Value float64 `json:"Value"` 20 } `json:"GetFeed"` 21 } 22 client := gql.NewClient("https://api.diadata.org" + "/graphql/query") 23 req := gql.NewRequest(` 24 query { 25 GetFeed( 26 Filter: "mair", 27 BlockSizeSeconds: ` + strconv.Itoa(blockDuration) + `, 28 BlockShiftSeconds: ` + strconv.Itoa(blockDuration) + `, 29 StartTime: ` + strconv.FormatInt(starttime.Unix(), 10) + `, 30 EndTime: ` + strconv.FormatInt(currentTime.Unix(), 10) + `, 31 FeedSelection: [{ 32 Address: "` + address + `", 33 Blockchain: "` + blockchain + `" 34 }, 35 ], 36 ) { 37 Name 38 Time 39 Value 40 } 41 }`) 42 43 ctx := context.Background() 44 var r Response 45 if err := client.Run(ctx, req, &r); err != nil { 46 return 0.0, err 47 } 48 if len(r.GetFeed) == 0 { 49 return 0.0, errors.New("no results") 50 } 51 return r.GetFeed[len(r.GetFeed)-1].Value, nil 52 }