-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from ChronoBank/develop
Develop
- Loading branch information
Showing
16 changed files
with
8,659 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
node_modules | ||
tests | ||
test | ||
migrations |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package-lock.json | ||
node_modules | ||
build | ||
.env |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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$/, | ||
}); |
Oops, something went wrong.