"use strict";async function saveValidCurrencies(e,r){const t=await db_1.models.exchangeCurrency.findAll({attributes:["currency"],transaction:r}),s=new Set(t.map(e=>e.currency)),a=Object.keys(e);for(const t of a){const a=e[t];try{const e=void 0!==a.fee&&null!==a.fee?Number(a.fee):0;if(isNaN(e))continue;s.has(t)?await db_1.models.exchangeCurrency.update({name:a.name||a.currency,precision:a.precision,status:!1,fee:e},{where:{currency:t},transaction:r}):await db_1.models.exchangeCurrency.create({currency:a.currency,name:a.name||a.currency,precision:a.precision,status:!1,fee:e},{transaction:r})}catch(e){continue}}}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"),query_1=require("@b/utils/query"),utils_1=require("../../exchange/utils"),sequelize_1=require("sequelize"),cron_1=require("@b/cron"),console_1=require("@b/utils/console"),error_1=require("@b/utils/error");exports.metadata={summary:"Import Exchange Currencies",operationId:"importCurrencies",tags:["Admin","Settings","Exchange"],description:"Imports currencies from the specified exchange, processes their data, and saves them to the database.",requiresAuth:!0,responses:{200:{description:"Currencies 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.spot.currency"};exports.default=async e=>{const{ctx:r}=e,t=await exchange_1.default.startExchange(r),s=await exchange_1.default.getProvider();if(!t)throw(0,error_1.createError)({statusCode:503,message:`Failed to start exchange provider: ${s}`});await t.loadMarkets();const a=t.currencies,n={};Object.values(a).forEach(e=>{let r;"binance"===s?r=(0,utils_1.standardizeBinanceData)(e.networks||{}):"kucoin"===s?r=(0,utils_1.standardizeKucoinData)(e):"okx"===s?r=(0,utils_1.standardizeOkxData)(e.networks||{}):"xt"===s&&(r=(0,utils_1.standardizeXtData)(e));e.precision&&(n[e.code]={currency:e.code,name:e.name||e.code,precision:parseInt(e.precision),status:e.active,deposit:e.deposit,withdraw:e.withdraw,fee:e.fee,chains:r})});const c=Object.keys(n),i=await db_1.models.exchangeCurrency.findAll({attributes:["currency"]}),o=[...new Set(i.map(e=>e.currency))].filter(e=>!c.includes(e));await db_1.sequelize.transaction(async e=>{o.length>0&&await db_1.models.exchangeCurrency.destroy({where:{currency:{[sequelize_1.Op.in]:o}},transaction:e});await saveValidCurrencies(n,e)});try{await(0,cron_1.processCurrenciesPrices)()}catch(e){console_1.logger.error("CURRENCY","Error processing currencies prices",e)}return{message:"Exchange currencies imported and saved successfully!"}};