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

pass wallets data to strategy executors #251

Merged
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
43 changes: 43 additions & 0 deletions lib/ws_servers/api/handlers/strategy/strategy_manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ const debug = require('debug')('bfx:hf:server:strategy-manager')

const CLOSE_CONNECTIONS_DELAY = 60 * 1000 // one minute
const EXECUTION_RESULTS_OMIT_FIELDS = ['candles', 'trades']
const WALLET_SNAPSHOT_EVENT = 'auth:ws'
const WALLET_UPDATE_EVENT = 'auth:wu'

class StrategyManager {
constructor (settings, bcast, strategyExecutionDB, sendDataToMetricsServer) {
Expand Down Expand Up @@ -106,11 +108,51 @@ class StrategyManager {
this.ws2Manager.onceWS('event:auth:success', {}, this._onAuthSuccess.bind(this, resolve))
this.ws2Manager.onceWS('event:auth:error', {}, this._onAuthError.bind(this, reject))
this.ws2Manager.onWS('close', {}, this._onWSClose.bind(this))

// user wallets data for startegy execution
this.ws2Manager.onWS(WALLET_SNAPSHOT_EVENT, {}, async (data) => {
this._onWalletsData(data, WALLET_SNAPSHOT_EVENT)
})

this.ws2Manager.onWS(WALLET_UPDATE_EVENT, {}, async (data) => {
this._onWalletsData(data, WALLET_UPDATE_EVENT)
})

this.ws2Manager.openWS()

await onConnected
}

_onWalletsData (data, type) {
if (type === WALLET_SNAPSHOT_EVENT) {
this.userWallets = data._collection
? data._collection.map(wallet => {
return {
currency: wallet.currency,
type: wallet.type,
balance: wallet.balance,
balanceAvailable: wallet.balanceAvailable
}
})
: []
} else if (type === WALLET_UPDATE_EVENT && this.userWallets) {
this.userWallets.forEach(wallet => {
if (wallet.currency === data.currency && wallet.type === data.type) {
wallet.balance = data.balance || wallet.balance
wallet.balanceAvailable = data.balanceAvailable || wallet.balanceAvailable
}
})
}

// pass wallets data to strategy exeuctors
for (const [, strategy] of this.strategy.entries()) {
const { liveStrategyExecutor } = strategy
if (liveStrategyExecutor) {
liveStrategyExecutor.setWallets(this.userWallets)
}
}
}

/**
* @private
*/
Expand Down Expand Up @@ -315,6 +357,7 @@ class StrategyManager {
})

this._registerStrategyExecutionListeners(liveStrategyExecutor, strategyMapKey, strategyOptions)
liveStrategyExecutor.setWallets(this.userWallets)

await liveStrategyExecutor.execute()

Expand Down
3 changes: 2 additions & 1 deletion test/unit/lib/ws_servers/api/strategy/manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ const StrategyExecutionStub = {
execute: sandbox.stub(),
stopExecution: sandbox.stub(),
generateResults: sandbox.stub(),
removeAllListeners: sandbox.stub()
removeAllListeners: sandbox.stub(),
setWallets: sandbox.stub()
}

const PriceFeedStub = {
Expand Down