Skip to content

Commit

Permalink
Removed go-preev (deprecated)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrz1836 committed Jun 2, 2022
1 parent ae4ee3a commit 6783908
Show file tree
Hide file tree
Showing 12 changed files with 30 additions and 264 deletions.
38 changes: 0 additions & 38 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"net/http"
"time"

"github.com/mrz1836/go-preev"
"github.com/mrz1836/go-whatsonchain"
)

Expand All @@ -26,7 +25,6 @@ type CoinPaprikaInterface interface {
// Client is the parent struct that contains the provider clients and list of providers to use
type Client struct {
coinPaprika CoinPaprikaInterface // Coin Paprika client
preev preev.ClientInterface // Preev Client
providers []Provider // List of providers to use (in order for fail-over)
whatsOnChain whatsonchain.ChainService // WhatsOnChain (chain services)
}
Expand All @@ -48,25 +46,6 @@ type ClientOptions struct {
UserAgent string `json:"user_agent"`
}

// ToPreevOptions will convert the current options to Preev Options
func (c *ClientOptions) ToPreevOptions() (options *preev.Options) {
options = preev.ClientDefaultOptions()
options.UserAgent = c.UserAgent + " using " + options.UserAgent
options.BackOffExponentFactor = c.BackOffExponentFactor
options.BackOffInitialTimeout = c.BackOffInitialTimeout
options.BackOffMaximumJitterInterval = c.BackOffMaximumJitterInterval
options.BackOffMaxTimeout = c.BackOffMaxTimeout
options.DialerKeepAlive = c.DialerKeepAlive
options.DialerTimeout = c.DialerTimeout
options.RequestRetryCount = c.RequestRetryCount
options.RequestTimeout = c.RequestTimeout
options.TransportExpectContinueTimeout = c.TransportExpectContinueTimeout
options.TransportIdleTimeout = c.TransportIdleTimeout
options.TransportMaxIdleConnections = c.TransportMaxIdleConnections
options.TransportTLSHandshakeTimeout = c.TransportTLSHandshakeTimeout
return
}

// ToWhatsOnChainOptions will convert the current options to WOC Options
func (c *ClientOptions) ToWhatsOnChainOptions() (options *whatsonchain.Options) {
options = whatsonchain.ClientDefaultOptions()
Expand Down Expand Up @@ -129,11 +108,6 @@ func NewClient(clientOptions *ClientOptions, customHTTPClient HTTPInterface,
clientOptions, customHTTPClient,
)

// Create a client for Preev
c.preev = preev.NewClient(
clientOptions.ToPreevOptions(), customHTTPClient,
)

// Create a client for WhatsOnChain
c.whatsOnChain = whatsonchain.NewClient(
whatsonchain.NetworkMain, clientOptions.ToWhatsOnChainOptions(), customHTTPClient,
Expand All @@ -159,18 +133,6 @@ func (c *Client) SetCoinPaprika(client CoinPaprikaInterface) {
}
}

// Preev will return the client
func (c *Client) Preev() preev.ClientInterface {
return c.preev
}

// SetPreev will set the client
func (c *Client) SetPreev(client preev.ClientInterface) {
if client != nil {
c.preev = client
}
}

// WhatsOnChain will return the client
func (c *Client) WhatsOnChain() whatsonchain.ChainService {
return c.whatsOnChain
Expand Down
29 changes: 2 additions & 27 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,14 @@ func TestNewClient(t *testing.T) {
// Test default providers
assert.Equal(t, ProviderCoinPaprika, client.Providers()[0])
assert.Equal(t, ProviderWhatsOnChain, client.Providers()[1])
assert.Equal(t, ProviderPreev, client.Providers()[2])
})

t.Run("custom http client", func(t *testing.T) {
client := NewClient(nil, http.DefaultClient, ProviderPreev)
client := NewClient(nil, http.DefaultClient, ProviderCoinPaprika)
assert.NotNil(t, client)

// Test custom providers
assert.Equal(t, ProviderPreev, client.Providers()[0])
assert.Equal(t, ProviderCoinPaprika, client.Providers()[0])
})

}
Expand Down Expand Up @@ -69,30 +68,6 @@ func TestDefaultClientOptions(t *testing.T) {
})
}

// TestClientOptions_ToPreevOptions tests setting ToPreevOptions()
func TestClientOptions_ToPreevOptions(t *testing.T) {
t.Parallel()

t.Run("convert default options to Preev options", func(t *testing.T) {
options := DefaultClientOptions()
preevOptions := options.ToPreevOptions()

assert.Contains(t, preevOptions.UserAgent, defaultUserAgent)
assert.Equal(t, 2.0, preevOptions.BackOffExponentFactor)
assert.Equal(t, 2*time.Millisecond, preevOptions.BackOffInitialTimeout)
assert.Equal(t, 2*time.Millisecond, preevOptions.BackOffMaximumJitterInterval)
assert.Equal(t, 10*time.Millisecond, preevOptions.BackOffMaxTimeout)
assert.Equal(t, 20*time.Second, preevOptions.DialerKeepAlive)
assert.Equal(t, 5*time.Second, preevOptions.DialerTimeout)
assert.Equal(t, 2, preevOptions.RequestRetryCount)
assert.Equal(t, 10*time.Second, preevOptions.RequestTimeout)
assert.Equal(t, 3*time.Second, preevOptions.TransportExpectContinueTimeout)
assert.Equal(t, 20*time.Second, preevOptions.TransportIdleTimeout)
assert.Equal(t, 10, preevOptions.TransportMaxIdleConnections)
assert.Equal(t, 5*time.Second, preevOptions.TransportTLSHandshakeTimeout)
})
}

// TestClientOptions_ToWhatsOnChainOptions tests setting ToWhatsOnChainOptions()
func TestClientOptions_ToWhatsOnChainOptions(t *testing.T) {
t.Parallel()
Expand Down
2 changes: 1 addition & 1 deletion coinpaprika_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,7 @@ func TestPaprikaClient_GetHistoricalTickers(t *testing.T) {
})

t.Run("invalid response", func(t *testing.T) {
client = newMockClient(&mockWOCValid{}, &mockPaprikaFailed{}, &mockPreevValid{})
client = newMockClient(&mockWOCValid{}, &mockPaprikaFailed{})
assert.NotNil(t, client)

resp, rateErr := client.CoinPaprika().GetHistoricalTickers(
Expand Down
12 changes: 3 additions & 9 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,18 @@ import "strings"
const (

// version is the current package version
version = "v0.2.2"
version = "v0.3.0"

// defaultUserAgent is the default user agent for all requests
defaultUserAgent string = "go-bsvrates: " + version

// CoinPaprikaQuoteID is the id for CoinPaprika (BSV)
CoinPaprikaQuoteID = "bsv-bitcoin-sv"

// PreevTickerID is the id for Preev (BSV)
PreevTickerID = "12eLTxv1vyUeJtp5zqWbqpdWvfLdZ7dGf8"
)

var (
// defaultProviders (if no provider slice is set, use this as the default set)
defaultProviders = []Provider{ProviderCoinPaprika, ProviderWhatsOnChain, ProviderPreev}
defaultProviders = []Provider{ProviderCoinPaprika, ProviderWhatsOnChain}
)

// Provider is a provider for rates or prices
Expand All @@ -32,8 +29,7 @@ const (

ProviderWhatsOnChain // 1
ProviderCoinPaprika // 2
ProviderPreev // 3
providerLast // 4
providerLast // 3
)

// IsValid tests if the provider is valid or not
Expand All @@ -48,8 +44,6 @@ func (p Provider) Name() string {
return "WhatsOnChain"
case ProviderCoinPaprika:
return "CoinPaprika"
case ProviderPreev:
return "Preev"
case providerLast:
return ""
default:
Expand Down
12 changes: 3 additions & 9 deletions config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,9 @@ func TestProvider_IsValid(t *testing.T) {
{"provider 0", 0, false},
{"provider 1", 1, true},
{"provider 2", 2, true},
{"provider 3", 3, true},
{"provider 4", 4, false},
{"provider 3", 3, false},
{"ProviderWhatsOnChain", ProviderWhatsOnChain, true},
{"ProviderCoinPaprika", ProviderCoinPaprika, true},
{"ProviderPreev", ProviderPreev, true},
{"providerLast", providerLast, false},
}
for _, test := range tests {
Expand All @@ -45,11 +43,9 @@ func TestProvider_Name(t *testing.T) {
{"provider 0", 0, ""},
{"provider 1", 1, "WhatsOnChain"},
{"provider 2", 2, "CoinPaprika"},
{"provider 3", 3, "Preev"},
{"provider 4", 4, ""},
{"provider 4", 3, ""},
{"ProviderWhatsOnChain", ProviderWhatsOnChain, "WhatsOnChain"},
{"ProviderCoinPaprika", ProviderCoinPaprika, "CoinPaprika"},
{"ProviderPreev", ProviderPreev, "Preev"},
{"providerLast", providerLast, ""},
}
for _, test := range tests {
Expand All @@ -72,11 +68,9 @@ func TestProviderToName(t *testing.T) {
{"provider 0", 0, ""},
{"provider 1", 1, "WhatsOnChain"},
{"provider 2", 2, "CoinPaprika"},
{"provider 3", 3, "Preev"},
{"provider 4", 4, ""},
{"provider 4", 3, ""},
{"ProviderWhatsOnChain", ProviderWhatsOnChain, "WhatsOnChain"},
{"ProviderCoinPaprika", ProviderCoinPaprika, "CoinPaprika"},
{"ProviderPreev", ProviderPreev, "Preev"},
{"providerLast", providerLast, ""},
}
for _, test := range tests {
Expand Down
8 changes: 0 additions & 8 deletions conversions.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"strconv"

"github.com/mrz1836/go-preev"
"github.com/mrz1836/go-whatsonchain"
)

Expand Down Expand Up @@ -38,13 +37,6 @@ func (c *Client) GetConversion(ctx context.Context, currency Currency, amount fl
satoshis, err = ConvertPriceToSatoshis(rate, amount)
}
}
case ProviderPreev:
var response *preev.Ticker
if response, err = c.Preev().GetTicker(
ctx, PreevTickerID,
); err == nil && response != nil {
satoshis, err = ConvertPriceToSatoshis(response.Prices.Ppi.LastPrice, amount)
}
case providerLast:
err = fmt.Errorf("provider unknown")
return
Expand Down
40 changes: 9 additions & 31 deletions conversions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func TestClient_GetConversion(t *testing.T) {
t.Parallel()

t.Run("valid get conversion - default", func(t *testing.T) {
client := newMockClient(&mockWOCValid{}, &mockPaprikaValid{}, &mockPreevValid{})
client := newMockClient(&mockWOCValid{}, &mockPaprikaValid{})
assert.NotNil(t, client)

satoshis, provider, err := client.GetConversion(context.Background(), CurrencyDollars, 1)
Expand All @@ -22,19 +22,8 @@ func TestClient_GetConversion(t *testing.T) {
assert.Equal(t, "CoinPaprika", provider.Name())
})

t.Run("valid get conversion - preev", func(t *testing.T) {
client := newMockClient(&mockWOCValid{}, &mockPaprikaValid{}, &mockPreevValid{}, ProviderPreev)
assert.NotNil(t, client)

satoshis, provider, err := client.GetConversion(context.Background(), CurrencyDollars, 1)
assert.NoError(t, err)
assert.Equal(t, int64(628260), satoshis)
assert.Equal(t, true, provider.IsValid())
assert.Equal(t, "Preev", provider.Name())
})

t.Run("valid get conversion - whats on chain", func(t *testing.T) {
client := newMockClient(&mockWOCValid{}, &mockPaprikaValid{}, &mockPreevValid{}, ProviderWhatsOnChain)
client := newMockClient(&mockWOCValid{}, &mockPaprikaValid{}, ProviderWhatsOnChain)
assert.NotNil(t, client)

satoshis, provider, err := client.GetConversion(context.Background(), CurrencyDollars, 1)
Expand All @@ -45,7 +34,7 @@ func TestClient_GetConversion(t *testing.T) {
})

t.Run("valid get conversion - coin paprika", func(t *testing.T) {
client := newMockClient(&mockWOCValid{}, &mockPaprikaValid{}, &mockPreevValid{}, ProviderCoinPaprika)
client := newMockClient(&mockWOCValid{}, &mockPaprikaValid{}, ProviderCoinPaprika)
assert.NotNil(t, client)

satoshis, provider, err := client.GetConversion(context.Background(), CurrencyDollars, 1)
Expand All @@ -56,37 +45,26 @@ func TestClient_GetConversion(t *testing.T) {
})

t.Run("valid get conversion - custom provider list", func(t *testing.T) {
client := newMockClient(&mockWOCValid{}, &mockPaprikaValid{}, &mockPreevValid{}, ProviderPreev, ProviderWhatsOnChain)
client := newMockClient(&mockWOCValid{}, &mockPaprikaValid{}, ProviderCoinPaprika, ProviderWhatsOnChain)
assert.NotNil(t, client)

satoshis, provider, err := client.GetConversion(context.Background(), CurrencyDollars, 1)
assert.NoError(t, err)
assert.NotEqual(t, 633157, satoshis)
assert.Equal(t, true, provider.IsValid())
assert.Equal(t, "Preev", provider.Name())
assert.Equal(t, "CoinPaprika", provider.Name())
})

t.Run("non accepted currency", func(t *testing.T) {
client := newMockClient(&mockWOCValid{}, &mockPaprikaFailed{}, &mockPreevValid{})
client := newMockClient(&mockWOCValid{}, &mockPaprikaFailed{})
assert.NotNil(t, client)

_, _, rateErr := client.GetConversion(context.Background(), 123, 1)
assert.Error(t, rateErr)
})

t.Run("failed conversion - default", func(t *testing.T) {
client := newMockClient(&mockWOCValid{}, &mockPaprikaFailed{}, &mockPreevValid{})
assert.NotNil(t, client)

satoshis, provider, err := client.GetConversion(context.Background(), CurrencyDollars, 1)
assert.NoError(t, err)
assert.Equal(t, int64(628892), satoshis)
assert.Equal(t, true, provider.IsValid())
assert.Equal(t, "WhatsOnChain", provider.Name())
})

t.Run("failed conversion - preev", func(t *testing.T) {
client := newMockClient(&mockWOCValid{}, &mockPaprikaFailed{}, &mockPreevFailed{})
client := newMockClient(&mockWOCValid{}, &mockPaprikaFailed{})
assert.NotNil(t, client)

satoshis, provider, err := client.GetConversion(context.Background(), CurrencyDollars, 1)
Expand All @@ -97,7 +75,7 @@ func TestClient_GetConversion(t *testing.T) {
})

t.Run("failed conversion - whats on chain", func(t *testing.T) {
client := newMockClient(&mockWOCFailed{}, &mockPaprikaValid{}, &mockPreevFailed{})
client := newMockClient(&mockWOCFailed{}, &mockPaprikaValid{})
assert.NotNil(t, client)

satoshis, provider, err := client.GetConversion(context.Background(), CurrencyDollars, 1)
Expand All @@ -108,7 +86,7 @@ func TestClient_GetConversion(t *testing.T) {
})

t.Run("failed conversion - all providers", func(t *testing.T) {
client := newMockClient(&mockWOCFailed{}, &mockPaprikaFailed{}, &mockPreevFailed{})
client := newMockClient(&mockWOCFailed{}, &mockPaprikaFailed{})
assert.NotNil(t, client)

satoshis, _, err := client.GetConversion(context.Background(), CurrencyDollars, 1)
Expand Down
3 changes: 0 additions & 3 deletions interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package bsvrates
import (
"context"

"github.com/mrz1836/go-preev"
"github.com/mrz1836/go-whatsonchain"
)

Expand All @@ -17,10 +16,8 @@ type RateService interface {
type ClientInterface interface {
RateService
CoinPaprika() CoinPaprikaInterface
Preev() preev.ClientInterface
Providers() []Provider
SetCoinPaprika(client CoinPaprikaInterface)
SetPreev(client preev.ClientInterface)
SetWhatsOnChain(client whatsonchain.ChainService)
WhatsOnChain() whatsonchain.ChainService
}
Loading

0 comments on commit 6783908

Please sign in to comment.