@ledgerhq/coin-casper
Version:
Ledger Casper integration
121 lines • 4.62 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.broadcastTx = exports.fetchTxs = exports.fetchBlockHeight = exports.fetchBalance = exports.fetchAccountStateInfo = exports.getCasperNodeRpcClient = void 0;
const logs_1 = require("@ledgerhq/logs");
const live_network_1 = __importDefault(require("@ledgerhq/live-network"));
const casper_js_sdk_1 = require("casper-js-sdk");
const config_1 = require("../config");
const bignumber_js_1 = __importDefault(require("bignumber.js"));
const consts_1 = require("../consts");
const getCasperIndexerURL = (path) => {
const baseUrl = (0, config_1.getCoinConfig)().infra.API_CASPER_INDEXER;
if (!baseUrl)
throw new Error("API base URL not available");
return new URL(path, baseUrl).toString();
};
const getCasperNodeURL = () => {
const baseUrl = (0, config_1.getCoinConfig)().infra.API_CASPER_NODE_ENDPOINT;
if (!baseUrl)
throw new Error("API base URL not available");
return baseUrl;
};
const getCasperNodeRpcClient = () => {
const url = getCasperNodeURL();
const handler = new casper_js_sdk_1.HttpHandler(url);
return new casper_js_sdk_1.RpcClient(handler);
};
exports.getCasperNodeRpcClient = getCasperNodeRpcClient;
const casperIndexerWrapper = async (path) => {
const url = getCasperIndexerURL(path);
try {
const rawResponse = await (0, live_network_1.default)({
method: "GET",
url,
});
(0, logs_1.log)("http", url);
const { data, status } = rawResponse;
if (status >= 300) {
(0, logs_1.log)("http", url, data);
}
return data;
}
catch (error) {
(0, logs_1.log)("error", "Casper indexer error: ", error);
throw error;
}
};
const fetchAccountStateInfo = async (publicKey) => {
const client = (0, exports.getCasperNodeRpcClient)();
try {
const { account } = await client.getAccountInfo(null, new casper_js_sdk_1.AccountIdentifier(undefined, casper_js_sdk_1.PublicKey.fromHex(publicKey)));
const accountHash = account.accountHash.toHex();
const purseURefString = account.mainPurse.toPrefixedString();
return { purseUref: purseURefString, accountHash };
}
catch (error) {
if (error instanceof Error &&
[consts_1.NodeErrorCodeAccountNotFound, consts_1.NodeErrorCodeQueryFailed].includes(error.statusCode)) {
return {
purseUref: undefined,
accountHash: undefined,
};
}
throw error;
}
};
exports.fetchAccountStateInfo = fetchAccountStateInfo;
const fetchBalance = async (purseUref) => {
const client = (0, exports.getCasperNodeRpcClient)();
try {
const { stateRootHash } = await client.getStateRootHashLatest();
const balance = await client.getBalanceByStateRootHash(purseUref, stateRootHash.toHex());
return new bignumber_js_1.default(balance.balanceValue.toString());
}
catch (error) {
(0, logs_1.log)("error", "Failed to fetch balance", error);
throw error;
}
};
exports.fetchBalance = fetchBalance;
const fetchBlockHeight = async () => {
const client = (0, exports.getCasperNodeRpcClient)();
try {
const latestBlock = await client.getLatestBlock();
return latestBlock.block.height;
}
catch (error) {
(0, logs_1.log)("error", "Failed to fetch block height", error);
throw error;
}
};
exports.fetchBlockHeight = fetchBlockHeight;
const fetchTxs = async (addr) => {
let page = 1;
let res = [];
const limit = 100;
let response = await casperIndexerWrapper(`accounts/${addr}/ledgerlive-deploys?limit=${limit}&page=${page}`);
res = res.concat(response.data);
while (response.pageCount > page) {
page++;
response = await casperIndexerWrapper(`accounts/${addr}/ledgerlive-deploys?limit=${limit}&page=${page}`);
res = res.concat(response.data);
}
return res;
};
exports.fetchTxs = fetchTxs;
const broadcastTx = async (transaction) => {
const client = (0, exports.getCasperNodeRpcClient)();
try {
const response = await client.putTransaction(transaction);
return response.transactionHash.toHex();
}
catch (error) {
(0, logs_1.log)("error", "Failed to broadcast transaction", error);
throw error;
}
};
exports.broadcastTx = broadcastTx;
//# sourceMappingURL=index.js.map