From 2d3c45c6036560ad83d380548da5ad6f8b718cfa Mon Sep 17 00:00:00 2001 From: Aleksandr Volkov Date: Tue, 21 Sep 2021 20:18:45 +0700 Subject: [PATCH 1/9] adding monero provider --- providers/xmr/clients/rest.js | 39 +++++++++++++++++++++++++++++++++++ providers/xmr/index.js | 24 +++++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 providers/xmr/clients/rest.js create mode 100644 providers/xmr/index.js diff --git a/providers/xmr/clients/rest.js b/providers/xmr/clients/rest.js new file mode 100644 index 0000000..3e4efed --- /dev/null +++ b/providers/xmr/clients/rest.js @@ -0,0 +1,39 @@ +'use strict'; +const request = require('superagent'); +const Cache = require('../../../utils/cache'); + +module.exports = class MoneroRest { + constructor({url}) { + this.url = url; + this.cache = new Cache(); + } + + async cmd(command, ...args) { + if (command === 'getHeight') { + return request + .get(`${this.url}/blocks/api/get_stats`) + .then(response => JSON.parse(response.text)) + .then(response => response.height); + } + if (command === 'getblock') { + return request + .get(`${this.url}/blocks/api/get_block_data/${args[0]}`) + .then(response => JSON.parse(response.text)) + .then(response => response.block_data.result); + } + } + + async getCurrentHeight() { + return this.cache.getWithCache('getHeight', () => this.cmd('getHeight'), 60); + } + + async getBlock(height) { + let block = await this.cmd('getblock', height); + return { + height, + hash: block.block_header.hash, + prev_hash: block.block_header.prev_hash, + txs: block.block_header.tx_hashes || [] + } + } +} \ No newline at end of file diff --git a/providers/xmr/index.js b/providers/xmr/index.js new file mode 100644 index 0000000..5ce7563 --- /dev/null +++ b/providers/xmr/index.js @@ -0,0 +1,24 @@ +'use strict'; + +const Provider = require('../provider'); +const MoneroRest = require('./clients/rest'); + +module.exports = class XMR extends Provider { + + constructor(options) { + super(options.currency || 'xmr'); + this.client = new MoneroRest(options); + } + + async getBlock(height) { + return this.client.getBlock(height); + } + + async getHeight() { + return this.client.getCurrentHeight(); + } + + async getPool() { + return []; + } +}; From bdb6c3238da234ff63304fcb1004e0edbfc7b664 Mon Sep 17 00:00:00 2001 From: Aleksandr Volkov Date: Thu, 14 Oct 2021 21:03:51 +0700 Subject: [PATCH 2/9] fix --- providers/xmr/index.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/providers/xmr/index.js b/providers/xmr/index.js index 5ce7563..268946d 100644 --- a/providers/xmr/index.js +++ b/providers/xmr/index.js @@ -7,15 +7,15 @@ module.exports = class XMR extends Provider { constructor(options) { super(options.currency || 'xmr'); - this.client = new MoneroRest(options); + this.clientRest = new MoneroRest(options); } async getBlock(height) { - return this.client.getBlock(height); + return this.clientRest.getBlock(height); } async getHeight() { - return this.client.getCurrentHeight(); + return this.clientRest.getCurrentHeight(); } async getPool() { From 94235af5af9c85c81d3102074dabf4ec9f1ed188 Mon Sep 17 00:00:00 2001 From: Aleksandr Volkov Date: Thu, 14 Oct 2021 21:04:27 +0700 Subject: [PATCH 3/9] fix spaces --- providers/xmr/clients/rest.js | 56 +++++++++++++++++------------------ 1 file changed, 28 insertions(+), 28 deletions(-) diff --git a/providers/xmr/clients/rest.js b/providers/xmr/clients/rest.js index 3e4efed..6e1cd73 100644 --- a/providers/xmr/clients/rest.js +++ b/providers/xmr/clients/rest.js @@ -3,37 +3,37 @@ const request = require('superagent'); const Cache = require('../../../utils/cache'); module.exports = class MoneroRest { - constructor({url}) { - this.url = url; - this.cache = new Cache(); - } + constructor({url}) { + this.url = url; + this.cache = new Cache(); + } - async cmd(command, ...args) { - if (command === 'getHeight') { - return request - .get(`${this.url}/blocks/api/get_stats`) - .then(response => JSON.parse(response.text)) - .then(response => response.height); - } - if (command === 'getblock') { - return request - .get(`${this.url}/blocks/api/get_block_data/${args[0]}`) - .then(response => JSON.parse(response.text)) - .then(response => response.block_data.result); - } + async cmd(command, ...args) { + if (command === 'getHeight') { + return request + .get(`${this.url}/blocks/api/get_stats`) + .then(response => JSON.parse(response.text)) + .then(response => response.height); } - - async getCurrentHeight() { - return this.cache.getWithCache('getHeight', () => this.cmd('getHeight'), 60); + if (command === 'getblock') { + return request + .get(`${this.url}/blocks/api/get_block_data/${args[0]}`) + .then(response => JSON.parse(response.text)) + .then(response => response.block_data.result); } + } + + async getCurrentHeight() { + return this.cache.getWithCache('getHeight', () => this.cmd('getHeight'), 60); + } - async getBlock(height) { - let block = await this.cmd('getblock', height); - return { - height, - hash: block.block_header.hash, - prev_hash: block.block_header.prev_hash, - txs: block.block_header.tx_hashes || [] - } + async getBlock(height) { + let block = await this.cmd('getblock', height); + return { + height, + hash: block.block_header.hash, + prev_hash: block.block_header.prev_hash, + txs: block.block_header.tx_hashes || [] } + } } \ No newline at end of file From 65a204e03cae298b902f9bd10f3c17f6225ae5ef Mon Sep 17 00:00:00 2001 From: Aleksandr Volkov Date: Fri, 22 Oct 2021 10:11:24 +0700 Subject: [PATCH 4/9] fix pr --- providers/xmr/clients/rpc.js | 25 +++++++++++++++++++++++++ providers/xmr/index.js | 6 ++++++ 2 files changed, 31 insertions(+) create mode 100644 providers/xmr/clients/rpc.js diff --git a/providers/xmr/clients/rpc.js b/providers/xmr/clients/rpc.js new file mode 100644 index 0000000..7f25a22 --- /dev/null +++ b/providers/xmr/clients/rpc.js @@ -0,0 +1,25 @@ +'use strict'; +const request = require('superagent'); + +module.exports = class MoneroRPC { + constructor({wallet}) { + this.walletUrl = wallet; + } + + async request({method, params}) { + try { + const res = await request + .post(`${this.walletUrl}/json_rpc`) + .send({ + jsonrpc: "2.0", + method, + params, + id: new Date().getTime() + }); + return res.body.result; + } catch (e) { + console.error(e); + } + } + +} \ No newline at end of file diff --git a/providers/xmr/index.js b/providers/xmr/index.js index 268946d..1ac58c8 100644 --- a/providers/xmr/index.js +++ b/providers/xmr/index.js @@ -2,12 +2,14 @@ const Provider = require('../provider'); const MoneroRest = require('./clients/rest'); +const MoneroRPC = require('./clients/rpc'); module.exports = class XMR extends Provider { constructor(options) { super(options.currency || 'xmr'); this.clientRest = new MoneroRest(options); + this.clientRpc = new MoneroRPC(options); } async getBlock(height) { @@ -18,6 +20,10 @@ module.exports = class XMR extends Provider { return this.clientRest.getCurrentHeight(); } + async request(method, params = {}) { + return this.clientRpc.request({method, params}); + } + async getPool() { return []; } From 20853c79fdd93ab412f705ef31f7372aa5e2c894 Mon Sep 17 00:00:00 2001 From: Aleksandr Volkov Date: Tue, 26 Oct 2021 20:42:33 +0700 Subject: [PATCH 5/9] 2.6.1 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 48faf6e..d7231c0 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "bc-listener", - "version": "2.6.0", + "version": "2.6.1", "description": "BlockChain listener", "main": "index.js", "scripts": { From 741eedf64557b7b950837e6f36514f198df4cf29 Mon Sep 17 00:00:00 2001 From: Aleksandr Volkov Date: Tue, 2 Nov 2021 15:35:25 +0700 Subject: [PATCH 6/9] changed lib to axios --- package-lock.json | 24 ++++++++++++++++-------- package.json | 1 + providers/xmr/clients/rpc.js | 15 ++++++++++----- 3 files changed, 27 insertions(+), 13 deletions(-) diff --git a/package-lock.json b/package-lock.json index 2e68512..1aab053 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "bc-listener", - "version": "2.6.0", + "version": "2.6.1", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -441,11 +441,11 @@ "integrity": "sha512-xh1Rl34h6Fi1DC2WWKfxUTVqRsNnr6LsKz2+hfwDxQJWmrx8+c7ylaqBMcHfl1U1r2dsifOvKX3LQuLNZ+XSvA==" }, "axios": { - "version": "0.21.1", - "resolved": "https://registry.npmjs.org/axios/-/axios-0.21.1.tgz", - "integrity": "sha512-dKQiRHxGD9PPRIUNIWvZhPTPpl1rf/OxTYKsqKUDjBwYylTvV7SjSHJb9ratfyzM6wCdLCOYLzs73qpg5c4iGA==", + "version": "0.24.0", + "resolved": "https://registry.npmjs.org/axios/-/axios-0.24.0.tgz", + "integrity": "sha512-Q6cWsys88HoPgAaFAVUb0WpPk0O8iTeisR9IMqy9G8AbO4NlpVknrnQS03zzF9PGAWgO3cgletO3VjV/P7VztA==", "requires": { - "follow-redirects": "^1.10.0" + "follow-redirects": "^1.14.4" } }, "balanced-match": { @@ -1456,9 +1456,9 @@ } }, "follow-redirects": { - "version": "1.14.0", - "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.14.0.tgz", - "integrity": "sha512-0vRwd7RKQBTt+mgu87mtYeofLFZpTas2S9zY+jIeuLJMNvudIgF52nr19q40HOwH5RrhWIPuj9puybzSJiRrVg==" + "version": "1.14.5", + "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.14.5.tgz", + "integrity": "sha512-wtphSXy7d4/OR+MvIFbCVBDzZ5520qV8XfPklSN5QtxuMUJZ+b0Wnst1e1lCDocfzuCkHqj8k0FpZqO+UIaKNA==" }, "foreach": { "version": "2.0.5", @@ -2941,6 +2941,14 @@ "utility-types": "^3.7.0" }, "dependencies": { + "axios": { + "version": "0.21.1", + "resolved": "https://registry.npmjs.org/axios/-/axios-0.21.1.tgz", + "integrity": "sha512-dKQiRHxGD9PPRIUNIWvZhPTPpl1rf/OxTYKsqKUDjBwYylTvV7SjSHJb9ratfyzM6wCdLCOYLzs73qpg5c4iGA==", + "requires": { + "follow-redirects": "^1.10.0" + } + }, "bignumber.js": { "version": "4.1.0", "resolved": "https://registry.npmjs.org/bignumber.js/-/bignumber.js-4.1.0.tgz", diff --git a/package.json b/package.json index d7231c0..a2614ee 100644 --- a/package.json +++ b/package.json @@ -17,6 +17,7 @@ }, "homepage": "https://github.com/YouToken/bc-listener#readme", "dependencies": { + "axios": "^0.24.0", "bignumber.js": "^9.0.1", "bitcoin-core": "^3.0.0", "eosjs": "^21.0.4", diff --git a/providers/xmr/clients/rpc.js b/providers/xmr/clients/rpc.js index 7f25a22..6431d3c 100644 --- a/providers/xmr/clients/rpc.js +++ b/providers/xmr/clients/rpc.js @@ -1,5 +1,6 @@ 'use strict'; -const request = require('superagent'); +const axios = require('axios'); +const _ = require('lodash') module.exports = class MoneroRPC { constructor({wallet}) { @@ -8,15 +9,19 @@ module.exports = class MoneroRPC { async request({method, params}) { try { - const res = await request - .post(`${this.walletUrl}/json_rpc`) - .send({ + const res = await axios + .post(`${this.walletUrl}/json_rpc`, JSON.stringify({ jsonrpc: "2.0", method, params, id: new Date().getTime() + }), { + headers: { + 'Content-Type': 'application/json', + }, }); - return res.body.result; + + return _.get(res, 'data.result'); } catch (e) { console.error(e); } From b7490aff1d694a2827885c6d2348456f6038d2f7 Mon Sep 17 00:00:00 2001 From: Aleksandr Volkov Date: Tue, 2 Nov 2021 16:39:52 +0700 Subject: [PATCH 7/9] fix params --- providers/xmr/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/providers/xmr/index.js b/providers/xmr/index.js index 1ac58c8..872fb78 100644 --- a/providers/xmr/index.js +++ b/providers/xmr/index.js @@ -20,7 +20,7 @@ module.exports = class XMR extends Provider { return this.clientRest.getCurrentHeight(); } - async request(method, params = {}) { + async request({method, params}) { return this.clientRpc.request({method, params}); } From 8e5ba3e2248b9b2b43a070b7057b78851967a5aa Mon Sep 17 00:00:00 2001 From: Aleksandr Volkov Date: Thu, 4 Nov 2021 12:59:17 +0700 Subject: [PATCH 8/9] fix pr --- providers/xmr/clients/cmd.js | 4 ++++ providers/xmr/clients/rest.js | 40 ++++++++++++++++++++--------------- providers/xmr/clients/rpc.js | 28 ++++++++++-------------- providers/xmr/index.js | 1 - 4 files changed, 38 insertions(+), 35 deletions(-) create mode 100644 providers/xmr/clients/cmd.js diff --git a/providers/xmr/clients/cmd.js b/providers/xmr/clients/cmd.js new file mode 100644 index 0000000..54de4ef --- /dev/null +++ b/providers/xmr/clients/cmd.js @@ -0,0 +1,4 @@ +const REST_COMMAND_GET_HEIGHT = 'getHeight'; +const REST_COMMAND_GET_BLOCK = 'getBlock'; + +module.exports = { REST_COMMAND_GET_BLOCK, REST_COMMAND_GET_HEIGHT }; \ No newline at end of file diff --git a/providers/xmr/clients/rest.js b/providers/xmr/clients/rest.js index 6e1cd73..4adb5b2 100644 --- a/providers/xmr/clients/rest.js +++ b/providers/xmr/clients/rest.js @@ -1,39 +1,45 @@ 'use strict'; -const request = require('superagent'); + +const axios = require('axios'); const Cache = require('../../../utils/cache'); +const { REST_COMMAND_GET_BLOCK, REST_COMMAND_GET_HEIGHT } = require('./cmd'); module.exports = class MoneroRest { - constructor({url}) { + constructor({ url }) { this.url = url; this.cache = new Cache(); } async cmd(command, ...args) { - if (command === 'getHeight') { - return request - .get(`${this.url}/blocks/api/get_stats`) - .then(response => JSON.parse(response.text)) - .then(response => response.height); + if (command === REST_COMMAND_GET_HEIGHT) { + const { data } = await axios.get(`${this.url}/blocks/api/get_stats`); + + return data.height; } - if (command === 'getblock') { - return request - .get(`${this.url}/blocks/api/get_block_data/${args[0]}`) - .then(response => JSON.parse(response.text)) - .then(response => response.block_data.result); + if (command === REST_COMMAND_GET_BLOCK) { + const { data } = await axios.get( + `${this.url}/blocks/api/get_block_data/${args[0]}`, + ); + + return data.block_data.result; } } async getCurrentHeight() { - return this.cache.getWithCache('getHeight', () => this.cmd('getHeight'), 60); + return this.cache.getWithCache( + REST_COMMAND_GET_HEIGHT, + () => this.cmd(REST_COMMAND_GET_HEIGHT), + 60, + ); } async getBlock(height) { - let block = await this.cmd('getblock', height); + const block = await this.cmd(REST_COMMAND_GET_BLOCK, height); return { height, hash: block.block_header.hash, prev_hash: block.block_header.prev_hash, - txs: block.block_header.tx_hashes || [] - } + txs: block.block_header.tx_hashes || [], + }; } -} \ No newline at end of file +}; diff --git a/providers/xmr/clients/rpc.js b/providers/xmr/clients/rpc.js index 6431d3c..a2fc83a 100644 --- a/providers/xmr/clients/rpc.js +++ b/providers/xmr/clients/rpc.js @@ -1,30 +1,24 @@ 'use strict'; + const axios = require('axios'); -const _ = require('lodash') module.exports = class MoneroRPC { - constructor({wallet}) { + constructor({ wallet }) { this.walletUrl = wallet; } - async request({method, params}) { + async request({ method, params }) { try { - const res = await axios - .post(`${this.walletUrl}/json_rpc`, JSON.stringify({ - jsonrpc: "2.0", - method, - params, - id: new Date().getTime() - }), { - headers: { - 'Content-Type': 'application/json', - }, - }); + const { data } = await axios.post(`${this.walletUrl}/json_rpc`, { + jsonrpc: '2.0', + method, + params, + id: new Date().getTime(), + }); - return _.get(res, 'data.result'); + return data.result; } catch (e) { console.error(e); } } - -} \ No newline at end of file +}; diff --git a/providers/xmr/index.js b/providers/xmr/index.js index 872fb78..3724f00 100644 --- a/providers/xmr/index.js +++ b/providers/xmr/index.js @@ -5,7 +5,6 @@ const MoneroRest = require('./clients/rest'); const MoneroRPC = require('./clients/rpc'); module.exports = class XMR extends Provider { - constructor(options) { super(options.currency || 'xmr'); this.clientRest = new MoneroRest(options); From 7c3754679840c3047ed8d726d2d36887be1aef29 Mon Sep 17 00:00:00 2001 From: Aleksandr Volkov Date: Thu, 4 Nov 2021 19:18:08 +0700 Subject: [PATCH 9/9] fix pr --- providers/xmr/clients/rpc.js | 2 +- providers/xmr/index.js | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/providers/xmr/clients/rpc.js b/providers/xmr/clients/rpc.js index a2fc83a..f112777 100644 --- a/providers/xmr/clients/rpc.js +++ b/providers/xmr/clients/rpc.js @@ -18,7 +18,7 @@ module.exports = class MoneroRPC { return data.result; } catch (e) { - console.error(e); + return null; } } }; diff --git a/providers/xmr/index.js b/providers/xmr/index.js index 3724f00..99af423 100644 --- a/providers/xmr/index.js +++ b/providers/xmr/index.js @@ -12,15 +12,15 @@ module.exports = class XMR extends Provider { } async getBlock(height) { - return this.clientRest.getBlock(height); + return await this.clientRest.getBlock(height); } async getHeight() { - return this.clientRest.getCurrentHeight(); + return await this.clientRest.getCurrentHeight(); } - async request({method, params}) { - return this.clientRpc.request({method, params}); + async request({ method, params }) { + return await this.clientRpc.request({ method, params }); } async getPool() {