-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
304 additions
and
42 deletions.
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
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,172 @@ | ||
package polling | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
"net/url" | ||
"strings" | ||
"time" | ||
|
||
"github.com/cenkalti/backoff" | ||
"github.com/pegnet/pegnet/common" | ||
"github.com/zpatrick/go-config" | ||
) | ||
|
||
// OneForgeDataSource is the datasource at https://1forge.com | ||
type OneForgeDataSource struct { | ||
config *config.Config | ||
apikey string | ||
} | ||
|
||
func NewOneForgeDataSourceDataSource(config *config.Config) (*OneForgeDataSource, error) { | ||
s := new(OneForgeDataSource) | ||
s.config = config | ||
|
||
var err error | ||
// Load api key | ||
s.apikey, err = s.config.String(common.Config1ForgeKey) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return s, nil | ||
} | ||
|
||
func (d *OneForgeDataSource) Name() string { | ||
return "1Forge" | ||
} | ||
|
||
func (d *OneForgeDataSource) Url() string { | ||
return "https://1forge.com" | ||
} | ||
|
||
func (d *OneForgeDataSource) ApiUrl() string { | ||
return "https://forex.1forge.com/1.0.3/" | ||
} | ||
|
||
func (d *OneForgeDataSource) SupportedPegs() []string { | ||
// Does not have all the currencies, commodities, or crypto | ||
return common.MergeLists([]string{"EUR", "JPY", "GBP", "CAD", "CHF", "SGD", "HKD", "MXN"}, []string{"XAU", "XAG"}, []string{"XBT", "XBC", "LTC", "ETH", "DASH"}) | ||
} | ||
|
||
// AssetMapping changes some asset symbols to others to match 1forge | ||
func (d *OneForgeDataSource) AssetMapping() map[string]string { | ||
return map[string]string{ | ||
"XBT": "BTC", | ||
"XBC": "BCH", | ||
"DASH": "DSH", | ||
} | ||
} | ||
|
||
func (d *OneForgeDataSource) FetchPegPrices() (peg PegAssets, err error) { | ||
resp, err := d.Call1Forge() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
peg = make(map[string]PegItem) | ||
|
||
respRates := make(map[string]OneForgeDataSourceRate) | ||
for _, r := range resp { | ||
respRates[r.Symbol] = r | ||
} | ||
|
||
mapping := d.AssetMapping() | ||
|
||
// Look for each asset we support | ||
for _, asset := range d.SupportedPegs() { | ||
|
||
assetSym := asset | ||
if v, ok := mapping[asset]; ok { | ||
assetSym = v | ||
} | ||
|
||
index := fmt.Sprintf("%sUSD", assetSym) | ||
currency, ok := respRates[index] | ||
if !ok { | ||
continue | ||
} | ||
|
||
timestamp := time.Unix(currency.Timestamp, 0) | ||
peg[asset] = PegItem{Value: currency.Price, WhenUnix: timestamp.Unix(), When: timestamp} | ||
} | ||
|
||
return | ||
} | ||
|
||
func (d *OneForgeDataSource) FetchPegPrice(peg string) (i PegItem, err error) { | ||
return FetchPegPrice(peg, d.FetchPegPrices) | ||
} | ||
|
||
func (d *OneForgeDataSource) Call1Forge() ([]OneForgeDataSourceRate, error) { | ||
var resp []OneForgeDataSourceRate | ||
|
||
operation := func() error { | ||
data, err := d.FetchPeggedPrices() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
resp, err = d.ParseFetchedPrices(data) | ||
if err != nil { | ||
// Try the other variation | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
err := backoff.Retry(operation, PollingExponentialBackOff()) | ||
return resp, err | ||
} | ||
|
||
func (d *OneForgeDataSource) ParseFetchedPrices(data []byte) ([]OneForgeDataSourceRate, error) { | ||
var resp []OneForgeDataSourceRate | ||
|
||
err := json.Unmarshal(data, &resp) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return resp, nil | ||
} | ||
|
||
func (d *OneForgeDataSource) FetchPeggedPrices() ([]byte, error) { | ||
client := NewHTTPClient() | ||
req, err := http.NewRequest("GET", d.ApiUrl()+"quotes", nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
mapping := d.AssetMapping() | ||
|
||
var ids []string | ||
for _, asset := range d.SupportedPegs() { | ||
assetSym := asset | ||
if v, ok := mapping[asset]; ok { | ||
assetSym = v | ||
} | ||
ids = append(ids, assetSym+"USD") | ||
} | ||
|
||
q := url.Values{} | ||
q.Add("pairs", strings.Join(ids, ",")) | ||
q.Add("api_key", d.apikey) | ||
req.URL.RawQuery = q.Encode() | ||
|
||
resp, err := client.Do(req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer resp.Body.Close() | ||
|
||
return ioutil.ReadAll(resp.Body) | ||
} | ||
|
||
type OneForgeDataSourceRate struct { | ||
Symbol string `json:"symbol"` | ||
Bid float64 `json:"bid"` | ||
Ask float64 `json:"ask"` | ||
Price float64 `json:"price"` | ||
Timestamp int64 `json:"timestamp"` | ||
} |
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,18 @@ | ||
// Copyright (c) of parts are held by the various contributors (see the CLA) | ||
// Licensed under the MIT License. See LICENSE file in the project root for full license information. | ||
package polling_test | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestFixed1ForgePeggedAssets(t *testing.T) { | ||
FixedDataSourceTest(t, "1Forge", []byte(oneForgeResp)) | ||
} | ||
|
||
// Needs a key | ||
//func TestActual1ForgePeggedAssets(t *testing.T) { | ||
// ActualDataSourceTest(t, "1Forge") | ||
//} | ||
|
||
var oneForgeResp = `[{"symbol":"EURUSD","bid":1.10911,"ask":1.10911,"price":1.10911,"timestamp":1565971649},{"symbol":"JPYUSD","bid":0.00940673,"ask":0.00940689,"price":0.00940681,"timestamp":1565971649},{"symbol":"GBPUSD","bid":1.21479,"ask":1.21481,"price":1.2148,"timestamp":1565971649},{"symbol":"CADUSD","bid":0.752808,"ask":0.752819,"price":0.752814,"timestamp":1565971649},{"symbol":"CHFUSD","bid":1.02032,"ask":1.02034,"price":1.02033,"timestamp":1565971649},{"symbol":"SGDUSD","bid":0.72198,"ask":0.722006,"price":0.721993,"timestamp":1565971649},{"symbol":"HKDUSD","bid":0.127504,"ask":0.127507,"price":0.127506,"timestamp":1565971649},{"symbol":"MXNUSD","bid":0.0511065,"ask":0.0511099,"price":0.0511082,"timestamp":1565971649},{"symbol":"XAUUSD","bid":1512.68,"ask":1512.79,"price":1512.735,"timestamp":1565971649},{"symbol":"XAGUSD","bid":17.142,"ask":17.147,"price":17.1445,"timestamp":1565971649},{"symbol":"BTCUSD","bid":10335.2,"ask":10349.36,"price":10342.28,"timestamp":1565971649},{"symbol":"BCHUSD","bid":308.341,"ask":311.448,"price":309.8945,"timestamp":1565971649},{"symbol":"LTCUSD","bid":74.81,"ask":75.5,"price":75.155,"timestamp":1565971649},{"symbol":"ETHUSD","bid":184.31,"ask":186.64,"price":185.475,"timestamp":1565971649},{"symbol":"DSHUSD","bid":93.5413,"ask":94.4832,"price":94.0122,"timestamp":1565971649}]` |
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
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.