@ledgerhq/coin-filecoin
Version:
Ledger Filecoin Coin integration
78 lines • 3.17 kB
JavaScript
import { log } from "@ledgerhq/logs";
import network from "@ledgerhq/live-network/network";
import { makeLRUCache } from "@ledgerhq/live-network/cache";
import { getEnv } from "@ledgerhq/live-env";
import { FilecoinFeeEstimationFailed } from "../errors";
const getFilecoinURL = (path) => {
const baseUrl = 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 network(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;
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 network(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;
log("http", url);
return responseData;
};
export const fetchBalances = async (addr) => {
const data = await fetch(`/addresses/${addr}/balance`);
return data; // TODO Validate if the response fits this interface
};
export const fetchEstimatedFees = makeLRUCache(async (request) => {
try {
const data = await send(`/fees/estimate`, request);
return data; // TODO Validate if the response fits this interface
}
catch (e) {
log("error", "filecoin fetchEstimatedFees", e);
throw new FilecoinFeeEstimationFailed();
}
}, request => `${request.from}-${request.to}`, {
ttl: 5 * 1000, // 5 seconds
});
export const fetchBlockHeight = async () => {
const data = await fetch("/network/status");
return data; // TODO Validate if the response fits this interface
};
export const fetchTxs = async (addr) => {
const response = await fetch(`/addresses/${addr}/transactions`);
return response.txs; // TODO Validate if the response fits this interface
};
export const broadcastTx = async (message) => {
const response = await send(`/transaction/broadcast`, message);
return response; // TODO Validate if the response fits this interface
};
export 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";
};
export const fetchERC20Transactions = async (ethAddr) => {
const res = await fetch(`/addresses/${ethAddr}/transactions/erc20`);
return res.txs.sort((a, b) => b.timestamp - a.timestamp);
};
//# sourceMappingURL=api.js.map