Back

blockchain - provider的使用(json rpc ) JsonRpcProvider failed to startup; retry in 1s

发布时间: 2024-03-26 07:22:00

refer to:
https://stackoverflow.com/questions/72986080/how-to-connect-to-custom-provider-using-ethers


https://ethereum.stackexchange.com/questions/144451/typeerror-cannot-read-properties-of-undefined-reading-jsonrpcprovider

https://github.com/ethers-io/ethers.js/discussions/3842
const { ethers, JsonRpcProvider } = require("ethers");
//const {Web3 }= require("web3");

async function bytecodeToAbi(bytecode) {
  try {

    const address = "0x2462EE3624d7C8678865E4b248930325d65697bF";
    const providerUrl = "http://35.89.151.219:8000/7eb0d1b4-551c-4e7e-94a3-433d9658c11c";
// 注意这里, 第三个参数:
// 否则会报错: JsonRpcProvider failed to startup; retry in 1s const provider = new JsonRpcProvider(providerUrl, undefined, { batchMaxCount: 1} ) const contract = new ethers.Contract(address, [], provider); // 从字节码中解析生成合约的 ABI const abi = await contract.interface.parseBytecode(bytecode); return abi; } catch (error) { console.error("Failed to convert bytecode to ABI:", error); throw error; } } // 调用函数并打印结果 const bytecode = "0x6080604052348015600f57600080fd5b506004361060325760003560e01c8063521eb27314603757806364d98f6e146051575b600080fd5b603d6062565b60405160489190608d565b60405180910390f35b60576071565b604051604891906082565b6000546001600160a01b031681565b6000546001600160a01b0316311590565b901515815260200190565b6001600160a01b039190911681526020019056fea2646970667358221220de10fbfb38dd3c11abcc4574a6e3dd40ad0900d9a88dd9bdc8a04e5c6153302964736f6c63430008000033"; // 替换为您的字节码 bytecodeToAbi(bytecode) .then((abi) => { console.log("ABI:", abi); }) .catch((error) => { console.error("Error:", error); });

Back