@tatumio/celo-connector
Version:
Celo Connector for Tatum API
363 lines (335 loc) • 15 kB
text/typescript
import {BadRequestException, Body, Get, HttpCode, HttpStatus, Param, Post, Query, Req} from '@nestjs/common';
import {CeloService} from './CeloService';
import {PathAddress} from './dto/PathAddress';
import {CeloError} from './CeloError';
import {PathHash} from './dto/PathHash';
import {PathXpubI} from './dto/PathXpubI';
import {
BroadcastTx,
BurnCeloErc20,
Currency,
DeployCeloErc20,
MintCeloErc20,
TransferCeloOrCeloErc20Token,
CeloSmartContractMethodInvocation,
SmartContractReadMethodInvocation,
} from '@tatumio/tatum';
import {PathAddressContractAddressI} from './dto/PathAddressContractAddressI';
import {PathTokenContractAddress} from './dto/PathTokenContractAddress';
import {PathAddressContractAddress} from './dto/PathAddressContractAddress';
import {QueryMnemonic} from './dto/QueryMnemonic';
import {GeneratePrivateKey} from './dto/GeneratePrivateKey';
import {Request} from 'express';
export abstract class CeloController {
protected constructor(protected readonly service: CeloService) {
}
public async web3Driver( body: any) {
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 === CeloError.name) {
throw e;
}
throw new CeloError(`Unexpected error occurred. Reason: ${e.message?.message || e.response?.data || e.message || e}`, 'celo.error');
}
}
public async createAccount( query: QueryMnemonic) {
try {
return await this.service.generateWallet(query.mnemonic);
} catch (e) {
throw new CeloError(`Unexpected error occurred. Reason: ${e.message?.message || e.response?.data || e.message || e}`, 'celo.error');
}
}
public async generateAddressPrivateKey( body: GeneratePrivateKey) {
try {
return await this.service.generateAddressPrivateKey(body.index, body.mnemonic);
} catch (e) {
throw new CeloError(`Unexpected error occurred. Reason: ${e.message?.message || e.response?.data || e.message || e}`, 'celo.error');
}
}
public async transfer( body: TransferCeloOrCeloErc20Token) {
try {
return await this.service.transfer(body);
} catch (e) {
if (['Array', 'ValidationError'].includes(e.constructor.name)) {
throw new BadRequestException(e);
}
if (e.constructor.name === 'TatumError' || e.constructor.name === CeloError.name) {
throw e;
}
throw new CeloError(`Unexpected error occurred. Reason: ${e.message?.message || e.response?.data || e.message || e}`, 'celo.error');
}
}
public async countTransactions( param: PathAddress) {
try {
return await this.service.getTransactionCount(param.address);
} catch (e) {
throw new CeloError(`Unexpected error occurred. Reason: ${e.message?.message || e.response?.data || e.message || e}`, 'celo.error');
}
}
public async getBalanceErc721( path: PathAddressContractAddress) {
try {
return await this.service.getBalanceErc721(path.address, path.contractAddress);
} catch (e) {
throw new CeloError(`Unexpected error occurred. Reason: ${e.message?.message || e.response?.data || e.message || e}`, 'celo.error');
}
}
public async getOwnerErc721( path: PathTokenContractAddress) {
try {
return await this.service.getOwnerErc721(path.token, path.contractAddress);
} catch (e) {
throw new CeloError(`Unexpected error occurred. Reason: ${e.message?.message || e.response?.data || e.message || e}`, 'celo.error');
}
}
public async getMetadataErc721( path: PathTokenContractAddress) {
try {
return await this.service.getMetadataErc721(path.token, path.contractAddress);
} catch (e) {
throw new CeloError(`Unexpected error occurred. Reason: ${e.message?.message || e.response?.data || e.message || e}`, 'celo.error');
}
}
public async getTokenErc721( path: PathAddressContractAddressI) {
try {
return await this.service.getTokenErc721(path.address, parseInt(path.i), path.contractAddress);
} catch (e) {
throw new CeloError(`Unexpected error occurred. Reason: ${e.message?.message || e.response?.data || e.message || e}`, 'celo.error');
}
}
public async getTokensByAddress( path: PathAddressContractAddress) {
try {
return await this.service.getTokensOfOwner(path.address, path.contractAddress);
} catch (e) {
throw new CeloError(`Unexpected error occurred. Reason: ${e.message?.message || e.response?.data || e.message || e}`, 'celo.error');
}
}
public async transactionErc721( req: Request) {
try {
req.body.chain = Currency.CELO;
return await this.service.transferErc721(req.body);
} catch (e) {
if (['Array', 'ValidationError'].includes(e.constructor.name)) {
throw new BadRequestException(e);
}
if (e.constructor.name === 'TatumError' || e.constructor.name === CeloError.name) {
throw e;
}
throw new CeloError(`Unexpected error occurred. Reason: ${e.message?.message || e.response?.data || e.message || e}`, 'celo.error');
}
}
public async mintErc721( req: Request) {
try {
req.body.chain = Currency.CELO;
return await this.service.mintErc721(req.body);
} catch (e) {
if (['Array', 'ValidationError'].includes(e.constructor.name)) {
throw new BadRequestException(e);
}
if (e.constructor.name === 'TatumError' || e.constructor.name === CeloError.name) {
throw e;
}
throw new CeloError(`Unexpected error occurred. Reason: ${e.message?.message || e.response?.data || e.message || e}`, 'celo.error');
}
}
public async mintMultipleErc721( req: Request) {
try {
req.body.chain = Currency.CELO;
return await this.service.mintMultipleErc721(req.body);
} catch (e) {
if (['Array', 'ValidationError'].includes(e.constructor.name)) {
throw new BadRequestException(e);
}
if (e.constructor.name === 'TatumError' || e.constructor.name === CeloError.name) {
throw e;
}
throw new CeloError(`Unexpected error occurred. Reason: ${e.message?.message || e.response?.data || e.message || e}`, 'celo.error');
}
}
public async burnErc721( req: Request) {
try {
req.body.chain = Currency.CELO;
return await this.service.burnErc721(req.body);
} catch (e) {
if (['Array', 'ValidationError'].includes(e.constructor.name)) {
throw new BadRequestException(e);
}
if (e.constructor.name === 'TatumError' || e.constructor.name === CeloError.name) {
throw e;
}
throw new CeloError(`Unexpected error occurred. Reason: ${e.message?.message || e.response?.data || e.message || e}`, 'celo.error');
}
}
public async deployErc721( req: Request) {
try {
req.body.chain = Currency.CELO;
return await this.service.deployErc721(req.body);
} catch (e) {
if (['Array', 'ValidationError'].includes(e.constructor.name)) {
throw new BadRequestException(e);
}
if (e.constructor.name === 'TatumError' || e.constructor.name === CeloError.name) {
throw e;
}
throw new CeloError(`Unexpected error occurred. Reason: ${e.message?.message || e.response?.data || e.message || e}`, 'celo.error');
}
}
public async transactionErc20( body: TransferCeloOrCeloErc20Token) {
try {
return await this.service.transferErc20(body);
} catch (e) {
if (['Array', 'ValidationError'].includes(e.constructor.name)) {
throw new BadRequestException(e);
}
if (e.constructor.name === 'TatumError' || e.constructor.name === CeloError.name) {
throw e;
}
throw new CeloError(`Unexpected error occurred. Reason: ${e.message?.message || e.response?.data || e.message || e}`, 'celo.error');
}
}
public async invokeCeloSmartContractMethod( body: CeloSmartContractMethodInvocation | SmartContractReadMethodInvocation) {
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 === CeloError.name) {
throw e;
}
throw new CeloError(`Unexpected error occurred. Reason: ${e.message?.message || e.response?.data || e.message || e}`, 'celo.error');
}
}
public async mintErc20( body: MintCeloErc20) {
try {
return await this.service.mintErc20(body);
} catch (e) {
if (['Array', 'ValidationError'].includes(e.constructor.name)) {
throw new BadRequestException(e);
}
if (e.constructor.name === 'TatumError' || e.constructor.name === CeloError.name) {
throw e;
}
throw new CeloError(`Unexpected error occurred. Reason: ${e.message?.message || e.response?.data || e.message || e}`, 'celo.error');
}
}
public async burnErc20( body: BurnCeloErc20) {
try {
return await this.service.burnErc20(body);
} catch (e) {
if (['Array', 'ValidationError'].includes(e.constructor.name)) {
throw new BadRequestException(e);
}
if (e.constructor.name === 'TatumError' || e.constructor.name === CeloError.name) {
throw e;
}
throw new CeloError(`Unexpected error occurred. Reason: ${e.message?.message || e.response?.data || e.message || e}`, 'celo.error');
}
}
public async deployErc20( body: DeployCeloErc20) {
try {
return await this.service.deployErc20(body);
} catch (e) {
if (['Array', 'ValidationError'].includes(e.constructor.name)) {
throw new BadRequestException(e);
}
if (e.constructor.name === 'TatumError' || e.constructor.name === CeloError.name) {
throw e;
}
throw new CeloError(`Unexpected error occurred. Reason: ${e.message?.message || e.response?.data || e.message || e}`, 'celo.error');
}
}
public async getAccountErc20Balance( path: PathAddressContractAddress) {
return this.service.getErc20Balance(path.address, path.contractAddress);
}
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 === CeloError.name) {
throw e;
}
throw new CeloError(`Unexpected error occurred. Reason: ${e.message?.message || e.response?.data || e.message || e}`, 'celo.error');
}
}
public async getCurrentBlock() {
try {
return await this.service.getCurrentBlock();
} catch (e) {
throw new CeloError(`Unexpected error occurred. Reason: ${e.message?.message || e.response?.data || e.message || e}`, 'celo.error');
}
}
public async getBlock( path: PathHash) {
try {
return await this.service.getBlock(path.hash);
} catch (e) {
throw new CeloError(`Unexpected error occurred. Reason: ${e.message?.message || e.response?.data || e.message || e}`, 'celo.error');
}
}
public async getAccountBalance( path: PathAddress) {
try {
return await this.service.getBalance(path.address);
} catch (e) {
throw new CeloError(`Unexpected error occurred. Reason: ${e.message?.message || e.response?.data || e.message || e}`, 'celo.error');
}
}
public async getRawTransaction( path: PathHash) {
try {
return await this.service.getTransaction(path.hash);
} catch (e) {
throw new CeloError(`Unexpected error occurred. Reason: ${e.message?.message || e.response?.data || e.message || e}`, 'celo.error');
}
}
public async generateAddress( path: PathXpubI) {
try {
return await this.service.generateAddress(path.xpub, parseInt(path.i));
} catch (e) {
throw new CeloError(`Unexpected error occurred. Reason: ${e.message?.message || e.response?.data || e.message || e}`, 'celo.error');
}
}
}