blockchain-api
Version:
API utilities for interacting with the Exatechl2 blockchain
43 lines (42 loc) • 1.43 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.callRpc = callRpc;
const node_fetch_1 = __importDefault(require("node-fetch"));
const config_1 = require("../config");
/**
* Make an RPC call to the blockchain
* @template T - The expected return type
* @param {string} method - The RPC method to call
* @param {unknown[]} params - The parameters for the RPC method
* @returns {Promise<T>} - A promise that resolves with the result of the RPC call
*/
async function callRpc(method, params = []) {
try {
const rpcUrl = (0, config_1.getRpcUrl)();
const response = await (0, node_fetch_1.default)(rpcUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
jsonrpc: '2.0',
id: 1,
method,
params,
}),
});
const data = await response.json();
if (data.error) {
console.error('RPC Error:', data.error);
throw new Error(data.error.message || 'RPC call failed');
}
return data.result;
}
catch (error) {
console.error('Error calling RPC:', error);
throw error;
}
}