UNPKG

@ledgerhq/coin-internet_computer

Version:
69 lines 2.35 kB
import { log } from "@ledgerhq/logs"; import { getEnv } from "@ledgerhq/live-env"; import network from "@ledgerhq/live-network/network"; import { ICP_BLK_NAME_ROSETTA, ICP_NET_ID_ROSETTA } from "../consts"; const getICPURL = (path) => { const baseUrl = getEnv("API_ICP_ENDPOINT"); if (!baseUrl) throw new Error("API base URL not available"); return `${baseUrl}${path ? path : ""}`; }; const ICPFetchWrapper = async (path, body) => { const url = getICPURL(path); // We force data to this way as network func is not using the correct param type. Changing that func will generate errors in other implementations const opts = { method: "POST", data: body, url, }; const rawResponse = await network(opts); if (rawResponse && rawResponse.data && rawResponse.data.details?.error_message) { log("error", rawResponse.data.details?.error_message); } // We force data to this way as network func is not using the correct param type. Changing that func will generate errors in other implementations const { data } = rawResponse; log("http", url); return data; }; export const getICPRosettaNetworkIdentifier = () => { return { network_identifier: { blockchain: ICP_BLK_NAME_ROSETTA, network: ICP_NET_ID_ROSETTA, }, }; }; export const fetchBlockHeight = async () => { const data = await ICPFetchWrapper("network/status", getICPRosettaNetworkIdentifier()); return data; }; export const fetchBalances = async (accountId) => { const body = { ...getICPRosettaNetworkIdentifier(), account_identifier: { address: accountId, metadata: {}, }, }; const data = await ICPFetchWrapper("account/balance", body); return data; }; export const fetchTxns = async (accountId) => { const body = { ...getICPRosettaNetworkIdentifier(), account_identifier: { address: accountId, metadata: {}, }, }; const data = await ICPFetchWrapper("search/transactions", body); return data; }; export const constructionInvoke = async (opts, method) => { const body = { ...opts, }; const data = await ICPFetchWrapper(`construction/${method}`, body); return data; }; //# sourceMappingURL=api.js.map