Commit c60b827e authored by ml's avatar ml

资产对账-除了做市相关的

parent e6c94470
import * as assetCheckService from "../../service/v2/assetCheck.service"; import * as assetCheckService from "../../service/v2/assetCheck.service";
import { CommonParam ,setPageAndSize} from "../../service/v2/abkCommonService"; import { CommonParam, setPageAndSize } from "../../service/v2/abkCommonService";
let { logger, Res3Utils, optionalUtils: Optional, apiAssertUtils: ApiAssert } = require('@madex/ex-js-public'); let { logger, Res3Utils, optionalUtils: Optional, apiAssertUtils: ApiAssert } = require('@madex/ex-js-public');
import { ErrorCode } from "../../../../constant/errorCode"; import { ErrorCode } from "../../../../constant/errorCode";
...@@ -31,10 +31,10 @@ export const assetDetails = async (req: any, commonParam: CommonParam) => { ...@@ -31,10 +31,10 @@ export const assetDetails = async (req: any, commonParam: CommonParam) => {
let func_name = "assetCheck.control.assetDetails"; let func_name = "assetCheck.control.assetDetails";
try { try {
setPageAndSize(commonParam); setPageAndSize(commonParam);
if (!commonParam.user_id){ if (!commonParam.user_id) {
throw ErrorCode.PARAM_MISS; throw ErrorCode.PARAM_MISS;
} }
let res = await assetCheckService.assetDetails(commonParam.user_id); let res = await assetCheckService.assetDetails(commonParam.page, commonParam.size, commonParam.user_id);
return Res3Utils.result(res); return Res3Utils.result(res);
} }
catch (e) { catch (e) {
...@@ -52,7 +52,28 @@ export const siteAssets = async (req: any, commonParam: CommonParam) => { ...@@ -52,7 +52,28 @@ export const siteAssets = async (req: any, commonParam: CommonParam) => {
let func_name = "assetCheck.control.siteAssets"; let func_name = "assetCheck.control.siteAssets";
try { try {
setPageAndSize(commonParam); setPageAndSize(commonParam);
let res = await assetCheckService.siteAssets(commonParam.page,commonParam.size); let res = await assetCheckService.siteAssets(commonParam.page, commonParam.size);
return Res3Utils.result(res);
}
catch (e) {
logger.error(`${func_name} error:${e}`);
return Res3Utils.getErrorResult(e);
}
};
/**
* 站内资金
* @param req
* @param commonParam
*/
export const siteAssetsDetails = async (req: any, commonParam: CommonParam) => {
let func_name = "assetCheck.control.siteAssetsDetails";
try {
if (!commonParam.asset_id) {
throw ErrorCode.PARAM_MISS;
}
let res = await assetCheckService.siteAssetsDetails(Number(commonParam.asset_id));
return Res3Utils.result(res); return Res3Utils.result(res);
} }
catch (e) { catch (e) {
...@@ -71,7 +92,7 @@ export const depositList = async (req: any, commonParam: CommonParam) => { ...@@ -71,7 +92,7 @@ export const depositList = async (req: any, commonParam: CommonParam) => {
let func_name = "assetCheck.control.depositList"; let func_name = "assetCheck.control.depositList";
try { try {
setPageAndSize(commonParam); setPageAndSize(commonParam);
let res = await assetCheckService.depositList(commonParam.page,commonParam.size,commonParam.uid_or_addr); let res = await assetCheckService.depositList(commonParam.page, commonParam.size, commonParam.uid_or_addr);
return Res3Utils.result(res); return Res3Utils.result(res);
} }
catch (e) { catch (e) {
...@@ -90,7 +111,7 @@ export const withdrawList = async (req: any, commonParam: CommonParam) => { ...@@ -90,7 +111,7 @@ export const withdrawList = async (req: any, commonParam: CommonParam) => {
let func_name = "assetCheck.control.withdrawList"; let func_name = "assetCheck.control.withdrawList";
try { try {
setPageAndSize(commonParam); setPageAndSize(commonParam);
let res = await assetCheckService.withdrawList(commonParam.page,commonParam.size,commonParam.uid_or_addr); let res = await assetCheckService.withdrawList(commonParam.page, commonParam.size, commonParam.uid_or_addr);
return Res3Utils.result(res); return Res3Utils.result(res);
} }
catch (e) { catch (e) {
......
...@@ -18,6 +18,8 @@ export interface CommonParam { ...@@ -18,6 +18,8 @@ export interface CommonParam {
address?: any//查询条件 用户id 或 充提地址 address?: any//查询条件 用户id 或 充提地址
user_type?: any//查询条件 用户类型:1:普通用户;2:代理用户(crm用户) user_type?: any//查询条件 用户类型:1:普通用户;2:代理用户(crm用户)
asset_id?: any
} }
export function setPageAndSize(commonParam: CommonParam) { export function setPageAndSize(commonParam: CommonParam) {
......
import { madAdminOrmDB, coinAddress, coinType, mainUserAsset, dwdSpotAssetsApi, ormDB, coinTx, coinWithdraw } from "@madex/ex-ts-dao"; import { madAdminOrmDB, coinAddress, coinType, mainUserAsset, dwdSpotAssetsApi, ormDB, coinTx, coinWithdraw, walletAssets } from "@madex/ex-ts-dao";
import { getOneAbkUserByAccount, getOneAbkUserByUid, checkAbkTotp } from "../../../../utils/abkUserUtils";
import { ErrorCode } from "../../../../constant/errorCode";
import { AbkUserInfoConst } from "../../../../constant/abkUserConstant";
import { CryptUtils } from "../../../../utils/crypt-utils";
import { RedisVal } from "../../../../constant/redis-val";
import Config from "../../../../../config";
import BigNumber from "bignumber.js"; import BigNumber from "bignumber.js";
import { it } from "node:test";
const Otplib = require('otplib');
let { logger, apiAssertUtils: ApiAssert, BigNumberUtils } = require('@madex/ex-js-public'); let { logger, apiAssertUtils: ApiAssert, BigNumberUtils } = require('@madex/ex-js-public');
let { authCommon: AuthCommon, redisUtilsCommon: RedisClient, } = require('@madex/ex-js-common'); let { authCommon: AuthCommon, redisUtilsCommon: RedisClient, } = require('@madex/ex-js-common');
...@@ -40,87 +31,128 @@ export async function userAddrList(page: any, size: any, user_id: any) { ...@@ -40,87 +31,128 @@ export async function userAddrList(page: any, size: any, user_id: any) {
return res; return res;
} }
export async function assetDetails(user_id: any) { export async function assetDetails(page: any, size: any, user_id: number) {
let where: any = {}; let res = await coinAddress.prototype.findAndCount({
where["user_id"] = Number(user_id); attributes: ['coin_id', 'coin_symbol', 'balance'],
where: {
user_id: user_id
},
limit: size,
offset: (page - 1) * size,
order: [["createdAt", "desc"]],
raw: true
});
if (res.rows.length) {
let coinIds = res.rows.map(item => item.coin_id);
let coinMap = await getCoinTypeMapByCoinOrAssetIds(coinIds);
for (let item of res.rows) {
item.chain_type = coinMap[item.coin_id].chain_type;
item.coin_symbol = coinMap[item.coin_id].symbol;
}
}
return res;
}
let assets_map: any = {};
let asset_id_list: any = []; export async function siteAssets(page: any, size: any) {
let task1 = mainUserAsset.prototype.findAll({ let pageData = await coinType.prototype.findAndCount({
attributes: ['asset_id', 'symbol', 'balance', 'holds'], attributes: [ormDB.literal('DISTINCT(asset_id) as asset_id')],
where: where, where: {
main_status: 2//已经提交到撮合
},
limit: size,
offset: (page - 1) * size,
order: [['asset_id', 'asc']],
raw: true raw: true
}); });
if (!pageData.rows.length) {
return pageData;
}
let asset_ids = pageData.rows.map(item => item.asset_id);
let task2 = dwdSpotAssetsApi.prototype.findAll({ let dbCoinList = await coinType.prototype.findAll({
attributes: ['asset_id', 'asset_symbol', 'balance', 'unreal_profit'], attributes: ['id', 'asset_id', 'symbol', 'chain_type'],
where: where, where: {
asset_id: asset_ids
},
order: [['asset_id', 'asc'], ['id', 'asc']],
raw: true raw: true
}); });
let [dbMainAssets, dbTradeAssets] = await Promise.all([task1, task2]); let coinMap: any = {};
for (let item of dbCoinList) {
for (let item of dbMainAssets) { //同名币 asset_id 一样 id 不一样
let total = new BigNumber(item.balance).add(new BigNumber(item.holds)); if (!coinMap[item.asset_id]) {
assets_map[item.asset_id] = assets_map[item.asset_id] ? assets_map[item.asset_id].add(total) : total; coinMap[item.asset_id] = item
if (!asset_id_list.includes(item.asset_id)) {
asset_id_list.push(item.asset_id);
} }
} }
for (let item of dbTradeAssets) {
let total = new BigNumber(item.balance).add(new BigNumber(item.unreal_profit));
assets_map[item.asset_id] = assets_map[item.asset_id] ? assets_map[item.asset_id].add(total) : total;
if (!asset_id_list.includes(item.asset_id)) {
asset_id_list.push(item.asset_id);
}
}
let coinMap = await getCoinTypeMapByCoinOrAssetIds([], asset_id_list); let [mainAssetsMap, tradeAssetsMap, { hotWalletMap, coldWalletMap, totalWalletMap }] = await Promise.all([getSiteMainAssetsGroupBy(asset_ids, 'asset_id'),
getSiteTradeAssetsGroupBy(asset_ids, 'asset_id'),
getSiteWalletAssetsGroupBy(asset_ids, 'asset_id')]);
let asset_keys = _.keys(assets_map); for (let item of pageData.rows) {
let res: any = []; let assetId = item.asset_id;
for (let assetKey of asset_keys) { item.symbol = coinMap[assetId] ? coinMap[assetId].symbol : "";
let balance = assets_map[Number(assetKey)]; item.chain_type = coinMap[assetId] ? coinMap[assetId].chain_type : "";
let symbol = coinMap[Number(assetKey)] ? coinMap[Number(assetKey)].symbol : ""; item.site_assets = (mainAssetsMap[assetId] ? mainAssetsMap[assetId] : new BigNumber(0)).add(
let chain_type = coinMap[Number(assetKey)] ? coinMap[Number(assetKey)].chain_type : ""; tradeAssetsMap[assetId] ? tradeAssetsMap[assetId] : new BigNumber(0)
res.push({ );
chain_type, item.total_wallet_assets = totalWalletMap[assetId] ? totalWalletMap[assetId] : new BigNumber(0);
symbol, item.hot_wallet_assets = hotWalletMap[assetId] ? hotWalletMap[assetId] : new BigNumber(0);
balance item.cold_wallet_assets = coldWalletMap[assetId] ? coldWalletMap[assetId] : new BigNumber(0);
});
} }
return res; return pageData;
} }
export async function siteAssets(page: any, size: any) { export async function siteAssetsDetails(asset_id: number) {
let pageData = await coinType.prototype.findAndCount({ let symbolList = await coinType.prototype.findAll({
attributes: ['symbol', 'chain_type'], attributes: ['symbol'],
where: { where: {
main_status: 2//已经提交到撮合 asset_id
}, },
limit: size,
offset: (page - 1) * size,
order: [['id', 'asc']],
raw: true raw: true
}); });
if (!pageData.rows.length) { //单个币种没必要查
return pageData; if (symbolList.length < 2) {
return [];
} }
let symbols = pageData.rows.map(item => item.symbol);
let [mainAssetsMap, tradeAssetsMap] = await Promise.all([getSiteMainAssetsBySymbols(symbols), getSiteTradeAssetsBySymbols(symbols)]); let symbols = symbolList.map(item => item.symbol);
for (let item of pageData.rows) { let dbCoinList = await coinType.prototype.findAll({
let balance = new BigNumber(mainAssetsMap[item.symbol] || 0).add(new BigNumber(tradeAssetsMap[item.symbol] || 0)); attributes: ['id', 'asset_id', 'symbol', 'chain_type'],
item.balance = balance; where: {
symbol: symbols
},
raw: true
});
let coinMap: any = {};
for (let item of dbCoinList) {
coinMap[item.symbol] = item
} }
return pageData;
let [mainAssetsMap, tradeAssetsMap, { hotWalletMap, coldWalletMap, totalWalletMap }] = await Promise.all([getSiteMainAssetsGroupBy(symbols, 'symbol'),
getSiteTradeAssetsGroupBy(symbols, 'asset_symbol'),
getSiteWalletAssetsGroupBy(symbols, 'coin_symbol')]);
for (let item of symbolList) {
let symbol = item.symbol;
item.chain_type = coinMap[symbol] ? coinMap[symbol].chain_type : "";
item.site_assets = (mainAssetsMap[symbol] ? mainAssetsMap[symbol] : new BigNumber(0)).add(
tradeAssetsMap[symbol] ? tradeAssetsMap[symbol] : new BigNumber(0)
);
item.total_wallet_assets = totalWalletMap[symbol] ? totalWalletMap[symbol] : new BigNumber(0);
item.hot_wallet_assets = hotWalletMap[symbol] ? hotWalletMap[symbol] : new BigNumber(0);
item.cold_wallet_assets = coldWalletMap[symbol] ? coldWalletMap[symbol] : new BigNumber(0);
}
return symbolList;
} }
export async function depositList(page: any, size: any, uid_or_addr: any) { export async function depositList(page: any, size: any, uid_or_addr: any) {
...@@ -212,39 +244,67 @@ async function getCoinTypeMapByCoinOrAssetIds(coin_ids: any[], asset_ids?: any[] ...@@ -212,39 +244,67 @@ async function getCoinTypeMapByCoinOrAssetIds(coin_ids: any[], asset_ids?: any[]
return map; return map;
} }
async function getSiteMainAssetsBySymbols(symbols: any[]) { async function getSiteMainAssetsGroupBy(conditions: any[], group_by_field: string) {
let map: any = {}; let map: any = {};
let where: any = {};
where[group_by_field] = conditions;
let mainAssetsList = await mainUserAsset.prototype.findAll({ let mainAssetsList = await mainUserAsset.prototype.findAll({
attributes: ['symbol', ormDB.literal('sum(balance) as balance'), attributes: [group_by_field, ormDB.literal('sum(balance) as balance'),
ormDB.literal('sum(holds) as holds'),], ormDB.literal('sum(holds) as holds'),],
where: { where: where,
symbol: symbols group: [group_by_field, 'symbol'],
},
group: ['symbol'],
raw: true raw: true
}); });
for (let item of mainAssetsList) { for (let item of mainAssetsList) {
map[item.symbol] = new BigNumber(item.balance).add(new BigNumber(item.holds)); map[item[group_by_field]] = new BigNumber(item.balance).add(new BigNumber(item.holds));
} }
return map; return map;
} }
async function getSiteTradeAssetsBySymbols(symbols: any[]) { async function getSiteTradeAssetsGroupBy(conditions: any[], group_by_field: string) {
let map: any = {}; let map: any = {};
let where: any = {};
where[group_by_field] = conditions;
let tradeAssetsList = await dwdSpotAssetsApi.prototype.findAll({ let tradeAssetsList = await dwdSpotAssetsApi.prototype.findAll({
attributes: ['asset_symbol', ormDB.literal('sum(balance) as balance'), attributes: [group_by_field, ormDB.literal('sum(balance) as balance'),
ormDB.literal('sum(unreal_profit) as unreal_profit'),], ormDB.literal('sum(unreal_profit) as unreal_profit'),],
where: { where: where,
asset_symbol: symbols group: [group_by_field],
},
group: ['asset_symbol'],
raw: true raw: true
}); });
for (let item of tradeAssetsList) { for (let item of tradeAssetsList) {
map[item.asset_symbol] = new BigNumber(item.balance).add(new BigNumber(item.unreal_profit)); map[item[group_by_field]] = new BigNumber(item.balance).add(new BigNumber(item.unreal_profit));
} }
return map; return map;
} }
async function getSiteWalletAssetsGroupBy(conditions: any[], group_by_field: string) {
let where: any = {};
where[group_by_field] = conditions;
let walletAssetsList = await walletAssets.prototype.findAll({
attributes: [group_by_field, 'type', ormDB.literal('sum(balance) as balance')],
where: where,
group: [group_by_field, 'type'],
raw: true
});
let hotWalletMap: any = {};
let coldWalletMap: any = {};
let totalWalletMap: any = {};
for (let item of walletAssetsList) {
if (item.type == 3) {//冷钱包
coldWalletMap[item[group_by_field]] = new BigNumber(item.balance);
}
else {//热钱包
hotWalletMap[item[group_by_field]] = hotWalletMap[item[group_by_field]] ? hotWalletMap[item[group_by_field]].add(new BigNumber(item.balance)) : new BigNumber(item.balance);
}
totalWalletMap[item[group_by_field]] = totalWalletMap[item[group_by_field]] ? totalWalletMap[item[group_by_field]].add(new BigNumber(item.balance)) : new BigNumber(item.balance);
}
return {
hotWalletMap, coldWalletMap, totalWalletMap
}
}
...@@ -31,6 +31,7 @@ const postFunc = { ...@@ -31,6 +31,7 @@ const postFunc = {
'assetCheck/userAddrList': assetCheckCtrl.userAddrList,//用户地址 'assetCheck/userAddrList': assetCheckCtrl.userAddrList,//用户地址
'assetCheck/assetDetails': assetCheckCtrl.assetDetails,//资产明细 'assetCheck/assetDetails': assetCheckCtrl.assetDetails,//资产明细
'assetCheck/siteAssets': assetCheckCtrl.siteAssets,//站内资产 'assetCheck/siteAssets': assetCheckCtrl.siteAssets,//站内资产
'assetCheck/siteAssetsDetails': assetCheckCtrl.siteAssetsDetails,//站内资产-详情
'assetCheck/depositList': assetCheckCtrl.depositList,//实时充值 'assetCheck/depositList': assetCheckCtrl.depositList,//实时充值
'assetCheck/withdrawList': assetCheckCtrl.withdrawList,//实时提现 'assetCheck/withdrawList': assetCheckCtrl.withdrawList,//实时提现
//收入分析 //收入分析
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment