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 = {
USDTAddr: '0xdac17f958d2ee523a2206206994597c13d831ec7',
startBlock: 8510200
VICAddr: '0xA5D1Eb8bBB42b7f2EBBebF174B3966510243F30c',
startBlock: 8510200,
VIC_DECIMAL: 1e18,
USDT_DECIMAL: 1e6,
};

View File

@@ -1,6 +1,6 @@
'use strict';
const { ERC20 } = require('../utils/erc20');
const { USDTAddr, startBlock } = require('./constant');
const { USDTAddr, VICAddr, startBlock, VIC_DECIMAL, USDT_DECIMAL } = require('./constant');
const getUSDTTxList = async address => {
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); //只取转入到本地址的
};
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 [];
return txList.map(tx => {
tx.amount = (+tx.value) / 1e6;
tx.amount = (+tx.value) / VIC_DECIMAL;
return tx;
});
};
const getNewDepositTx = (usdtTx = [], fundList = []) => {
// usdt
const getNewUSDTDepositTx = (usdtTx = [], fundList = []) => {
if (fundList.length > usdtTx.length) return null;
// fundList的顺序应该与usdtTx的顺序一致所以只要截取新的
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 = {
getVICTxList,
getUSDTTxList,
getNewDepositTx
};

View File

@@ -8,13 +8,13 @@ const DayLong = 24 * 60 * 60 * 1000;
*/
class Fund {
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.type = type;
this.amount = +amount;
this.from = from;
this.to = to;
this.usdtValue = usdtValue;
this.rawAmount = rawAmount;
this.timeStamp = timeStamp;
this.blockNumber = blockNumber;
this.blockHash = blockHash;

View File

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