UNPKG

@tatumio/tron-connector

Version:

Tron Connector for Tatum API

411 lines (410 loc) 19.3 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; var __param = (this && this.__param) || function (paramIndex, decorator) { return function (target, key) { decorator(target, key, paramIndex); } }; Object.defineProperty(exports, "__esModule", { value: true }); exports.TronController = void 0; const common_1 = require("@nestjs/common"); const tatum_1 = require("@tatumio/tatum"); const PathAddress_1 = require("./dto/PathAddress"); const PathTxId_1 = require("./dto/PathTxId"); const TronError_1 = require("./TronError"); const PathTokenId_1 = require("./dto/PathTokenId"); const QueryMnemonic_1 = require("./dto/QueryMnemonic"); const GeneratePrivateKey_1 = require("./dto/GeneratePrivateKey"); const PathXpubI_1 = require("./dto/PathXpubI"); class TronController { constructor(service) { this.service = service; } async broadcast(body) { var _a, _b; try { return await this.service.broadcast(body.txData, body.signatureId); } catch (e) { if (['Array', 'ValidationError'].includes(e.constructor.name)) { throw new common_1.BadRequestException(e); } if (e.constructor.name === 'TatumError' || e.constructor.name === TronError_1.TronError.name) { throw e; } throw new TronError_1.TronError(`Unexpected error occurred. Reason: ${((_a = e.message) === null || _a === void 0 ? void 0 : _a.message) || ((_b = e.response) === null || _b === void 0 ? void 0 : _b.data) || e.message || e}`, 'tron.error'); } } async generateWallet(query) { var _a, _b; try { return await this.service.generateWallet(query.mnemonic); } catch (e) { if (['Array', 'ValidationError'].includes(e.constructor.name)) { throw new common_1.BadRequestException(e); } if (e.constructor.name === 'TatumError' || e.constructor.name === TronError_1.TronError.name) { throw e; } throw new TronError_1.TronError(`Unexpected error occurred. Reason: ${((_a = e.message) === null || _a === void 0 ? void 0 : _a.message) || ((_b = e.response) === null || _b === void 0 ? void 0 : _b.data) || e.message || e}`, 'tron.error'); } } async generatePrivKey(body) { var _a, _b; try { return await this.service.generatePrivateKey(body.mnemonic, body.index); } catch (e) { if (['Array', 'ValidationError'].includes(e.constructor.name)) { throw new common_1.BadRequestException(e); } if (e.constructor.name === 'TatumError' || e.constructor.name === TronError_1.TronError.name) { throw e; } throw new TronError_1.TronError(`Unexpected error occurred. Reason: ${((_a = e.message) === null || _a === void 0 ? void 0 : _a.message) || ((_b = e.response) === null || _b === void 0 ? void 0 : _b.data) || e.message || e}`, 'tron.error'); } } async generateAccount(params) { var _a, _b; try { return await this.service.generateAddress(params.xpub, params.i); } catch (e) { if (['Array', 'ValidationError'].includes(e.constructor.name)) { throw new common_1.BadRequestException(e); } if (e.constructor.name === 'TatumError' || e.constructor.name === TronError_1.TronError.name) { throw e; } throw new TronError_1.TronError(`Unexpected error occurred. Reason: ${((_a = e.message) === null || _a === void 0 ? void 0 : _a.message) || ((_b = e.response) === null || _b === void 0 ? void 0 : _b.data) || e.message || e}`, 'tron.error'); } } async getInfo() { var _a, _b; try { return await this.service.getBlockChainInfo(); } catch (e) { if (['Array', 'ValidationError'].includes(e.constructor.name)) { throw new common_1.BadRequestException(e); } if (e.constructor.name === 'TatumError' || e.constructor.name === TronError_1.TronError.name) { throw e; } throw new TronError_1.TronError(`Unexpected error occurred. Reason: ${((_a = e.message) === null || _a === void 0 ? void 0 : _a.message) || ((_b = e.response) === null || _b === void 0 ? void 0 : _b.data) || e.message || e}`, 'tron.error'); } } async getBlock(hashOrHeight) { var _a, _b; try { return await this.service.getBlock(hashOrHeight); } catch (e) { if (['Array', 'ValidationError'].includes(e.constructor.name)) { throw new common_1.BadRequestException(e); } if (e.constructor.name === 'TatumError' || e.constructor.name === TronError_1.TronError.name) { throw e; } throw new TronError_1.TronError(`Unexpected error occurred. Reason: ${((_a = e.message) === null || _a === void 0 ? void 0 : _a.message) || ((_b = e.response) === null || _b === void 0 ? void 0 : _b.data) || e.message || e}`, 'tron.error'); } } async getAccount(path) { var _a; try { return await this.service.getAccount(path.address); } catch (e) { if (['Array', 'ValidationError'].includes(e.constructor.name)) { throw new common_1.BadRequestException(e); } if (e.constructor.name === 'TatumError' || e.constructor.name === TronError_1.TronError.name) { throw e; } throw new TronError_1.TronError(`Unexpected error occurred. Reason: ${e.message}` || ((_a = e.response) === null || _a === void 0 ? void 0 : _a.data), 'tron.error'); } } async getTransaction(path) { var _a, _b; try { return await this.service.getTransaction(path.txId); } catch (e) { if (['Array', 'ValidationError'].includes(e.constructor.name)) { throw new common_1.BadRequestException(e); } if (e.constructor.name === 'TatumError' || e.constructor.name === TronError_1.TronError.name) { throw e; } throw new TronError_1.TronError(`Unexpected error occurred. Reason: ${((_a = e.message) === null || _a === void 0 ? void 0 : _a.message) || ((_b = e.response) === null || _b === void 0 ? void 0 : _b.data) || e.message || e}`, 'tron.error'); } } async getTransactionsByAccount(path, next) { var _a, _b; try { return await this.service.getTransactionsByAccount(path.address, next); } catch (e) { if (['Array', 'ValidationError'].includes(e.constructor.name)) { throw new common_1.BadRequestException(e); } if (e.constructor.name === 'TatumError' || e.constructor.name === TronError_1.TronError.name) { throw e; } throw new TronError_1.TronError(`Unexpected error occurred. Reason: ${((_a = e.message) === null || _a === void 0 ? void 0 : _a.message) || ((_b = e.response) === null || _b === void 0 ? void 0 : _b.data) || e.message || e}`, 'tron.error'); } } async getTransactions20ByAccount(path, next) { var _a, _b; try { return await this.service.getTrc20TransactionsByAccount(path.address, next); } catch (e) { if (['Array', 'ValidationError'].includes(e.constructor.name)) { throw new common_1.BadRequestException(e); } if (e.constructor.name === 'TatumError' || e.constructor.name === TronError_1.TronError.name) { throw e; } throw new TronError_1.TronError(`Unexpected error occurred. Reason: ${((_a = e.message) === null || _a === void 0 ? void 0 : _a.message) || ((_b = e.response) === null || _b === void 0 ? void 0 : _b.data) || e.message || e}`, 'tron.error'); } } async sendTransaction(body) { var _a, _b; try { return await this.service.sendTransaction(body); } catch (e) { if (['Array', 'ValidationError'].includes(e.constructor.name)) { throw new common_1.BadRequestException(e); } if (e.constructor.name === 'TatumError' || e.constructor.name === TronError_1.TronError.name) { throw e; } throw new TronError_1.TronError(`Unexpected error occurred. Reason: ${((_a = e.message) === null || _a === void 0 ? void 0 : _a.message) || ((_b = e.response) === null || _b === void 0 ? void 0 : _b.data) || e.message || e}`, 'tron.error'); } } async freezeBalance(body) { var _a, _b; try { return await this.service.freezeBalance(body); } catch (e) { if (['Array', 'ValidationError'].includes(e.constructor.name)) { throw new common_1.BadRequestException(e); } if (e.constructor.name === 'TatumError' || e.constructor.name === TronError_1.TronError.name) { throw e; } throw new TronError_1.TronError(`Unexpected error occurred. Reason: ${((_a = e.message) === null || _a === void 0 ? void 0 : _a.message) || ((_b = e.response) === null || _b === void 0 ? void 0 : _b.data) || e.message || e}`, 'tron.error'); } } async getTrc10Detail(path) { var _a, _b; try { return await this.service.getTrc10Detail(path.id); } catch (e) { if (['Array', 'ValidationError'].includes(e.constructor.name)) { throw new common_1.BadRequestException(e); } if (e.constructor.name === 'TatumError' || e.constructor.name === TronError_1.TronError.name) { throw e; } throw new TronError_1.TronError(`Unexpected error occurred. Reason: ${((_a = e.message) === null || _a === void 0 ? void 0 : _a.message) || ((_b = e.response) === null || _b === void 0 ? void 0 : _b.data) || e.message || e}`, 'tron.error'); } } async sendTrc10Transaction(body) { var _a, _b; try { return await this.service.sendTrc10Transaction(body); } catch (e) { if (['Array', 'ValidationError'].includes(e.constructor.name)) { throw new common_1.BadRequestException(e); } if (e.constructor.name === 'TatumError' || e.constructor.name === TronError_1.TronError.name) { throw e; } throw new TronError_1.TronError(`Unexpected error occurred. Reason: ${((_a = e.message) === null || _a === void 0 ? void 0 : _a.message) || ((_b = e.response) === null || _b === void 0 ? void 0 : _b.data) || e.message || e}`, 'tron.error'); } } async createTrc10(body) { var _a, _b; try { return await this.service.createTrc10Transaction(body); } catch (e) { if (['Array', 'ValidationError'].includes(e.constructor.name)) { throw new common_1.BadRequestException(e); } if (e.constructor.name === 'TatumError' || e.constructor.name === TronError_1.TronError.name) { throw e; } throw new TronError_1.TronError(`Unexpected error occurred. Reason: ${((_a = e.message) === null || _a === void 0 ? void 0 : _a.message) || ((_b = e.response) === null || _b === void 0 ? void 0 : _b.data) || e.message || e}`, 'tron.error'); } } async createTrc20(body) { var _a, _b; try { return await this.service.createTrc20Transaction(body); } catch (e) { if (['Array', 'ValidationError'].includes(e.constructor.name)) { throw new common_1.BadRequestException(e); } if (e.constructor.name === 'TatumError' || e.constructor.name === TronError_1.TronError.name) { throw e; } throw new TronError_1.TronError(`Unexpected error occurred. Reason: ${((_a = e.message) === null || _a === void 0 ? void 0 : _a.message) || ((_b = e.response) === null || _b === void 0 ? void 0 : _b.data) || e.message || e}`, 'tron.error'); } } async sendTrc20Transaction(body) { var _a, _b; try { return await this.service.sendTrc20Transaction(body); } catch (e) { if (['Array', 'ValidationError'].includes(e.constructor.name)) { throw new common_1.BadRequestException(e); } if (e.constructor.name === 'TatumError' || e.constructor.name === TronError_1.TronError.name) { throw e; } throw new TronError_1.TronError(`Unexpected error occurred. Reason: ${((_a = e.message) === null || _a === void 0 ? void 0 : _a.message) || ((_b = e.response) === null || _b === void 0 ? void 0 : _b.data) || e.message || e}`, 'tron.error'); } } } __decorate([ common_1.Post('/broadcast'), common_1.HttpCode(common_1.HttpStatus.OK), __param(0, common_1.Body()), __metadata("design:type", Function), __metadata("design:paramtypes", [tatum_1.BroadcastTx]), __metadata("design:returntype", Promise) ], TronController.prototype, "broadcast", null); __decorate([ common_1.Get('/wallet'), __param(0, common_1.Query()), __metadata("design:type", Function), __metadata("design:paramtypes", [QueryMnemonic_1.QueryMnemonic]), __metadata("design:returntype", Promise) ], TronController.prototype, "generateWallet", null); __decorate([ common_1.Post('/wallet/priv'), __param(0, common_1.Body()), __metadata("design:type", Function), __metadata("design:paramtypes", [GeneratePrivateKey_1.GeneratePrivateKey]), __metadata("design:returntype", Promise) ], TronController.prototype, "generatePrivKey", null); __decorate([ common_1.Get('/address/:xpub/:i'), __param(0, common_1.Param()), __metadata("design:type", Function), __metadata("design:paramtypes", [PathXpubI_1.PathXpubI]), __metadata("design:returntype", Promise) ], TronController.prototype, "generateAccount", null); __decorate([ common_1.Get('/info'), __metadata("design:type", Function), __metadata("design:paramtypes", []), __metadata("design:returntype", Promise) ], TronController.prototype, "getInfo", null); __decorate([ common_1.Get('/block/:hashOrHeight'), __param(0, common_1.Param('hashOrHeight')), __metadata("design:type", Function), __metadata("design:paramtypes", [String]), __metadata("design:returntype", Promise) ], TronController.prototype, "getBlock", null); __decorate([ common_1.Get('/account/:address'), __param(0, common_1.Param()), __metadata("design:type", Function), __metadata("design:paramtypes", [PathAddress_1.PathAddress]), __metadata("design:returntype", Promise) ], TronController.prototype, "getAccount", null); __decorate([ common_1.Get('/transaction/:txId'), __param(0, common_1.Param()), __metadata("design:type", Function), __metadata("design:paramtypes", [PathTxId_1.PathTxId]), __metadata("design:returntype", Promise) ], TronController.prototype, "getTransaction", null); __decorate([ common_1.Get('/transaction/account/:address'), __param(0, common_1.Param()), __param(1, common_1.Query('next')), __metadata("design:type", Function), __metadata("design:paramtypes", [PathAddress_1.PathAddress, String]), __metadata("design:returntype", Promise) ], TronController.prototype, "getTransactionsByAccount", null); __decorate([ common_1.Get('/transaction/account/:address/trc20'), __param(0, common_1.Param()), __param(1, common_1.Query('next')), __metadata("design:type", Function), __metadata("design:paramtypes", [PathAddress_1.PathAddress, String]), __metadata("design:returntype", Promise) ], TronController.prototype, "getTransactions20ByAccount", null); __decorate([ common_1.Post('/transaction'), common_1.HttpCode(common_1.HttpStatus.OK), __param(0, common_1.Body()), __metadata("design:type", Function), __metadata("design:paramtypes", [tatum_1.TransferTron]), __metadata("design:returntype", Promise) ], TronController.prototype, "sendTransaction", null); __decorate([ common_1.Post('/freezeBalance'), common_1.HttpCode(common_1.HttpStatus.OK), __param(0, common_1.Body()), __metadata("design:type", Function), __metadata("design:paramtypes", [tatum_1.FreezeTron]), __metadata("design:returntype", Promise) ], TronController.prototype, "freezeBalance", null); __decorate([ common_1.Get('/trc10/detail/:id'), __param(0, common_1.Param()), __metadata("design:type", Function), __metadata("design:paramtypes", [PathTokenId_1.PathTokenId]), __metadata("design:returntype", Promise) ], TronController.prototype, "getTrc10Detail", null); __decorate([ common_1.Post('/trc10/transaction'), common_1.HttpCode(common_1.HttpStatus.OK), __param(0, common_1.Body()), __metadata("design:type", Function), __metadata("design:paramtypes", [tatum_1.TransferTronTrc10]), __metadata("design:returntype", Promise) ], TronController.prototype, "sendTrc10Transaction", null); __decorate([ common_1.Post('/trc10/deploy'), common_1.HttpCode(common_1.HttpStatus.OK), __param(0, common_1.Body()), __metadata("design:type", Function), __metadata("design:paramtypes", [tatum_1.CreateTronTrc10]), __metadata("design:returntype", Promise) ], TronController.prototype, "createTrc10", null); __decorate([ common_1.Post('/trc20/deploy'), common_1.HttpCode(common_1.HttpStatus.OK), __param(0, common_1.Body()), __metadata("design:type", Function), __metadata("design:paramtypes", [tatum_1.CreateTronTrc20]), __metadata("design:returntype", Promise) ], TronController.prototype, "createTrc20", null); __decorate([ common_1.Post('/trc20/transaction'), common_1.HttpCode(common_1.HttpStatus.OK), __param(0, common_1.Body()), __metadata("design:type", Function), __metadata("design:paramtypes", [tatum_1.TransferTronTrc20]), __metadata("design:returntype", Promise) ], TronController.prototype, "sendTrc20Transaction", null); exports.TronController = TronController;