feat: vic init

This commit is contained in:
chaosBreaking
2019-10-23 16:20:13 +08:00
parent 6351c697da
commit 9a3dbd9375
4 changed files with 51 additions and 20 deletions

View File

@@ -2,5 +2,8 @@
module.exports = { module.exports = {
USDTAddr: '0xdac17f958d2ee523a2206206994597c13d831ec7', USDTAddr: '0xdac17f958d2ee523a2206206994597c13d831ec7',
startBlock: 8510200 VICAddr: '0xA5D1Eb8bBB42b7f2EBBebF174B3966510243F30c',
startBlock: 8510200,
VIC_DECIMAL: 1e18,
USDT_DECIMAL: 1e6,
}; };

View File

@@ -1,6 +1,6 @@
'use strict'; 'use strict';
const { ERC20 } = require('../utils/erc20'); const { ERC20 } = require('../utils/erc20');
const { USDTAddr, startBlock } = require('./constant'); const { USDTAddr, VICAddr, startBlock, VIC_DECIMAL, USDT_DECIMAL } = require('./constant');
const getUSDTTxList = async address => { const getUSDTTxList = async address => {
if (!address) return null; if (!address) return null;
@@ -9,23 +9,43 @@ const getUSDTTxList = async address => {
}); });
return res.filter(tx => tx.tokenName === 'Tether USD' && tx.tokenSymbol === 'USDT' && tx.to === address && tx.from !== address); //只取转入到本地址的 return res.filter(tx => tx.tokenName === 'Tether USD' && tx.tokenSymbol === 'USDT' && tx.to === address && tx.from !== address); //只取转入到本地址的
}; };
const getVICTxList = async address => {
if (!address) return null;
const res = await ERC20.getActions(address, VICAddr, startBlock).catch(err => {
if (err) return [];
});
return res.filter(tx => tx.tokenName === 'Value of Individual' && tx.tokenSymbol === 'VIC' && tx.to === address && tx.from !== address); //只取转入到本地址的
};
const parseUsdtTx = txList => { const parseETHTx = txList => {
if (!txList || !Array.isArray(txList)) return []; if (!txList || !Array.isArray(txList)) return [];
return txList.map(tx => { return txList.map(tx => {
tx.amount = (+tx.value) / 1e6; tx.amount = (+tx.value) / VIC_DECIMAL;
return tx; return tx;
}); });
}; };
// usdt
const getNewDepositTx = (usdtTx = [], fundList = []) => { const getNewUSDTDepositTx = (usdtTx = [], fundList = []) => {
if (fundList.length > usdtTx.length) return null; if (fundList.length > usdtTx.length) return null;
// fundList的顺序应该与usdtTx的顺序一致所以只要截取新的 // fundList的顺序应该与usdtTx的顺序一致所以只要截取新的
const newUsdtTxList = usdtTx.slice(fundList.length); const newUsdtTxList = usdtTx.slice(fundList.length);
return parseUsdtTx(newUsdtTxList); return parseETHTx(newUsdtTxList);
};
// vic
const getNewDepositTx = (vicTx = [], fundList = []) => {
if (fundList.length >= vicTx.length) return null;
const newList = [];
const existedFund = fundList.map(fund => fund.txHash).filter(f => f !== null);
for (let i = 0, len = vicTx.length; i < len; ++i) {
if (!existedFund.includes(vicTx[i].hash)) {
newList.push(vicTx[i]);
}
}
return parseETHTx(newList);
}; };
module.exports = { module.exports = {
getVICTxList,
getUSDTTxList, getUSDTTxList,
getNewDepositTx getNewDepositTx
}; };

View File

@@ -8,13 +8,13 @@ const DayLong = 24 * 60 * 60 * 1000;
*/ */
class Fund { class Fund {
constructor (data = {}) { constructor (data = {}) {
const { amount, blockNumber, hash = '', blockHash = '', from = '', to = '', timeStamp = '', usdtValue = 0, type = 'deposit' } = data; const { amount, blockNumber, hash = '', blockHash = '', from = '', to = '', timeStamp = '', rawAmount = 0, type = 'deposit' } = data;
this.status = 0; this.status = 0;
this.type = type; this.type = type;
this.amount = +amount; this.amount = +amount;
this.from = from; this.from = from;
this.to = to; this.to = to;
this.usdtValue = usdtValue; this.rawAmount = rawAmount;
this.timeStamp = timeStamp; this.timeStamp = timeStamp;
this.blockNumber = blockNumber; this.blockNumber = blockNumber;
this.blockHash = blockHash; this.blockHash = blockHash;

View File

@@ -4,10 +4,9 @@ const mongoose = require('mongoose');
const ASSETTYPES = ['gin', 'vic', 'usdtBTC', 'usdtETH', 'usdtTRX']; const ASSETTYPES = ['gin', 'vic', 'usdtBTC', 'usdtETH', 'usdtTRX'];
const { createAction } = require('../action/action.handler'); const { createAction } = require('../action/action.handler');
const { createNewFund } = require('./fund.handler'); const { createNewFund } = require('./fund.handler');
const { getNewDepositTx, getUSDTTxList } = require('../../common/deposit'); const { getNewDepositTx, getVICTxList } = require('../../common/deposit');
const User = require('../user/user.model'); const User = require('../user/user.model');
const System = require('../system/system.model');
const { USDT2VIC_RATE } = require('./constant');
const FundSchema = new mongoose.Schema({ const FundSchema = new mongoose.Schema({
userId: { userId: {
@@ -16,6 +15,7 @@ const FundSchema = new mongoose.Schema({
tokenAddress: { tokenAddress: {
type: {}, type: {},
default: { default: {
'vic': '',
'usdtBTC': '', 'usdtBTC': '',
'usdtETH': '', 'usdtETH': '',
'usdtTRX': '' 'usdtTRX': ''
@@ -33,6 +33,10 @@ const FundSchema = new mongoose.Schema({
type: Number, type: Number,
default: 0 default: 0
}, },
vicFundSum: {
type: Number,
default: 0
},
// asset表示用户的收益包括VIC和团队奖励的USDT // asset表示用户的收益包括VIC和团队奖励的USDT
// vic是挖矿得到的奖励 // vic是挖矿得到的奖励
// usdt是社群奖励后面加上了糖果也代表糖果数量 // usdt是社群奖励后面加上了糖果也代表糖果数量
@@ -77,10 +81,10 @@ const FundModel = mongoose.model('Fund', FundSchema);
FundModel.prototype.hasNewDeposit = async function () { FundModel.prototype.hasNewDeposit = async function () {
if (this.tokenAddress.usdtETH) { if (this.tokenAddress.usdtETH) {
const usdtTx = await getUSDTTxList(this.tokenAddress.usdtETH.address); const vicTx = await getVICTxList(this.tokenAddress.usdtETH.address);
if (usdtTx && usdtTx.length > 0) { if (vicTx && vicTx.length > 0) {
const fundList = this.fundList; const fundList = this.fundList;
const newDepositList = getNewDepositTx(usdtTx, fundList); const newDepositList = getNewDepositTx(vicTx, fundList);
if (newDepositList && newDepositList.length > 0) { if (newDepositList && newDepositList.length > 0) {
mylog.info(`用户${this.userId} 有新的充值记录`); mylog.info(`用户${this.userId} 有新的充值记录`);
return { hasNew: true, list: newDepositList }; return { hasNew: true, list: newDepositList };
@@ -95,10 +99,14 @@ FundModel.prototype.hasNewDeposit = async function () {
、 */ 、 */
FundModel.prototype.addFund = async function (txlist = [], option) { FundModel.prototype.addFund = async function (txlist = [], option) {
let newSum = 0; let newSum = 0;
const VIC_RATE = (await System.findOne({ key: 'VIC_RATE' }).exec().catch(() => {
mylog.error('没有获取到vic兑换比例');
return 1;
})) || 1;
const actionPromiseList = []; const actionPromiseList = [];
const newFundList = txlist.map(txObj => { const newFundList = txlist.map(txObj => {
txObj.usdtValue = txObj.amount; txObj.rawAmount = txObj.amount; // tx.amout是区块链交易的数值已经除了精度
const depositAmount = +txObj.amount * USDT2VIC_RATE; const depositAmount = +txObj.amount * VIC_RATE;
newSum += depositAmount; newSum += depositAmount;
const action = createAction(0, { const action = createAction(0, {
userId: this.userId, userId: this.userId,
@@ -116,7 +124,7 @@ FundModel.prototype.addFund = async function (txlist = [], option) {
fundList: newFundList fundList: newFundList
}, },
$inc: { $inc: {
fundSum: newSum vicFundSum: newSum
}, },
$set: { $set: {
lastDeposit: new Date().toISOString() lastDeposit: new Date().toISOString()
@@ -147,7 +155,7 @@ FundModel.prototype.addFund = async function (txlist = [], option) {
}); });
User.findByIdAndUpdate(user.inviter, { User.findByIdAndUpdate(user.inviter, {
$push: { $push: {
fundList: inviteFund vicFundSum: inviteFund
} }
}); });
createAction(4, { createAction(4, {
@@ -156,7 +164,7 @@ FundModel.prototype.addFund = async function (txlist = [], option) {
tokenType: 'vic', // 目前设定只能挖出矿晶vic tokenType: 'vic', // 目前设定只能挖出矿晶vic
remain: this.asset, remain: this.asset,
detail: { detail: {
desc: `${user.id}充值${newSum} 奖励其推荐人${user.inviter}: ${award}矿晶`, desc: `${user.id}购买${newSum} 奖励其推荐人${user.inviter}: ${award}矿晶`,
from: myId, from: myId,
to: user.inviter to: user.inviter
} }