Changeset View
Changeset View
Standalone View
Standalone View
apps/ecash-herald/src/events.js
// Copyright (c) 2023 The Bitcoin developers | // Copyright (c) 2023 The Bitcoin developers | ||||
// Distributed under the MIT software license, see the accompanying | // Distributed under the MIT software license, see the accompanying | ||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | // file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
'use strict'; | 'use strict'; | ||||
const config = require('../config'); | const config = require('../config'); | ||||
const { parseBlock, getBlockTgMessage } = require('./parse'); | const { parseBlock, getBlockTgMessage } = require('./parse'); | ||||
const { getCoingeckoPrices } = require('./utils'); | const { getCoingeckoPrices } = require('./utils'); | ||||
const { sendBlockSummary } = require('./telegram'); | const { sendBlockSummary } = require('./telegram'); | ||||
const { getTokenInfoMap } = require('./chronik'); | |||||
module.exports = { | module.exports = { | ||||
handleBlockConnected: async function ( | handleBlockConnected: async function ( | ||||
chronik, | chronik, | ||||
telegramBot, | telegramBot, | ||||
channelId, | channelId, | ||||
blockHash, | blockHash, | ||||
returnMocks = false, | returnMocks = false, | ||||
Show All 36 Lines | ) { | ||||
err, | err, | ||||
); | ); | ||||
return false; | return false; | ||||
} | } | ||||
} | } | ||||
const parsedBlock = parseBlock(blockDetails); | const parsedBlock = parseBlock(blockDetails); | ||||
// Get token genesis info for token IDs in this block | |||||
const { tokenIds } = parsedBlock; | |||||
const tokenInfoMap = await getTokenInfoMap(chronik, tokenIds); | |||||
// Get price info for tg msg, if available | // Get price info for tg msg, if available | ||||
const { coingeckoResponse, coingeckoPrices } = await getCoingeckoPrices( | const { coingeckoResponse, coingeckoPrices } = await getCoingeckoPrices( | ||||
config.priceApi, | config.priceApi, | ||||
); | ); | ||||
const blockSummaryTgMsgs = getBlockTgMessage( | const blockSummaryTgMsgs = getBlockTgMessage( | ||||
parsedBlock, | parsedBlock, | ||||
coingeckoPrices, | coingeckoPrices, | ||||
tokenInfoMap, | |||||
); | ); | ||||
// returnMocks is used in the script function generateMocks | // returnMocks is used in the script function generateMocks | ||||
// Using it as a flag here ensures the script is always using the same function | // Using it as a flag here ensures the script is always using the same function | ||||
// as the app | // as the app | ||||
if (returnMocks) { | if (returnMocks) { | ||||
// Note you need coingeckoResponse so you can mock the axios response for coingecko | // Note you need coingeckoResponse so you can mock the axios response for coingecko | ||||
return { | return { | ||||
blockDetails, | blockDetails, | ||||
parsedBlock, | parsedBlock, | ||||
coingeckoResponse, | coingeckoResponse, | ||||
coingeckoPrices, | coingeckoPrices, | ||||
tokenInfoMap, | |||||
blockSummaryTgMsgs, | blockSummaryTgMsgs, | ||||
blockSummaryTgMsgsPriceFailure: getBlockTgMessage( | blockSummaryTgMsgsApiFailure: getBlockTgMessage( | ||||
parsedBlock, | parsedBlock, | ||||
false, | false, // failed coingecko price lookup | ||||
false, // failed chronik token ID lookup | |||||
), | ), | ||||
}; | }; | ||||
} | } | ||||
// Broadcast block summary telegram message(s) | // Broadcast block summary telegram message(s) | ||||
return await sendBlockSummary( | return await sendBlockSummary( | ||||
blockSummaryTgMsgs, | blockSummaryTgMsgs, | ||||
telegramBot, | telegramBot, | ||||
channelId, | channelId, | ||||
); | ); | ||||
}, | }, | ||||
}; | }; |