Skip to content

Commit

Permalink
major changes. demo on site index. ropsten network. etc.
Browse files Browse the repository at this point in the history
  • Loading branch information
Navlis93 committed Dec 28, 2017
1 parent 368da79 commit 485b1ad
Show file tree
Hide file tree
Showing 94 changed files with 7,940 additions and 2,136 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Expand Up @@ -29,7 +29,7 @@ coverage
benchmarks/graphs

config/database.js
frontend/config/config.js
public/javascripts/build.*
frontend/**/src/config/config.js

public/profile-pic/*
!/public/profile-pic/.gitkeep
1 change: 1 addition & 0 deletions abi/Token.json
@@ -0,0 +1 @@
[{"constant":true,"inputs":[],"name":"MULTIPLIER","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"name","outputs":[{"name":"","type":"string"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_spender","type":"address"},{"name":"_value","type":"uint256"}],"name":"approve","outputs":[{"name":"success","type":"bool"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"crowdsaleAgent","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"FROZEN_TOKENS","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"totalSupply","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_from","type":"address"},{"name":"_to","type":"address"},{"name":"_value","type":"uint256"}],"name":"transferFrom","outputs":[{"name":"success","type":"bool"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"decimals","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_crowdsaleAgent","type":"address"}],"name":"setCrowdsaleAgent","outputs":[],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"receiver","type":"address"},{"name":"amount","type":"uint256"}],"name":"mint","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"crowdSaleOverTimestamp","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[],"name":"releaseTokenTransfer","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"_owner","type":"address"}],"name":"balanceOf","outputs":[{"name":"balance","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"FREEZE_PERIOD","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"owner","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"symbol","outputs":[{"name":"","type":"string"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"released","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_to","type":"address"},{"name":"_value","type":"uint256"}],"name":"transfer","outputs":[{"name":"success","type":"bool"}],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"_owner","type":"address"},{"name":"_spender","type":"address"}],"name":"allowance","outputs":[{"name":"remaining","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"amount","type":"uint256"}],"name":"convertToDecimal","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"newOwner","type":"address"}],"name":"transferOwnership","outputs":[],"payable":false,"type":"function"},{"inputs":[],"payable":false,"type":"constructor"},{"payable":true,"type":"fallback"},{"anonymous":false,"inputs":[{"indexed":true,"name":"from","type":"address"},{"indexed":true,"name":"to","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"Transfer","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"owner","type":"address"},{"indexed":true,"name":"spender","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"Approval","type":"event"}]
14 changes: 11 additions & 3 deletions app.js
Expand Up @@ -2,7 +2,12 @@ const express = require('express');
const path = require('path');
const logger = require('morgan');
const bodyParser = require('body-parser');
const cookieParser = require('cookie-parser');

const Web3 = require('web3');
const web3 = new Web3(
new Web3.providers.HttpProvider('https://ropsten.infura.io/')
);
const app = express();

const passport = require('passport');
Expand All @@ -21,6 +26,7 @@ app.use(passport.initialize());
// app.set('view engine', 'jade');

app.use(logger('dev'));
app.use(cookieParser());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));

Expand All @@ -34,11 +40,13 @@ app.use(function(req, res, next) {
next();
});

app.use('/', require('./routes/index'));
app.use('/users', require('./routes/users'));
app.use('/deals', require('./routes/deals'));
app.use('/', require('./routes/index')(web3));
app.use('/users', require('./routes/users')(web3));
app.use('/deals', require('./routes/deals')(web3));
app.use('/wallet', require('./routes/wallet')(web3));
app.use('/exchanges', require('./routes/exchanges'));
app.use('/suggestions', require('./routes/suggestions'));
app.use('/attachments', require('./routes/attachments'));

// catch 404 and forward to error handler
app.use(function(req, res, next) {
Expand Down
58 changes: 58 additions & 0 deletions bcg-jobs/escrows.js
@@ -0,0 +1,58 @@
const Deal = require('../db/models/Deal.js');
const User = require('../db/models/User.js');

const settings = require('../config/settings')('bcg-escrows');

function findAndChangeEscrows(deal) {
let used_ids = deal.escrows.map(function (escrow) {
return escrow.escrow;
});
return new Promise(function(resolve, reject){
User.find({
$and: [
{type: 'escrow'},
{_id: {$nin: used_ids}}
]
}).then(function(escrows){
if (escrows && escrows.length > 0) {
let random = Math.floor(Math.random() * escrows.length);
deal.escrows.push({escrow: escrows[random]._id});
}
return deal.save();
}).then(function (deal){
resolve(deal);
}).catch(function(err){
reject(err);
});
})
}

module.exports = function(io){
setInterval(function(){
Deal.find({
status: 'dispute'
}).then(function (deals){
let _deals = [];
deals.forEach(function(deal, index){
if (deal.escrows.length){
let lastEscrow = deal.escrows[deal.escrows.length - 1];
let created_at = new Date(lastEscrow.created_at);
let join_at = new Date(lastEscrow.join_at);
let now = new Date();

if ((join_at.getTime() == 0 && Math.floor(now - created_at) > settings.time_from_add * 1000) ||
(join_at.getTime() != 0 && Math.floor(now - join_at) > settings.time_from_join * 1000)){
deal.escrows.id(lastEscrow._id).set({decision: 'rejected'});
findAndChangeEscrows(deal).then(function(deal){
io.in(deal._id.toString()).emit('disputeChanged');
}).catch(function (err){
console.log(err);
});
}
}
});
}).catch(function (err){
console.log(err);
});
}, 1000 * settings.time_to_check);
};
10 changes: 9 additions & 1 deletion bin/www
Expand Up @@ -24,11 +24,19 @@ var server = http.createServer(app);
/**
* sockets
*/
require('../socket')(server);
var io = require('../socket')(server, app);

/**
* Listen on provided port, on all network interfaces.
*/

/**
* Send io to files:
*/
require('../bcg-jobs/escrows')(io);
/**
* End send io to files
*/
server.listen(port);
server.on('error', onError);
server.on('listening', onListening);
Expand Down
34 changes: 34 additions & 0 deletions cmd-tools/deals.js
@@ -0,0 +1,34 @@
/**
* Generate wallets for users which didnt have them
*/
const config = require('../config/database');
const mongoose = require('mongoose');
mongoose.Promise = global.Promise;
mongoose.connect(config.database, {
useMongoClient: true
});

const User = require('../db/models/User.js');
const Account = require('../db/models/crypto/Account.js');
const Deal = require('../db/models/Deal.js');

const Web3 = require('web3');
const web3 = new Web3(
new Web3.providers.HttpProvider('https://ropsten.infura.io/')
);

function createAccount (data) {
return new Account(data).save();
}

Deal.find({coin: {$exists: false}}).then(async function (deals) {
await Promise.all(deals.map(async function (deal) {
deal.coin = 'ETH';
await deal.save();
})
);
console.log("done\n");
process.exit();
}).catch(function(err) {
console.log(err);
});
34 changes: 34 additions & 0 deletions cmd-tools/users.js
@@ -0,0 +1,34 @@
/**
* Generate wallets for users which didnt have them
*/
const config = require('../config/database');
const mongoose = require('mongoose');
mongoose.Promise = global.Promise;
mongoose.connect(config.database, {
useMongoClient: true
});

const User = require('../db/models/User.js');
const Account = require('../db/models/crypto/Account.js');

const Web3 = require('web3');
const web3 = new Web3(
new Web3.providers.HttpProvider('https://ropsten.infura.io/')
);

function createAccount (data) {
return new Account(data).save();
}

User.find({holds: {$exists: false}}).then(async function (users) {
await Promise.all(users.map(async function (user) {
user.holds = {eth: 0, pfr: 0};
user.markModified('holds');
await user.save();
})
);
console.log("done\n");
process.exit();
}).catch(function(err) {
console.log(err);
});
39 changes: 39 additions & 0 deletions cmd-tools/wallets.js
@@ -0,0 +1,39 @@
/**
* Generate wallets for users which didnt have them
*/
const config = require('../config/database');
const mongoose = require('mongoose');
mongoose.Promise = global.Promise;
mongoose.connect(config.database, {
useMongoClient: true
});

const User = require('../db/models/User.js');
const Account = require('../db/models/crypto/Account.js');

const Web3 = require('web3');
const web3 = new Web3(
new Web3.providers.HttpProvider('https://ropsten.infura.io/')
);

function createAccount (data) {
return new Account(data).save();
}

User.find({wallet: {$exists: false}, status: 'active'}).then(async function (users) {
await Promise.all(users.map(async function (user) {
let newAccount = web3.eth.accounts.create();
let account = await createAccount({
address: newAccount.address,
privateKey: newAccount.privateKey,
coin: 'eth',
owner: user._id
});
user.wallet = account._id;
await user.save();
}));
console.log("done\n");
process.exit();
}).catch(function(err) {
console.log(err);
});
5 changes: 5 additions & 0 deletions config/crypto.js
@@ -0,0 +1,5 @@


module.exports = {
pfr_address: '0x4a39e8bae23eff8dbd9ac2ad9fcb59218a731d3d', // ropsten address
};
24 changes: 18 additions & 6 deletions config/passport.js
@@ -1,13 +1,25 @@
var JwtStrategy = require('passport-jwt').Strategy;
var ExtractJwt = require('passport-jwt').ExtractJwt;
const JwtStrategy = require('passport-jwt').Strategy;
const ExtractJwt = require('passport-jwt').ExtractJwt;

// load up the user model
var User = require('../db/models/User.js');
var config = require('../config/database'); // get db config file
const User = require('../db/models/User.js');
const config = require('../config/database'); // get db config file

module.exports = function(passport) {
var opts = {};
opts.jwtFromRequest = ExtractJwt.fromAuthHeaderAsBearerToken();
const opts = {};
opts.jwtFromRequest = ExtractJwt.fromExtractors(
[
ExtractJwt.fromAuthHeaderAsBearerToken(),
function (req) {
let token = null;
if (req && req.cookies)
{
token = req.cookies['jwt_token'];
}
return token;
}
]
);
opts.secretOrKey = config.secret;
passport.use(new JwtStrategy(opts, function(jwt_payload, done) {
User.findById(jwt_payload._id, function(err, user) {
Expand Down
15 changes: 15 additions & 0 deletions config/settings.js
@@ -0,0 +1,15 @@
/**
* Get values of settings by name of category
* @param {String} setting_name Category of settings
* @returns {array} Return values of settings by name of category
*/
module.exports = function(setting_name){
var s = {
'bcg-escrows': {
time_from_add: 60 * 5, // sec
time_from_join: 60 * 90, // sec
time_to_check: 60 * 5 // sec
}
}
return s[setting_name];
};
8 changes: 8 additions & 0 deletions db/models/Deal.js
Expand Up @@ -22,6 +22,10 @@ const Deal = new Schema({
type: Schema.Types.ObjectId,
ref: 'User'
},
coin: {
type: String, // 'BTC' 'ETH' 'PFR'
required: true
},
sum: {
type: Number
},
Expand Down Expand Up @@ -59,6 +63,10 @@ const Deal = new Schema({
created_at: {
type: Date,
default: Date.now
},
join_at: {
type: Date,
default: 0
}
}
],
Expand Down
6 changes: 5 additions & 1 deletion db/models/Notification.js
Expand Up @@ -22,7 +22,11 @@ const Notification = new Schema({
default: Date.now
},
type: {
type: String,
type: String
},
viewed: {
type: Boolean,
default: false
}
});

Expand Down
33 changes: 33 additions & 0 deletions db/models/Review.js
@@ -0,0 +1,33 @@
const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const Review = new Schema({
_id: {
type: Schema.Types.ObjectId
},
user: {
type: Schema.Types.ObjectId,
ref: 'User'
},
author: {
type: Schema.Types.ObjectId,
ref: 'User'
},
comment: {
type: String
},
rating: {
type: Number,
default: 5
},
created_at: {
type: Date,
default: Date.now
},
deal: {
type: Schema.Types.ObjectId,
ref: 'Deal'
}
});

module.exports = mongoose.model('Review', Review);

0 comments on commit 485b1ad

Please sign in to comment.