Skip to content

Commit

Permalink
Merge pull request #3 from ChronoBank/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
ega-forever authored Nov 28, 2017
2 parents d7c2745 + cc37ab4 commit 3c34b87
Show file tree
Hide file tree
Showing 16 changed files with 8,659 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node_modules
tests
test
migrations
18 changes: 18 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module.exports = {
"env": {
"node": true,
"es6": true
},
"extends": ['plugin:chronobank/recommended'],
"rules": {
"quotes": ["error", "single"],
"semi": ["error", "always"],
"no-console": 1,
"no-unused-vars": 1,
"curly": ["error", "multi"],
"no-empty": ["error", {"allowEmptyCatch": true}]
},
"parserOptions": {
"ecmaVersion": 8
}
};
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package-lock.json
node_modules
build
.env
37 changes: 37 additions & 0 deletions config/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* Chronobank/eth-nem-action-processor configuration
* @module config
* @returns {Object} Configuration
*/
require('dotenv').config();

const config = {
mongo: {
uri: process.env.MONGO_URI || 'mongodb://localhost:27017/data'
},
rabbit: {
url: process.env.RABBIT_URI || 'amqp://localhost:5672',
serviceName: process.env.RABBIT_SERVICE_NAME || 'app_eth'
},
rest: {
domain: process.env.DOMAIN || 'localhost',
port: parseInt(process.env.REST_PORT) || 8081,
auth: process.env.USE_AUTH || false
},
web3: {
network: process.env.NETWORK || 'development',
uri: `${/^win/.test(process.platform) ? '\\\\.\\pipe\\' : ''}${process.env.WEB3_URI || `/tmp/${(process.env.NETWORK || 'development')}/geth.ipc`}`
},
nem: {
mosaic: 'cb:minutes',
txFee: 100000,
bonusRate: 60,
host: 'http://localhost',
privateKey: 'secret_key',
},
sc: {
path: 'defaultPath' // TODO: ...
}
};

module.exports = config;
12 changes: 12 additions & 0 deletions factories/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* Bootstrap file for rescursive search for all factories
* @returns {Object} factories
*/

const requireAll = require('require-all');

module.exports = requireAll({
dirname : __dirname,
filter : /(.+Factory)\.js$/,
recursive: true
});
9 changes: 9 additions & 0 deletions factories/messages/accountMessageFactory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* accountMessageFactory factory
* @module factory/accountMessage
* @returns {Object}
*/

module.exports = {
wrongAddress: {success: false, message: 'wrong address'},
};
10 changes: 10 additions & 0 deletions factories/messages/genericMessageFactory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* genericMessageFactory factory
* @module factory/genericMessage
* @returns {Object}
*/

module.exports = {
success: {success: true, message: 'ok'},
fail: {success: false, message: 'fail'}
};
10 changes: 10 additions & 0 deletions factories/messages/transactionMessageFactory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* transactionMessageFactory factory
* @module factory/transactionMessage
* @returns {Object}
*/

module.exports = {
wrongTo: {success: false, message: 'wrong \'to\' address'},
wrongFrom: {success: false, message: 'wrong \'from\' address'}
};
78 changes: 78 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/**
* Middleware service for handling transfers to NEM crypto cyrrency
* Update balances & make transfers from ETH wallet to NEM
* in received transactions from blockParser via amqp
*
* @module Chronobank/eth-nem-action-processor
* @requires config
* @requires models/accountModel
*/

const config = require('./config'),
_ = require('lodash'),
mongoose = require('mongoose'),
makeTransfer = require('./services/makeTransfer'),
Web3 = require('web3'),
net = require('net'),
bunyan = require('bunyan'),
Promise = require('bluebird'),
log = bunyan.createLogger({name: 'core.nemActionProcessor'}),
amqp = require('amqplib');

mongoose.Promise = Promise;
mongoose.connect(config.mongo.uri, {useMongoClient: true});

const defaultQueue = `${config.rabbit.serviceName}.chrono_nem_processor`;
const triggerEvents = ['Deposit', 'WithdrawShares', 'FeatureFeeTaken'];

let init = async () => {
let conn = await amqp.connect(config.rabbit.url)
.catch(() => {
log.error('rabbitmq is not available!');
process.exit(0);
});

let channel = await conn.createChannel();

channel.on('close', () => {
log.error('rabbitmq process has finished!');
process.exit(0);
});

let provider = new Web3.providers.IpcProvider(config.web3.uri, net);
const web3 = new Web3();
web3.setProvider(provider);

web3.currentProvider.connection.on('end', () => {
log.error('ipc process has finished!');
process.exit(0);
});

web3.currentProvider.connection.on('error', () => {
log.error('ipc process has finished!');
process.exit(0);
});

await channel.assertExchange('events', 'topic', {durable: false});
await channel.assertQueue(defaultQueue);
await channel.bindQueue(defaultQueue, 'events', `${config.rabbit.serviceName}_chrono_sc.*`);

channel.prefetch(2);

channel.consume(defaultQueue, async (data) => {
try {
let event = JSON.parse(data.content.toString());

if(triggerEvents.indexOf(event.name) !== -1) {
const who = _.get(event, 'payload.who');
await makeTransfer.checkCredit(who);
}
} catch (e) {
log.error(e);
}

channel.ack(data);
});
};

module.exports = init();
26 changes: 26 additions & 0 deletions models/accountModel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* Mongoose model. Accounts
* @module models/accountModel
* @returns {Object} Mongoose model
* @requires factory/accountMessageFactory
*/

const mongoose = require('mongoose'),
messages = require('../factories').messages.accountMessageFactory;

require('mongoose-long')(mongoose);

const Account = new mongoose.Schema({
address: {
type: String,
unique: true,
required: true,
validate: [a=> /^(0x)?[0-9a-fA-F]{40}$/.test(a), messages.wrongAddress]
},
balance: {type: mongoose.Schema.Types.Long, default: 0},
created: {type: Date, required: true, default: Date.now},
erc20token : {type: mongoose.Schema.Types.Mixed, default: {}},
maxTimeBalance: {type: mongoose.Schema.Types.Long, default: 0}
});

module.exports = mongoose.model('EthAccount', Account);
11 changes: 11 additions & 0 deletions models/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* Bootstrap file for rescursive search & exposing models
* @returns {Object} models
*/

const requireAll = require('require-all');

module.exports = requireAll({
dirname : __dirname,
filter : /(.+Model)\.js$/,
});
Loading

0 comments on commit 3c34b87

Please sign in to comment.