@tatumio/celo-connector
Version:
Celo Connector for Tatum API
285 lines (284 loc) • 13.1 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.CeloService = void 0;
const axios_1 = __importDefault(require("axios"));
const celo_ethers_wrapper_1 = require("@celo-tools/celo-ethers-wrapper");
const CeloError_1 = require("./CeloError");
const tatum_1 = require("@tatumio/tatum");
const erc721_abi_1 = __importDefault(require("@tatumio/tatum/dist/src/contracts/erc721/erc721_abi"));
const token_abi_1 = __importDefault(require("@tatumio/tatum/dist/src/contracts/erc20/token_abi"));
const web3_utils_1 = require("web3-utils");
const web3_1 = __importDefault(require("web3"));
class CeloService {
constructor(logger) {
this.logger = logger;
}
async broadcast(txData, signatureId, withdrawalId) {
this.logger.info(`Broadcast tx for CELO with data '${txData}'`);
let txId;
try {
const url = (await this.getNodesUrl(await this.isTestnet()))[0];
const { result, error } = (await axios_1.default.post(url, {
jsonrpc: '2.0',
id: 0,
method: 'eth_sendRawTransaction',
params: [txData]
}, { headers: { 'Content-Type': 'application/json' } })).data;
if (error) {
throw new CeloError_1.CeloError(`Unable to broadcast transaction due to ${error.message}.`, 'celo.broadcast.failed');
}
txId = result;
}
catch (e) {
if (e.constructor.name === CeloError_1.CeloError.name) {
throw e;
}
this.logger.error(e);
throw new CeloError_1.CeloError(`Unable to broadcast transaction due to ${e.message}.`, 'celo.broadcast.failed');
}
if (signatureId) {
try {
await this.completeKMSTransaction(txId, signatureId);
}
catch (e) {
this.logger.error(e);
return { txId, failed: true };
}
}
return { txId };
}
async getCurrentBlock(testnet) {
const t = testnet === undefined ? await this.isTestnet() : testnet;
return (await this.getClient(t)).eth.getBlockNumber();
}
async getBalance(address, testnet) {
const t = testnet === undefined ? await this.isTestnet() : testnet;
const provider = new celo_ethers_wrapper_1.CeloProvider((await this.getNodesUrl(t))[0]);
const cUsd = new ((await this.getClient(t))).eth.Contract(token_abi_1.default, t ? tatum_1.CUSD_ADDRESS_TESTNET : tatum_1.CUSD_ADDRESS_MAINNET);
const cEur = new ((await this.getClient(t))).eth.Contract(token_abi_1.default, t ? tatum_1.CEUR_ADDRESS_TESTNET : tatum_1.CEUR_ADDRESS_MAINNET);
return {
celo: web3_utils_1.fromWei((await provider.getBalance(address)).toString(), 'ether'),
cUsd: web3_utils_1.fromWei(await cUsd.methods.balanceOf(address).call(), 'ether'),
cEur: web3_utils_1.fromWei(await cEur.methods.balanceOf(address).call(), 'ether'),
};
}
async getBlock(hash, testnet) {
const t = testnet === undefined ? await this.isTestnet() : testnet;
return (await this.getClient(t)).eth.getBlock(hash, true);
}
async getTransaction(txId, testnet) {
const t = testnet === undefined ? await this.isTestnet() : testnet;
try {
const web3 = await this.getClient(t);
const { r, s, v, hash, ...transaction } = (await web3.eth.getTransaction(txId));
let receipt = undefined;
try {
receipt = await web3.eth.getTransactionReceipt(hash);
}
catch (_) {
transaction.transactionHash = hash;
}
return { ...transaction, ...receipt };
}
catch (e) {
this.logger.error(e);
throw new CeloError_1.CeloError('Transaction not found. Possible not exists or is still pending.', 'tx.not.found');
}
}
async transfer(body) {
const testnet = await this.isTestnet();
const txData = await tatum_1.prepareCeloOrCUsdSignedTransaction(testnet, body, (await this.getNodesUrl(testnet))[0]);
if (body.signatureId) {
return { signatureId: await this.storeKMSTransaction(txData, tatum_1.Currency.CELO, [body.signatureId], body.index) };
}
else {
return this.broadcast(txData);
}
}
async transferErc20(body) {
const testnet = await this.isTestnet();
const txData = await tatum_1.prepareCeloTransferErc20SignedTransaction(testnet, body, (await this.getNodesUrl(testnet))[0]);
if (body.signatureId) {
return { signatureId: await this.storeKMSTransaction(txData, tatum_1.Currency.CELO, [body.signatureId], body.index) };
}
else {
return this.broadcast(txData);
}
}
async invokeSmartContractMethod(body) {
const testnet = await this.isTestnet();
if (body.methodABI.stateMutability === 'view') {
return tatum_1.sendCeloSmartContractReadMethodInvocationTransaction(testnet, body, (await this.getNodesUrl(testnet))[0]);
}
const txData = await tatum_1.prepareCeloSmartContractWriteMethodInvocation(testnet, body, (await this.getNodesUrl(testnet))[0]);
if (body.signatureId) {
return { signatureId: await this.storeKMSTransaction(txData, tatum_1.Currency.CELO, [body.signatureId], body.index) };
}
else {
return this.broadcast(txData);
}
}
async mintErc20(body) {
const testnet = await this.isTestnet();
const txData = await tatum_1.prepareCeloMintErc20SignedTransaction(testnet, body, (await this.getNodesUrl(testnet))[0]);
if (body.signatureId) {
return { signatureId: await this.storeKMSTransaction(txData, tatum_1.Currency.CELO, [body.signatureId], body.index) };
}
else {
return this.broadcast(txData);
}
}
async burnErc20(body) {
const testnet = await this.isTestnet();
const txData = await tatum_1.prepareCeloBurnErc20SignedTransaction(testnet, body, (await this.getNodesUrl(testnet))[0]);
if (body.signatureId) {
return { signatureId: await this.storeKMSTransaction(txData, tatum_1.Currency.CELO, [body.signatureId], body.index) };
}
else {
return this.broadcast(txData);
}
}
async deployErc20(body) {
const testnet = await this.isTestnet();
const txData = await tatum_1.prepareCeloDeployErc20SignedTransaction(testnet, body, (await this.getNodesUrl(testnet))[0]);
if (body.signatureId) {
return { signatureId: await this.storeKMSTransaction(txData, tatum_1.Currency.CELO, [body.signatureId], body.index) };
}
else {
return this.broadcast(txData);
}
}
async getErc20Balance(address, contractAddress) {
const c = new ((await this.getClient(await this.isTestnet()))).eth.Contract(token_abi_1.default, contractAddress);
return { balance: await c.methods.balanceOf(address).call() };
}
async getTransactionCount(address, c) {
const client = c || await this.getClient(await this.isTestnet());
return client.eth.getTransactionCount(address, 'pending');
}
async getBalanceErc721(address, contractAddress) {
const c = new (await this.getClient(await this.isTestnet())).eth.Contract(erc721_abi_1.default, contractAddress);
try {
return { data: await c.methods.balanceOf(address).call() };
}
catch (e) {
this.logger.error(e);
throw new CeloError_1.CeloError(`Unable to obtain information for token. ${e}`, 'celo.erc721.failed');
}
}
async getTokenErc721(address, index, contractAddress) {
const c = new (await this.getClient(await this.isTestnet())).eth.Contract(erc721_abi_1.default, contractAddress);
try {
return { data: await c.methods.tokenOfOwnerByIndex(address, index).call() };
}
catch (e) {
this.logger.error(e);
throw new CeloError_1.CeloError(`Unable to obtain information for token. ${e}`, 'celo.erc721.failed');
}
}
async getMetadataErc721(token, contractAddress) {
const c = new (await this.getClient(await this.isTestnet())).eth.Contract(erc721_abi_1.default, contractAddress);
try {
return { data: await c.methods.tokenURI(token).call() };
}
catch (e) {
this.logger.error(e);
throw new CeloError_1.CeloError(`Unable to obtain information for token. ${e}`, 'celo.erc721.failed');
}
}
async getOwnerErc721(token, contractAddress) {
const c = new (await this.getClient(await this.isTestnet())).eth.Contract(erc721_abi_1.default, contractAddress);
try {
return { data: await c.methods.ownerOf(token).call() };
}
catch (e) {
this.logger.error(e);
throw new CeloError_1.CeloError(`Unable to obtain information for token. ${e}`, 'celo.erc721.failed');
}
}
async getTokensOfOwner(address, contractAddress) {
const c = new (await this.getClient(await this.isTestnet())).eth.Contract(erc721_abi_1.default, contractAddress);
try {
return { data: await c.methods.tokensOfOwner(address).call() };
}
catch (e) {
this.logger.error(e);
throw new CeloError_1.CeloError(`Unable to obtain information for token. ${e}`, 'celo.erc721.failed');
}
}
async transferErc721(body) {
const testnet = await this.isTestnet();
const txData = await tatum_1.prepareCeloTransferErc721SignedTransaction(testnet, body, (await this.getNodesUrl(testnet))[0]);
if (body.signatureId) {
return { signatureId: await this.storeKMSTransaction(txData, tatum_1.Currency.CELO, [body.signatureId], body.index) };
}
else {
return this.broadcast(txData);
}
}
async mintErc721(body) {
const testnet = await this.isTestnet();
const txData = await tatum_1.prepareCeloMintErc721SignedTransaction(testnet, body, (await this.getNodesUrl(testnet))[0]);
if (body.signatureId) {
return { signatureId: await this.storeKMSTransaction(txData, tatum_1.Currency.CELO, [body.signatureId], body.index) };
}
else {
return this.broadcast(txData);
}
}
async mintMultipleErc721(body) {
const testnet = await this.isTestnet();
const txData = await tatum_1.prepareCeloMintMultipleErc721SignedTransaction(testnet, body, (await this.getNodesUrl(testnet))[0]);
if (body.signatureId) {
return { signatureId: await this.storeKMSTransaction(txData, tatum_1.Currency.CELO, [body.signatureId], body.index) };
}
else {
return this.broadcast(txData);
}
}
async burnErc721(body) {
const testnet = await this.isTestnet();
const txData = await tatum_1.prepareCeloBurnErc721SignedTransaction(testnet, body, (await this.getNodesUrl(testnet))[0]);
if (body.signatureId) {
return { signatureId: await this.storeKMSTransaction(txData, tatum_1.Currency.CELO, [body.signatureId], body.index) };
}
else {
return this.broadcast(txData);
}
}
async deployErc721(body) {
const testnet = await this.isTestnet();
const txData = await tatum_1.prepareCeloDeployErc721SignedTransaction(testnet, body, (await this.getNodesUrl(testnet))[0]);
if (body.signatureId) {
return { signatureId: await this.storeKMSTransaction(txData, tatum_1.Currency.CELO, [body.signatureId], body.index) };
}
else {
return this.broadcast(txData);
}
}
async web3Method(body) {
try {
return (await axios_1.default.post((await this.getNodesUrl(await this.isTestnet()))[0], body, { headers: { 'Content-Type': 'application/json' } })).data;
}
catch (e) {
this.logger.error(e);
throw e;
}
}
async generateAddress(xpub, i) {
return { address: await tatum_1.generateAddressFromXPub(tatum_1.Currency.CELO, await this.isTestnet(), xpub, i) };
}
async generateWallet(mnemonic) {
return tatum_1.generateWallet(tatum_1.Currency.CELO, await this.isTestnet(), mnemonic);
}
async generateAddressPrivateKey(index, mnemonic) {
return { key: await tatum_1.generatePrivateKeyFromMnemonic(tatum_1.Currency.CELO, await this.isTestnet(), mnemonic, index) };
}
async getClient(testnet) {
return new web3_1.default((await this.getNodesUrl(testnet))[0]);
}
}
exports.CeloService = CeloService;