Skip to content

Commit

Permalink
feeder now reads from source
Browse files Browse the repository at this point in the history
  • Loading branch information
scottburch committed Feb 13, 2021
1 parent d9bc2d0 commit 9a37c22
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 8 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ go 1.14
require (
github.com/cosmos/cosmos-sdk v0.39.1
github.com/cosmos/modules/incubator/faucet v0.0.0-20200315124306-c86f71ae76a0
github.com/go-resty/resty/v2 v2.3.0
github.com/golang/mock v1.4.0
github.com/gorilla/mux v1.7.4
github.com/magiconair/properties v1.8.1
github.com/robfig/cron/v3 v3.0.1
github.com/spf13/cobra v1.0.0
github.com/spf13/viper v1.6.3
github.com/stretchr/testify v1.6.1
Expand Down
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ github.com/go-logfmt/logfmt v0.4.0 h1:MP4Eh7ZCb31lleYCFuwm0oe4/YGak+5l1vA2NOE80n
github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk=
github.com/go-logfmt/logfmt v0.5.0 h1:TrB8swr/68K7m9CcGut2g3UOihhbcbiMAYiuTXdEih4=
github.com/go-logfmt/logfmt v0.5.0/go.mod h1:wCYkCAKZfumFQihp8CzCvQ3paCTfi41vtzG1KdI/P7A=
github.com/go-resty/resty/v2 v2.3.0 h1:JOOeAvjSlapTT92p8xiS19Zxev1neGikoHsXJeOq8So=
github.com/go-resty/resty/v2 v2.3.0/go.mod h1:UpN9CgLZNsv4e9XG50UU8xdI0F43UQ4HmxLBDwaroHU=
github.com/go-sql-driver/mysql v1.4.0/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w=
github.com/go-stack/stack v1.8.0 h1:5SgMzNM5HxrEjV0ww2lTmX6E2Izsfxas4+YHWRs3Lsk=
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
Expand Down Expand Up @@ -406,8 +408,6 @@ github.com/rcrowley/go-metrics v0.0.0-20181016184325-3113b8401b8a h1:9ZKAASQSHhD
github.com/rcrowley/go-metrics v0.0.0-20181016184325-3113b8401b8a/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4=
github.com/rcrowley/go-metrics v0.0.0-20200313005456-10cdbea86bc0 h1:MkV+77GLUNo5oJ0jf870itWm3D0Sjh7+Za9gazKc5LQ=
github.com/rcrowley/go-metrics v0.0.0-20200313005456-10cdbea86bc0/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4=
github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=
github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/rs/cors v1.7.0 h1:+88SsELBHx5r+hZ8TCkggzSstaWNbDvThkVK8H6f9ik=
Expand Down Expand Up @@ -574,6 +574,8 @@ golang.org/x/net v0.0.0-20190813141303-74dc4d7220e7 h1:fHDIZ2oxGnUZRN6WgWFCbYBjH
golang.org/x/net v0.0.0-20190813141303-74dc4d7220e7/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e h1:3G+cUijn7XD+S4eJFddp53Pv7+slrESplyjG25HgL+k=
golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
golang.org/x/net v0.0.0-20200513185701-a91f0712d120 h1:EZ3cVSzKOlJxAd8e8YAJ7no8nNypTxexh/YE/xW3ZEY=
golang.org/x/net v0.0.0-20200513185701-a91f0712d120/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
Expand Down
43 changes: 38 additions & 5 deletions x/oracle/feeder.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,21 @@ import (
"github.com/bluzelle/curium/x/crud"
sdk "github.com/cosmos/cosmos-sdk/types"
"net/url"
"strconv"
"time"
"github.com/go-resty/resty/v2"
)


type source struct {
Name string
Url string `json:"url"`
Property string `json:"property"`
}

type JSONResponse map[string]interface{}


var currCtx *sdk.Context

func StartFeeder(crudKeeper crud.Keeper) {
Expand All @@ -33,18 +39,45 @@ func StartFeeder(crudKeeper crud.Keeper) {
func feederTick(crudKeeper crud.Keeper) {
result := crudKeeper.Search(*currCtx, crudKeeper.GetKVStore(*currCtx), "oracle", "source", 1, 100, "asc", nil)
keyValues := result.KeyValues
sources := make([]source, len(result.KeyValues))

for i, v := range keyValues {
for _, v := range keyValues {
res := source{}
json.Unmarshal([]byte(decodeSafe(v.Value)), &res)
sources[i] = res
res.Name = v.Key
value, err := fetchFromSource(res)
// TODO: SUBMIT PREFLIGHT AND VOTE HERE
fmt.Println(err)
fmt.Println(value)
}
}

func fetchFromSource(source source) (float64, error) {
fmt.Println("************** SOURCE **************")
fmt.Println(source)

client := resty.New()

resp, err := client.R().
EnableTrace().
Get(source.Url)

if err != nil {
return 0, err
}

var respBody JSONResponse

json.Unmarshal(resp.Body(), &respBody)

value := respBody[source.Property]

switch i := value.(type) {
case string:
f, err := strconv.ParseFloat(i, 32)
return f, err
}
return 0, nil

fmt.Println("**********************************")
fmt.Println(sources)
}

func decodeSafe(str string) string {
Expand Down

0 comments on commit 9a37c22

Please sign in to comment.