Skip to content
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: Require stablecoins to be converted to USD (backport #925) #938

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions price-feeder/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ Ref: https://keepachangelog.com/en/1.0.0/

## [Unreleased]

### Features

- [#925](https://github.com/umee-network/umee/pull/925) Require stablecoins to be converted to USD to protect against depegging.

## [v0.2.1](https://github.com/umee-network/umee/releases/tag/price-feeder%2Fv0.2.1) - 2022-04-06

### Improvements
Expand Down
21 changes: 17 additions & 4 deletions price-feeder/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

const (
denomUSD = "USD"
DenomUSD = "USD"

defaultListenAddr = "0.0.0.0:7171"
defaultSrvWriteTimeout = 15 * time.Second
Expand Down Expand Up @@ -184,13 +184,14 @@ func ParseConfig(configPath string) (Config, error) {
}

pairs := make(map[string]map[string]struct{})
coinQuotes := make(map[string]struct{})
for _, cp := range cfg.CurrencyPairs {
if !strings.Contains(strings.ToUpper(cp.Quote), denomUSD) {
return cfg, fmt.Errorf("unsupported pair quote: %s", cp.Quote)
}
if _, ok := pairs[cp.Base]; !ok {
pairs[cp.Base] = make(map[string]struct{})
}
if strings.ToUpper(cp.Quote) != DenomUSD {
coinQuotes[cp.Quote] = struct{}{}
}

for _, provider := range cp.Providers {
if _, ok := SupportedProviders[provider]; !ok {
Expand All @@ -200,6 +201,18 @@ func ParseConfig(configPath string) (Config, error) {
}
}

// Use coinQuotes to ensure that any quotes can be converted to USD.
for quote := range coinQuotes {
for index, pair := range cfg.CurrencyPairs {
if pair.Base == quote && pair.Quote == DenomUSD {
break
}
if index == len(cfg.CurrencyPairs)-1 {
return cfg, fmt.Errorf("all non-usd quotes require a conversion rate feed")
}
}
}

for base, providers := range pairs {
if _, ok := pairs[base]["mock"]; !ok && len(providers) < 3 {
return cfg, fmt.Errorf("must have at least three providers for %s", base)
Expand Down
55 changes: 52 additions & 3 deletions price-feeder/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,15 @@ providers = [
"huobi"
]

[[currency_pairs]]
base = "USDT"
quote = "USD"
providers = [
"kraken",
"binance",
"huobi"
]

[account]
address = "umee15nejfgcaanqpw25ru4arvfd0fwy6j8clccvwx4"
validator = "umeevalcons14rjlkfzp56733j5l5nfk6fphjxymgf8mj04d5p"
Expand Down Expand Up @@ -182,7 +191,7 @@ global_labels = [["chain-id", "umee-local-testnet"]]
require.Equal(t, "20s", cfg.Server.WriteTimeout)
require.Equal(t, "20s", cfg.Server.ReadTimeout)
require.True(t, cfg.Server.VerboseCORS)
require.Len(t, cfg.CurrencyPairs, 2)
require.Len(t, cfg.CurrencyPairs, 3)
require.Equal(t, "ATOM", cfg.CurrencyPairs[0].Base)
require.Equal(t, "USDT", cfg.CurrencyPairs[0].Quote)
require.Len(t, cfg.CurrencyPairs[0].Providers, 3)
Expand Down Expand Up @@ -222,6 +231,15 @@ providers = [
"huobi"
]

[[currency_pairs]]
base = "USDT"
quote = "USD"
providers = [
"kraken",
"binance",
"huobi"
]

[account]
address = "umee15nejfgcaanqpw25ru4arvfd0fwy6j8clccvwx4"
validator = "umeevalcons14rjlkfzp56733j5l5nfk6fphjxymgf8mj04d5p"
Expand Down Expand Up @@ -250,7 +268,7 @@ enabled = false
require.Equal(t, "20s", cfg.Server.WriteTimeout)
require.Equal(t, "20s", cfg.Server.ReadTimeout)
require.True(t, cfg.Server.VerboseCORS)
require.Len(t, cfg.CurrencyPairs, 2)
require.Len(t, cfg.CurrencyPairs, 3)
require.Equal(t, "ATOM", cfg.CurrencyPairs[0].Base)
require.Equal(t, "USDT", cfg.CurrencyPairs[0].Quote)
require.Len(t, cfg.CurrencyPairs[0].Providers, 3)
Expand All @@ -267,6 +285,37 @@ func TestParseConfig_InvalidProvider(t *testing.T) {
content := []byte(`
listen_addr = ""

[[currency_pairs]]
base = "ATOM"
quote = "USD"
providers = [
"kraken",
"binance"
]

[[currency_pairs]]
base = "UMEE"
quote = "USD"
providers = [
"kraken",
"foobar"
]
`)
_, err = tmpFile.Write(content)
require.NoError(t, err)

_, err = config.ParseConfig(tmpFile.Name())
require.Error(t, err)
}

func TestParseConfig_NonUSDQuote(t *testing.T) {
tmpFile, err := ioutil.TempFile("", "price-feeder.toml")
require.NoError(t, err)
defer os.Remove(tmpFile.Name())

content := []byte(`
listen_addr = ""

[[currency_pairs]]
base = "ATOM"
quote = "USDT"
Expand All @@ -280,7 +329,7 @@ base = "UMEE"
quote = "USDT"
providers = [
"kraken",
"foobar"
"binance"
]
`)
_, err = tmpFile.Write(content)
Expand Down
Loading