"use strict";async function saveValidMarkets(e,t){const a=await db_1.models.exchangeMarket.findAll({attributes:["currency","pair"],transaction:t}),r=new Set(a.map(e=>`${e.currency}/${e.pair}`));for(const a of Object.keys(e)){const s=e[a],[i,n]=a.split("/");r.has(a)||await db_1.models.exchangeMarket.create({currency:i,pair:n,metadata:s,status:!1},{transaction:t})}}var __importDefault=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(exports,"__esModule",{value:!0});exports.metadata=void 0;const exchange_1=__importDefault(require("@b/utils/exchange")),db_1=require("@b/db"),sequelize_1=require("sequelize"),query_1=require("@b/utils/query"),utils_1=require("../utils"),error_1=require("@b/utils/error");exports.metadata={summary:"Import Exchange Markets",operationId:"importMarkets",tags:["Admin","Settings","Exchange"],description:"Imports markets from the specified exchange, processes their data, and saves them to the database.",requiresAuth:!0,responses:{200:{description:"Markets imported successfully",content:{"application/json":{schema:{type:"object",properties:{message:{type:"string"}}}}}},401:query_1.unauthorizedResponse,404:(0,query_1.notFoundMetadataResponse)("Exchange"),500:query_1.serverErrorResponse},permission:"create.exchange.market"};exports.default=async e=>{const{ctx:t}=e,a=await exchange_1.default.startExchange(t),r=await exchange_1.default.getProvider();if(!a)throw(0,error_1.createError)({statusCode:500,message:`Failed to start exchange provider: ${r}`});await a.loadMarkets();const s=a.markets,i={};for(const e of Object.values(s))if(e.active&&e.precision.price&&e.precision.amount){if(r&&["binance","xt"].includes(r)&&"spot"!==e.type)continue;const{symbol:t,precision:a,limits:s,taker:n,maker:o}=e;i[t]={taker:n,maker:o,precision:{price:(0,utils_1.countDecimals)(a.price),amount:(0,utils_1.countDecimals)(a.amount)},limits:{amount:s.amount||{min:0,max:null},price:s.price||{min:0,max:null},cost:s.cost||{min:1e-4,max:9e6},leverage:s.leverage||{}}}}const n=Object.keys(i),o=await db_1.models.exchangeMarket.findAll({attributes:["currency","pair"]}),c=[...new Set(o.map(e=>`${e.currency}/${e.pair}`))].filter(e=>!n.includes(e));await db_1.sequelize.transaction(async e=>{if(c.length>0){await db_1.models.exchangeMarket.destroy({where:{[sequelize_1.Op.or]:c.map(e=>{const[t,a]=e.split("/");return{currency:t,pair:a}})},transaction:e});await db_1.models.exchangeOrder.destroy({where:{symbol:{[sequelize_1.Op.in]:c}},transaction:e});await db_1.models.exchangeWatchlist.destroy({where:{symbol:{[sequelize_1.Op.in]:c}},transaction:e})}await saveValidMarkets(i,e)});return{message:"Exchange markets imported and saved successfully!"}};