-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: support Stork oracle #13
Open
ThanhNhann
wants to merge
20
commits into
InjectiveLabs:master
Choose a base branch
from
decentrio:nhan/stork-relayer
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+543
−56
Open
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
94986be
add baseline for stork relayer
ThanhNhann 0306079
go mod tidy
ThanhNhann addccfa
add stork price puller
ThanhNhann e05c1a1
connect websocket
ThanhNhann 289065f
add logic get msg and parse
ThanhNhann 14fc651
pull
ThanhNhann 1f9176b
complete logic pull asset pair for stork feed
ThanhNhann 54a30d3
update logic commitSetPrices to compatible stork oracle
ThanhNhann ca84799
update with sdk pr 224
ThanhNhann 7c72b62
chore: add convert timestamp to second
ThanhNhann 58f2a05
update flow connect stork websocket and pull assetpairs
ThanhNhann 4136531
update env variable and test with multiple tickets
ThanhNhann 7d7e2bb
clean
ThanhNhann 6ef15fd
feat/stork-relayer improvs
danidomi 99be119
feat/stork-relayer refactor
danidomi 08e14c9
Merge pull request #1 from danidomi/feat/stork-improvs
ThanhNhann dee052d
minor: fix return pull asset pairs
ThanhNhann 3dcaef3
minor: update logging
ThanhNhann 1bf959c
Merge branch 'master' of https://github.com/InjectiveLabs/injective-p…
ThanhNhann 75bd2c3
udpate with Hilari's suggestions
ThanhNhann File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,9 +2,11 @@ package main | |
|
||
import ( | ||
"context" | ||
"encoding/base64" | ||
"fmt" | ||
"io/fs" | ||
"io/ioutil" | ||
"net/http" | ||
"net/url" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
|
@@ -13,6 +15,7 @@ import ( | |
exchangetypes "github.com/InjectiveLabs/sdk-go/chain/exchange/types" | ||
oracletypes "github.com/InjectiveLabs/sdk-go/chain/oracle/types" | ||
rpchttp "github.com/cometbft/cometbft/rpc/client/http" | ||
"github.com/gorilla/websocket" | ||
cli "github.com/jawher/mow.cli" | ||
"github.com/pkg/errors" | ||
"github.com/xlab/closer" | ||
|
@@ -49,13 +52,18 @@ func oracleCmd(cmd *cli.Cmd) { | |
// External Feeds params | ||
dynamicFeedsDir *string | ||
binanceBaseURL *string | ||
storkFeedsDir *string | ||
|
||
// Metrics | ||
statsdPrefix *string | ||
statsdAddr *string | ||
statsdStuckDur *string | ||
statsdMocking *string | ||
statsdDisabled *string | ||
|
||
// Stork Oracle websocket params | ||
websocketUrl *string | ||
websocketHeader *string | ||
) | ||
|
||
initCosmosOptions( | ||
|
@@ -82,6 +90,7 @@ func oracleCmd(cmd *cli.Cmd) { | |
cmd, | ||
&binanceBaseURL, | ||
&dynamicFeedsDir, | ||
&storkFeedsDir, | ||
) | ||
|
||
initStatsdOptions( | ||
|
@@ -93,7 +102,14 @@ func oracleCmd(cmd *cli.Cmd) { | |
&statsdDisabled, | ||
) | ||
|
||
initStorkOracleWebSocket( | ||
cmd, | ||
&websocketUrl, | ||
&websocketHeader, | ||
) | ||
|
||
cmd.Action = func() { | ||
ctx := context.Background() | ||
// ensure a clean exit | ||
defer closer.Close() | ||
|
||
|
@@ -150,12 +166,13 @@ func oracleCmd(cmd *cli.Cmd) { | |
log.Infoln("waiting for GRPC services") | ||
time.Sleep(1 * time.Second) | ||
|
||
daemonWaitCtx, cancelWait := context.WithTimeout(context.Background(), 10*time.Second) | ||
daemonWaitCtx, cancelWait := context.WithTimeout(ctx, 10*time.Second) | ||
defer cancelWait() | ||
|
||
daemonConn := cosmosClient.QueryClient() | ||
if err := waitForService(daemonWaitCtx, daemonConn); err != nil { | ||
panic(fmt.Errorf("failed to wait for cosmos client connection: %w", err)) | ||
} | ||
cancelWait() | ||
feedProviderConfigs := map[oracle.FeedProvider]interface{}{ | ||
oracle.FeedProviderBinance: &oracle.BinanceEndpointConfig{ | ||
BaseURL: *binanceBaseURL, | ||
|
@@ -173,7 +190,7 @@ func oracleCmd(cmd *cli.Cmd) { | |
return nil | ||
} | ||
|
||
cfgBody, err := ioutil.ReadFile(path) | ||
cfgBody, err := os.ReadFile(path) | ||
if err != nil { | ||
err = errors.Wrapf(err, "failed to read dynamic feed config") | ||
return err | ||
|
@@ -201,12 +218,61 @@ func oracleCmd(cmd *cli.Cmd) { | |
log.Infof("found %d dynamic feed configs", len(dynamicFeedConfigs)) | ||
} | ||
|
||
storkFeedConfigs := make([]*oracle.StorkFeedConfig, 0, 10) | ||
if len(*storkFeedsDir) > 0 { | ||
err := filepath.WalkDir(*storkFeedsDir, func(path string, d fs.DirEntry, err error) error { | ||
if err != nil { | ||
return err | ||
} else if d.IsDir() { | ||
return nil | ||
} else if filepath.Ext(path) != ".toml" { | ||
return nil | ||
} | ||
|
||
cfgBody, err := os.ReadFile(path) | ||
if err != nil { | ||
err = errors.Wrapf(err, "failed to read stork feed config") | ||
return err | ||
} | ||
|
||
feedCfg, err := oracle.ParseStorkFeedConfig(cfgBody) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we should support price updates for multiple tickers There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated flow, can you plz recheck? |
||
if err != nil { | ||
log.WithError(err).WithFields(log.Fields{ | ||
"filename": d.Name(), | ||
}).Errorln("failed to parse stork feed config") | ||
return nil | ||
} | ||
|
||
storkFeedConfigs = append(storkFeedConfigs, feedCfg) | ||
|
||
return nil | ||
}) | ||
|
||
if err != nil { | ||
err = errors.Wrapf(err, "stork feeds dir is specified, but failed to read from it: %s", *storkFeedsDir) | ||
log.WithError(err).Fatalln("failed to load stork feeds") | ||
return | ||
} | ||
|
||
log.Infof("found %d stork feed configs", len(storkFeedConfigs)) | ||
} | ||
|
||
storkWebsocket, err := ConnectWebSocket(ctx, *websocketUrl, *websocketHeader) | ||
if err != nil { | ||
err = errors.Wrapf(err, "can not connect with stork oracle websocket") | ||
log.WithError(err).Errorln("failed to load stork feeds") | ||
return | ||
} | ||
log.Info("Connected to stork websocket") | ||
|
||
svc, err := oracle.NewService( | ||
cosmosClient, | ||
exchangetypes.NewQueryClient(daemonConn), | ||
oracletypes.NewQueryClient(daemonConn), | ||
feedProviderConfigs, | ||
dynamicFeedConfigs, | ||
storkFeedConfigs, | ||
storkWebsocket, | ||
) | ||
if err != nil { | ||
log.Fatalln(err) | ||
|
@@ -228,3 +294,39 @@ func oracleCmd(cmd *cli.Cmd) { | |
closer.Hold() | ||
} | ||
} | ||
|
||
func ConnectWebSocket(ctx context.Context, websocketUrl, urlHeader string) (conn *websocket.Conn, err error) { | ||
u, err := url.Parse(websocketUrl) | ||
if err != nil { | ||
return &websocket.Conn{}, errors.Wrapf(err, "can not parse WS url %s: %v", websocketUrl, err) | ||
} | ||
|
||
header := http.Header{} | ||
header.Add("Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte(urlHeader))) | ||
|
||
dialer := websocket.DefaultDialer | ||
dialer.EnableCompression = true | ||
retries := 0 | ||
for { | ||
conn, _, err = websocket.DefaultDialer.DialContext(ctx, u.String(), header) | ||
if ctx.Err() != nil { | ||
return nil, ctx.Err() | ||
} else if err != nil { | ||
log.Infof("Failed to connect to WebSocket server: %v", err) | ||
retries++ | ||
if retries > oracle.MaxRetriesReConnectWebSocket { | ||
log.Infof("Reached maximum retries (%d), exiting...", oracle.MaxRetriesReConnectWebSocket) | ||
return | ||
} | ||
log.Infof("Retrying connect %sth in 5s...", fmt.Sprint(retries)) | ||
select { | ||
case <-ctx.Done(): | ||
return nil, ctx.Err() | ||
case <-time.NewTimer(5*time.Second).C: | ||
} | ||
} else { | ||
log.Infof("Connected to WebSocket server") | ||
return | ||
} | ||
} | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
provider = "Stork" | ||
ticker = "BTCUSD" | ||
pullInterval = "1m" | ||
oracleType = "Stork" | ||
message = "{\"type\":\"subscribe\",\"trace_id\":\"123\",\"data\":[\"BTCUSD\",\"ETHUSD\"]}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Provide guidance for
STORK_WEBSOCKET_HEADER
.The variable is left empty, which might be confusing for users. Consider adding a comment or example value to clarify what kind of data might be expected here, such as authentication tokens or specific headers required by the Stork oracle.