@ledgerhq/coin-filecoin
Version:
Ledger Filecoin Coin integration
90 lines • 3.91 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.fetchERC20Transactions = exports.fetchERC20TokenBalance = exports.broadcastTx = exports.fetchTxs = exports.fetchBlockHeight = exports.fetchEstimatedFees = exports.fetchBalances = void 0;
const logs_1 = require("@ledgerhq/logs");
const network_1 = __importDefault(require("@ledgerhq/live-network/network"));
const cache_1 = require("@ledgerhq/live-network/cache");
const live_env_1 = require("@ledgerhq/live-env");
const errors_1 = require("../errors");
const getFilecoinURL = (path) => {
const baseUrl = (0, live_env_1.getEnv)("API_FILECOIN_ENDPOINT");
if (!baseUrl)
throw new Error("API base URL not available");
return `${baseUrl}${path ? path : ""}`;
};
const fetch = async (path) => {
const url = getFilecoinURL(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, 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, data) => {
const url = getFilecoinURL(path);
const opts = {
method: "POST",
url,
data: JSON.stringify(data),
headers: { "Content-Type": "application/json" },
};
const rawResponse = await (0, 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`);
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`, 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");
return data; // TODO Validate if the response fits this interface
};
exports.fetchBlockHeight = fetchBlockHeight;
const fetchTxs = async (addr) => {
const response = await fetch(`/addresses/${addr}/transactions`);
return response.txs; // TODO Validate if the response fits this interface
};
exports.fetchTxs = fetchTxs;
const broadcastTx = async (message) => {
const response = await send(`/transaction/broadcast`, 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`);
if (res.data.length) {
return res.data[0].balance;
}
return "0";
};
exports.fetchERC20TokenBalance = fetchERC20TokenBalance;
const fetchERC20Transactions = async (ethAddr) => {
const res = await fetch(`/addresses/${ethAddr}/transactions/erc20`);
return res.txs.sort((a, b) => b.timestamp - a.timestamp);
};
exports.fetchERC20Transactions = fetchERC20Transactions;
//# sourceMappingURL=api.js.map