UNPKG

@ledgerhq/coin-multiversx

Version:
153 lines 5.99 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const live_network_1 = __importDefault(require("@ledgerhq/live-network")); const constants_1 = require("../constants"); const types_1 = require("../types"); const decodeTransactionMode = (action) => { if (!action) { return "send"; } if (!action.category) { return "send"; } if (action.category !== "stake") { return "send"; } const mode = action.name; return mode; }; class MultiversXApi { API_URL; DELEGATION_API_URL; constructor(API_URL, DELEGATION_API_URL) { this.API_URL = API_URL; this.DELEGATION_API_URL = DELEGATION_API_URL; } async getAccountDetails(addr) { const { data: { balance, nonce, isGuarded }, } = await (0, live_network_1.default)({ method: "GET", url: `${this.API_URL}/accounts/${addr}?withGuardianInfo=true`, }); return { balance, nonce, isGuarded, }; } async getProviders() { const { data: providers } = await (0, live_network_1.default)({ method: "GET", url: `${this.DELEGATION_API_URL}/providers`, }); return providers; } async getNetworkConfig() { const { data: { data: { config: { erd_chain_id: chainId, erd_denomination: denomination, erd_min_gas_limit: gasLimit, erd_min_gas_price: gasPrice, erd_gas_per_data_byte: gasPerByte, erd_gas_price_modifier: gasPriceModifier, }, }, }, } = await (0, live_network_1.default)({ method: "GET", url: `${this.API_URL}/network/config`, }); return { chainID: chainId, denomination, gasLimit, gasPrice, gasPerByte, gasPriceModifier, }; } async submit(signedOperation) { const transaction = { ...signedOperation.rawData, signature: signedOperation.signature, }; const { data: { data: { txHash: hash }, }, } = await (0, live_network_1.default)({ method: "POST", url: `${this.API_URL}/transaction/send`, data: transaction, }); return hash; } async getHistory(addr, startAt) { const { data: transactionsCount } = await (0, live_network_1.default)({ method: "GET", url: `${this.API_URL}/accounts/${addr}/transactions/count?after=${startAt}`, }); let allTransactions = []; let from = 0; while (from < transactionsCount) { const { data: transactions } = await (0, live_network_1.default)({ method: "GET", url: `${this.API_URL}/accounts/${addr}/transactions?after=${startAt}&from=${from}&size=${constants_1.MAX_PAGINATION_SIZE}&withOperations=true&withScResults=true`, }); for (const transaction of transactions) { transaction.mode = decodeTransactionMode(transaction.action); } allTransactions = [...allTransactions, ...transactions]; from = from + constants_1.MAX_PAGINATION_SIZE; } return allTransactions; } async getAccountDelegations(addr) { const { data: delegations } = await (0, live_network_1.default)({ method: "GET", url: `${this.DELEGATION_API_URL}/accounts/${addr}/delegations`, }); return delegations; } async getESDTTransactionsForAddress(addr, token, startAt) { const { data: tokenTransactionsCount } = await (0, live_network_1.default)({ method: "GET", url: `${this.API_URL}/accounts/${addr}/transactions/count?token=${token}&after=${startAt}`, }); let allTokenTransactions = []; let from = 0; while (from < tokenTransactionsCount) { const { data: tokenTransactions } = await (0, live_network_1.default)({ method: "GET", url: `${this.API_URL}/accounts/${addr}/transactions?token=${token}&from=${from}&after=${startAt}&size=${constants_1.MAX_PAGINATION_SIZE}`, }); allTokenTransactions = [...allTokenTransactions, ...tokenTransactions]; from = from + constants_1.MAX_PAGINATION_SIZE; } for (const esdtTransaction of allTokenTransactions) { esdtTransaction.transfer = types_1.MultiversXTransferOptions.esdt; } return allTokenTransactions; } async getESDTTokensForAddress(addr) { const { data: tokensCount } = await (0, live_network_1.default)({ method: "GET", url: `${this.API_URL}/accounts/${addr}/tokens/count`, }); let allTokens = []; let from = 0; while (from < tokensCount) { const { data: tokens } = await (0, live_network_1.default)({ method: "GET", url: `${this.API_URL}/accounts/${addr}/tokens?from=${from}&size=${constants_1.MAX_PAGINATION_SIZE}`, }); allTokens = [...allTokens, ...tokens]; from = from + constants_1.MAX_PAGINATION_SIZE; } return allTokens; } async getESDTTokensCountForAddress(addr) { const { data: tokensCount } = await (0, live_network_1.default)({ method: "GET", url: `${this.API_URL}/accounts/${addr}/tokens/count`, }); return tokensCount; } async getBlockchainBlockHeight() { const { data: [{ round: blockHeight }], } = await (0, live_network_1.default)({ method: "GET", url: `${this.API_URL}/blocks?shard=${constants_1.METACHAIN_SHARD}&fields=round`, }); return blockHeight; } } exports.default = MultiversXApi; //# sourceMappingURL=apiCalls.js.map