UNPKG

@ledgerhq/coin-filecoin

Version:
135 lines 5.65 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.fetchERC20TransactionsWithPages = exports.fetchERC20Transactions = exports.fetchERC20TokenBalance = exports.broadcastTx = exports.fetchTxsWithPages = exports.fetchTxs = exports.fetchBlockHeight = exports.fetchEstimatedFees = exports.fetchBalances = void 0; const live_env_1 = require("@ledgerhq/live-env"); const live_network_1 = __importDefault(require("@ledgerhq/live-network")); const cache_1 = require("@ledgerhq/live-network/cache"); const logs_1 = require("@ledgerhq/logs"); const errors_1 = require("../errors"); const txsPerPageLimit = 1000; const currentVersion = "/v2"; const fromHeightQueryParam = "from_height"; const getFilecoinURL = (version = currentVersion, path) => { const baseUrl = (0, live_env_1.getEnv)("API_FILECOIN_ENDPOINT"); if (!baseUrl) throw new Error("API base URL not available"); return `${baseUrl}${version ? version : ""}${path ? path : ""}`; }; const fetch = async (path, { version }) => { const url = getFilecoinURL(version, 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: "GET", url, }; const rawResponse = await (0, live_network_1.default)(opts); // 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; (0, logs_1.log)("http", url); return data; }; const send = async (path, { version, data }) => { const url = getFilecoinURL(version, path); const opts = { method: "POST", url, data: JSON.stringify(data), headers: { "Content-Type": "application/json" }, }; const rawResponse = await (0, live_network_1.default)(opts); // We force data to this way as network func is not using generics. Changing that func will generate errors in other implementations const { data: responseData } = rawResponse; (0, logs_1.log)("http", url); return responseData; }; const fetchBalances = async (addr) => { const data = await fetch(`/addresses/${addr}/balance`, { version: currentVersion, }); return data; // TODO Validate if the response fits this interface }; exports.fetchBalances = fetchBalances; exports.fetchEstimatedFees = (0, cache_1.makeLRUCache)(async (request) => { try { const data = await send(`/fees/estimate`, { version: currentVersion, data: request, }); return data; // TODO Validate if the response fits this interface } catch (e) { (0, logs_1.log)("error", "filecoin fetchEstimatedFees", e); throw new errors_1.FilecoinFeeEstimationFailed(); } }, request => `${request.from}-${request.to}`, { ttl: 5 * 1000, // 5 seconds }); const fetchBlockHeight = async () => { const data = await fetch("/network/status", { version: currentVersion, }); return data; // TODO Validate if the response fits this interface }; exports.fetchBlockHeight = fetchBlockHeight; const fetchTxs = async (addr, lastHeight, offset = 0, limit = 0) => { const response = await fetch(`/addresses/${addr}/transactions?${fromHeightQueryParam}=${lastHeight}&offset=${offset}&limit=${limit}`, { version: currentVersion, }); return response; // TODO Validate if the response fits this interface }; exports.fetchTxs = fetchTxs; const fetchTxsWithPages = async (addr, lastHeight) => { let result = []; let offset = 0; let txsLen = txsPerPageLimit; while (txsLen === txsPerPageLimit) { const { txs } = await (0, exports.fetchTxs)(addr, lastHeight, offset, txsPerPageLimit); result = result.concat(txs); txsLen = txs.length; offset += txsLen; } return result; }; exports.fetchTxsWithPages = fetchTxsWithPages; const broadcastTx = async (message) => { const response = await send(`/transaction/broadcast`, { version: currentVersion, data: message, }); return response; // TODO Validate if the response fits this interface }; exports.broadcastTx = broadcastTx; const fetchERC20TokenBalance = async (ethAddr, contractAddr) => { const res = await fetch(`/contract/${contractAddr}/address/${ethAddr}/balance/erc20`, { version: currentVersion, }); if (res.data.length) { return res.data[0].balance; } return "0"; }; exports.fetchERC20TokenBalance = fetchERC20TokenBalance; const fetchERC20Transactions = async (ethAddr, lastHeight, offset = 0, limit = 0) => { const res = await fetch(`/addresses/${ethAddr}/transactions/erc20?${fromHeightQueryParam}=${lastHeight}&offset=${offset}&limit=${limit}`, { version: currentVersion, }); return res; }; exports.fetchERC20Transactions = fetchERC20Transactions; const fetchERC20TransactionsWithPages = async (addr, lastHeight) => { let result = []; let offset = 0; let txsLen = txsPerPageLimit; while (txsLen === txsPerPageLimit) { const { txs } = await (0, exports.fetchERC20Transactions)(addr, lastHeight, offset, txsPerPageLimit); result = result.concat(txs); txsLen = txs.length; offset += txsLen; } return result.sort((a, b) => b.timestamp - a.timestamp); }; exports.fetchERC20TransactionsWithPages = fetchERC20TransactionsWithPages; //# sourceMappingURL=api.js.map