import { NodeProvider } from '@liquality/node-provider' ; import { SwapParams , Transaction , bitcoin } from '@liquality/types' ; import { payments } from 'bitcoinjs-lib' ; type TransactionMatchesFunction = ( tx : Transaction < bitcoin . Transaction > ) => boolean ; type PaymentVariants = { [ bitcoin . SwapMode . P2WSH ] ? : payments . Payment ; [ bitcoin . SwapMode . P2SH_SEGWIT ] ? : payments . Payment ; [ bitcoin . SwapMode . P2SH ] ? : payments . Payment ; } ; export default class BitcoinEsploraSwapFindProvider extends NodeProvider { constructor ( url ) { super ( { baseURL : url , responseType : 'text' , transformResponse : undefined } ) ; } async findAddressTransaction ( address , currentHeight , predicate ) { const transactions = await this . nodeGet ( `/address/${ address }/txs` ) ; for ( const transaction of transactions ) { const formattedTransaction = await this . getMethod ( 'formatTransaction' ) ( transaction , currentHeight ) ; if ( predicate ( formattedTransaction ) ) { return formattedTransaction ; } } } async findSwapTransaction ( swapParams , blockNumber , predicate ) { const currentHeight = await this . getMethod ( 'getBlockHeight' ) ( ) ; const swapOutput = this . getMethod ( 'getSwapOutput' ) ( swapParams ) ; const paymentVariants = this . getMethod ( 'getSwapPaymentVariants' ) ( swapOutput ) ; for ( const paymentVariant of Object . values ( paymentVariants ) ) { const addressTransaction = this . findAddressTransaction ( paymentVariant . address , currentHeight , predicate ) ; if ( addressTransaction ) return addressTransaction ; } } doesBlockScan ( ) { return false ; } }