@ledgerhq/coin-internet_computer
Version:
Ledger Internet Computer integration
80 lines • 3.03 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.constructionInvoke = exports.fetchTxns = exports.fetchBalances = exports.fetchBlockHeight = exports.getICPRosettaNetworkIdentifier = void 0;
const logs_1 = require("@ledgerhq/logs");
const live_env_1 = require("@ledgerhq/live-env");
const network_1 = __importDefault(require("@ledgerhq/live-network/network"));
const consts_1 = require("../consts");
const getICPURL = (path) => {
const baseUrl = (0, live_env_1.getEnv)("API_ICP_ENDPOINT");
if (!baseUrl)
throw new Error("API base URL not available");
return `${baseUrl}${path ? path : ""}`;
};
const ICPFetchWrapper = async (path, body) => {
const url = getICPURL(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: "POST",
data: body,
url,
};
const rawResponse = await (0, network_1.default)(opts);
if (rawResponse && rawResponse.data && rawResponse.data.details?.error_message) {
(0, logs_1.log)("error", rawResponse.data.details?.error_message);
}
// 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 getICPRosettaNetworkIdentifier = () => {
return {
network_identifier: {
blockchain: consts_1.ICP_BLK_NAME_ROSETTA,
network: consts_1.ICP_NET_ID_ROSETTA,
},
};
};
exports.getICPRosettaNetworkIdentifier = getICPRosettaNetworkIdentifier;
const fetchBlockHeight = async () => {
const data = await ICPFetchWrapper("network/status", (0, exports.getICPRosettaNetworkIdentifier)());
return data;
};
exports.fetchBlockHeight = fetchBlockHeight;
const fetchBalances = async (accountId) => {
const body = {
...(0, exports.getICPRosettaNetworkIdentifier)(),
account_identifier: {
address: accountId,
metadata: {},
},
};
const data = await ICPFetchWrapper("account/balance", body);
return data;
};
exports.fetchBalances = fetchBalances;
const fetchTxns = async (accountId) => {
const body = {
...(0, exports.getICPRosettaNetworkIdentifier)(),
account_identifier: {
address: accountId,
metadata: {},
},
};
const data = await ICPFetchWrapper("search/transactions", body);
return data;
};
exports.fetchTxns = fetchTxns;
const constructionInvoke = async (opts, method) => {
const body = {
...opts,
};
const data = await ICPFetchWrapper(`construction/${method}`, body);
return data;
};
exports.constructionInvoke = constructionInvoke;
//# sourceMappingURL=api.js.map