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); | |||||
// Build a token info reference object to add token info to telegram msg | |||||
// 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( | ||||
bytesofman: I don't think it's worth saving mocks of "coingecko fails but chronik doesn't" and "chronik… | |||||
parsedBlock, | parsedBlock, | ||||
false, | false, | ||||
false, | |||||
), | ), | ||||
}; | }; | ||||
} | } | ||||
// Broadcast block summary telegram message(s) | // Broadcast block summary telegram message(s) | ||||
return await sendBlockSummary( | return await sendBlockSummary( | ||||
blockSummaryTgMsgs, | blockSummaryTgMsgs, | ||||
telegramBot, | telegramBot, | ||||
channelId, | channelId, | ||||
); | ); | ||||
}, | }, | ||||
}; | }; |
I don't think it's worth saving mocks of "coingecko fails but chronik doesn't" and "chronik fails but coingecko doesn't"
If we test "everything works --> good" and "nothing works --> stuff we expect to not be there is not there" ... imo good enough for a telegram bot
opportunity to refactor and make this type of unit testing more straightforward by incrementally building the telegram msg, e.g. "getBlockSummaryPriceLines", "getBlockSummaryTokenSendLines" ... but imo that makes everything way more complicated, esp when you need to deal with splitting overflow msgs