Skip to content

Commit

Permalink
feat: order history stream (#72)
Browse files Browse the repository at this point in the history
* feat: order history stream

* feat: order history stream

* feat: order history stream
  • Loading branch information
afterburn authored Sep 1, 2022
1 parent e95489f commit 4a1f6cd
Show file tree
Hide file tree
Showing 6 changed files with 194 additions and 48 deletions.
2 changes: 1 addition & 1 deletion packages/sdk-ts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"@injectivelabs/chain-api": "^1.8.0-rc8",
"@injectivelabs/exceptions": "^1.0.9",
"@injectivelabs/exchange-api": "^2.2.77",
"@injectivelabs/indexer-api": "^1.0.15",
"@injectivelabs/indexer-api": "^1.0.16",
"@injectivelabs/networks": "^1.0.14",
"@injectivelabs/token-metadata": "^1.0.22",
"@injectivelabs/ts-types": "^1.0.8",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,20 @@ import {
StreamPositionsResponse,
StreamMarketRequest,
StreamMarketResponse,
StreamOrdersHistoryRequest,
StreamOrdersHistoryResponse,
} from '@injectivelabs/indexer-api/injective_derivative_exchange_rpc_pb'
import { InjectiveDerivativeExchangeRPCClient } from '@injectivelabs/indexer-api/injective_derivative_exchange_rpc_pb_service'
import { TradeDirection, TradeExecutionSide } from '../../../types'
import {
TradeDirection,
TradeExecutionSide,
TradeExecutionType,
} from '../../../types'
import { StreamStatusResponse } from '../types'
import { isServerSide } from '../../../utils/helpers'
import { NodeHttpTransport } from '@improbable-eng/grpc-web-node-http-transport'
import { PaginationOption } from '../../../types/pagination'
import { DerivativeOrderSide } from '../types/derivatives'
import { DerivativeOrderSide, DerivativeOrderState } from '../types/derivatives'
import { IndexerDerivativeStreamTransformer } from '../transformers'

export type DerivativeOrderbookStreamCallback = (
Expand All @@ -31,6 +37,12 @@ export type DerivativeOrdersStreamCallback = (
>,
) => void

export type DerivativeOrderHistoryStreamCallback = (
resposne: ReturnType<
typeof IndexerDerivativeStreamTransformer.orderHistoryStreamCallback
>,
) => void

export type DerivativeTradesStreamCallback = (
response: ReturnType<
typeof IndexerDerivativeStreamTransformer.tradesStreamCallback
Expand Down Expand Up @@ -138,6 +150,72 @@ export class IndexerGrpcDerivativesStream {
return stream
}

streamDerivativeOrderHistory({
subaccountId,
marketId,
orderTypes,
executionTypes,
direction,
state,
callback,
onEndCallback,
onStatusCallback,
}: {
marketId?: string
subaccountId?: string
orderTypes?: DerivativeOrderSide[]
executionTypes?: TradeExecutionType[]
direction?: TradeDirection
state?: DerivativeOrderState
callback: DerivativeOrderHistoryStreamCallback
onEndCallback?: (status?: StreamStatusResponse) => void
onStatusCallback?: (status: StreamStatusResponse) => void
}) {
const request = new StreamOrdersHistoryRequest()

if (subaccountId) {
request.setSubaccountId(subaccountId)
}

if (marketId) {
request.setMarketId(marketId)
}

if (orderTypes) {
request.setOrderTypesList(orderTypes)
}

if (direction) {
request.setDirection(direction)
}

if (state) {
request.setState(state)
}

if (executionTypes) {
request.setExecutionTypesList(executionTypes)
}

const stream = this.client.streamOrdersHistory(request)

stream.on('data', (response: StreamOrdersHistoryResponse) => {
callback(
IndexerDerivativeStreamTransformer.orderHistoryStreamCallback(response),
)
})

if (onEndCallback) {
stream.on('end', onEndCallback)
}

if (onStatusCallback) {
stream.on('status', onStatusCallback)
}

return stream
}

streamDerivativeTrades({
marketIds,
marketId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,20 @@ import {
StreamTradesResponse,
StreamMarketsRequest,
StreamMarketsResponse,
StreamOrdersHistoryRequest,
StreamOrdersHistoryResponse,
} from '@injectivelabs/indexer-api/injective_spot_exchange_rpc_pb'
import { InjectiveSpotExchangeRPCClient } from '@injectivelabs/indexer-api/injective_spot_exchange_rpc_pb_service'
import { TradeExecutionSide, TradeDirection } from '../../../types'
import {
TradeExecutionSide,
TradeDirection,
TradeExecutionType,
} from '../../../types'
import { StreamStatusResponse } from '../types'
import { isServerSide } from '../../../utils/helpers'
import { NodeHttpTransport } from '@improbable-eng/grpc-web-node-http-transport'
import { PaginationOption } from '../../../types/pagination'
import { SpotOrderSide } from '../types/spot'
import { SpotOrderSide, SpotOrderState } from '../types/spot'
import { IndexerSpotStreamTransformer } from '../transformers'

export type MarketsStreamCallback = (response: StreamMarketsResponse) => void
Expand All @@ -31,6 +37,12 @@ export type SpotOrdersStreamCallback = (
>,
) => void

export type SpotOrderHistoryStreamCallback = (
response: ReturnType<
typeof IndexerSpotStreamTransformer.orderHistoryStreamCallback
>,
) => void

export type SpotTradesStreamCallback = (
response: ReturnType<
typeof IndexerSpotStreamTransformer.tradesStreamCallback
Expand Down Expand Up @@ -126,6 +138,72 @@ export class IndexerGrpcSpotStream {
return stream
}

streamSpotOrderHistory({
marketId,
subaccountId,
orderTypes,
executionTypes,
direction,
state,
callback,
onEndCallback,
onStatusCallback,
}: {
marketId?: string
subaccountId?: string
orderTypes?: SpotOrderSide[]
executionTypes?: TradeExecutionType[]
direction?: TradeDirection
state?: SpotOrderState
callback: SpotOrderHistoryStreamCallback
onEndCallback?: (status?: StreamStatusResponse) => void
onStatusCallback?: (status: StreamStatusResponse) => void
}) {
const request = new StreamOrdersHistoryRequest()

if (subaccountId) {
request.setSubaccountId(subaccountId)
}

if (marketId) {
request.setMarketId(marketId)
}

if (orderTypes) {
request.setOrderTypesList(orderTypes)
}

if (direction) {
request.setDirection(direction)
}

if (state) {
request.setState(state)
}

if (executionTypes) {
request.setExecutionTypesList(executionTypes)
}

const stream = this.client.streamOrdersHistory(request)

stream.on('data', (response: StreamOrdersHistoryResponse) => {
callback(
IndexerSpotStreamTransformer.orderHistoryStreamCallback(response)
)
})

if (onEndCallback) {
stream.on('end', onEndCallback)
}

if (onStatusCallback) {
stream.on('status', onStatusCallback)
}

return stream
}

streamSpotTrades({
marketIds,
marketId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
StreamTradesResponse,
StreamPositionsResponse,
StreamOrdersResponse,
StreamOrdersHistoryResponse
} from '@injectivelabs/indexer-api/injective_derivative_exchange_rpc_pb'
import { StreamOperation } from '../../../types/index'
import { IndexerGrpcDerivativeTransformer } from './IndexerGrpcDerivativeTransformer'
Expand Down Expand Up @@ -61,4 +62,16 @@ export class IndexerDerivativeStreamTransformer {
timestamp: response.getTimestamp(),
}
}

static orderHistoryStreamCallback = (response: StreamOrdersHistoryResponse) => {
const order = response.getOrder()

return {
order: order
? IndexerGrpcDerivativeTransformer.grpcOrderHistoryToOrderHistory(order)
: undefined,
operation: response.getOperationType() as StreamOperation,
timestamp: response.getTimestamp()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
StreamOrderbookResponse,
StreamTradesResponse,
StreamOrdersResponse,
StreamOrdersHistoryResponse,
} from '@injectivelabs/indexer-api/injective_spot_exchange_rpc_pb'
import { StreamOperation } from '../../../types'
import { IndexerGrpcSpotTransformer } from './IndexerGrpcSpotTransformer'
Expand Down Expand Up @@ -49,4 +50,16 @@ export class IndexerSpotStreamTransformer {
timestamp: response.getTimestamp(),
}
}

static orderHistoryStreamCallback = (response: StreamOrdersHistoryResponse) => {
const order = response.getOrder()

return {
order: order
? IndexerGrpcSpotTransformer.grpcOrderHistoryToOrderHistory(order)
: undefined,
operation: response.getOperationType() as StreamOperation,
timestamp: response.getTimestamp(),
}
}
}
50 changes: 7 additions & 43 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1718,10 +1718,10 @@
"@improbable-eng/grpc-web" "^0.14.0"
google-protobuf "^3.14.0"

"@injectivelabs/indexer-api@^1.0.15":
version "1.0.15"
resolved "https://registry.yarnpkg.com/@injectivelabs/indexer-api/-/indexer-api-1.0.15.tgz#cf71963973cf78eb843106ff412dd7d566d8bc89"
integrity sha512-eT0MFEhlRLwaqOE/i1m4VfQDh8dJzwW1RMm1lHeuwBCLuz/0VpFiFnmWQvgPnU6769l9SlIncvsFVx/cBfE4jQ==
"@injectivelabs/indexer-api@^1.0.16":
version "1.0.16"
resolved "https://registry.yarnpkg.com/@injectivelabs/indexer-api/-/indexer-api-1.0.16.tgz#36a762a771c2644a26b56250eed56ff23500dfbc"
integrity sha512-694Pije1YfNwe8UyrflvfzPgTvVF70hxkmWdzOkEZkQ1fybGcpXtAlCI2rIxcUXKqU13qnK9g8drl7tNqqb/fg==
dependencies:
"@improbable-eng/grpc-web" "^0.14.0"
google-protobuf "^3.14.0"
Expand Down Expand Up @@ -8075,7 +8075,7 @@ glob-parent@^5.1.1, glob-parent@^5.1.2, glob-parent@~5.1.2:
dependencies:
is-glob "^4.0.1"

glob@7.2.3, glob@^7.0.0, glob@^7.0.5, glob@^7.1.1, glob@^7.1.3, glob@^7.1.4, glob@^7.1.6, glob@^7.2.0:
glob@7.2.3, glob@^7.0.5, glob@^7.1.1, glob@^7.1.3, glob@^7.1.4, glob@^7.1.6, glob@^7.2.0:
version "7.2.3"
resolved "https://registry.yarnpkg.com/glob/-/glob-7.2.3.tgz#b8df0fb802bbfa8e89bd1d938b4e16578ed44f2b"
integrity sha512-nFR0zLpU2YCaRxwoCJvL6UvCH2JFyFVIvwTLsIf21AuHlMskA1hhTdk+LlYJtOlYt9v6dvszD2BGRqBL+iQK9Q==
Expand Down Expand Up @@ -8725,11 +8725,6 @@ internal-slot@^1.0.3:
has "^1.0.3"
side-channel "^1.0.4"

interpret@^1.0.0:
version "1.4.0"
resolved "https://registry.yarnpkg.com/interpret/-/interpret-1.4.0.tgz#665ab8bc4da27a774a40584e812e3e0fa45b1a1e"
integrity sha512-agE4QfB2Lkp9uICn7BAqoscw4SZP9kTE2hxiFI3jBPmXJfdqiahTbUuKGsMoN2GtqL9AxhYioAcVvgsb1HvRbA==

invariant@2:
version "2.2.4"
resolved "https://registry.yarnpkg.com/invariant/-/invariant-2.2.4.tgz#610f3c92c9359ce1db616e538008d23ff35158e6"
Expand Down Expand Up @@ -10040,13 +10035,6 @@ lines-and-columns@^1.1.6:
resolved "https://registry.yarnpkg.com/lines-and-columns/-/lines-and-columns-1.2.4.tgz#eca284f75d2965079309dc0ad9255abb2ebc1632"
integrity sha512-7ylylesZQ/PV29jhEDl3Ufjo6ZX7gCqJr5F7PKrqc93v7fzSymt1BpwEU8nAUXs8qzzvqhbjhK5QZg6Mt/HkBg==

link-module-alias@^1.2.0:
version "1.2.0"
resolved "https://registry.yarnpkg.com/link-module-alias/-/link-module-alias-1.2.0.tgz#6a3b7b014cfe18b2759a1222fffce6a40fc120e4"
integrity sha512-ahPjXepbSVKbahTB6LxR//VHm8HPfI+QQygCH+E82spBY4HR5VPJTvlhKBc9F7muVxnS6C1rRfoPOXAbWO/fyw==
dependencies:
chalk "^2.4.1"

linkify-it@^3.0.1:
version "3.0.3"
resolved "https://registry.yarnpkg.com/linkify-it/-/linkify-it-3.0.3.tgz#a98baf44ce45a550efb4d49c769d07524cc2fa2e"
Expand Down Expand Up @@ -10732,7 +10720,7 @@ minimist-options@4.1.0:
is-plain-obj "^1.1.0"
kind-of "^6.0.3"

minimist@1.2.6, minimist@^1.2.0, minimist@^1.2.3, minimist@^1.2.5, minimist@^1.2.6, minimist@~1.2.5:
minimist@1.2.6, minimist@^1.2.0, minimist@^1.2.5, minimist@^1.2.6, minimist@~1.2.5:
version "1.2.6"
resolved "https://registry.yarnpkg.com/minimist/-/minimist-1.2.6.tgz#8637a5b759ea0d6e98702cfb3a9283323c93af44"
integrity sha512-Jsjnk4bw3YJqYzbdyBiNsPWHPfO++UGG749Cxs6peCu5Xg4nrena6OVxOYxrQTqww0Jmwt+Ref8rggumkTLz9Q==
Expand Down Expand Up @@ -12466,13 +12454,6 @@ readonly-date@^1.0.0:
resolved "https://registry.yarnpkg.com/readonly-date/-/readonly-date-1.0.0.tgz#5af785464d8c7d7c40b9d738cbde8c646f97dcd9"
integrity sha512-tMKIV7hlk0h4mO3JTmmVuIlJVXjKk3Sep9Bf5OH0O+758ruuVkUy2J9SttDLm91IEX/WHlXPSpxMGjPj4beMIQ==

rechoir@^0.6.2:
version "0.6.2"
resolved "https://registry.yarnpkg.com/rechoir/-/rechoir-0.6.2.tgz#85204b54dba82d5742e28c96756ef43af50e3384"
integrity sha512-HFM8rkZ+i3zrV+4LQjwQ0W+ez98pApMGM3HUrN04j3CqzPOzl9nmP15Y8YXNm8QHGv/eacOVEjqhmWpkRV0NAw==
dependencies:
resolve "^1.1.6"

redent@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/redent/-/redent-3.0.0.tgz#e557b7998316bb53c9f1f56fa626352c6963059f"
Expand Down Expand Up @@ -12626,7 +12607,7 @@ resolve.exports@^1.1.0:
resolved "https://registry.yarnpkg.com/resolve.exports/-/resolve.exports-1.1.0.tgz#5ce842b94b05146c0e03076985d1d0e7e48c90c9"
integrity sha512-J1l+Zxxp4XK3LUDZ9m60LRJF/mAe4z6a4xyabPHk7pvK5t35dACV32iIjJDFeWZFfZlO29w6SZ67knR0tHzJtQ==

resolve@^1.0.0, resolve@^1.1.6, resolve@^1.10.0, resolve@^1.14.2, resolve@^1.20.0, resolve@^1.22.0:
resolve@^1.0.0, resolve@^1.10.0, resolve@^1.14.2, resolve@^1.20.0, resolve@^1.22.0:
version "1.22.1"
resolved "https://registry.yarnpkg.com/resolve/-/resolve-1.22.1.tgz#27cb2ebb53f91abb49470a928bba7558066ac177"
integrity sha512-nBpuuYuY5jFsli/JIs1oldw6fOQCBioohqWZg/2hiaOybXOft4lonv85uDOKXdf8rhyK159cxU5cDcK/NKk8zw==
Expand Down Expand Up @@ -13057,15 +13038,6 @@ shell-quote@^1.6.1:
resolved "https://registry.yarnpkg.com/shell-quote/-/shell-quote-1.7.3.tgz#aa40edac170445b9a431e17bb62c0b881b9c4123"
integrity sha512-Vpfqwm4EnqGdlsBFNmHhxhElJYrdfcxPThu+ryKS5J8L/fhAwLazFZtq+S+TWZ9ANj2piSQLGj6NQg+lKPmxrw==

shelljs@^0.8.5:
version "0.8.5"
resolved "https://registry.yarnpkg.com/shelljs/-/shelljs-0.8.5.tgz#de055408d8361bed66c669d2f000538ced8ee20c"
integrity sha512-TiwcRcrkhHvbrZbnRcFYMLl30Dfov3HKqzp5tO5b4pt6G/SezKcYhmDg15zXVBswHmctSAQKznqNW2LO5tTDow==
dependencies:
glob "^7.0.0"
interpret "^1.0.0"
rechoir "^0.6.2"

shiki@^0.10.1:
version "0.10.1"
resolved "https://registry.yarnpkg.com/shiki/-/shiki-0.10.1.tgz#6f9a16205a823b56c072d0f1a0bcd0f2646bef14"
Expand All @@ -13075,14 +13047,6 @@ shiki@^0.10.1:
vscode-oniguruma "^1.6.1"
vscode-textmate "5.2.0"

shx@^0.3.2, shx@^0.3.3:
version "0.3.4"
resolved "https://registry.yarnpkg.com/shx/-/shx-0.3.4.tgz#74289230b4b663979167f94e1935901406e40f02"
integrity sha512-N6A9MLVqjxZYcVn8hLmtneQWIJtp8IKzMP4eMnx+nqkvXoqinUPCbUFLp2UcWTEIUONhlk0ewxr/jaVGlc+J+g==
dependencies:
minimist "^1.2.3"
shelljs "^0.8.5"

side-channel@^1.0.4:
version "1.0.4"
resolved "https://registry.yarnpkg.com/side-channel/-/side-channel-1.0.4.tgz#efce5c8fdc104ee751b25c58d4290011fa5ea2cf"
Expand Down

0 comments on commit 4a1f6cd

Please sign in to comment.