Skip to content

Commit

Permalink
Added test for perp pricing
Browse files Browse the repository at this point in the history
  • Loading branch information
chathumi-k committed Jan 8, 2025
1 parent 9ac0b21 commit cf969f6
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 58 deletions.
16 changes: 16 additions & 0 deletions marketdata/rest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1468,3 +1468,19 @@ func TestLatestCryptoPerpQuotes(t *testing.T) {
}, got["ETH-PERP"])
assert.Equal(t, 1.9428, got["BTC-PERP"].BidSize)
}

func TestGetLatestCryptoPerpPricing(t *testing.T) {
c := DefaultClient
c.do = mockResp(`{"futuresPricing": {"BTC-PERP": {"t": "2024-12-19T09:33:36.311Z", "ft": "2024-12-19T10:33:36.311Z", "oi": 90.7367, "ip": 50702.8, "mp": 50652.3553, "fr": 0.000565699}}}`)
got, err := c.GetLatestCryptoPerpPricing("BTC-PERP", GetLatestCryptoPerpPricingRequest{})
require.NoError(t, err)
require.NotNil(t, got)
assert.Equal(t, CryptoPerpPricing{
IndexPrice: 50702.8,
MarkPrice: 50652.3553,
OpenInterest: 90.7367,
FundingRate: 0.000565699,
Timestamp: time.Date(2024, 12, 19, 9, 33, 36, 311000000, time.UTC),
NextFundingTime: time.Date(2024, 12, 19, 10, 33, 36, 311000000, time.UTC),
}, *got)
}
94 changes: 66 additions & 28 deletions marketdata/stream/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func TestConnectFails(t *testing.T) {
WithReconnectSettings(1, 0),
withConnCreator(connCreator))
case cryptoPerpTests:
c = NewCryptoPerpClient(
c = NewCryptoClient(
marketdata.GLOBAL,
WithReconnectSettings(1, 0),
withConnCreator(connCreator))
Expand Down Expand Up @@ -94,7 +94,7 @@ func TestConnectWithInvalidURL(t *testing.T) {
WithBaseURL("http://192.168.0.%31/"),
WithReconnectSettings(1, 0))
case cryptoPerpTests:
c = NewCryptoPerpClient(
c = NewCryptoClient(
marketdata.GLOBAL,
WithBaseURL("http://192.168.0.%31/"),
WithReconnectSettings(1, 0))
Expand Down Expand Up @@ -141,7 +141,7 @@ func TestConnectImmediatelyFailsAfterIrrecoverableErrors(t *testing.T) {
case cryptoTests:
c = NewCryptoClient(marketdata.US, reconnectSettings, withConnCreator(connCreator))
case cryptoPerpTests:
c = NewCryptoPerpClient(marketdata.GLOBAL, reconnectSettings, withConnCreator(connCreator))
c = NewCryptoClient(marketdata.GLOBAL, reconnectSettings, withConnCreator(connCreator))
}

// server welcomes the client
Expand Down Expand Up @@ -192,7 +192,7 @@ func TestContextCancelledBeforeConnect(t *testing.T) {
WithBaseURL("http://test.paca/v2"),
withConnCreator(connCreator))
case cryptoPerpTests:
c = NewCryptoPerpClient(
c = NewCryptoClient(
marketdata.GLOBAL,
WithBaseURL("http://test.paca/v2"),
withConnCreator(connCreator))
Expand Down Expand Up @@ -225,7 +225,7 @@ func TestConnectSucceeds(t *testing.T) {
case cryptoTests:
c = NewCryptoClient(marketdata.US, withConnCreator(connCreator))
case cryptoPerpTests:
c = NewCryptoPerpClient(marketdata.GLOBAL, withConnCreator(connCreator))
c = NewCryptoClient(marketdata.GLOBAL, withConnCreator(connCreator))
}

ctx, cancel := context.WithCancel(context.Background())
Expand Down Expand Up @@ -279,7 +279,7 @@ func TestCallbacksCalledOnConnectAndDisconnect(t *testing.T) {
WithConnectCallback(connectCallback),
WithDisconnectCallback(disconnectCallback))
case cryptoPerpTests:
c = NewCryptoPerpClient(marketdata.GLOBAL,
c = NewCryptoClient(marketdata.GLOBAL,
withConnCreator(connCreator),
WithConnectCallback(connectCallback),
WithDisconnectCallback(disconnectCallback))
Expand Down Expand Up @@ -359,6 +359,8 @@ func TestSubscribeBeforeConnectCrypto(t *testing.T) {
assert.Equal(t, ErrSubscriptionChangeBeforeConnect, err)
err = c.SubscribeToOrderbooks(func(_ CryptoOrderbook) {})
assert.Equal(t, ErrSubscriptionChangeBeforeConnect, err)
err = c.SubscribeToPerpPricing(func(_ CryptoPerpPricing) {})
assert.Equal(t, ErrSubscriptionChangeBeforeConnect, err)
err = c.UnsubscribeFromTrades()
assert.Equal(t, ErrSubscriptionChangeBeforeConnect, err)
err = c.UnsubscribeFromQuotes()
Expand All @@ -371,6 +373,8 @@ func TestSubscribeBeforeConnectCrypto(t *testing.T) {
assert.Equal(t, ErrSubscriptionChangeBeforeConnect, err)
err = c.UnsubscribeFromOrderbooks()
assert.Equal(t, ErrSubscriptionChangeBeforeConnect, err)
err = c.UnsubscribeFromPerpPricing()
assert.Equal(t, ErrSubscriptionChangeBeforeConnect, err)
}

func TestSubscribeMultipleCallsStocks(t *testing.T) {
Expand Down Expand Up @@ -970,7 +974,7 @@ func TestPingFails(t *testing.T) {
c = NewCryptoClient(marketdata.US,
WithReconnectSettings(1, 0), withConnCreator(connCreator))
case cryptoPerpTests:
c = NewCryptoPerpClient(marketdata.GLOBAL,
c = NewCryptoClient(marketdata.GLOBAL,
WithReconnectSettings(1, 0), withConnCreator(connCreator))
}

Expand All @@ -993,7 +997,7 @@ func TestPingFails(t *testing.T) {
return nil, connErr
}
case cryptoPerpTests:
c.(*CryptoPerpClient).connCreator = func(_ context.Context, _ url.URL) (conn, error) {
c.(*CryptoClient).connCreator = func(_ context.Context, _ url.URL) (conn, error) {
return nil, connErr
}
}
Expand Down Expand Up @@ -1614,7 +1618,7 @@ func TestCryptoClientConstructURL(t *testing.T) {
}

func TestSubscribeBeforeConnectCryptoPerp(t *testing.T) {
c := NewCryptoPerpClient(marketdata.GLOBAL, WithBaseURL(perpBaseURL))
c := NewCryptoClient(marketdata.GLOBAL, WithBaseURL(perpBaseURL))

err := c.SubscribeToTrades(func(_ CryptoTrade) {})
assert.Equal(t, ErrSubscriptionChangeBeforeConnect, err)
Expand All @@ -1628,6 +1632,8 @@ func TestSubscribeBeforeConnectCryptoPerp(t *testing.T) {
assert.Equal(t, ErrSubscriptionChangeBeforeConnect, err)
err = c.SubscribeToOrderbooks(func(_ CryptoOrderbook) {})
assert.Equal(t, ErrSubscriptionChangeBeforeConnect, err)
err = c.SubscribeToPerpPricing(func(_ CryptoPerpPricing) {})
assert.Equal(t, ErrSubscriptionChangeBeforeConnect, err)
err = c.UnsubscribeFromTrades()
assert.Equal(t, ErrSubscriptionChangeBeforeConnect, err)
err = c.UnsubscribeFromQuotes()
Expand All @@ -1640,14 +1646,16 @@ func TestSubscribeBeforeConnectCryptoPerp(t *testing.T) {
assert.Equal(t, ErrSubscriptionChangeBeforeConnect, err)
err = c.UnsubscribeFromOrderbooks()
assert.Equal(t, ErrSubscriptionChangeBeforeConnect, err)
err = c.UnsubscribeFromPerpPricing()
assert.Equal(t, ErrSubscriptionChangeBeforeConnect, err)
}

func TestSubscribeCalledButClientTerminatesCryptoPerp(t *testing.T) {
connection := newMockConn()
defer connection.close()
writeInitialFlowMessagesToConn(t, connection, subscriptions{})

c := NewCryptoPerpClient(marketdata.GLOBAL,
c := NewCryptoClient(marketdata.GLOBAL,
WithBaseURL(perpBaseURL),
WithCredentials("my_key", "my_secret"),
withConnCreator(func(_ context.Context, _ url.URL) (conn, error) { return connection, nil }))
Expand Down Expand Up @@ -1687,7 +1695,7 @@ func TestSubscribeFailsDueToErrorCryptoPerp(t *testing.T) {
defer connection.close()
writeInitialFlowMessagesToConn(t, connection, subscriptions{})

c := NewCryptoPerpClient(marketdata.GLOBAL,
c := NewCryptoClient(marketdata.GLOBAL,
WithBaseURL(perpBaseURL),
WithCredentials("my_key", "my_secret"),
withConnCreator(func(_ context.Context, _ url.URL) (conn, error) {
Expand Down Expand Up @@ -1774,12 +1782,13 @@ func TestCoreFunctionalityCryptoPerp(t *testing.T) {
connection := newMockConn()
defer connection.close()
writeInitialFlowMessagesToConn(t, connection, subscriptions{
trades: []string{"BTC-PERP"},
quotes: []string{"BTC-PERP"},
bars: []string{"BTC-PERP"},
updatedBars: []string{"BTC-PERP"},
dailyBars: []string{"BTC-PERP"},
orderbooks: []string{"BTC-PERP"},
trades: []string{"BTC-PERP"},
quotes: []string{"BTC-PERP"},
bars: []string{"BTC-PERP"},
updatedBars: []string{"BTC-PERP"},
dailyBars: []string{"BTC-PERP"},
orderbooks: []string{"BTC-PERP"},
futuresPricing: []string{"BTC-PERP"},
})

trades := make(chan CryptoTrade, 10)
Expand All @@ -1788,14 +1797,16 @@ func TestCoreFunctionalityCryptoPerp(t *testing.T) {
updatedBars := make(chan CryptoBar, 10)
dailyBars := make(chan CryptoBar, 10)
orderbooks := make(chan CryptoOrderbook, 10)
c := NewCryptoPerpClient(marketdata.GLOBAL,
pricing := make(chan CryptoPerpPricing, 10)
c := NewCryptoClient(marketdata.GLOBAL,
WithBaseURL(perpBaseURL),
WithCryptoTrades(func(t CryptoTrade) { trades <- t }, "BTC-PERP"),
WithCryptoQuotes(func(q CryptoQuote) { quotes <- q }, "BTC-PERP"),
WithCryptoBars(func(b CryptoBar) { bars <- b }, "BTC-PERP"),
WithCryptoUpdatedBars(func(b CryptoBar) { updatedBars <- b }, "BTC-PERP"),
WithCryptoDailyBars(func(b CryptoBar) { dailyBars <- b }, "BTC-PERP"),
WithCryptoOrderbooks(func(ob CryptoOrderbook) { orderbooks <- ob }, "BTC-PERP"),
WithCryptoPerpPricing(func(p CryptoPerpPricing) { pricing <- p }, "BTC-PERP"),
withConnCreator(func(_ context.Context, _ url.URL) (conn, error) {
return connection, nil
}))
Expand All @@ -1811,15 +1822,15 @@ func TestCoreFunctionalityCryptoPerp(t *testing.T) {
cryptoBarWithT{
Type: "b",
Symbol: "BTC-PERP",
Exchange: "TEST",
Exchange: "PERP",
Volume: 10,
TradeCount: 3,
VWAP: 123.45,
},
cryptoBarWithT{
Type: "d",
Symbol: "BTC-PERP",
Exchange: "TES7",
Exchange: "PERP",
Open: 196.05,
High: 196.3,
TradeCount: 32,
Expand All @@ -1828,7 +1839,7 @@ func TestCoreFunctionalityCryptoPerp(t *testing.T) {
cryptoBarWithT{
Type: "u",
Symbol: "BTC-PERP",
Exchange: "TES7",
Exchange: "PERP",
TradeCount: 33,
},
cryptoQuoteWithT{
Expand All @@ -1838,7 +1849,7 @@ func TestCoreFunctionalityCryptoPerp(t *testing.T) {
AskSize: 3.12,
BidPrice: 2712.82,
BidSize: 3.982,
Exchange: "TEST",
Exchange: "PERP",
},
})
// sending a trade
Expand All @@ -1848,7 +1859,7 @@ func TestCoreFunctionalityCryptoPerp(t *testing.T) {
Type: "t",
Symbol: "BTC-PERP",
Timestamp: ts,
Exchange: "TST",
Exchange: "PERP",
Price: 4123.123,
Size: 34.876,
ID: 25,
Expand All @@ -1860,7 +1871,7 @@ func TestCoreFunctionalityCryptoPerp(t *testing.T) {
cryptoOrderbookWithT{
Type: "o",
Symbol: "BTC-PERP",
Exchange: "TST",
Exchange: "PERP",
Bids: []cryptoOrderbookEntry{
{Price: 111.1, Size: 222.2},
{Price: 333.3, Size: 444.4},
Expand All @@ -1871,14 +1882,29 @@ func TestCoreFunctionalityCryptoPerp(t *testing.T) {
},
},
})
// sending perp pricing
nft := time.Date(2021, 6, 2, 20, 12, 4, 3534, time.UTC)
connection.readCh <- serializeToMsgpack(t, []interface{}{
cryptoPerpPricingWithT{
Type: "p",
Symbol: "BTC-PERP",
Exchange: "PERP",
Timestamp: ts,
IndexPrice: 0.72817,
MarkPrice: 0.727576296,
FundingRate: -0.000439065,
OpenInterest: 46795,
NextFundingTime: nft,
},
})

// checking contents
select {
case bar := <-bars:
assert.EqualValues(t, 10, bar.Volume)
assert.EqualValues(t, 3, bar.TradeCount)
assert.EqualValues(t, 123.45, bar.VWAP)
assert.Equal(t, "TEST", bar.Exchange)
assert.Equal(t, "PERP", bar.Exchange)
case <-time.After(time.Second):
require.Fail(t, "no bar received in time")
}
Expand All @@ -1896,7 +1922,7 @@ func TestCoreFunctionalityCryptoPerp(t *testing.T) {
assert.EqualValues(t, 196.3, dailyBar.High)
assert.EqualValues(t, 32, dailyBar.TradeCount)
assert.EqualValues(t, 196.21, dailyBar.VWAP)
assert.Equal(t, "TES7", dailyBar.Exchange)
assert.Equal(t, "PERP", dailyBar.Exchange)
case <-time.After(time.Second):
require.Fail(t, "no daily bar received in time")
}
Expand All @@ -1907,7 +1933,7 @@ func TestCoreFunctionalityCryptoPerp(t *testing.T) {
assert.EqualValues(t, 2848.53, quote.AskPrice)
assert.EqualValues(t, 3.12, quote.AskSize)
assert.EqualValues(t, 3.982, quote.BidSize)
assert.EqualValues(t, "TEST", quote.Exchange)
assert.EqualValues(t, "PERP", quote.Exchange)
case <-time.After(time.Second):
require.Fail(t, "no quote received in time")
}
Expand All @@ -1928,6 +1954,18 @@ func TestCoreFunctionalityCryptoPerp(t *testing.T) {
case <-time.After(time.Second):
require.Fail(t, "no orderbook received in time")
}

select {
case p := <-pricing:
assert.Equal(t, "BTC-PERP", p.Symbol)
assert.EqualValues(t, 0.72817, p.IndexPrice)
assert.EqualValues(t, 0.727576296, p.MarkPrice)
assert.EqualValues(t, -0.000439065, p.FundingRate)
assert.EqualValues(t, 46795, p.OpenInterest)
assert.EqualValues(t, "PERP", p.Exchange)
case <-time.After(time.Second):
require.Fail(t, "no perp pricing received in time")
}
}

func TestCryptoPerpClientConstructURL(t *testing.T) {
Expand Down Expand Up @@ -1959,7 +1997,7 @@ func TestCryptoPerpClientConstructURL(t *testing.T) {
},
} {
t.Run(test.name, func(t *testing.T) {
c := NewCryptoPerpClient(
c := NewCryptoClient(
marketdata.GLOBAL,
WithBaseURL(test.baseURL),
)
Expand Down
Loading

0 comments on commit cf969f6

Please sign in to comment.