UNPKG

@cryptodevops/n8n-nodes-mempool-api

Version:

n8n node for Mempool.space API - Mempool live, fees recommandées, détails blocs & transactions

347 lines (346 loc) 16.1 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.MempoolApi = void 0; const n8n_workflow_1 = require("n8n-workflow"); const axios_1 = __importDefault(require("axios")); class MempoolApi { constructor() { this.description = { displayName: 'Mempool.space API', name: 'mempoolApi', icon: 'file:nodes/MempoolApi/mempool.svg', group: ['transform'], version: 1, subtitle: '={{$parameter["operation"]}}', description: 'Accède aux données Bitcoin via l\'API Mempool.space - Mempool live, fees recommandées, détails blocs & transactions', defaults: { name: 'Mempool.space API', }, inputs: ["main" /* NodeConnectionType.Main */], outputs: ["main" /* NodeConnectionType.Main */], properties: [ { displayName: 'Operation', name: 'operation', type: 'options', noDataExpression: true, options: [ { name: 'Get Mempool Live', value: 'getMempoolLive', description: 'Récupère les statistiques en temps réel du mempool', action: 'Get live mempool statistics', }, { name: 'Get Recommended Fees', value: 'getRecommendedFees', description: 'Récupère les frais recommandés pour les transactions', action: 'Get recommended transaction fees', }, { name: 'Get Difficulty Adjustment', value: 'getDifficultyAdjustment', description: 'Récupère les informations sur l\'ajustement de difficulté', action: 'Get difficulty adjustment info', }, { name: 'Get Bitcoin Price', value: 'getBitcoinPrice', description: 'Récupère le prix actuel du Bitcoin', action: 'Get current Bitcoin price', }, { name: 'Get Historical Price', value: 'getHistoricalPrice', description: 'Récupère le prix historique du Bitcoin', action: 'Get historical Bitcoin price', }, { name: 'Get Transaction', value: 'getTransaction', description: 'Récupère les détails d\'une transaction', action: 'Get transaction details', }, { name: 'Get Transaction Status', value: 'getTransactionStatus', description: 'Récupère le statut de confirmation d\'une transaction', action: 'Get transaction status', }, { name: 'Get Address Info', value: 'getAddressInfo', description: 'Récupère les informations d\'une adresse', action: 'Get address information', }, { name: 'Get Address Transactions', value: 'getAddressTransactions', description: 'Récupère l\'historique des transactions d\'une adresse', action: 'Get address transactions', }, { name: 'Get Address UTXO', value: 'getAddressUtxo', description: 'Récupère les UTXO d\'une adresse', action: 'Get address UTXO', }, { name: 'Get Block Info', value: 'getBlockInfo', description: 'Récupère les informations d\'un bloc', action: 'Get block information', }, { name: 'Get Latest Blocks', value: 'getLatestBlocks', description: 'Récupère les derniers blocs', action: 'Get latest blocks', }, { name: 'Broadcast Transaction', value: 'broadcastTransaction', description: 'Diffuse une transaction sur le réseau', action: 'Broadcast transaction', }, ], default: 'getMempoolLive', }, { displayName: 'Currency', name: 'currency', type: 'options', displayOptions: { show: { operation: ['getHistoricalPrice'], }, }, options: [ { name: 'USD', value: 'USD', }, { name: 'EUR', value: 'EUR', }, { name: 'GBP', value: 'GBP', }, { name: 'CAD', value: 'CAD', }, { name: 'CHF', value: 'CHF', }, { name: 'AUD', value: 'AUD', }, { name: 'JPY', value: 'JPY', }, ], default: 'USD', description: 'Devise pour le prix historique', }, { displayName: 'Timestamp', name: 'timestamp', type: 'number', displayOptions: { show: { operation: ['getHistoricalPrice'], }, }, default: '', placeholder: '1500000000', description: 'Timestamp Unix pour le prix historique (optionnel)', }, { displayName: 'Transaction ID', name: 'txid', type: 'string', displayOptions: { show: { operation: ['getTransaction', 'getTransactionStatus'], }, }, default: '', placeholder: 'f4184fc596403b9d638783cf57adfe4c75c605f6356fbc91338530e9831e9e16', description: 'ID de la transaction à récupérer', required: true, }, { displayName: 'Address', name: 'address', type: 'string', displayOptions: { show: { operation: ['getAddressInfo', 'getAddressTransactions', 'getAddressUtxo'], }, }, default: '', placeholder: '1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa', description: 'Adresse Bitcoin à analyser', required: true, }, { displayName: 'Block Hash or Height', name: 'blockIdentifier', type: 'string', displayOptions: { show: { operation: ['getBlockInfo'], }, }, default: '', placeholder: '000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f ou 800000', description: 'Hash du bloc ou hauteur du bloc à récupérer', required: true, }, { displayName: 'Raw Transaction (Hex)', name: 'rawTransaction', type: 'string', displayOptions: { show: { operation: ['broadcastTransaction'], }, }, default: '', description: 'Transaction brute en format hexadécimal', required: true, }, { displayName: 'Limit', name: 'limit', type: 'number', displayOptions: { show: { operation: ['getAddressTransactions'], }, }, default: 25, description: 'Nombre maximum de transactions à retourner', typeOptions: { minValue: 1, maxValue: 50, }, }, ], }; } async execute() { const items = this.getInputData(); const returnData = []; for (let i = 0; i < items.length; i++) { try { const operation = this.getNodeParameter('operation', i); const baseUrl = 'https://mempool.space/api/v1/'; let responseData; switch (operation) { case 'getMempoolLive': const mempoolResponse = await axios_1.default.get(`${baseUrl}mempool`); responseData = mempoolResponse.data; break; case 'getRecommendedFees': const feesResponse = await axios_1.default.get(`${baseUrl}fees/recommended`); responseData = feesResponse.data; break; case 'getDifficultyAdjustment': const difficultyResponse = await axios_1.default.get(`${baseUrl}difficulty-adjustment`); responseData = difficultyResponse.data; break; case 'getBitcoinPrice': const priceResponse = await axios_1.default.get(`${baseUrl}prices`); responseData = priceResponse.data; break; case 'getHistoricalPrice': const currency = this.getNodeParameter('currency', i); const timestamp = this.getNodeParameter('timestamp', i); let historicalUrl = `${baseUrl}historical-price`; const params = []; if (currency) params.push(`currency=${currency}`); if (timestamp) params.push(`timestamp=${timestamp}`); if (params.length > 0) historicalUrl += `?${params.join('&')}`; const historicalResponse = await axios_1.default.get(historicalUrl); responseData = historicalResponse.data; break; case 'getTransaction': const txid = this.getNodeParameter('txid', i); const txResponse = await axios_1.default.get(`${baseUrl}tx/${txid}`); responseData = txResponse.data; break; case 'getTransactionStatus': const statusTxid = this.getNodeParameter('txid', i); const statusResponse = await axios_1.default.get(`${baseUrl}tx/${statusTxid}/status`); responseData = statusResponse.data; break; case 'getAddressInfo': const address = this.getNodeParameter('address', i); const addressResponse = await axios_1.default.get(`${baseUrl}address/${address}`); responseData = addressResponse.data; break; case 'getAddressTransactions': const txAddress = this.getNodeParameter('address', i); const limit = this.getNodeParameter('limit', i); const txsResponse = await axios_1.default.get(`${baseUrl}address/${txAddress}/txs`); responseData = limit ? txsResponse.data.slice(0, limit) : txsResponse.data; break; case 'getAddressUtxo': const utxoAddress = this.getNodeParameter('address', i); const utxoResponse = await axios_1.default.get(`${baseUrl}address/${utxoAddress}/utxo`); responseData = utxoResponse.data; break; case 'getBlockInfo': const blockIdentifier = this.getNodeParameter('blockIdentifier', i); const blockResponse = await axios_1.default.get(`${baseUrl}block/${blockIdentifier}`); responseData = blockResponse.data; break; case 'getLatestBlocks': const blocksResponse = await axios_1.default.get(`${baseUrl}blocks`); responseData = blocksResponse.data; break; case 'broadcastTransaction': const rawTx = this.getNodeParameter('rawTransaction', i); const broadcastResponse = await axios_1.default.post(`${baseUrl}tx`, rawTx, { headers: { 'Content-Type': 'text/plain', }, }); responseData = { txid: broadcastResponse.data }; break; default: throw new n8n_workflow_1.NodeOperationError(this.getNode(), `Opération non supportée: ${operation}`, { itemIndex: i }); } returnData.push({ json: responseData, pairedItem: { item: i }, }); } catch (error) { if (this.continueOnFail()) { returnData.push({ json: { error: error.message }, pairedItem: { item: i }, }); continue; } throw new n8n_workflow_1.NodeOperationError(this.getNode(), error, { itemIndex: i }); } } return [returnData]; } } exports.MempoolApi = MempoolApi;