import { Block , PaymentAddress , Transaction } from '@cardano-graphql/client-ts' ; import { BadRequestException , Body , Get , Param , Post , Query } from '@nestjs/common' ; import { AdaUtxo , TransferBtcBasedBlockchain } from '@tatumio/tatum' ; import { AdaError } from './AdaError' ; import { AdaService } from './AdaService' ; import { AdaBlockchainInfo } from './constants' ; import { BlockchainError , BtcBasedBlockchainControllerInterface , GeneratePrivateKey , Pagination , PathAddress , PathHash , PathXpub , QueryMnemonic , TransactionKMSResponse , TransactionResponse , TxData , } from '@tatumio/blockchain-connector-common' ; export abstract class AdaController implements BtcBasedBlockchainControllerInterface { protected constructor ( protected readonly service ) { } @ Get ( '/info' ) async getInfo ( ) { try { return await this . service . getBlockChainInfo ( ) ; } catch ( e ) { this . throwError ( e ) ; } } @ Get ( '/wallet' ) async generateWallet ( @ Query ( ) { mnemonic } ) { try { return await this . service . generateWallet ( mnemonic ) ; } catch ( e ) { this . throwError ( e ) ; } } @ Get ( '/address/:xpub/:i' ) async generateAddress ( @ Param ( ) { xpub , i } ) { try { return await this . service . generateAddress ( xpub , Number ( i ) ) ; } catch ( e ) { this . throwError ( e ) ; } } @ Post ( '/wallet/priv' ) async generatePrivateKey ( @ Body ( ) body ) { try { return await this . service . generatePrivateKey ( body . mnemonic , body . index ) ; } catch ( e ) { this . throwError ( e ) ; } } @ Get ( '/block/:hash' ) async getBlock ( @ Param ( ) path ) { try { return await this . service . getBlock ( path . hash ) ; } catch ( e ) { this . throwError ( e ) ; } } @ Get ( '/transaction/:hash' ) async getTransaction ( @ Param ( ) path ) { try { return await this . service . getTransaction ( path . hash ) ; } catch ( e ) { this . throwError ( e ) ; } } @ Get ( '/account/:address' ) async getAccount ( @ Param ( ) path ) { try { return await this . service . getAccount ( path . address ) ; } catch ( e ) { this . throwError ( e ) ; } } @ Get ( '/transaction/address/:address' ) async getTransactionsByAccount ( @ Param ( ) path , @ Query ( ) query ) { try { return await this . service . getTransactionsByAccount ( path . address , query . pageSize , query . offset ) ; } catch ( e ) { this . throwError ( e ) ; } } @ Post ( '/broadcast' ) async broadcast ( @ Body ( ) txData ) { try { return await this . service . broadcast ( txData ) ; } catch ( e ) { this . throwError ( e ) ; } } @ Get ( '/:address/utxos' ) async getUTxosByAddress ( @ Param ( ) path ) { try { return await this . service . getUtxosByAddress ( path . address ) ; } catch ( e ) { this . throwError ( e ) ; } } @ Post ( '/transaction' ) async sendTransaction ( @ Body ( ) body ) { try { return await this . service . sendTransaction ( body ) ; } catch ( e ) { this . throwError ( e ) ; } } throwError ( e ) { if ( [ 'Array' , 'ValidationError' ] . includes ( e . constructor . name ) ) { throw new BadRequestException ( e ) ; } if ( e . constructor . name === 'TatumError' ) { throw e ; } throw new AdaError ( `Unexpected error occurred. Reason: ${ e . message ?. message || e . response ?. data || e . message || e }` , 'ada.error' ) ; } }