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

Fix cryptocom fetcher and tests #121

Merged
merged 1 commit into from
Jan 26, 2023
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
11 changes: 7 additions & 4 deletions aggregator/fetchers/cryptocom.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ type cryptocomPriceRequest struct {
}

type cryptocomData struct {
Data cryptocomPair `json:"data"`
Data []cryptocomPair `json:"data"`
}

type cryptocomPair struct {
Price float64 `json:"a"`
Price string `json:"a"`
}

type cryptocom struct {
Expand All @@ -41,10 +41,13 @@ func (c *cryptocom) FetchPrice(ctx context.Context, base, quote string) (float64
if err != nil {
return 0, err
}
if cpr.Result.Data.Price <= 0 {
if len(cpr.Result.Data) == 0 {
return 0, errInvalidResponseData
}
return cpr.Result.Data.Price, nil
if cpr.Result.Data[0].Price == "" {
return 0, errInvalidResponseData
}
return StrToPositiveFloat64(cpr.Result.Data[0].Price)
}

// Name returns the name
Expand Down
74 changes: 67 additions & 7 deletions aggregator/fetchers/fetchers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,24 @@ import (
"time"

"github.com/multiversx/mx-chain-core-go/core/check"
"github.com/multiversx/mx-chain-crypto-go/signing"
"github.com/multiversx/mx-chain-crypto-go/signing/ed25519"
"github.com/multiversx/mx-sdk-go/aggregator"
"github.com/multiversx/mx-sdk-go/aggregator/mock"
"github.com/multiversx/mx-sdk-go/authentication"
"github.com/multiversx/mx-sdk-go/blockchain"
"github.com/multiversx/mx-sdk-go/blockchain/cryptoProvider"
"github.com/multiversx/mx-sdk-go/core"
"github.com/multiversx/mx-sdk-go/examples"
"github.com/multiversx/mx-sdk-go/interactors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

var errShouldSkipTest = errors.New("should skip test")

const networkAddress = "https://testnet-gateway.multiversx.com"

func createMockMap() map[string]MaiarTokensPair {
return map[string]MaiarTokensPair{
"ETH-USD": {
Expand All @@ -35,20 +45,70 @@ func createMockMap() map[string]MaiarTokensPair {
}
}

func createAuthClient() (authentication.AuthClient, error) {
w := interactors.NewWallet()
privateKeyBytes, err := w.LoadPrivateKeyFromPemData([]byte(examples.AlicePemContents))
if err != nil {
return nil, err
}

argsProxy := blockchain.ArgsProxy{
ProxyURL: networkAddress,
SameScState: false,
ShouldBeSynced: false,
FinalityCheck: false,
AllowedDeltaToFinal: 1,
CacheExpirationTime: time.Second,
EntityType: core.Proxy,
}

proxy, err := blockchain.NewProxy(argsProxy)
if err != nil {
return nil, err
}

keyGen := signing.NewKeyGenerator(ed25519.NewEd25519())
holder, _ := cryptoProvider.NewCryptoComponentsHolder(keyGen, privateKeyBytes)
args := authentication.ArgsNativeAuthClient{
Signer: cryptoProvider.NewSigner(),
ExtraInfo: nil,
Proxy: proxy,
CryptoComponentsHolder: holder,
TokenExpiryInSeconds: 60 * 60 * 24,
Host: "oracle",
}

authClient, err := authentication.NewNativeAuthClient(args)
if err != nil {
return nil, err
}

return authClient, nil
}

func Test_FunctionalTesting(t *testing.T) {
t.Parallel()

responseGetter, err := aggregator.NewHttpResponseGetter()
require.Nil(t, err)

authClient, err := createAuthClient()
require.Nil(t, err)

graphqlGetter, err := aggregator.NewGraphqlResponseGetter(authClient)
require.Nil(t, err)

for f := range ImplementedFetchers {
fetcherName := f
t.Run("Test_FunctionalTesting_"+fetcherName, func(t *testing.T) {
t.Skip("this test should be run only when doing debugging work on the component")

t.Parallel()
fetcher, _ := NewPriceFetcher(fetcherName, &mock.HttpResponseGetterStub{}, &mock.GraphqlResponseGetterStub{}, createMockMap())
fetcher, _ := NewPriceFetcher(fetcherName, responseGetter, graphqlGetter, createMockMap())
ethTicker := "ETH"
fetcher.AddPair(ethTicker, quoteUSDFiat)
price, err := fetcher.FetchPrice(context.Background(), ethTicker, quoteUSDFiat)
require.Nil(t, err)
price, fetchErr := fetcher.FetchPrice(context.Background(), ethTicker, quoteUSDFiat)
require.Nil(t, fetchErr)
fmt.Printf("price between %s and %s is: %v from %s\n", ethTicker, quoteUSDFiat, price, fetcherName)
require.True(t, price > 0)
})
Expand Down Expand Up @@ -319,10 +379,10 @@ func getFuncGetCalled(name, returnPrice, pair string, returnErr error) func(ctx
case CryptocomName:
return func(ctx context.Context, url string, response interface{}) error {
cast, _ := response.(*cryptocomPriceRequest)
var err error
cast.Result.Data.Price, err = strconv.ParseFloat(returnPrice, 64)
if err != nil {
return errShouldSkipTest
cast.Result.Data = []cryptocomPair{
{
Price: returnPrice,
},
}
return returnErr
}
Expand Down
2 changes: 1 addition & 1 deletion aggregator/fetchers/maiar.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

const (
// TODO EN-13146: extract this urls constants in a file
dataApiUrl = "https://tools.elrond.com/data-api/graphql"
dataApiUrl = "https://tools.multiversx.com/data-api/graphql"
query = "query MaiarPriceUrl($base: String!, $quote: String!) { trading { pair(first_token: $base, second_token: $quote) { price { last time } } } }"
)

Expand Down
2 changes: 1 addition & 1 deletion examples/examplesPriceAggregator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ func createAuthClient() (authentication.AuthClient, error) {
FinalityCheck: false,
AllowedDeltaToFinal: 1,
CacheExpirationTime: time.Second,
EntityType: core.RestAPIEntityType("Proxy"),
EntityType: core.Proxy,
}

proxy, err := blockchain.NewProxy(argsProxy)
Expand Down