Skip to content
Permalink
Browse files

store user data to mongodb

  • Loading branch information...
thanhson1085 committed Dec 19, 2017
1 parent a0fd73f commit ca89c991389bc500001e3ab7524da7f63d4251e3
Showing with 204 additions and 44 deletions.
  1. +21 −0 apis/wallets.js
  2. +22 −20 app/App.vue
  3. +99 −0 models/mongodb/wallet.js
  4. +29 −24 package-lock.json
  5. +33 −0 sockets/index.js
@@ -2,6 +2,7 @@
const express = require('express'),
router = express.Router();
const Web3 = require('web3');
const db = require('../models/mongodb');

const {RewardEngine,
rootAddressSidechain,
@@ -53,4 +54,24 @@ router.post('/cashIn', function(req, res, next) {
});
});

router.get('/get/:walletAddress', function(req, res, next) {

const walletAddress = req.params.walletAddress;
console.info('Get Wallet details', walletAddress);

db.Wallet.findOne({
walletAddress: walletAddress
}).then(w => {
if (w) {
return res.json(w);
}
return res.json({
walletAddress: walletAddress,
tmcSidechain: "0",
tmcMainchain: "0",
logs: []
});
}).catch(e => next(e));
});

module.exports = router;
@@ -57,10 +57,10 @@
<div class="sumaryCoin">
<h3>You have total:</h3>
<h1>
<strong>{{(tmcSidechain + tmcMainchain).toFixed(2)}}</strong>
<strong>{{(Math.floor((tmcSidechain + tmcMainchain)*100)/100).toFixed(2)}}</strong>
<small>TMC</small>
<span v-if="expandSumaryCoin">
= {{tmcSidechain.toFixed(2)}} <small>TMC in Sidechian</small> + {{tmcMainchain.toFixed(2)}} <small>TMC in Mainchain</small>
= {{tmcSidechain}} <small>TMC in Tomochain</small> + {{tmcMainchain}} <small>TMC in Ethereum</small>
</span>
<md-button class="md-icon-button" @click="toggleExpandSumaryCoin">
<md-icon v-if="expandSumaryCoin">keyboard_arrow_left</md-icon>
@@ -88,13 +88,13 @@
<md-card-header>
<md-card-header-text>
<div class="md-title side-chain">
{{ tmcSidechain.toFixed(2) }}
<small>TMC in Sidechain</small>
{{ (Math.floor(tmcSidechain*100)/100).toFixed(2) }}
<small>TMC in <a href="https://stats.tomocoin.io" target="blank">Tomochain</a></small>
</div>
</md-card-header-text>
</md-card-header>
<md-card-content>
The coins in <strong class="side-chain">sidechain</strong>. You can cashout to <strong class="main-chain">mainchain</strong> and bal bla bal bal bal bla bla bla bla bla bla
The coins you have in <strong class="side-chain">tomochain</strong>. You can transfer the coins to <strong class="main-chain">ethereum</strong> by clicking <strong>cash out</strong> button.
</md-card-content>
</md-card>
</div>
@@ -117,13 +117,13 @@
<md-card-header>
<md-card-header-text>
<div class="md-title main-chain">
{{ tmcMainchain.toFixed(2) }}
<small>TMC in Mainchain</small>
{{ (Math.floor(tmcMainchain*100)/100).toFixed(2) }}
<small>TMC in Ethereum</small>
</div>
</md-card-header-text>
</md-card-header>
<md-card-content>
The coins in <strong class="main-chain">mainchain</strong>. You can cashin to <strong class="side-chain">sidechain</strong> and bal bal bal bal bal bla bla bla bla bla bla bla
The coins you have in <strong class="main-chain">ethereum</strong>. You can transfer the coins to <strong class="side-chain">tomochain</strong> by clicking <strong>cash in</strong> button.
</md-card-content>
</md-card>
</div>
@@ -140,8 +140,8 @@
<md-table-head style="width: 230px">Time</md-table-head>
<md-table-head>Type</md-table-head>
<md-table-head>Detail</md-table-head>
<md-table-head md-numeric>TMC in Sidechain</md-table-head>
<md-table-head md-numeric>TMC in Mainchain</md-table-head>
<md-table-head md-numeric>TMC in Tomochain</md-table-head>
<md-table-head md-numeric>TMC in Ethereum</md-table-head>
<md-table-head md-numeric>Total TMC</md-table-head>
</md-table-row>

@@ -159,15 +159,15 @@
<md-table-cell>{{e.msg}}</md-table-cell>
<md-table-cell md-numeric>
<span class="color-side-chain">
{{e.tmcSidechain.toFixed(2)}}
{{e.tmcSidechain}}
</span>
</md-table-cell>
<md-table-cell md-numeric>
<span class="color-main-chain">
{{e.tmcMainchain.toFixed(2)}}
{{e.tmcMainchain}}
</span>
</md-table-cell>
<md-table-cell md-numeric>{{e.total.toFixed(2)}}</md-table-cell>
<md-table-cell md-numeric>{{e.total}}</md-table-cell>
</md-table-row>
</md-table>
</div>
@@ -183,14 +183,14 @@
<md-dialog-prompt
:md-active.sync="showPromptCashIn"
v-model="cashInValue"
md-title="How many coins to cash in?"
md-title="How many coins do you want to transfer (cash in)?"
md-input-placeholder="Amount"
md-confirm-text="Done"
@md-confirm="cashIn" />
<md-dialog-prompt
:md-active.sync="showPromptCashOut"
v-model="cashOutValue"
md-title="How many coins to cash out?"
md-title="How many coins do you want to transfer (cash out)?"
md-input-placeholder="Amount"
md-confirm-text="Done"
@md-confirm="cashOut" />
@@ -229,10 +229,6 @@ Vue.use(VueMaterial)
Vue.use(VueSocketio, '/')
//////
// Vue.use(VueSocketio, 'https://testnet.tomocoin.io');
// axios.defaults.baseURL = 'https://testnet.tomocoin.io'
export default {
name: 'app',
data() {
@@ -263,7 +259,7 @@ export default {
cashOutValue: '',
cashInValue: '',
isProcessing: false,
logs: localStorage.logs ? JSON.parse(localStorage.logs) : [{
logs: [{
time: new Date(),
msg: 'Your TomoWallet created',
tmcMainchain: 0,
@@ -294,6 +290,12 @@ export default {
connect: function(){
this.$socket.emit('user', {address: this.walletAddress})
},
user: function(user){
this.logs = user.logs;
this.tmcSidechain = parseFloat(user.tmcSidechain);
this.tmcMainchain = parseFloat(user.tmcMainchain);
localStorage.logs = JSON.stringify(this.logs);
},
reward: function(val){
this.logs.unshift({
time: new Date(),
@@ -0,0 +1,99 @@
'use strict';

var mongoose = require('mongoose');
var crypto = require('crypto');
var Schema = mongoose.Schema;

// Define User Schema
var Wallet = new Schema({
walletAddress: {
type: String,
index: true
},
tmcSidechain: String,
tmcMainchain: String,
logs: [{
time: Date,
type: {type: String},
change: String,
message: String,
tmcSidechain: String,
tmcMainchain: String,
total: String
}]
}, {timestamps: true});

Wallet.statics = {
reward: function({walletAddress, tmcSidechain}) {
this.findOne({
walletAddress: walletAddress
}).then(w =>{
if (!w) {
return this.create({
tmcSidechain: parseFloat(tmcSidechain/10**18),
walletAddress: walletAddress,
logs: [{
type: 'reward',
change: (parseFloat(tmcSidechain/10**18)),
message: `Tomo rewarded you ${parseFloat(tmcSidechain/10**18)} TMC`,
tmcSidechain: parseFloat(tmcSidechain/10**18),
tmcMainchain: "0",
time: new Date()
}]
}).then(wallet => wallet);
}
const preTmcSidechain = parseFloat(w.tmcSidechain);
const change = (parseFloat(tmcSidechain/10**18) - preTmcSidechain);
w.logs.unshift({
type: 'reward',
change: change,
message: `Tomo rewarded you ${change} TMC`,
tmcSidechain: parseFloat(tmcSidechain/10**18),
tmcMainchain: w.tmcMainchain,
time: new Date()
});
w.tmcSidechain = (String(tmcSidechain/10**18));
return w.save().then(() => w);
});
},
cashOut: function({walletAddress, tmcSidechain, tmcMainchain}) {
this.findOne({
walletAddress: walletAddress
}).then(w =>{
if (!w) return false;
const change = (parseFloat(tmcMainchain/10**18) - parseFloat(w.tmcMainchain));
w.logs.unshift({
type: 'cashOut',
change: change,
message: `You cashed out ${change} TMC`,
tmcSidechain: parseFloat(tmcSidechain/10**18),
tmcMainchain: parseFloat(tmcMainchain/10**18),
time: new Date()
});
w.tmcSidechain = (String(tmcSidechain/10**18));
w.tmcMainchain = (String(tmcMainchain/10**18));
return w.save().then(() => w);
});
},
cashIn: function({walletAddress, tmcSidechain, tmcMainchain}) {
this.findOne({
walletAddress: walletAddress
}).then(w =>{
if (!w) return false;
const change = (parseFloat(tmcSidechain/10**18) - parseFloat(w.tmcSidechain));
w.logs.unshift({
type: 'cashIn',
change: change,
message: `You cashed in ${change} TMC`,
tmcSidechain: parseFloat(tmcSidechain/10**18),
tmcMainchain: parseFloat(tmcMainchain/10**18),
time: new Date()
});
w.tmcSidechain = (String(tmcSidechain/10**18));
w.tmcMainchain = (String(tmcMainchain/10**18));
return w.save().then(() => w);
});
}
};

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

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
Oops, something went wrong.

0 comments on commit ca89c99

Please sign in to comment.
You can’t perform that action at this time.