@tatumio/tron-connector
Version:
Tron Connector for Tatum API
286 lines (267 loc) • 11.7 kB
text/typescript
import {BadRequestException, Body, Get, HttpCode, HttpStatus, Param, Post, Query} from '@nestjs/common';
import {TronService} from './TronService';
import {
BroadcastTx,
CreateTronTrc10,
CreateTronTrc20,
FreezeTron,
TransferTron,
TransferTronTrc10,
TransferTronTrc20,
} from '@tatumio/tatum';
import {PathAddress} from './dto/PathAddress';
import {PathTxId} from './dto/PathTxId';
import {TronError} from './TronError';
import {PathTokenId} from './dto/PathTokenId';
import {QueryMnemonic} from './dto/QueryMnemonic';
import {GeneratePrivateKey} from './dto/GeneratePrivateKey';
import {PathXpubI} from './dto/PathXpubI';
export abstract class TronController {
protected constructor(protected readonly service: TronService) {
}
('/broadcast')
(HttpStatus.OK)
async broadcast(() body: BroadcastTx) {
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 === TronError.name) {
throw e;
}
throw new TronError(`Unexpected error occurred. Reason: ${e.message?.message || e.response?.data || e.message || e}`, 'tron.error');
}
}
('/wallet')
async generateWallet(() query: QueryMnemonic) {
try {
return await this.service.generateWallet(query.mnemonic);
} catch (e) {
if (['Array', 'ValidationError'].includes(e.constructor.name)) {
throw new BadRequestException(e);
}
if (e.constructor.name === 'TatumError' || e.constructor.name === TronError.name) {
throw e;
}
throw new TronError(`Unexpected error occurred. Reason: ${e.message?.message || e.response?.data || e.message || e}`, 'tron.error');
}
}
('/wallet/priv')
async generatePrivKey(() body: GeneratePrivateKey) {
try {
return await this.service.generatePrivateKey(body.mnemonic, body.index);
} catch (e) {
if (['Array', 'ValidationError'].includes(e.constructor.name)) {
throw new BadRequestException(e);
}
if (e.constructor.name === 'TatumError' || e.constructor.name === TronError.name) {
throw e;
}
throw new TronError(`Unexpected error occurred. Reason: ${e.message?.message || e.response?.data || e.message || e}`, 'tron.error');
}
}
('/address/:xpub/:i')
async generateAccount(() params: PathXpubI) {
try {
return await this.service.generateAddress(params.xpub, params.i);
} catch (e) {
if (['Array', 'ValidationError'].includes(e.constructor.name)) {
throw new BadRequestException(e);
}
if (e.constructor.name === 'TatumError' || e.constructor.name === TronError.name) {
throw e;
}
throw new TronError(`Unexpected error occurred. Reason: ${e.message?.message || e.response?.data || e.message || e}`, 'tron.error');
}
}
('/info')
async getInfo() {
try {
return await this.service.getBlockChainInfo();
} catch (e) {
if (['Array', 'ValidationError'].includes(e.constructor.name)) {
throw new BadRequestException(e);
}
if (e.constructor.name === 'TatumError' || e.constructor.name === TronError.name) {
throw e;
}
throw new TronError(`Unexpected error occurred. Reason: ${e.message?.message || e.response?.data || e.message || e}`, 'tron.error');
}
}
('/block/:hashOrHeight')
async getBlock(('hashOrHeight') hashOrHeight: string) {
try {
return await this.service.getBlock(hashOrHeight);
} catch (e) {
if (['Array', 'ValidationError'].includes(e.constructor.name)) {
throw new BadRequestException(e);
}
if (e.constructor.name === 'TatumError' || e.constructor.name === TronError.name) {
throw e;
}
throw new TronError(`Unexpected error occurred. Reason: ${e.message?.message || e.response?.data || e.message || e}`, 'tron.error');
}
}
('/account/:address')
async getAccount(() path: PathAddress) {
try {
return await this.service.getAccount(path.address);
} catch (e) {
if (['Array', 'ValidationError'].includes(e.constructor.name)) {
throw new BadRequestException(e);
}
if (e.constructor.name === 'TatumError' || e.constructor.name === TronError.name) {
throw e;
}
throw new TronError(`Unexpected error occurred. Reason: ${e.message}` || e.response?.data, 'tron.error');
}
}
('/transaction/:txId')
async getTransaction(() path: PathTxId) {
try {
return await this.service.getTransaction(path.txId);
} catch (e) {
if (['Array', 'ValidationError'].includes(e.constructor.name)) {
throw new BadRequestException(e);
}
if (e.constructor.name === 'TatumError' || e.constructor.name === TronError.name) {
throw e;
}
throw new TronError(`Unexpected error occurred. Reason: ${e.message?.message || e.response?.data || e.message || e}`, 'tron.error');
}
}
('/transaction/account/:address')
async getTransactionsByAccount(() path: PathAddress, ('next') next?: string) {
try {
return await this.service.getTransactionsByAccount(path.address, next);
} catch (e) {
if (['Array', 'ValidationError'].includes(e.constructor.name)) {
throw new BadRequestException(e);
}
if (e.constructor.name === 'TatumError' || e.constructor.name === TronError.name) {
throw e;
}
throw new TronError(`Unexpected error occurred. Reason: ${e.message?.message || e.response?.data || e.message || e}`, 'tron.error');
}
}
('/transaction/account/:address/trc20')
async getTransactions20ByAccount(() path: PathAddress, ('next') next?: string) {
try {
return await this.service.getTrc20TransactionsByAccount(path.address, next);
} catch (e) {
if (['Array', 'ValidationError'].includes(e.constructor.name)) {
throw new BadRequestException(e);
}
if (e.constructor.name === 'TatumError' || e.constructor.name === TronError.name) {
throw e;
}
throw new TronError(`Unexpected error occurred. Reason: ${e.message?.message || e.response?.data || e.message || e}`, 'tron.error');
}
}
('/transaction')
(HttpStatus.OK)
async sendTransaction(() body: TransferTron) {
try {
return await this.service.sendTransaction(body);
} catch (e) {
if (['Array', 'ValidationError'].includes(e.constructor.name)) {
throw new BadRequestException(e);
}
if (e.constructor.name === 'TatumError' || e.constructor.name === TronError.name) {
throw e;
}
throw new TronError(`Unexpected error occurred. Reason: ${e.message?.message || e.response?.data || e.message || e}`, 'tron.error');
}
}
('/freezeBalance')
(HttpStatus.OK)
async freezeBalance(() body: FreezeTron) {
try {
return await this.service.freezeBalance(body);
} catch (e) {
if (['Array', 'ValidationError'].includes(e.constructor.name)) {
throw new BadRequestException(e);
}
if (e.constructor.name === 'TatumError' || e.constructor.name === TronError.name) {
throw e;
}
throw new TronError(`Unexpected error occurred. Reason: ${e.message?.message || e.response?.data || e.message || e}`, 'tron.error');
}
}
('/trc10/detail/:id')
async getTrc10Detail(() path: PathTokenId) {
try {
return await this.service.getTrc10Detail(path.id);
} catch (e) {
if (['Array', 'ValidationError'].includes(e.constructor.name)) {
throw new BadRequestException(e);
}
if (e.constructor.name === 'TatumError' || e.constructor.name === TronError.name) {
throw e;
}
throw new TronError(`Unexpected error occurred. Reason: ${e.message?.message || e.response?.data || e.message || e}`, 'tron.error');
}
}
('/trc10/transaction')
(HttpStatus.OK)
async sendTrc10Transaction(() body: TransferTronTrc10) {
try {
return await this.service.sendTrc10Transaction(body);
} catch (e) {
if (['Array', 'ValidationError'].includes(e.constructor.name)) {
throw new BadRequestException(e);
}
if (e.constructor.name === 'TatumError' || e.constructor.name === TronError.name) {
throw e;
}
throw new TronError(`Unexpected error occurred. Reason: ${e.message?.message || e.response?.data || e.message || e}`, 'tron.error');
}
}
('/trc10/deploy')
(HttpStatus.OK)
async createTrc10(() body: CreateTronTrc10) {
try {
return await this.service.createTrc10Transaction(body);
} catch (e) {
if (['Array', 'ValidationError'].includes(e.constructor.name)) {
throw new BadRequestException(e);
}
if (e.constructor.name === 'TatumError' || e.constructor.name === TronError.name) {
throw e;
}
throw new TronError(`Unexpected error occurred. Reason: ${e.message?.message || e.response?.data || e.message || e}`, 'tron.error');
}
}
('/trc20/deploy')
(HttpStatus.OK)
async createTrc20(() body: CreateTronTrc20) {
try {
return await this.service.createTrc20Transaction(body);
} catch (e) {
if (['Array', 'ValidationError'].includes(e.constructor.name)) {
throw new BadRequestException(e);
}
if (e.constructor.name === 'TatumError' || e.constructor.name === TronError.name) {
throw e;
}
throw new TronError(`Unexpected error occurred. Reason: ${e.message?.message || e.response?.data || e.message || e}`, 'tron.error');
}
}
('/trc20/transaction')
(HttpStatus.OK)
async sendTrc20Transaction(() body: TransferTronTrc20) {
try {
return await this.service.sendTrc20Transaction(body);
} catch (e) {
if (['Array', 'ValidationError'].includes(e.constructor.name)) {
throw new BadRequestException(e);
}
if (e.constructor.name === 'TatumError' || e.constructor.name === TronError.name) {
throw e;
}
throw new TronError(`Unexpected error occurred. Reason: ${e.message?.message || e.response?.data || e.message || e}`, 'tron.error');
}
}
}