@ledgerhq/coin-multiversx
Version:
Ledger MultiversX Coin integration
147 lines • 5.42 kB
JavaScript
import network from "@ledgerhq/live-network";
import { MAX_PAGINATION_SIZE, METACHAIN_SHARD } from "../constants";
import { MultiversXTransferOptions, } from "../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;
};
export default 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 network({
method: "GET",
url: `${this.API_URL}/accounts/${addr}?withGuardianInfo=true`,
});
return {
balance,
nonce,
isGuarded,
};
}
async getProviders() {
const { data: providers } = await network({
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 network({
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 network({
method: "POST",
url: `${this.API_URL}/transaction/send`,
data: transaction,
});
return hash;
}
async getHistory(addr, startAt) {
const { data: transactionsCount } = await network({
method: "GET",
url: `${this.API_URL}/accounts/${addr}/transactions/count?after=${startAt}`,
});
let allTransactions = [];
let from = 0;
while (from < transactionsCount) {
const { data: transactions } = await network({
method: "GET",
url: `${this.API_URL}/accounts/${addr}/transactions?after=${startAt}&from=${from}&size=${MAX_PAGINATION_SIZE}&withOperations=true&withScResults=true`,
});
for (const transaction of transactions) {
transaction.mode = decodeTransactionMode(transaction.action);
}
allTransactions = [...allTransactions, ...transactions];
from = from + MAX_PAGINATION_SIZE;
}
return allTransactions;
}
async getAccountDelegations(addr) {
const { data: delegations } = await network({
method: "GET",
url: `${this.DELEGATION_API_URL}/accounts/${addr}/delegations`,
});
return delegations;
}
async getESDTTransactionsForAddress(addr, token, startAt) {
const { data: tokenTransactionsCount } = await network({
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 network({
method: "GET",
url: `${this.API_URL}/accounts/${addr}/transactions?token=${token}&from=${from}&after=${startAt}&size=${MAX_PAGINATION_SIZE}`,
});
allTokenTransactions = [...allTokenTransactions, ...tokenTransactions];
from = from + MAX_PAGINATION_SIZE;
}
for (const esdtTransaction of allTokenTransactions) {
esdtTransaction.transfer = MultiversXTransferOptions.esdt;
}
return allTokenTransactions;
}
async getESDTTokensForAddress(addr) {
const { data: tokensCount } = await network({
method: "GET",
url: `${this.API_URL}/accounts/${addr}/tokens/count`,
});
let allTokens = [];
let from = 0;
while (from < tokensCount) {
const { data: tokens } = await network({
method: "GET",
url: `${this.API_URL}/accounts/${addr}/tokens?from=${from}&size=${MAX_PAGINATION_SIZE}`,
});
allTokens = [...allTokens, ...tokens];
from = from + MAX_PAGINATION_SIZE;
}
return allTokens;
}
async getESDTTokensCountForAddress(addr) {
const { data: tokensCount } = await network({
method: "GET",
url: `${this.API_URL}/accounts/${addr}/tokens/count`,
});
return tokensCount;
}
async getBlockchainBlockHeight() {
const { data: [{ round: blockHeight }], } = await network({
method: "GET",
url: `${this.API_URL}/blocks?shard=${METACHAIN_SHARD}&fields=round`,
});
return blockHeight;
}
}
//# sourceMappingURL=apiCalls.js.map