@tatumio/celo-connector
Version:
Celo Connector for Tatum API
555 lines (554 loc) • 26 kB
JavaScript
;
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.CeloController = void 0;
const common_1 = require("@nestjs/common");
const PathAddress_1 = require("./dto/PathAddress");
const CeloError_1 = require("./CeloError");
const PathHash_1 = require("./dto/PathHash");
const PathXpubI_1 = require("./dto/PathXpubI");
const tatum_1 = require("@tatumio/tatum");
const PathAddressContractAddressI_1 = require("./dto/PathAddressContractAddressI");
const PathTokenContractAddress_1 = require("./dto/PathTokenContractAddress");
const PathAddressContractAddress_1 = require("./dto/PathAddressContractAddress");
const QueryMnemonic_1 = require("./dto/QueryMnemonic");
const GeneratePrivateKey_1 = require("./dto/GeneratePrivateKey");
class CeloController {
constructor(service) {
this.service = service;
}
async web3Driver(body) {
var _a, _b;
try {
return await this.service.web3Method(body);
}
catch (e) {
if (['Array', 'ValidationError'].includes(e.constructor.name)) {
throw new common_1.BadRequestException(e);
}
if (e.constructor.name === 'TatumError' || e.constructor.name === CeloError_1.CeloError.name) {
throw e;
}
throw new CeloError_1.CeloError(`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}`, 'celo.error');
}
}
async createAccount(query) {
var _a, _b;
try {
return await this.service.generateWallet(query.mnemonic);
}
catch (e) {
throw new CeloError_1.CeloError(`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}`, 'celo.error');
}
}
async generateAddressPrivateKey(body) {
var _a, _b;
try {
return await this.service.generateAddressPrivateKey(body.index, body.mnemonic);
}
catch (e) {
throw new CeloError_1.CeloError(`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}`, 'celo.error');
}
}
async transfer(body) {
var _a, _b;
try {
return await this.service.transfer(body);
}
catch (e) {
if (['Array', 'ValidationError'].includes(e.constructor.name)) {
throw new common_1.BadRequestException(e);
}
if (e.constructor.name === 'TatumError' || e.constructor.name === CeloError_1.CeloError.name) {
throw e;
}
throw new CeloError_1.CeloError(`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}`, 'celo.error');
}
}
async countTransactions(param) {
var _a, _b;
try {
return await this.service.getTransactionCount(param.address);
}
catch (e) {
throw new CeloError_1.CeloError(`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}`, 'celo.error');
}
}
async getBalanceErc721(path) {
var _a, _b;
try {
return await this.service.getBalanceErc721(path.address, path.contractAddress);
}
catch (e) {
throw new CeloError_1.CeloError(`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}`, 'celo.error');
}
}
async getOwnerErc721(path) {
var _a, _b;
try {
return await this.service.getOwnerErc721(path.token, path.contractAddress);
}
catch (e) {
throw new CeloError_1.CeloError(`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}`, 'celo.error');
}
}
async getMetadataErc721(path) {
var _a, _b;
try {
return await this.service.getMetadataErc721(path.token, path.contractAddress);
}
catch (e) {
throw new CeloError_1.CeloError(`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}`, 'celo.error');
}
}
async getTokenErc721(path) {
var _a, _b;
try {
return await this.service.getTokenErc721(path.address, parseInt(path.i), path.contractAddress);
}
catch (e) {
throw new CeloError_1.CeloError(`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}`, 'celo.error');
}
}
async getTokensByAddress(path) {
var _a, _b;
try {
return await this.service.getTokensOfOwner(path.address, path.contractAddress);
}
catch (e) {
throw new CeloError_1.CeloError(`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}`, 'celo.error');
}
}
async transactionErc721(req) {
var _a, _b;
try {
req.body.chain = tatum_1.Currency.CELO;
return await this.service.transferErc721(req.body);
}
catch (e) {
if (['Array', 'ValidationError'].includes(e.constructor.name)) {
throw new common_1.BadRequestException(e);
}
if (e.constructor.name === 'TatumError' || e.constructor.name === CeloError_1.CeloError.name) {
throw e;
}
throw new CeloError_1.CeloError(`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}`, 'celo.error');
}
}
async mintErc721(req) {
var _a, _b;
try {
req.body.chain = tatum_1.Currency.CELO;
return await this.service.mintErc721(req.body);
}
catch (e) {
if (['Array', 'ValidationError'].includes(e.constructor.name)) {
throw new common_1.BadRequestException(e);
}
if (e.constructor.name === 'TatumError' || e.constructor.name === CeloError_1.CeloError.name) {
throw e;
}
throw new CeloError_1.CeloError(`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}`, 'celo.error');
}
}
async mintMultipleErc721(req) {
var _a, _b;
try {
req.body.chain = tatum_1.Currency.CELO;
return await this.service.mintMultipleErc721(req.body);
}
catch (e) {
if (['Array', 'ValidationError'].includes(e.constructor.name)) {
throw new common_1.BadRequestException(e);
}
if (e.constructor.name === 'TatumError' || e.constructor.name === CeloError_1.CeloError.name) {
throw e;
}
throw new CeloError_1.CeloError(`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}`, 'celo.error');
}
}
async burnErc721(req) {
var _a, _b;
try {
req.body.chain = tatum_1.Currency.CELO;
return await this.service.burnErc721(req.body);
}
catch (e) {
if (['Array', 'ValidationError'].includes(e.constructor.name)) {
throw new common_1.BadRequestException(e);
}
if (e.constructor.name === 'TatumError' || e.constructor.name === CeloError_1.CeloError.name) {
throw e;
}
throw new CeloError_1.CeloError(`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}`, 'celo.error');
}
}
async deployErc721(req) {
var _a, _b;
try {
req.body.chain = tatum_1.Currency.CELO;
return await this.service.deployErc721(req.body);
}
catch (e) {
if (['Array', 'ValidationError'].includes(e.constructor.name)) {
throw new common_1.BadRequestException(e);
}
if (e.constructor.name === 'TatumError' || e.constructor.name === CeloError_1.CeloError.name) {
throw e;
}
throw new CeloError_1.CeloError(`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}`, 'celo.error');
}
}
async transactionErc20(body) {
var _a, _b;
try {
return await this.service.transferErc20(body);
}
catch (e) {
if (['Array', 'ValidationError'].includes(e.constructor.name)) {
throw new common_1.BadRequestException(e);
}
if (e.constructor.name === 'TatumError' || e.constructor.name === CeloError_1.CeloError.name) {
throw e;
}
throw new CeloError_1.CeloError(`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}`, 'celo.error');
}
}
async invokeCeloSmartContractMethod(body) {
var _a, _b;
try {
return await this.service.invokeSmartContractMethod(body);
}
catch (e) {
if (['Array', 'ValidationError'].includes(e.constructor.name)) {
throw new common_1.BadRequestException(e);
}
if (e.constructor.name === 'TatumError' || e.constructor.name === CeloError_1.CeloError.name) {
throw e;
}
throw new CeloError_1.CeloError(`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}`, 'celo.error');
}
}
async mintErc20(body) {
var _a, _b;
try {
return await this.service.mintErc20(body);
}
catch (e) {
if (['Array', 'ValidationError'].includes(e.constructor.name)) {
throw new common_1.BadRequestException(e);
}
if (e.constructor.name === 'TatumError' || e.constructor.name === CeloError_1.CeloError.name) {
throw e;
}
throw new CeloError_1.CeloError(`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}`, 'celo.error');
}
}
async burnErc20(body) {
var _a, _b;
try {
return await this.service.burnErc20(body);
}
catch (e) {
if (['Array', 'ValidationError'].includes(e.constructor.name)) {
throw new common_1.BadRequestException(e);
}
if (e.constructor.name === 'TatumError' || e.constructor.name === CeloError_1.CeloError.name) {
throw e;
}
throw new CeloError_1.CeloError(`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}`, 'celo.error');
}
}
async deployErc20(body) {
var _a, _b;
try {
return await this.service.deployErc20(body);
}
catch (e) {
if (['Array', 'ValidationError'].includes(e.constructor.name)) {
throw new common_1.BadRequestException(e);
}
if (e.constructor.name === 'TatumError' || e.constructor.name === CeloError_1.CeloError.name) {
throw e;
}
throw new CeloError_1.CeloError(`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}`, 'celo.error');
}
}
async getAccountErc20Balance(path) {
return this.service.getErc20Balance(path.address, path.contractAddress);
}
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 === CeloError_1.CeloError.name) {
throw e;
}
throw new CeloError_1.CeloError(`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}`, 'celo.error');
}
}
async getCurrentBlock() {
var _a, _b;
try {
return await this.service.getCurrentBlock();
}
catch (e) {
throw new CeloError_1.CeloError(`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}`, 'celo.error');
}
}
async getBlock(path) {
var _a, _b;
try {
return await this.service.getBlock(path.hash);
}
catch (e) {
throw new CeloError_1.CeloError(`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}`, 'celo.error');
}
}
async getAccountBalance(path) {
var _a, _b;
try {
return await this.service.getBalance(path.address);
}
catch (e) {
throw new CeloError_1.CeloError(`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}`, 'celo.error');
}
}
async getRawTransaction(path) {
var _a, _b;
try {
return await this.service.getTransaction(path.hash);
}
catch (e) {
throw new CeloError_1.CeloError(`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}`, 'celo.error');
}
}
async generateAddress(path) {
var _a, _b;
try {
return await this.service.generateAddress(path.xpub, parseInt(path.i));
}
catch (e) {
throw new CeloError_1.CeloError(`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}`, 'celo.error');
}
}
}
__decorate([
common_1.Post('/web3/:xApiKey'),
common_1.HttpCode(common_1.HttpStatus.OK),
__param(0, common_1.Body()),
__metadata("design:type", Function),
__metadata("design:paramtypes", [Object]),
__metadata("design:returntype", Promise)
], CeloController.prototype, "web3Driver", 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)
], CeloController.prototype, "createAccount", null);
__decorate([
common_1.Post('/wallet/priv'),
common_1.HttpCode(common_1.HttpStatus.OK),
__param(0, common_1.Body()),
__metadata("design:type", Function),
__metadata("design:paramtypes", [GeneratePrivateKey_1.GeneratePrivateKey]),
__metadata("design:returntype", Promise)
], CeloController.prototype, "generateAddressPrivateKey", 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.TransferCeloOrCeloErc20Token]),
__metadata("design:returntype", Promise)
], CeloController.prototype, "transfer", null);
__decorate([
common_1.Get('/transaction/count/:address'),
__param(0, common_1.Param()),
__metadata("design:type", Function),
__metadata("design:paramtypes", [PathAddress_1.PathAddress]),
__metadata("design:returntype", Promise)
], CeloController.prototype, "countTransactions", null);
__decorate([
common_1.Get('/erc721/balance/:address/:contractAddress'),
__param(0, common_1.Param()),
__metadata("design:type", Function),
__metadata("design:paramtypes", [PathAddressContractAddress_1.PathAddressContractAddress]),
__metadata("design:returntype", Promise)
], CeloController.prototype, "getBalanceErc721", null);
__decorate([
common_1.Get('/erc721/owner/:token/:contractAddress'),
__param(0, common_1.Param()),
__metadata("design:type", Function),
__metadata("design:paramtypes", [PathTokenContractAddress_1.PathTokenContractAddress]),
__metadata("design:returntype", Promise)
], CeloController.prototype, "getOwnerErc721", null);
__decorate([
common_1.Get('/erc721/metadata/:token/:contractAddress'),
__param(0, common_1.Param()),
__metadata("design:type", Function),
__metadata("design:paramtypes", [PathTokenContractAddress_1.PathTokenContractAddress]),
__metadata("design:returntype", Promise)
], CeloController.prototype, "getMetadataErc721", null);
__decorate([
common_1.Get('/erc721/token/:address/:i/:contractAddress'),
__param(0, common_1.Param()),
__metadata("design:type", Function),
__metadata("design:paramtypes", [PathAddressContractAddressI_1.PathAddressContractAddressI]),
__metadata("design:returntype", Promise)
], CeloController.prototype, "getTokenErc721", null);
__decorate([
common_1.Get('/erc721/token/:address/:contractAddress'),
__param(0, common_1.Param()),
__metadata("design:type", Function),
__metadata("design:paramtypes", [PathAddressContractAddress_1.PathAddressContractAddress]),
__metadata("design:returntype", Promise)
], CeloController.prototype, "getTokensByAddress", null);
__decorate([
common_1.Post('/erc721/transaction'),
common_1.HttpCode(common_1.HttpStatus.OK),
__param(0, common_1.Req()),
__metadata("design:type", Function),
__metadata("design:paramtypes", [Object]),
__metadata("design:returntype", Promise)
], CeloController.prototype, "transactionErc721", null);
__decorate([
common_1.Post('/erc721/mint'),
common_1.HttpCode(common_1.HttpStatus.OK),
__param(0, common_1.Req()),
__metadata("design:type", Function),
__metadata("design:paramtypes", [Object]),
__metadata("design:returntype", Promise)
], CeloController.prototype, "mintErc721", null);
__decorate([
common_1.Post('/erc721/mint/batch'),
common_1.HttpCode(common_1.HttpStatus.OK),
__param(0, common_1.Req()),
__metadata("design:type", Function),
__metadata("design:paramtypes", [Object]),
__metadata("design:returntype", Promise)
], CeloController.prototype, "mintMultipleErc721", null);
__decorate([
common_1.Post('/erc721/burn'),
common_1.HttpCode(common_1.HttpStatus.OK),
__param(0, common_1.Req()),
__metadata("design:type", Function),
__metadata("design:paramtypes", [Object]),
__metadata("design:returntype", Promise)
], CeloController.prototype, "burnErc721", null);
__decorate([
common_1.Post('/erc721/deploy'),
common_1.HttpCode(common_1.HttpStatus.OK),
__param(0, common_1.Req()),
__metadata("design:type", Function),
__metadata("design:paramtypes", [Object]),
__metadata("design:returntype", Promise)
], CeloController.prototype, "deployErc721", null);
__decorate([
common_1.Post('/erc20/transaction'),
common_1.HttpCode(common_1.HttpStatus.OK),
__param(0, common_1.Body()),
__metadata("design:type", Function),
__metadata("design:paramtypes", [tatum_1.TransferCeloOrCeloErc20Token]),
__metadata("design:returntype", Promise)
], CeloController.prototype, "transactionErc20", null);
__decorate([
common_1.Post('/smartcontract'),
common_1.HttpCode(common_1.HttpStatus.OK),
__param(0, common_1.Body()),
__metadata("design:type", Function),
__metadata("design:paramtypes", [Object]),
__metadata("design:returntype", Promise)
], CeloController.prototype, "invokeCeloSmartContractMethod", null);
__decorate([
common_1.Post('/erc20/mint'),
common_1.HttpCode(common_1.HttpStatus.OK),
__param(0, common_1.Body()),
__metadata("design:type", Function),
__metadata("design:paramtypes", [tatum_1.MintCeloErc20]),
__metadata("design:returntype", Promise)
], CeloController.prototype, "mintErc20", null);
__decorate([
common_1.Post('/erc20/burn'),
common_1.HttpCode(common_1.HttpStatus.OK),
__param(0, common_1.Body()),
__metadata("design:type", Function),
__metadata("design:paramtypes", [tatum_1.BurnCeloErc20]),
__metadata("design:returntype", Promise)
], CeloController.prototype, "burnErc20", null);
__decorate([
common_1.Post('/erc20/deploy'),
common_1.HttpCode(common_1.HttpStatus.OK),
__param(0, common_1.Body()),
__metadata("design:type", Function),
__metadata("design:paramtypes", [tatum_1.DeployCeloErc20]),
__metadata("design:returntype", Promise)
], CeloController.prototype, "deployErc20", null);
__decorate([
common_1.Get('/account/balance/erc20/:address/:contractAddress'),
__param(0, common_1.Param()),
__metadata("design:type", Function),
__metadata("design:paramtypes", [PathAddressContractAddress_1.PathAddressContractAddress]),
__metadata("design:returntype", Promise)
], CeloController.prototype, "getAccountErc20Balance", null);
__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)
], CeloController.prototype, "broadcast", null);
__decorate([
common_1.Get('/block/current'),
__metadata("design:type", Function),
__metadata("design:paramtypes", []),
__metadata("design:returntype", Promise)
], CeloController.prototype, "getCurrentBlock", null);
__decorate([
common_1.Get('/block/:hash'),
__param(0, common_1.Param()),
__metadata("design:type", Function),
__metadata("design:paramtypes", [PathHash_1.PathHash]),
__metadata("design:returntype", Promise)
], CeloController.prototype, "getBlock", null);
__decorate([
common_1.Get('/account/balance/:address'),
__param(0, common_1.Param()),
__metadata("design:type", Function),
__metadata("design:paramtypes", [PathAddress_1.PathAddress]),
__metadata("design:returntype", Promise)
], CeloController.prototype, "getAccountBalance", null);
__decorate([
common_1.Get('/transaction/:hash'),
__param(0, common_1.Param()),
__metadata("design:type", Function),
__metadata("design:paramtypes", [PathHash_1.PathHash]),
__metadata("design:returntype", Promise)
], CeloController.prototype, "getRawTransaction", 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)
], CeloController.prototype, "generateAddress", null);
exports.CeloController = CeloController;