@tatumio/algo-connector
Version:
ALGO Connector for Tatum API
214 lines (201 loc) • 8.21 kB
text/typescript
import { Get, Post, HttpCode, HttpStatus, Req, Query, Param, Body, BadRequestException } from '@nestjs/common';
import { AlgoService } from './AlgoService';
import { QueryMnemonic, PathAddress, AlgoNodeType } from '@tatumio/blockchain-connector-common';
import { AlgoError } from './AlgoError';
import { GeneratePrivateKey } from './dto/GeneratePrivateKey';
import { PathRountNumber } from './dto/PathRoundNumber';
import { PathTransactionId } from './dto/PathTransactionId';
import { AlgoTransaction, BroadcastTx } from '@tatumio/tatum';
import { PathFromTo } from './PathFromTo';
import { Pagination } from './Pagination';
import {Request} from 'express';
export abstract class AlgoController {
protected constructor(protected readonly service: AlgoService) { }
('/node/indexer/:key/*')
(HttpStatus.OK)
public async nodeGetIndexer(() req: Request, () param: { key: string }) {
try {
return await this.service.nodeMethod(req, param.key, AlgoNodeType.INDEXER);
} catch (e) {
if (['Array', 'ValidationError'].includes(e.constructor.name)) {
throw new BadRequestException(e);
}
if (e.constructor.name === 'TatumError' || e.constructor.name === AlgoError.name) {
throw e;
}
throw new AlgoError(`Unexpected error occurred. Reason: ${e.message?.message || e.response?.data || e.message || e}`, 'Algo.error');
}
}
('/node/indexer/:key/*')
(HttpStatus.OK)
public async nodePostIndexer(() req: Request, () param: { key: string }) {
try {
return await this.service.nodeMethod(req, param.key, AlgoNodeType.INDEXER);
} catch (e) {
if (['Array', 'ValidationError'].includes(e.constructor.name)) {
throw new BadRequestException(e);
}
if (e.constructor.name === 'TatumError' || e.constructor.name === AlgoError.name) {
throw e;
}
throw new AlgoError(`Unexpected error occurred. Reason: ${e.message?.message || e.response?.data || e.message || e}`, 'Algo.error');
}
}
('/node/algod/:key/*')
(HttpStatus.OK)
public async nodeGetAlgod(() req: Request, () param: { key: string }) {
try {
return await this.service.nodeMethod(req, param.key, AlgoNodeType.ALGOD);
} catch (e) {
if (['Array', 'ValidationError'].includes(e.constructor.name)) {
throw new BadRequestException(e);
}
if (e.constructor.name === 'TatumError' || e.constructor.name === AlgoError.name) {
throw e;
}
throw new AlgoError(`Unexpected error occurred. Reason: ${e.message?.message || e.response?.data || e.message || e}`, 'Algo.error');
}
}
('/node/algod/:key/*')
(HttpStatus.OK)
public async nodePostAlgod(() req: Request, () param: { key: string }) {
try {
return await this.service.nodeMethod(req, param.key, AlgoNodeType.ALGOD);
} catch (e) {
if (['Array', 'ValidationError'].includes(e.constructor.name)) {
throw new BadRequestException(e);
}
if (e.constructor.name === 'TatumError' || e.constructor.name === AlgoError.name) {
throw e;
}
throw new AlgoError(`Unexpected error occurred. Reason: ${e.message?.message || e.response?.data || e.message || e}`, 'Algo.error');
}
}
('/wallet')
(HttpStatus.OK)
public async generateWallet(() query: QueryMnemonic) {
try {
return await this.service.generateWallet(query.mnemonic)
} catch (e) {
throw new AlgoError(`Unexpected error occurred. Reason: ${e.message || e.response?.data || e}`, 'algo.error');
}
}
('/address/:fromPrivateKey')
(HttpStatus.OK)
public async generateAddress(() { fromPrivateKey }: GeneratePrivateKey) {
try {
return await this.service.generateAddress(fromPrivateKey);
} catch (e) {
if (['Array', 'ValidationError'].includes(e.constructor.name)) {
throw new BadRequestException(e);
}
if (e.constructor.name === 'TatumError' || e.constructor.name === AlgoError.name) {
throw e;
}
throw new AlgoError(`Unexpected error occurred. Reason: ${e.message || e.response?.data || e}`, 'algo.error');
}
}
('/account/balance/:address')
(HttpStatus.OK)
public async getAccountBalance(() param: PathAddress) {
try {
return await this.service.getBalance(param.address);
} catch (e) {
if (['Array', 'ValidationError'].includes(e.constructor.name)) {
throw new BadRequestException(e);
}
if (e.constructor.name === 'TatumError' || e.constructor.name === AlgoError.name) {
throw e;
}
throw new AlgoError(`Unexpected error occurred. Reason: ${e.message || e.response?.data || e}`, 'algo.error');
}
}
('/transaction')
(HttpStatus.OK)
public async sendTransaction(() body: AlgoTransaction) {
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 === AlgoError.name) {
throw e;
}
throw new AlgoError(`Unexpected error occurred. Reason: ${e.message || e.response?.data || e}`, 'algo.error');
}
}
('/broadcast')
(HttpStatus.OK)
public 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 === AlgoError.name) {
throw e;
}
throw new AlgoError(`Unexpected error occurred. Reason: ${e.message || e.response?.data || e}`, 'algo.error');
}
}
('/block/current')
(HttpStatus.OK)
public async getInfo() {
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 === AlgoError.name) {
throw e;
}
throw new AlgoError(`Unexpected error occurred. Reason: ${e.message || e.response?.data || e}`, 'algo.error');
}
}
('/block/:roundNumber')
(HttpStatus.OK)
public async getBlock(() { roundNumber }: PathRountNumber) {
try {
return await this.service.getBlock(Number(roundNumber));
} catch (e) {
if (['Array', 'ValidationError'].includes(e.constructor.name)) {
throw new BadRequestException(e);
}
if (e.constructor.name === 'TatumError' || e.constructor.name === AlgoError.name) {
throw e;
}
throw new AlgoError(`Unexpected error occurred. Reason: ${e.message || e.response?.data || e}`, 'algo.error');
}
}
('/transaction/:txid')
public async getTransaction(() { txid }: PathTransactionId) {
try {
return await this.service.getTransaction(txid);
} catch (e) {
if (['Array', 'ValidationError'].includes(e.constructor.name)) {
throw new BadRequestException(e);
}
if (e.constructor.name === 'TatumError' || e.constructor.name === AlgoError.name) {
throw e;
}
throw new AlgoError(`Unexpected error occurred. Reason: ${e.message || e.response?.data || e}`, 'algo.error');
}
}
('/transactions/:from/:to')
public async getPayTransactions(() { from, to }: PathFromTo, () { limit, next }: Pagination) {
try {
return await this.service.getPayTransactions(from, to, limit, next);
} catch (e) {
if (['Array', 'ValidationError'].includes(e.constructor.name)) {
throw new BadRequestException(e);
}
if (e.constructor.name === 'TatumError' || e.constructor.name === AlgoError.name) {
throw e;
}
throw new AlgoError(`Unexpected error occurred. Reason: ${e.message || e.response?.data || e}`, 'algo.error');
}
}
}