import { BadRequestException , Body , Get , HttpCode , HttpStatus , Param , Post , Query , } from '@nestjs/common' ; import { XdcService } from './XdcService' ; import { QueryMnemonic } from './dto/QueryMnemonic' ; import { GeneratePrivateKey } from './dto/GeneratePrivateKey' ; import { PathXpubI } from './dto/PathXpubI' ; import { BroadcastTx , EstimateGasEth , SmartContractMethodInvocation , SmartContractReadMethodInvocation , TransferErc20 , } from '@tatumio/tatum' ; import { PathAddress } from './dto/PathAddress' ; import { PathHash } from './dto/PathHash' ; import { XdcError } from './XdcError' ; export abstract class XdcController { protected constructor ( protected readonly service ) { } @ Post ( '/web3/:xApiKey' ) @ HttpCode ( HttpStatus . OK ) public async web3Driver ( @ Body ( ) body ) { try { return await this . service . web3Method ( body ) ; } catch ( e ) { if ( [ 'Array' , 'ValidationError' ] . includes ( e . constructor . name ) ) { throw new BadRequestException ( e ) ; } if ( e . constructor . name === 'TatumError' || e . constructor . name === XdcError . name ) { throw e ; } throw new XdcError ( `Unexpected error occurred. Reason: ${ e . message ?. message || e . response ?. data || e . message || e }` , 'xdc.error' ) ; } } @ Get ( '/wallet' ) @ HttpCode ( HttpStatus . OK ) async generateWallet ( @ Query ( ) { mnemonic } ) { try { return await this . service . generateWallet ( mnemonic ) ; } catch ( e ) { if ( [ 'Array' , 'ValidationError' ] . includes ( e . constructor . name ) ) { throw new BadRequestException ( e ) ; } if ( e . constructor . name === 'TatumError' || e . constructor . name === XdcError . name ) { throw e ; } throw new XdcError ( `Unexpected error occurred. Reason: ${ e . message ?. message || e . response ?. data || e . message || e }` , 'xdc.error' ) ; } } @ Post ( '/wallet/priv' ) @ HttpCode ( HttpStatus . OK ) async generatePrivateKey ( @ Body ( ) { mnemonic , index } ) { try { return await this . service . generatePrivateKey ( mnemonic , index ) ; } catch ( e ) { if ( [ 'Array' , 'ValidationError' ] . includes ( e . constructor . name ) ) { throw new BadRequestException ( e ) ; } if ( e . constructor . name === 'TatumError' || e . constructor . name === XdcError . name ) { throw e ; } throw new XdcError ( `Unexpected error occurred. Reason: ${ e . message ?. message || e . response ?. data || e . message || e }` , 'xdc.error' ) ; } } @ Post ( '/transaction' ) @ HttpCode ( HttpStatus . OK ) public async sendXdcOrErc20Transaction ( @ Body ( ) body ) { try { return await this . service . sendXdcOrErc20Transaction ( body ) ; } catch ( e ) { if ( [ 'Array' , 'ValidationError' ] . includes ( e . constructor . name ) ) { throw new BadRequestException ( e ) ; } if ( e . constructor . name === 'TatumError' || e . constructor . name === XdcError . name ) { throw e ; } throw new XdcError ( `Unexpected error occurred. Reason: ${ e . message ?. message || e . response ?. data || e . message || e }` , 'xdc.error' ) ; } } @ Post ( '/gas' ) @ HttpCode ( HttpStatus . OK ) public async estimateGas ( @ Body ( ) body ) { try { return await this . service . estimateGas ( body ) ; } catch ( e ) { if ( [ 'Array' , 'ValidationError' ] . includes ( e . constructor . name ) ) { throw new BadRequestException ( e ) ; } if ( e . constructor . name === 'TatumError' || e . constructor . name === XdcError . name ) { throw e ; } throw new XdcError ( `Unexpected error occurred. Reason: ${ e . message ?. message || e . response ?. data || e . message || e }` , 'xdc.error' ) ; } } @ Get ( '/transaction/count/:address' ) @ HttpCode ( HttpStatus . OK ) public async countTransactions ( @ Param ( ) param ) { try { return await this . service . getTransactionCount ( param . address ) ; } catch ( e ) { if ( [ 'Array' , 'ValidationError' ] . includes ( e . constructor . name ) ) { throw new BadRequestException ( e ) ; } if ( e . constructor . name === 'TatumError' || e . constructor . name === XdcError . name ) { throw e ; } throw new XdcError ( `Unexpected error occurred. Reason: ${ e . message ?. message || e . response ?. data || e . message || e }` , 'xdc.error' ) ; } } @ Post ( '/smartcontract' ) @ HttpCode ( HttpStatus . OK ) public async invokeSmartContractMethod ( @ Body ( ) body ) { try { return await this . service . invokeSmartContractMethod ( body ) ; } catch ( e ) { if ( [ 'Array' , 'ValidationError' ] . includes ( e . constructor . name ) ) { throw new BadRequestException ( e ) ; } if ( e . constructor . name === 'TatumError' || e . constructor . name === XdcError . name ) { throw e ; } throw new XdcError ( `Unexpected error occurred. Reason: ${ e . message ?. message || e . response ?. data || e . message || e }` , 'xdc.error' ) ; } } @ Post ( '/broadcast' ) @ HttpCode ( HttpStatus . OK ) public async broadcast ( @ Body ( ) body ) { try { return await this . service . broadcast ( body . txData , body . signatureId ) ; } catch ( e ) { if ( [ 'Array' , 'ValidationError' ] . includes ( e . constructor . name ) ) { throw new BadRequestException ( e ) ; } if ( e . constructor . name === 'TatumError' || e . constructor . name === XdcError . name ) { throw e ; } throw new XdcError ( `Unexpected error occurred. Reason: ${ e . message ?. message || e . response ?. data || e . message || e }` , 'xdc.error' ) ; } } @ Get ( '/block/current' ) @ HttpCode ( HttpStatus . OK ) public async getCurrentBlock ( ) { try { return await this . service . getCurrentBlock ( ) ; } catch ( e ) { if ( [ 'Array' , 'ValidationError' ] . includes ( e . constructor . name ) ) { throw new BadRequestException ( e ) ; } if ( e . constructor . name === 'TatumError' || e . constructor . name === XdcError . name ) { throw e ; } throw new XdcError ( `Unexpected error occurred. Reason: ${ e . message ?. message || e . response ?. data || e . message || e }` , 'xdc.error' ) ; } } @ Get ( '/block/:hash' ) @ HttpCode ( HttpStatus . OK ) public async getBlock ( @ Param ( ) path ) { try { return await this . service . getBlock ( path . hash ) ; } catch ( e ) { if ( [ 'Array' , 'ValidationError' ] . includes ( e . constructor . name ) ) { throw new BadRequestException ( e ) ; } if ( e . constructor . name === 'TatumError' || e . constructor . name === XdcError . name ) { throw e ; } throw new XdcError ( `Unexpected error occurred. Reason: ${ e . message ?. message || e . response ?. data || e . message || e }` , 'xdc.error' ) ; } } @ Get ( '/account/balance/:address' ) @ HttpCode ( HttpStatus . OK ) public async getAccountBalance ( @ Param ( ) path ) { try { return await this . service . getBalance ( path . address ) ; } catch ( e ) { if ( [ 'Array' , 'ValidationError' ] . includes ( e . constructor . name ) ) { throw new BadRequestException ( e ) ; } if ( e . constructor . name === 'TatumError' || e . constructor . name === XdcError . name ) { throw e ; } throw new XdcError ( `Unexpected error occurred. Reason: ${ e . message ?. message || e . response ?. data || e . message || e }` , 'xdc.error' ) ; } } @ Get ( '/address/:xpub/:i' ) @ HttpCode ( HttpStatus . OK ) public async generateAddress ( @ Param ( ) { xpub , i } ) { try { return await this . service . generateAddress ( xpub , i ) ; } catch ( e ) { if ( [ 'Array' , 'ValidationError' ] . includes ( e . constructor . name ) ) { throw new BadRequestException ( e ) ; } if ( e . constructor . name === 'TatumError' || e . constructor . name === XdcError . name ) { throw e ; } throw new XdcError ( `Unexpected error occurred. Reason: ${ e . message ?. message || e . response ?. data || e . message || e }` , 'xdc.error' ) ; } } @ Get ( '/transaction/:hash' ) public async getTransaction ( @ Param ( ) path ) { try { return await this . service . getTransaction ( path . hash ) ; } catch ( e ) { if ( [ 'Array' , 'ValidationError' ] . includes ( e . constructor . name ) ) { throw new BadRequestException ( e ) ; } if ( e . constructor . name === 'TatumError' || e . constructor . name === XdcError . name ) { throw e ; } throw new XdcError ( `Unexpected error occurred. Reason: ${ e . message ?. message || e . response ?. data || e . message || e }` , 'xdc.error' ) ; } } }