github.com/InjectiveLabs/sdk-go@v1.53.0/client/chain/markets_assistant_test.go (about) 1 package chain 2 3 import ( 4 "context" 5 "net/http" 6 "net/http/httptest" 7 "strings" 8 "testing" 9 10 "github.com/InjectiveLabs/sdk-go/client/common" 11 12 "github.com/InjectiveLabs/sdk-go/client/exchange" 13 derivativeExchangePB "github.com/InjectiveLabs/sdk-go/exchange/derivative_exchange_rpc/pb" 14 spotExchangePB "github.com/InjectiveLabs/sdk-go/exchange/spot_exchange_rpc/pb" 15 banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" 16 "github.com/stretchr/testify/assert" 17 ) 18 19 func TestMarketAssistantCreationUsingMarketsFromExchange(t *testing.T) { 20 httpServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 21 w.WriteHeader(http.StatusOK) 22 _, _ = w.Write([]byte("[]")) 23 })) 24 defer httpServer.Close() 25 26 network := common.NewNetwork() 27 network.OfficialTokensListURL = httpServer.URL 28 29 mockExchange := exchange.MockExchangeClient{} 30 mockExchange.Network = network 31 var spotMarketInfos []*spotExchangePB.SpotMarketInfo 32 var derivativeMarketInfos []*derivativeExchangePB.DerivativeMarketInfo 33 injUsdtSpotMarketInfo := createINJUSDTSpotMarketInfo() 34 apeUsdtSpotMarketInfo := createAPEUSDTSpotMarketInfo() 35 btcUsdtDerivativeMarketInfo := createBTCUSDTDerivativeMarketInfo() 36 37 spotMarketInfos = append(spotMarketInfos, injUsdtSpotMarketInfo) 38 spotMarketInfos = append(spotMarketInfos, apeUsdtSpotMarketInfo) 39 derivativeMarketInfos = append(derivativeMarketInfos, btcUsdtDerivativeMarketInfo) 40 41 mockExchange.SpotMarketsResponses = append(mockExchange.SpotMarketsResponses, &spotExchangePB.MarketsResponse{ 42 Markets: spotMarketInfos, 43 }) 44 mockExchange.DerivativeMarketsResponses = append(mockExchange.DerivativeMarketsResponses, &derivativeExchangePB.MarketsResponse{ 45 Markets: derivativeMarketInfos, 46 }) 47 48 ctx := context.Background() 49 assistant, err := NewMarketsAssistantInitializedFromChain(ctx, &mockExchange) 50 51 assert.NoError(t, err) 52 53 tokens := assistant.AllTokens() 54 55 assert.Len(t, tokens, 5) 56 57 symbols := strings.Split(injUsdtSpotMarketInfo.Ticker, "/") 58 injSymbol, usdtSymbol := symbols[0], symbols[1] 59 symbols = strings.Split(apeUsdtSpotMarketInfo.Ticker, "/") 60 apeSymbol := symbols[0] 61 alternativeUSDTName := apeUsdtSpotMarketInfo.QuoteTokenMeta.Name 62 usdtPerpSymbol := btcUsdtDerivativeMarketInfo.QuoteTokenMeta.Symbol 63 64 _, isPresent := tokens[injSymbol] 65 assert.True(t, isPresent) 66 _, isPresent = tokens[usdtSymbol] 67 assert.True(t, isPresent) 68 _, isPresent = tokens[alternativeUSDTName] 69 assert.True(t, isPresent) 70 _, isPresent = tokens[apeSymbol] 71 assert.True(t, isPresent) 72 _, isPresent = tokens[usdtPerpSymbol] 73 assert.True(t, isPresent) 74 75 spotMarkets := assistant.AllSpotMarkets() 76 assert.Len(t, spotMarkets, 2) 77 78 _, isPresent = spotMarkets[injUsdtSpotMarketInfo.MarketId] 79 assert.True(t, isPresent) 80 _, isPresent = spotMarkets[apeUsdtSpotMarketInfo.MarketId] 81 assert.True(t, isPresent) 82 83 derivativeMarkets := assistant.AllDerivativeMarkets() 84 assert.Len(t, derivativeMarkets, 1) 85 86 _, isPresent = derivativeMarkets[btcUsdtDerivativeMarketInfo.MarketId] 87 assert.True(t, isPresent) 88 } 89 90 func TestMarketAssistantCreationWithAllTokens(t *testing.T) { 91 httpServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 92 w.WriteHeader(http.StatusOK) 93 _, _ = w.Write([]byte("[]")) 94 })) 95 defer httpServer.Close() 96 97 network := common.NewNetwork() 98 network.OfficialTokensListURL = httpServer.URL 99 100 mockExchange := exchange.MockExchangeClient{} 101 mockExchange.Network = network 102 mockChain := MockChainClient{} 103 smartDenomMetadata := createSmartDenomMetadata() 104 105 mockExchange.SpotMarketsResponses = append(mockExchange.SpotMarketsResponses, &spotExchangePB.MarketsResponse{}) 106 mockExchange.DerivativeMarketsResponses = append(mockExchange.DerivativeMarketsResponses, &derivativeExchangePB.MarketsResponse{}) 107 108 mockChain.DenomsMetadataResponses = append(mockChain.DenomsMetadataResponses, &banktypes.QueryDenomsMetadataResponse{ 109 Metadatas: []banktypes.Metadata{smartDenomMetadata}, 110 }) 111 112 ctx := context.Background() 113 assistant, err := NewMarketsAssistantWithAllTokens(ctx, &mockExchange, &mockChain) 114 115 assert.NoError(t, err) 116 117 tokens := assistant.AllTokens() 118 119 assert.Len(t, tokens, 1) 120 121 _, isPresent := tokens[smartDenomMetadata.Symbol] 122 assert.True(t, isPresent) 123 }