Search code examples
javascriptsmartcontracts

Trying to interact with a contract using is function selector, how do i get the block hash?


I have the below code in a index.js file, but when i run it i get an error message: The method eth_sendTransaction does not exist/is not available. Please can anyone help?

require('dotenv').config();
require('events').EventEmitter.defaultMaxListeners = 0

const { ethers } = require('ethers');

// Provider and contract address
const provider = new ethers.providers.JsonRpcProvider('https://polygon-mumbai.infura.io/v3/7d803b173d114ba8a1bffafff7ff541a');
const wallet = new ethers.Wallet(process.env.KEY, provider)
//const signer = wallet.provider.getSigner(wallet.address);
const contractAddress = '0x57C98f1f2BC34A0054CBc1257fcc9333c1b6730c';

// Function selector
const functionSelector = '0x838ad0ee';


// Call the contract
const send = async () => {
  try {
    const result = await wallet.call({
        to: contractAddress,
        data: functionSelector
    });
    console.log("Result:", result);
} catch (error) {
    console.error("Error:", error);
}
}

send()

Solution

  • const result = await wallet.call({
        to: contractAddress,
        data: functionSelector
    });
    

    The code produces and output but it does not record the transaction on the Mumbai TestNet explorer

    Calls are read-only actions that are not recorded on the chain. This way, you can retrieve data from the chain without paying transaction fees, but you can't modify any state with a call.

    If you want to send a (read-write) transaction that will be stored on the chain, one of the ways to do it is to invoke the wallet.sendTransaction() method (Wallet type inherits from the Signer type).