Commit c60b827e authored by ml's avatar ml

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

parent e6c94470
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');
import { ErrorCode } from "../../../../constant/errorCode";
......@@ -31,10 +31,10 @@ export const assetDetails = async (req: any, commonParam: CommonParam) => {
let func_name = "assetCheck.control.assetDetails";
try {
setPageAndSize(commonParam);
if (!commonParam.user_id){
if (!commonParam.user_id) {
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);
}
catch (e) {
......@@ -52,7 +52,28 @@ export const siteAssets = async (req: any, commonParam: CommonParam) => {
let func_name = "assetCheck.control.siteAssets";
try {
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);
}
catch (e) {
......@@ -71,7 +92,7 @@ export const depositList = async (req: any, commonParam: CommonParam) => {
let func_name = "assetCheck.control.depositList";
try {
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);
}
catch (e) {
......@@ -90,7 +111,7 @@ export const withdrawList = async (req: any, commonParam: CommonParam) => {
let func_name = "assetCheck.control.withdrawList";
try {
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);
}
catch (e) {
......
......@@ -18,6 +18,8 @@ export interface CommonParam {
address?: any//查询条件 用户id 或 充提地址
user_type?: any//查询条件 用户类型:1:普通用户;2:代理用户(crm用户)
asset_id?: any
}
export function setPageAndSize(commonParam: CommonParam) {
......
import { madAdminOrmDB, coinAddress, coinType, mainUserAsset, dwdSpotAssetsApi, ormDB, coinTx, coinWithdraw } 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 { madAdminOrmDB, coinAddress, coinType, mainUserAsset, dwdSpotAssetsApi, ormDB, coinTx, coinWithdraw, walletAssets } from "@madex/ex-ts-dao";
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 { authCommon: AuthCommon, redisUtilsCommon: RedisClient, } = require('@madex/ex-js-common');
......@@ -40,87 +31,128 @@ export async function userAddrList(page: any, size: any, user_id: any) {
return res;
}
export async function assetDetails(user_id: any) {
let where: any = {};
where["user_id"] = Number(user_id);
export async function assetDetails(page: any, size: any, user_id: number) {
let res = await coinAddress.prototype.findAndCount({
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({
attributes: ['asset_id', 'symbol', 'balance', 'holds'],
where: where,
let pageData = await coinType.prototype.findAndCount({
attributes: [ormDB.literal('DISTINCT(asset_id) as asset_id')],
where: {
main_status: 2//已经提交到撮合
},
limit: size,
offset: (page - 1) * size,
order: [['asset_id', 'asc']],
raw: true
});
if (!pageData.rows.length) {
return pageData;
}
let asset_ids = pageData.rows.map(item => item.asset_id);
let task2 = dwdSpotAssetsApi.prototype.findAll({
attributes: ['asset_id', 'asset_symbol', 'balance', 'unreal_profit'],
where: where,
let dbCoinList = await coinType.prototype.findAll({
attributes: ['id', 'asset_id', 'symbol', 'chain_type'],
where: {
asset_id: asset_ids
},
order: [['asset_id', 'asc'], ['id', 'asc']],
raw: true
});
let [dbMainAssets, dbTradeAssets] = await Promise.all([task1, task2]);
for (let item of dbMainAssets) {
let total = new BigNumber(item.balance).add(new BigNumber(item.holds));
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: any = {};
for (let item of dbCoinList) {
//同名币 asset_id 一样 id 不一样
if (!coinMap[item.asset_id]) {
coinMap[item.asset_id] = item
}
}
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);
let res: any = [];
for (let assetKey of asset_keys) {
let balance = assets_map[Number(assetKey)];
let symbol = coinMap[Number(assetKey)] ? coinMap[Number(assetKey)].symbol : "";
let chain_type = coinMap[Number(assetKey)] ? coinMap[Number(assetKey)].chain_type : "";
res.push({
chain_type,
symbol,
balance
});
for (let item of pageData.rows) {
let assetId = item.asset_id;
item.symbol = coinMap[assetId] ? coinMap[assetId].symbol : "";
item.chain_type = coinMap[assetId] ? coinMap[assetId].chain_type : "";
item.site_assets = (mainAssetsMap[assetId] ? mainAssetsMap[assetId] : new BigNumber(0)).add(
tradeAssetsMap[assetId] ? tradeAssetsMap[assetId] : new BigNumber(0)
);
item.total_wallet_assets = totalWalletMap[assetId] ? totalWalletMap[assetId] : new BigNumber(0);
item.hot_wallet_assets = hotWalletMap[assetId] ? hotWalletMap[assetId] : new BigNumber(0);
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({
attributes: ['symbol', 'chain_type'],
let symbolList = await coinType.prototype.findAll({
attributes: ['symbol'],
where: {
main_status: 2//已经提交到撮合
asset_id
},
limit: size,
offset: (page - 1) * size,
order: [['id', 'asc']],
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 balance = new BigNumber(mainAssetsMap[item.symbol] || 0).add(new BigNumber(tradeAssetsMap[item.symbol] || 0));
item.balance = balance;
let dbCoinList = await coinType.prototype.findAll({
attributes: ['id', 'asset_id', 'symbol', 'chain_type'],
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) {
......@@ -212,39 +244,67 @@ async function getCoinTypeMapByCoinOrAssetIds(coin_ids: any[], asset_ids?: any[]
return map;
}
async function getSiteMainAssetsBySymbols(symbols: any[]) {
async function getSiteMainAssetsGroupBy(conditions: any[], group_by_field: string) {
let map: any = {};
let where: any = {};
where[group_by_field] = conditions;
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'),],
where: {
symbol: symbols
},
group: ['symbol'],
where: where,
group: [group_by_field, 'symbol'],
raw: true
});
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;
}
async function getSiteTradeAssetsBySymbols(symbols: any[]) {
async function getSiteTradeAssetsGroupBy(conditions: any[], group_by_field: string) {
let map: any = {};
let where: any = {};
where[group_by_field] = conditions;
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'),],
where: {
asset_symbol: symbols
},
group: ['asset_symbol'],
where: where,
group: [group_by_field],
raw: true
});
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;
}
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 = {
'assetCheck/userAddrList': assetCheckCtrl.userAddrList,//用户地址
'assetCheck/assetDetails': assetCheckCtrl.assetDetails,//资产明细
'assetCheck/siteAssets': assetCheckCtrl.siteAssets,//站内资产
'assetCheck/siteAssetsDetails': assetCheckCtrl.siteAssetsDetails,//站内资产-详情
'assetCheck/depositList': assetCheckCtrl.depositList,//实时充值
'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