Skip to content

Commit

Permalink
pass wallets data to strategy exeuctors (#251)
Browse files Browse the repository at this point in the history
  • Loading branch information
tekwani authored Oct 23, 2023
1 parent ac0452a commit 1ca7314
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
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

0 comments on commit 1ca7314

Please sign in to comment.