UNPKG

starknet-devnet

Version:
48 lines (47 loc) 1.61 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.RpcProvider = void 0; const axios_1 = require("axios"); const types_1 = require("./types"); class RpcProvider { constructor(httpProvider, url) { this.httpProvider = httpProvider; this.url = url; } // eslint-disable-next-line @typescript-eslint/no-explicit-any async sendRequest(method, params = {}) { // providing a string may be of use in case a bigint needs to be preserved const paramsSerialized = typeof params === "string" ? params : JSON.stringify(params); const jsonRpcBodySerialized = `{ "jsonrpc": "2.0", "id": "1", "method": "${method}", "params": ${paramsSerialized} }`; return this.httpProvider .post(this.url, jsonRpcBodySerialized, { headers: { "Content-Type": "application/json", }, }) .then((resp) => { if ("result" in resp.data) { return resp.data["result"]; } else if ("error" in resp.data) { // not wrapping in new Error to avoid printing as [Object object] throw resp.data["error"]; } else { throw resp.data; } }) .catch((err) => { if (err.code === axios_1.AxiosError.ECONNABORTED) { throw types_1.DevnetProviderError.fromAxiosError(err); } throw err; }); } } exports.RpcProvider = RpcProvider;