Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding monero provider #22

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 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
39 changes: 39 additions & 0 deletions providers/xmr/clients/rest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
'use strict';
flasher007 marked this conversation as resolved.
Show resolved Hide resolved
const request = require('superagent');
flasher007 marked this conversation as resolved.
Show resolved Hide resolved
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') {
flasher007 marked this conversation as resolved.
Show resolved Hide resolved
return request
flasher007 marked this conversation as resolved.
Show resolved Hide resolved
.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);
flasher007 marked this conversation as resolved.
Show resolved Hide resolved
return {
height,
hash: block.block_header.hash,
prev_hash: block.block_header.prev_hash,
txs: block.block_header.tx_hashes || []
}
flasher007 marked this conversation as resolved.
Show resolved Hide resolved
}
}
30 changes: 30 additions & 0 deletions providers/xmr/clients/rpc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
'use strict';
flasher007 marked this conversation as resolved.
Show resolved Hide resolved
const axios = require('axios');
const _ = require('lodash')

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

async request({method, params}) {
try {
const res = await axios
.post(`${this.walletUrl}/json_rpc`, JSON.stringify({
flasher007 marked this conversation as resolved.
Show resolved Hide resolved
jsonrpc: "2.0",
flasher007 marked this conversation as resolved.
Show resolved Hide resolved
method,
params,
id: new Date().getTime()
flasher007 marked this conversation as resolved.
Show resolved Hide resolved
}), {
headers: {
flasher007 marked this conversation as resolved.
Show resolved Hide resolved
'Content-Type': 'application/json',
},
});

return _.get(res, 'data.result');
flasher007 marked this conversation as resolved.
Show resolved Hide resolved
} catch (e) {
console.error(e);
flasher007 marked this conversation as resolved.
Show resolved Hide resolved
}
}

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

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

module.exports = class XMR extends Provider {

constructor(options) {
flasher007 marked this conversation as resolved.
Show resolved Hide resolved
super(options.currency || 'xmr');
this.clientRest = new MoneroRest(options);
this.clientRpc = new MoneroRPC(options);
}

async getBlock(height) {
return this.clientRest.getBlock(height);
flasher007 marked this conversation as resolved.
Show resolved Hide resolved
}

async getHeight() {
return this.clientRest.getCurrentHeight();
flasher007 marked this conversation as resolved.
Show resolved Hide resolved
}

async request({method, params}) {
return this.clientRpc.request({method, params});
flasher007 marked this conversation as resolved.
Show resolved Hide resolved
}

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