@msquared/etherbase-client
Version:
React hooks for interacting with Etherbase smart contracts
35 lines (34 loc) • 1.36 kB
JavaScript
"use client";
import { useCallback } from "react";
import { useEtherbaseContext } from "../EtherbaseProvider";
import { getConfig } from "../config";
import { WebSocketManager } from "./WebSocketManager";
export default function useEtherbaseContract({ contractAddress, }) {
useEtherbaseContext();
const { httpReaderUrl, wsReaderUrl, wsWriterUrl, useBackend, privateKey } = getConfig();
const emitEventBackend = useCallback(async ({ methodName, args, }) => {
if (!privateKey) {
throw new Error("No private key configured");
}
if (wsWriterUrl) {
WebSocketManager.get().executeContractMethod({
contractAddress,
methodName,
args,
});
}
else {
throw new Error("No backend ws URL configured");
}
}, [privateKey, wsWriterUrl, contractAddress]);
const executeContractMethod = useCallback(async ({ methodName, args, }) => {
if (useBackend) {
console.log("executing contract method backend", methodName, args);
await emitEventBackend({ methodName, args });
}
else {
console.error("executing contract method via browser is not supported yet");
}
}, [useBackend, emitEventBackend]);
return { execute: executeContractMethod };
}