Skip to content

Commit

Permalink
feat: added websocket, split http and websocket code
Browse files Browse the repository at this point in the history
  • Loading branch information
vafanassieff committed Aug 6, 2021
1 parent 61b00ef commit c00fcae
Show file tree
Hide file tree
Showing 10 changed files with 557 additions and 362 deletions.
149 changes: 85 additions & 64 deletions README.md

Large diffs are not rendered by default.

7 changes: 0 additions & 7 deletions examples/basic.js

This file was deleted.

7 changes: 7 additions & 0 deletions examples/http/basic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const { LNMarketsHttp } = require('../../index.js')

;(async () => {
const lnm = new LNMarketsHttp()
const info = await lnm.nodeState()
console.log(info)
})()
13 changes: 13 additions & 0 deletions examples/websocket/basic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const { LNMarketsWebsocket } = require('../../index.js')

;(async () => {
const lnm = new LNMarketsWebsocket()
lnm.on('connected', () => {
console.log('Connected to LN Markets Webosckets')
})
lnm.on('message', (message) => {
console.log(message)
lnm.terminate()
})
await lnm.connect()
})()
295 changes: 5 additions & 290 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,292 +1,7 @@
const https = require('https')
const querystring = require('querystring')
const LNMarketsHttp = require('./src/http.js')
const LNMarketsWebsocket = require('./src/Websocket.js')

module.exports = class LNMarkets {
constructor(opt = {}) {
const { token, network, version } = opt

this.token = token
this.network = network || 'mainnet'
this.version = version || 'v1'
this.hostname =
this.network === 'mainnet'
? 'api.lnmarkets.com'
: 'api.testnet.lnmarkets.com'
}

requestAPI(opt = {}) {
const { method, endpoint, params, credentials } = opt
const { hostname, version, token } = this
const options = {
port: 443,
hostname,
method,
path: `/${version}${endpoint}`,
headers: {
'Content-Type': 'application/json',
},
}

if (credentials) {
options.headers.Authorization = `Bearer ${token}`
}

if (method.match(/^(GET|DELETE)$/) && params) {
options.path += `?${querystring.stringify(params)}`
}

return new Promise((resolve, reject) => {
const call = https.request(options, (response) => {
let data = ''

response.on('data', (chunk) => {
data += chunk
})

response.on('error', (error) => {
reject(error)
})

response.on('end', () => {
try {
const body = JSON.parse(data)

if (response.statusCode === 200) {
resolve(body)
} else {
reject({ body, statusCode: response.statusCode })
}
} catch (error) {
error.data = data
reject(error)
}
})
})

if (method.match(/^(PUT|POST)$/) && params) {
call.write(JSON.stringify(params))
}

call.end()
})
}

futuresNewPosition(params) {
const options = {
method: 'POST',
endpoint: '/futures',
params,
credentials: true,
}

return this.requestAPI(options)
}

futuresUpdatePosition(params) {
const options = {
method: 'PUT',
endpoint: '/futures',
params,
credentials: true,
}

return this.requestAPI(options)
}

futuresClosePosition(params) {
const options = {
method: 'DELETE',
endpoint: '/futures',
params,
credentials: true,
}

return this.requestAPI(options)
}

futuresCloseAllPositions() {
const options = {
method: 'DELETE',
endpoint: '/futures/all/close',
credentials: true,
}

return this.requestAPI(options)
}

futuresCancelPosition(params) {
const options = {
method: 'POST',
endpoint: '/futures/cancel',
params,
credentials: true,
}

return this.requestAPI(options)
}

futuresCancelAllPositions() {
const options = {
method: 'DELETE',
endpoint: '/futures/all/cancel',
credentials: true,
}

return this.requestAPI(options)
}

futuresCashinPosition(params) {
const options = {
method: 'POST',
endpoint: '/futures/cash-in',
params,
credentials: true,
}

return this.requestAPI(options)
}

futuresAddMarginPosition(params) {
const options = {
method: 'POST',
endpoint: '/futures/add-margin',
params,
credentials: true,
}

return this.requestAPI(options)
}

futuresGetPositions(params) {
const options = {
method: 'GET',
endpoint: '/futures',
params,
credentials: true,
}

return this.requestAPI(options)
}

futuresHistory(params) {
const options = {
method: 'GET',
endpoint: '/futures/history',
params,
}

return this.requestAPI(options)
}

getUser() {
const options = {
method: 'GET',
endpoint: '/user',
credentials: true,
}

return this.requestAPI(options)
}

updateUser(params) {
const options = {
method: 'PUT',
endpoint: '/user',
params,
credentials: true,
}

return this.requestAPI(options)
}

deposit(params) {
const options = {
method: 'POST',
endpoint: '/user/deposit',
params,
credentials: true,
}

return this.requestAPI(options)
}

depositHistory(params) {
const options = {
method: 'GET',
endpoint: '/user/deposit',
params,
credentials: true,
}

return this.requestAPI(options)
}

withdraw(params) {
const options = {
method: 'POST',
endpoint: '/user/withdraw',
params,
credentials: true,
}

return this.requestAPI(options)
}

withdrawLNURL(params) {
const options = {
method: 'POST',
endpoint: '/lnurl/withdraw',
params,
credentials: true,
}

return this.requestAPI(options)
}

withdrawHistory(params) {
const options = {
method: 'GET',
endpoint: '/user/withdraw',
params,
credentials: true,
}

return this.requestAPI(options)
}

apiState() {
const options = {
method: 'GET',
endpoint: '/state',
}

return this.requestAPI(options)
}

nodeState() {
const options = {
method: 'GET',
endpoint: '/state/node',
}

return this.requestAPI(options)
}

getLeaderboard() {
const options = {
method: 'GET',
endpoint: '/state/leaderboard',
}

return this.requestAPI(options)
}

getAnnouncements() {
const options = {
method: 'GET',
endpoint: '/state/announcements',
}

return this.requestAPI(options)
}
module.exports = {
LNMarketsHttp,
LNMarketsWebsocket,
}
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"description": "A set of wrappers to easily communicate with LN Markets API !",
"main": "index.js",
"files": [
"index.js"
"index.js",
"src/**.js"
],
"author": "BOTREL KIlian",
"license": "MIT",
Expand All @@ -27,6 +28,9 @@
"test": "echo \"No tests.\"",
"prepare": "husky install"
},
"dependencies": {
"ws": "^8.0.0"
},
"devDependencies": {
"@commitlint/cli": "^13.1.0",
"@commitlint/config-conventional": "^13.1.0",
Expand Down
Loading

0 comments on commit c00fcae

Please sign in to comment.