@ledgerhq/coin-stacks
Version:
Ledger Stacks Coin integration
114 lines • 4.45 kB
JavaScript
import { getEnv } from "@ledgerhq/live-env";
import network from "@ledgerhq/live-network/network";
const getStacksURL = (path) => {
const baseUrl = getEnv("API_STACKS_ENDPOINT");
if (!baseUrl)
throw new Error("API base URL not available");
return `${baseUrl}${path ? path : ""}`;
};
const fetch = async (path) => {
const url = getStacksURL(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;
return data;
};
const send = async (path, data) => {
const url = getStacksURL(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;
return responseData;
};
const sendRaw = async (path, data) => {
const url = getStacksURL(path);
const opts = {
method: "POST",
url,
data,
headers: { "Content-Type": "application/octet-stream" },
};
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;
return responseData;
};
export const fetchBalances = async (addr) => {
const data = await fetch(`/extended/v1/address/${addr}/stx`);
return data; // TODO Validate if the response fits this interface
};
export const fetchEstimatedFees = async (request) => {
const feeRate = await send(`/v2/fees/transfer`, request);
return feeRate; // TODO Validate if the response fits this interface
};
export const fetchBlockHeight = async () => {
const data = await fetch("/extended");
return data; // TODO Validate if the response fits this interface
};
export const fetchTxs = async (addr, offset = 0) => {
const limit = 50;
try {
const response = await fetch(`/extended/v2/addresses/${addr}/transactions?offset=${offset}&limit=${limit}`);
return response; // TODO Validate if the response fits this interface
}
catch (e) {
return { limit, offset, total: 0, results: [] };
}
};
export const fetchFullTxs = async (addr) => {
let qty, offset = 0;
let txs = [];
do {
const { results, total, limit } = await fetchTxs(addr, offset);
txs = txs.concat(results.filter(t => {
if (t.tx?.tx_type === "token_transfer") {
return true;
}
if (t.tx?.tx_type === "contract_call" &&
t.tx?.contract_call?.function_name === "send-many") {
return true;
}
return false;
}));
offset += limit;
qty = total;
} while (offset < qty);
return txs; // TODO Validate if the response fits this interface
};
export const broadcastTx = async (message) => {
let response = await sendRaw(`/v2/transactions`, message);
if (response != "")
response = `0x${response}`;
return response; // TODO Validate if the response fits this interface
};
export const fetchMempoolTxs = async (addr, offset = 0) => {
const response = await fetch(`/extended/v1/tx/mempool?sender_address=${addr}&offset=${offset}`);
return response; // TODO Validate if the response fits this interface
};
export const fetchFullMempoolTxs = async (addr) => {
let qty, offset = 0;
let txs = [];
do {
const { results, total, limit } = await fetchMempoolTxs(addr, offset);
txs = txs.concat(results);
offset += limit;
qty = total;
} while (offset < qty);
return txs; // TODO Validate if the response fits this interface
};
export const fetchNonce = async (addr) => {
const response = await fetch(`/extended/v1/address/${addr}/nonces`);
return response; // TODO Validate if the response fits this interface
};
//# sourceMappingURL=api.js.map