Skip to content

Commit

Permalink
Merge pull request #44 from vegaprotocol/refactor-price-proxy
Browse files Browse the repository at this point in the history
feat: refactor price proxy to make it more readable and more optimal
  • Loading branch information
daniel1302 authored Nov 17, 2022
2 parents 7bfbe6c + 97bd39d commit 64e27be
Show file tree
Hide file tree
Showing 17 changed files with 949 additions and 547 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/continous-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ jobs:
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
version: v1.45
version: v1.50.1
args: --config .golangci.toml

5 changes: 5 additions & 0 deletions .golangci.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ max-same-issues = 0
[linters]
enable-all = true
disable = [
"nosnakecase",
"structcheck",
"varcheck",
"deadcode",
"promlinter",
"wrapcheck",
"tagliatelle",
Expand Down Expand Up @@ -61,6 +65,7 @@ disable = [
"errchkjson",
"ifshort",
"predeclared",
"exhaustruct",
"nolintlint",
]

Expand Down
21 changes: 21 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "priceproxy",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${workspaceFolder}/cmd/priceproxy/main.go",
"args": [
"-config", "${workspaceFolder}/config.yaml"
],
"env": {
"CMC_PRO_API_KEY": "XXXXXXXXXXXXX"
}
}
]
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ See the [releases page](https://github.com/vegaprotocol/priceproxy/releases/).
Either run `go get`:

```bash
go get code.vegaprotocol.io/priceproxy/cmd/priceproxy@latest
go get github.com/vegaprotocol/priceproxy/cmd/priceproxy@latest
```

Or clone the repository:
Expand Down
242 changes: 242 additions & 0 deletions config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,242 @@
server:
listen: ":80"
logformat: text # json, text
loglevel: debug
env: prod # dev, prod

sources:
- name: bitstamp
# Avoid BitStamp, it has a tendency to return HTTP 404 if the UserAgent is not a browser.
sleepReal: 60 # seconds
url:
scheme: https
host: www.bitstamp.net
path: /api/v2/ticker/

- name: coinmarketcap
sleepReal: 400 # seconds
auth_key_env_name: "CMC_PRO_API_KEY" # this env variable must be exported
url:
scheme: https
host: pro-api.coinmarketcap.com
path: /v1/cryptocurrency/listings/latest

- name: coingecko
sleepReal: 30 # seconds
url:
scheme: https
host: api.coingecko.com
path: /api/v3/simple/price
rawquery: ids=solana,ethereum,bitcoin,terra-luna-2,uniswap,dai,aave,aapl,litecoin,optimism,monero,cosmos&vs_currencies=usd,eur,btc,eth&include_last_updated_at=true

# - name: exchangeratesapi
# sleepReal: 600 # seconds
# sleepWander: 30 # seconds
# url:
# scheme: https
# host: api.exchangeratesapi.io
# path: /latest
# rawquery: base={base}&symbols={quote}


prices:
# Faked markets not available in our sources
- source: coinmarketcap
base: AAVE
base_override: AAPL
quote: USD
quote_override: USD
factor: 1.2
wander: true

- source: coingecko
base: monero
base_override: TSLA
quote: EUR
quote_override: EURO
factor: 1.4
wander: true

# Real currencies
- source: coinmarketcap
base: BTC
quote: USD
factor: 1.0
wander: true

- source: coingecko
base: monero
quote: ETH
factor: 1.0
wander: true

- source: coingecko
base: terra-luna-2
quote: ETH
factor: 1.0
wander: true

- source: coingecko
base: aave
quote: dai
factor: 1.0
wander: true

- source: coingecko
base: aave
base_override: AAVE
quote: dai
quote_override: DAI
factor: 1.0
wander: true

- source: coingecko
base: monero
quote: usd
factor: 1.0
wander: true

- source: coingecko
base: bitcoin
quote: ETH
factor: 1.0
wander: true

- source: coingecko
base: cosmos
quote: ETH
factor: 1.0
wander: true

- source: coingecko
base: terra-luna-2
quote: usd
factor: 1.0
wander: true

- source: coingecko
base: solana
quote: ETH
factor: 1.0
wander: true

- source: coingecko
base: cosmos
quote: usd
factor: 1.0
wander: true

- source: coingecko
base: ethereum
quote: ETH
factor: 1.0
wander: true

- source: coingecko
base: uniswap
quote: ETH
factor: 1.0
wander: true

- source: coingecko
base: optimism
quote: usd
factor: 1.0
wander: true

- source: coingecko
base: litecoin
quote: usd
factor: 1.0
wander: true

- source: coingecko
base: litecoin
quote: ETH
factor: 1.0
wander: true

- source: coingecko
base: dai
quote: ETH
factor: 1.0
wander: true

- source: coingecko
base: ethereum
quote: btc
factor: 1.0
wander: true

- source: coinmarketcap
base: LUNC
quote: USD
factor: 1.0
wander: true

- source: coinmarketcap
base: LUNC
base_override: LUNA
quote: USD
factor: 1.0
wander: true

- source: coinmarketcap
base: XMR
quote: USD
factor: 1.0
wander: true

- source: coingecko
base: optimism
quote: ETH
factor: 1.0
wander: true

- source: coinmarketcap
base: UNI
quote: DAI
factor: 1.0
wander: true

- source: coingecko
base: uniswap
quote: dai
factor: 1.0
wander: true

- source: coingecko
base: bitcoin
quote: usd
factor: 1.0
wander: true

- source: coingecko
base: aave
quote: ETH
factor: 1.0
wander: true

- source: coinmarketcap
base: ETH
quote: BTC
factor: 1.0
wander: true

- source: coinmarketcap
base: ETH
quote: DAI
factor: 1.0
wander: true

- source: bitstamp
base: IMX
quote: USD
factor: 1.0
wander: true

- source: bitstamp
base: IMX
quote: ETH
factor: 1.0
wander: true
56 changes: 41 additions & 15 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"errors"
"fmt"
"net/url"
"strings"
"time"

log "github.com/sirupsen/logrus"
Expand All @@ -21,29 +22,45 @@ type ServerConfig struct {

// PriceConfig describes one price setting, which uses one source.
type PriceConfig struct {
Source string `yaml:"source"`
Base string `yaml:"base"`
Quote string `yaml:"quote"`
Factor float64 `yaml:"factor"`
Wander bool `yaml:"wander"`
Source string `yaml:"source"`
Base string `yaml:"base"`
BaseOverride string `yaml:"base_override"`
Quote string `yaml:"quote"`
QuoteOverride string `yaml:"quote_override"`
Factor float64 `yaml:"factor"`
Wander bool `yaml:"wander"`
}

// SourceConfig describes one source setting (e.g. one API endpoint).
// The URL has "{base}" and "{quote}" replaced at runtime with entries from PriceConfig.
type SourceConfig struct {
Name string `yaml:"name"`
URL url.URL `yaml:"url"`
SleepReal int `yaml:"sleepReal"`
SleepWander int `yaml:"sleepWander"`
Name string `yaml:"name"`
URL url.URL `yaml:"url"`
AuthKeyEnvName string `yaml:"auth_key_env_name"`
SleepReal int `yaml:"sleepReal"`
}

type PriceList []PriceConfig

// Config describes the top level config file format.
type Config struct {
Server *ServerConfig `yaml:"server"`
Prices []*PriceConfig `yaml:"prices"`
Prices PriceList `yaml:"prices"`
Sources []*SourceConfig `yaml:"sources"`
}

func (pl PriceList) GetBySource(source string) PriceList {
result := PriceList{}

for _, price := range pl {
if price.Source == source {
result = append(result, price)
}
}

return result
}

var (
// ErrNil indicates that a nil/null pointer was encountered.
ErrNil = errors.New("nil pointer")
Expand Down Expand Up @@ -74,9 +91,6 @@ func CheckConfig(cfg *Config) error {
if sourcecfg.SleepReal == 0 {
return fmt.Errorf("%s: sleepReal", ErrInvalidValue.Error())
}
if sourcecfg.SleepWander == 0 {
return fmt.Errorf("%s: sleepWander", ErrInvalidValue.Error())
}
}

if cfg.Prices == nil {
Expand Down Expand Up @@ -144,6 +158,18 @@ func (pc PriceConfig) String() string {
}

func (ps SourceConfig) String() string {
return fmt.Sprintf("{SourceConfig Name:%s URL:%s SleepReal:%ds SleepWander:%ds}",
ps.Name, ps.URL.String(), ps.SleepReal, ps.SleepWander)
return fmt.Sprintf("{SourceConfig Name:%s URL:%s SleepReal:%ds}",
ps.Name, ps.URL.String(), ps.SleepReal)
}

func (ps SourceConfig) IsCoinGecko() bool {
return strings.Contains(ps.URL.Host, "coingecko.com")
}

func (ps SourceConfig) IsCoinMarketCap() bool {
return strings.Contains(ps.URL.Host, "coinmarketcap.com")
}

func (ps SourceConfig) IsBitstamp() bool {
return strings.Contains(ps.URL.Host, "bitstamp.net")
}
Loading

0 comments on commit 64e27be

Please sign in to comment.