UNPKG

@ledgerhq/coin-casper

Version:
109 lines 3.78 kB
import { log } from "@ledgerhq/logs"; import network from "@ledgerhq/live-network"; import { AccountIdentifier, HttpHandler, PublicKey, RpcClient } from "casper-js-sdk"; import { getCoinConfig } from "../config"; import BigNumber from "bignumber.js"; import { NodeErrorCodeAccountNotFound, NodeErrorCodeQueryFailed } from "../consts"; const getCasperIndexerURL = (path) => { const baseUrl = getCoinConfig().infra.API_CASPER_INDEXER; if (!baseUrl) throw new Error("API base URL not available"); return new URL(path, baseUrl).toString(); }; const getCasperNodeURL = () => { const baseUrl = getCoinConfig().infra.API_CASPER_NODE_ENDPOINT; if (!baseUrl) throw new Error("API base URL not available"); return baseUrl; }; export const getCasperNodeRpcClient = () => { const url = getCasperNodeURL(); const handler = new HttpHandler(url); return new RpcClient(handler); }; const casperIndexerWrapper = async (path) => { const url = getCasperIndexerURL(path); try { const rawResponse = await network({ method: "GET", url, }); log("http", url); const { data, status } = rawResponse; if (status >= 300) { log("http", url, data); } return data; } catch (error) { log("error", "Casper indexer error: ", error); throw error; } }; export const fetchAccountStateInfo = async (publicKey) => { const client = getCasperNodeRpcClient(); try { const { account } = await client.getAccountInfo(null, new AccountIdentifier(undefined, PublicKey.fromHex(publicKey))); const accountHash = account.accountHash.toHex(); const purseURefString = account.mainPurse.toPrefixedString(); return { purseUref: purseURefString, accountHash }; } catch (error) { if (error instanceof Error && [NodeErrorCodeAccountNotFound, NodeErrorCodeQueryFailed].includes(error.statusCode)) { return { purseUref: undefined, accountHash: undefined, }; } throw error; } }; export const fetchBalance = async (purseUref) => { const client = getCasperNodeRpcClient(); try { const { stateRootHash } = await client.getStateRootHashLatest(); const balance = await client.getBalanceByStateRootHash(purseUref, stateRootHash.toHex()); return new BigNumber(balance.balanceValue.toString()); } catch (error) { log("error", "Failed to fetch balance", error); throw error; } }; export const fetchBlockHeight = async () => { const client = getCasperNodeRpcClient(); try { const latestBlock = await client.getLatestBlock(); return latestBlock.block.height; } catch (error) { log("error", "Failed to fetch block height", error); throw error; } }; export const fetchTxs = async (addr) => { let page = 1; let res = []; const limit = 100; let response = await casperIndexerWrapper(`accounts/${addr}/ledgerlive-deploys?limit=${limit}&page=${page}`); res = res.concat(response.data); while (response.pageCount > page) { page++; response = await casperIndexerWrapper(`accounts/${addr}/ledgerlive-deploys?limit=${limit}&page=${page}`); res = res.concat(response.data); } return res; }; export const broadcastTx = async (transaction) => { const client = getCasperNodeRpcClient(); try { const response = await client.putTransaction(transaction); return response.transactionHash.toHex(); } catch (error) { log("error", "Failed to broadcast transaction", error); throw error; } }; //# sourceMappingURL=index.js.map