Skip to content
Closed
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
24 changes: 16 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "bc-listener",
"version": "2.6.0",
"version": "2.6.1",
"description": "BlockChain listener",
"main": "index.js",
"scripts": {
Expand All @@ -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",
Expand Down
4 changes: 4 additions & 0 deletions providers/xmr/clients/cmd.js
Original file line number Diff line number Diff line change
@@ -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 };
45 changes: 45 additions & 0 deletions providers/xmr/clients/rest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
'use strict';
Comment thread
flasher007 marked this conversation as resolved.

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 }) {
this.url = url;
this.cache = new Cache();
}

async cmd(command, ...args) {
if (command === REST_COMMAND_GET_HEIGHT) {
const { data } = await axios.get(`${this.url}/blocks/api/get_stats`);

return data.height;
}
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(
REST_COMMAND_GET_HEIGHT,
() => this.cmd(REST_COMMAND_GET_HEIGHT),
60,
);
}

async 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 || [],
};
}
};
24 changes: 24 additions & 0 deletions providers/xmr/clients/rpc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
'use strict';
Comment thread
flasher007 marked this conversation as resolved.

const axios = require('axios');

module.exports = class MoneroRPC {
constructor({ wallet }) {
this.walletUrl = wallet;
}

async request({ method, params }) {
try {
const { data } = await axios.post(`${this.walletUrl}/json_rpc`, {
jsonrpc: '2.0',
method,
params,
id: new Date().getTime(),
});

return data.result;
} catch (e) {
return null;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

а где обработка ошибок?

}
}
};
29 changes: 29 additions & 0 deletions providers/xmr/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use strict';

const Provider = require('../provider');
const MoneroRest = require('./clients/rest');
const MoneroRPC = require('./clients/rpc');

module.exports = class XMR extends Provider {
constructor(options) {
Comment thread
flasher007 marked this conversation as resolved.
super(options.currency || 'xmr');
this.clientRest = new MoneroRest(options);
this.clientRpc = new MoneroRPC(options);
}

async getBlock(height) {
return await this.clientRest.getBlock(height);
}

async getHeight() {
return await this.clientRest.getCurrentHeight();
}

async request({ method, params }) {
return await this.clientRpc.request({ method, params });
}

async getPool() {
return [];
}
};
Comment thread
flasher007 marked this conversation as resolved.