-
Notifications
You must be signed in to change notification settings - Fork 8
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
Oracle Plugin Forex #41
Open
NodeSphereGL
wants to merge
4
commits into
autonity:master
Choose a base branch
from
NodeSphereGL:master
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.
+207
−1
Open
Changes from all commits
Commits
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
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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# Example config file | ||
|
||
Add these code to plugins-conf.yml: | ||
|
||
- name: forex_nodesphere | ||
key: sandbox | ||
timeout: 30 | ||
refresh: 60 | ||
|
||
Notice key "sandbox" only for testing, data is not realtime ( past 24h ). | ||
|
||
Contact me for production key | ||
|
||
Tele: @NodeSphereGL | ||
Discord: nodesphere |
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,175 @@ | ||
package main | ||
|
||
import ( | ||
"autonity-oracle/plugins/common" | ||
"autonity-oracle/types" | ||
"encoding/json" | ||
"fmt" | ||
"github.com/hashicorp/go-hclog" | ||
"github.com/shopspring/decimal" | ||
"io" | ||
"net/url" | ||
"os" | ||
"strings" | ||
"time" | ||
) | ||
|
||
const ( | ||
version = "v0.1.0" | ||
pathLive = "live" | ||
accessKey = "access_key" | ||
) | ||
|
||
var defaultConfig = types.PluginConfig{ | ||
Name: "forex_nodesphere", | ||
Key: "sandbox", | ||
Scheme: "https", | ||
Endpoint: "api-currency.nodesphere.net", | ||
Timeout: 10, //10s | ||
DataUpdateInterval: 30, //30s | ||
} | ||
|
||
type NPResult struct { | ||
Success bool `json:"success"` | ||
Timestamp int64 `json:"timestamp"` | ||
Source string `json:"source"` | ||
Quotes Quotes `json:"quotes"` | ||
} | ||
|
||
type Quotes struct { | ||
EURUSD decimal.Decimal `json:"EURUSD"` | ||
JPYUSD decimal.Decimal `json:"JPYUSD"` | ||
GBPUSD decimal.Decimal `json:"GBPUSD"` | ||
AUDUSD decimal.Decimal `json:"AUDUSD"` | ||
CADUSD decimal.Decimal `json:"CADUSD"` | ||
SEKUSD decimal.Decimal `json:"SEKUSD"` | ||
} | ||
|
||
type NPClient struct { | ||
conf *types.PluginConfig | ||
client *common.Client | ||
logger hclog.Logger | ||
} | ||
|
||
func NewNPClient(conf *types.PluginConfig) *NPClient { | ||
client := common.NewClient(conf.Key, time.Second*time.Duration(conf.Timeout), conf.Endpoint) | ||
logger := hclog.New(&hclog.LoggerOptions{ | ||
Name: conf.Name, | ||
Level: hclog.Info, | ||
Output: os.Stdout, | ||
}) | ||
|
||
return &NPClient{conf: conf, client: client, logger: logger} | ||
} | ||
|
||
func (cl *NPClient) KeyRequired() bool { | ||
return true | ||
} | ||
|
||
func (cl *NPClient) FetchPrice(symbols []string) (common.Prices, error) { | ||
var prices common.Prices | ||
u := cl.buildURL(cl.conf.Key) | ||
|
||
res, err := cl.client.Conn.Request(cl.conf.Scheme, u) | ||
if err != nil { | ||
cl.logger.Error("http request", "error", err.Error()) | ||
return nil, err | ||
} | ||
defer res.Body.Close() | ||
|
||
if err = common.CheckHTTPStatusCode(res.StatusCode); err != nil { | ||
cl.logger.Error("data source return error", "error", err.Error()) | ||
return nil, err | ||
} | ||
|
||
body, err := io.ReadAll(res.Body) | ||
if err != nil { | ||
cl.logger.Error("io read", "error", err.Error()) | ||
return nil, err | ||
} | ||
|
||
var result NPResult | ||
err = json.Unmarshal(body, &result) | ||
if err != nil { | ||
cl.logger.Error("unmarshal price", "error", err.Error()) | ||
return nil, err | ||
} | ||
|
||
if !result.Success { | ||
cl.logger.Error("fetch price", "error", string(body)) | ||
return nil, fmt.Errorf("data source return error: %s", string(body)) | ||
} | ||
|
||
for _, s := range symbols { | ||
p, err := cl.symbolsToPrice(s, &result) | ||
if err != nil { | ||
cl.logger.Error("symbol to prices", "error", err.Error()) | ||
continue | ||
} | ||
prices = append(prices, p) | ||
} | ||
|
||
return prices, nil | ||
} | ||
|
||
// AvailableSymbols returns the adapted symbols for current data source. | ||
func (cl *NPClient) AvailableSymbols() ([]string, error) { | ||
return common.DefaultForexSymbols, nil | ||
} | ||
|
||
func (cl *NPClient) Close() { | ||
cl.client.Conn.Close() | ||
} | ||
|
||
func (cl *NPClient) symbolsToPrice(s string, res *NPResult) (common.Price, error) { | ||
var price common.Price | ||
sep := common.ResolveSeparator(s) | ||
codes := strings.Split(s, sep) | ||
if len(codes) != 2 { | ||
return price, fmt.Errorf("invalid symbol %s", s) | ||
} | ||
|
||
from := codes[0] | ||
to := codes[1] | ||
if to != res.Source { | ||
return price, fmt.Errorf("wrong base %s", to) | ||
} | ||
|
||
price.Symbol = s | ||
price.Volume = types.DefaultVolume.String() | ||
switch from { | ||
case "EUR": | ||
price.Price = res.Quotes.EURUSD.String() | ||
case "JPY": | ||
price.Price = res.Quotes.JPYUSD.String() | ||
case "GBP": | ||
price.Price = res.Quotes.GBPUSD.String() | ||
case "AUD": | ||
price.Price = res.Quotes.AUDUSD.String() | ||
case "CAD": | ||
price.Price = res.Quotes.CADUSD.String() | ||
case "SEK": | ||
price.Price = res.Quotes.SEKUSD.String() | ||
default: | ||
return price, fmt.Errorf("unknown symbol %s", from) | ||
} | ||
return price, nil | ||
} | ||
|
||
func (cl *NPClient) buildURL(apiKey string) *url.URL { | ||
endpoint := &url.URL{} | ||
endpoint.Path = pathLive | ||
|
||
query := endpoint.Query() | ||
query.Set(accessKey, apiKey) | ||
|
||
endpoint.RawQuery = query.Encode() | ||
return endpoint | ||
} | ||
|
||
func main() { | ||
conf := common.ResolveConf(os.Args[0], &defaultConfig) | ||
adapter := common.NewPlugin(conf, NewNPClient(conf), version, types.SrcCEX, nil) | ||
defer adapter.Close() | ||
common.PluginServe(adapter) | ||
} |
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,15 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/stretchr/testify/require" | ||
"testing" | ||
) | ||
|
||
func TestNewNPClient(t *testing.T) { | ||
defaultConfig.Key = "sandbox" | ||
client := NewNPClient(&defaultConfig) | ||
defer client.Close() | ||
prices, err := client.FetchPrice([]string{"EUR-USD", "JPY-USD", "GBP-USD", "AUD-USD", "CAD-USD", "SEK-USD"}) | ||
require.NoError(t, err) | ||
require.Equal(t, 6, len(prices)) | ||
} |
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.
please add below statement after line 138 as we introduced some update in the master.