@ledgerhq/coin-filecoin
Version:
Ledger Filecoin Coin integration
121 lines • 4.69 kB
JavaScript
import { getEnv } from "@ledgerhq/live-env";
import network from "@ledgerhq/live-network";
import { makeLRUCache } from "@ledgerhq/live-network/cache";
import { log } from "@ledgerhq/logs";
import { FilecoinFeeEstimationFailed } from "../errors";
const txsPerPageLimit = 1000;
const currentVersion = "/v2";
const fromHeightQueryParam = "from_height";
const getFilecoinURL = (version = currentVersion, path) => {
const baseUrl = 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 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, { version, data }) => {
const url = getFilecoinURL(version, 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`, {
version: currentVersion,
});
return data; // TODO Validate if the response fits this interface
};
export const fetchEstimatedFees = 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) {
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", {
version: currentVersion,
});
return data; // TODO Validate if the response fits this interface
};
export 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
};
export const fetchTxsWithPages = async (addr, lastHeight) => {
let result = [];
let offset = 0;
let txsLen = txsPerPageLimit;
while (txsLen === txsPerPageLimit) {
const { txs } = await fetchTxs(addr, lastHeight, offset, txsPerPageLimit);
result = result.concat(txs);
txsLen = txs.length;
offset += txsLen;
}
return result;
};
export const broadcastTx = async (message) => {
const response = await send(`/transaction/broadcast`, {
version: currentVersion,
data: 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`, {
version: currentVersion,
});
if (res.data.length) {
return res.data[0].balance;
}
return "0";
};
export 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;
};
export const fetchERC20TransactionsWithPages = async (addr, lastHeight) => {
let result = [];
let offset = 0;
let txsLen = txsPerPageLimit;
while (txsLen === txsPerPageLimit) {
const { txs } = await fetchERC20Transactions(addr, lastHeight, offset, txsPerPageLimit);
result = result.concat(txs);
txsLen = txs.length;
offset += txsLen;
}
return result.sort((a, b) => b.timestamp - a.timestamp);
};
//# sourceMappingURL=api.js.map