UNPKG

archethic

Version:
51 lines 1.88 kB
import fetch from "cross-fetch"; import { JSONRPCClient } from "json-rpc-2.0"; export class NodeRPCClient { client; core; constructor(archethic) { this.core = archethic; this.client = new JSONRPCClient((request) => this.handleRequest(request)); } async callFunction(contractAddress, functionName, args) { return this.client.request("contract_fun", { contract: contractAddress, function: functionName, args }); } async getTransactionFee(tx) { return this.client.request("estimate_transaction_fee", { transaction: tx.toNodeRPC() }); } async sendTransaction(tx) { return this.client.request("send_transaction", { transaction: tx.toNodeRPC() }); } async addOriginKey(origin) { return this.client.request("add_origin_key", origin); } async simulateContractExecution(tx) { return this.client.request("simulate_contract_execution", { transaction: tx.toNodeRPC() }); } async handleRequest(jsonRPCRequest) { return this.core.requestNode(async (endpoint) => { const url = new URL("/api/rpc", endpoint); return fetch(url, { method: "POST", headers: { "content-type": "application/json", }, body: JSON.stringify(jsonRPCRequest), }).then((response) => { if (response.status === 200) { return response .json() .then((jsonRPCResponse) => this.client.receive(jsonRPCResponse)); } else if (jsonRPCRequest.id !== undefined) { return Promise.reject(new Error(response.statusText)); } }); }); } } //# sourceMappingURL=node_rpc.js.map