@ledgerhq/coin-stacks
Version:
Ledger Stacks Coin integration
129 lines • 5.31 kB
JavaScript
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.fetchNonce = exports.fetchFullMempoolTxs = exports.fetchMempoolTxs = exports.broadcastTx = exports.fetchFullTxs = exports.fetchTxs = exports.fetchBlockHeight = exports.fetchEstimatedFees = exports.fetchBalances = void 0;
const live_env_1 = require("@ledgerhq/live-env");
const network_1 = __importDefault(require("@ledgerhq/live-network/network"));
const getStacksURL = (path) => {
const baseUrl = (0, live_env_1.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 (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;
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 (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;
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 (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;
return responseData;
};
const fetchBalances = async (addr) => {
const data = await fetch(`/extended/v1/address/${addr}/stx`);
return data; // TODO Validate if the response fits this interface
};
exports.fetchBalances = fetchBalances;
const fetchEstimatedFees = async (request) => {
const feeRate = await send(`/v2/fees/transfer`, request);
return feeRate; // TODO Validate if the response fits this interface
};
exports.fetchEstimatedFees = fetchEstimatedFees;
const fetchBlockHeight = async () => {
const data = await fetch("/extended");
return data; // TODO Validate if the response fits this interface
};
exports.fetchBlockHeight = fetchBlockHeight;
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: [] };
}
};
exports.fetchTxs = fetchTxs;
const fetchFullTxs = async (addr) => {
let qty, offset = 0;
let txs = [];
do {
const { results, total, limit } = await (0, exports.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
};
exports.fetchFullTxs = fetchFullTxs;
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
};
exports.broadcastTx = broadcastTx;
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
};
exports.fetchMempoolTxs = fetchMempoolTxs;
const fetchFullMempoolTxs = async (addr) => {
let qty, offset = 0;
let txs = [];
do {
const { results, total, limit } = await (0, exports.fetchMempoolTxs)(addr, offset);
txs = txs.concat(results);
offset += limit;
qty = total;
} while (offset < qty);
return txs; // TODO Validate if the response fits this interface
};
exports.fetchFullMempoolTxs = fetchFullMempoolTxs;
const fetchNonce = async (addr) => {
const response = await fetch(`/extended/v1/address/${addr}/nonces`);
return response; // TODO Validate if the response fits this interface
};
exports.fetchNonce = fetchNonce;
//# sourceMappingURL=api.js.map
;