Skip to content

Instantly share code, notes, and snippets.

@prdn
Last active September 8, 2018 22:40
Show Gist options
  • Save prdn/a4c4f9e0b19db4060853bcc5a60a46ab to your computer and use it in GitHub Desktop.
Save prdn/a4c4f9e0b19db4060853bcc5a60a46ab to your computer and use it in GitHub Desktop.
bfx_ws2_aggressive.js
/*
mkdir bfx_test
cd bfx_test
// save this script as run.js, and add your api key/secret
npm install ws
npm install async
node run.js
*/
const WebSocket = require('ws');
const crypto = require('crypto')
const async = require('async')
const fs = require('fs')
const w1_apiKey = '...'
const w1_apiSecret = '...'
const ATTEMPTS = 50
const STATS = {
success: 0,
error: 0,
errorsByType: {}
}
const test = (cb) => {
const w1 = new WebSocket('wss://api.bitfinex.com/ws/2')
const authNonce = (new Date()).getTime() * 1000
const payload = 'AUTH' + authNonce
const signature = crypto.createHmac('sha384', w1_apiSecret).update(payload).digest('hex')
let isExecuted = false
let isOpened = false
let isAuthed = false
const mto = setTimeout (() => {
if (isExecuted) {
// already handled
return
}
setExecuted(isOpened && isAuthed ? 'error no wallet data' : 'error connection')
}, 5000)
function setExecuted (err) {
if (isExecuted) {
return
}
isExecuted = true
try {
w1.close()
} catch (e) {}
clearTimeout(mto)
cb(err)
}
w1.on('message', function(data) {
data = JSON.parse(data)
//console.log(data)
if (data.event === 'auth') {
if (data.status === 'OK') {
isAuthed = true
} else {
return setExecuted('error auth')
}
} else if (data[0] === 0) {
if (data[1] === 'ws') {
// SUCCESS
return setExecuted()
}
}
})
w1.on('close', function() {
if (isExecuted) {
// already handled
return
}
setExecuted('error connection')
})
w1.on('open', function() {
isOpened = true
w1.send(JSON.stringify({
event: 'auth',
apiKey: w1_apiKey,
authSig: signature,
authPayload: payload,
authNonce: +authNonce
}))
})
}
let count = 0
const d1 = Date.now()
async.whilst(
() => {
return count < ATTEMPTS
},
(cb) => {
count++
test((err) => {
STATS[err ? 'error' : 'success']++
if (err) {
STATS['errorsByType'][err] = (STATS['errorsByType'][err] || 0) + 1
}
cb()
})
},
(err, n) => {
console.log(STATS, (Date.now() - d1))
}
)
@prdn
Copy link
Author

prdn commented Sep 8, 2018

That's rate limiting

@serzhiio
Copy link

serzhiio commented Sep 8, 2018

/*
mkdir bfx_test
cd bfx_test
// save this script as run.js, and add your api key/secret
npm install ws
npm install async
node run.js
*/

const WebSocket = require('ws');
const crypto = require('crypto')
const async = require('async')
const fs = require('fs')

const w1_apiKey = ''
const w1_apiSecret = ''

const ATTEMPTS = 100
const STATS = {
success: 0,
error: 0,
errorsByType: {}
}

const test = (cb) => {
const w1 = new WebSocket('wss://api.bitfinex.com/ws/2')
const authNonce = (new Date()).getTime() * 1000
const payload = 'AUTH' + authNonce
const signature = crypto.createHmac('sha384', w1_apiSecret).update(payload).digest('hex')

let isExecuted = false
let isOpened = false
let isAuthed = false
let gotWS = false
let gotWU = 0

const mto = setTimeout (() => {
if (isExecuted) {
// already handled
return
}

setExecuted(isOpened && isAuthed ? 'error no wallet data' : 'error connection')

}, 10000)

function setExecuted (err) {
if (isExecuted) {
return
}

isExecuted = true
try {
  w1.close()
} catch (e) {}
clearTimeout(mto)
cb(err)

}

w1.on('message', function(data) {
data = JSON.parse(data)
/if(data.length > 0 && (data[1] === 'ws'||data[1]==='wu'))
console.log(JSON.stringify(data))
/

if (data.event === 'auth') {
  if (data.status === 'OK') {
    isAuthed = true
  } else {
    return setExecuted('error auth')
  }
} else if (data[0] === 0) {
  if (data[1] === 'ws') {
    // SUCCESS
	gotWS = true;
	w1.send(JSON.stringify([
	  0,
	  "calc",
	  null,
	  [
		["wallet_exchange_ETC"],
		["wallet_exchange_USD"]
	  ]	  
	]));		
    //return setExecuted()
  }
  if(data[1] === 'wu'){
	//console.log(JSON.stringify(data));
	gotWU++;
    return setExecuted()
  }
}

})

w1.on('close', function() {
console.log('WS:',gotWS,'WU:',gotWU);
if (isExecuted) {
// already handled
return
}

setExecuted('error connection')

})

w1.on('open', function() {
isOpened = true

w1.send(JSON.stringify({
  event: 'auth',
  apiKey: w1_apiKey,
  authSig: signature,
  authPayload: payload,
  authNonce: +authNonce,
  filter: [
	'trading-tETCUSD',
	'wallet'
  ]	  
}))

})
}

let count = 0
const d1 = Date.now()

async.whilst(
() => {
return count < ATTEMPTS
},
(cb) => {
count++
console.log('Try#',count);
test((err) => {
STATS[err ? 'error' : 'success']++
if (err) {
STATS['errorsByType'][err] = (STATS['errorsByType'][err] || 0) + 1
}
cb()
})
},
(err, n) => {
console.log(STATS, (Date.now() - d1))
}
)

@serzhiio
Copy link

serzhiio commented Sep 8, 2018

I may be did something wrong in code, but please try it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment