Skip to content

Commit

Permalink
feat(okx.ws-private): listen balance
Browse files Browse the repository at this point in the history
  • Loading branch information
iam4x committed Apr 20, 2023
1 parent b29d856 commit 0d34072
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion src/exchanges/okx/okx.ws-private.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import createHmac from 'create-hmac';
import { sumBy } from 'lodash';

import { roundUSD } from '../../utils/round-usd';
import { subtract } from '../../utils/safe-math';
import { virtualClock } from '../../utils/virtual-clock';
import { BaseWebSocket } from '../base.ws';

Expand Down Expand Up @@ -61,10 +62,11 @@ export class OKXPrivateWebsocket extends BaseWebSocket<OKXExchange> {
JSON.stringify({
op: 'subscribe',
args: [
{ channel: 'account' },
{ channel: 'positions', instType: 'SWAP' },
{ channel: 'orders', instType: 'SWAP' },
{ channel: 'orders-algo', instType: 'SWAP' },
{ channel: 'algo-advance', instType: 'SWAP' },
{ channel: 'positions', instType: 'SWAP' },
],
})
);
Expand Down Expand Up @@ -96,6 +98,11 @@ export class OKXPrivateWebsocket extends BaseWebSocket<OKXExchange> {

if (data.includes('"channel":"positions"')) {
this.handlePositionTopic(JSON.parse(data));
return;
}

if (data.includes('"channel":"account"')) {
this.handleAccountTopic(JSON.parse(data));
}
};

Expand Down Expand Up @@ -153,4 +160,16 @@ export class OKXPrivateWebsocket extends BaseWebSocket<OKXExchange> {
});
}
};

handleAccountTopic = ({ data }: { data: Array<Record<string, any>> }) => {
const totalCollateral = roundUSD(sumBy(data, (b) => parseFloat(b.totalEq)));

this.store.update({
balance: {
...this.store.balance,
free: subtract(totalCollateral, this.store.balance.used),
total: subtract(totalCollateral, this.store.balance.upnl),
},
});
};
}

0 comments on commit 0d34072

Please sign in to comment.