1

I try to interact with a contract on my local hardhat network.

Here's how i declare a contract instance:

const account0 = "0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80";
const provider = new ethers.JsonRpcProvider("http://127.0.0.1:8545/", 31337);
const signer = new ethers.Wallet(account0, provider);
const elections = new ethers.Contract(contractAddress, abi, signer);

I then use eth_requestAccounts method:

userAccount = (await window.ethereum.request({ method: 'eth_requestAccounts' }))[0];

But when i do this:

(elections.connect(userAccount)).vote(`${document.getElementById("voteFor").value}`, 1)
// where 'vote' is a 'write' function that takes 2 parameters

I get this error: Error: contract runner does not support sending transactions (operation="sendTransaction", code=UNSUPPORTED_OPERATION, version=6.7.0).

Brief explanation: I want to create a simple website using plain html/js where people could vote. PS: I'm just learning.

Thanks all the kind people in advance!

2
  • You mentioned you're using building this with plain HTML and Javascript. Is your script deferred so that it runs when all external scripts are already loaded? You should make sure userAccount is a non-empty value.
    – Mila A
    Feb 11 at 18:06
  • Also, you're supposed to get the signer like this when testing with Hardhat: ` // Get the signers; accounts 0 through 2 let signer0 = provider.getSigner(0); let signer1 = provider.getSigner(1); let signer2 = provider.getSigner(2); `
    – Mila A
    Feb 11 at 18:15

1 Answer 1

1

When accessing the smart contract on the front-end, calling this line should be fine:

userAccount = (await window.ethereum.request({ method: 'eth_requestAccounts' }))[0];

However, in Hardhat, you're supposed to retrieve the signers like this:

// Get the signers; accounts 0 through 2
let signer0 = provider.getSigner(0);
let signer1 = provider.getSigner(1);
let signer2 = provider.getSigner(2);

Because there's no window object in NodeJS.


(Answer updated based on the new commend)

This code below actually returns an array[string]:

await window.ethereum.request({
  "method": "eth_requestAccounts",
  "params": []
});

If you want to call a write method, you should pass a signer (not just a string) here:

(elections.connect(userAccount)).vote(`${document.getElementById("voteFor").value}`, 1)

This approach above is incorrect and is the culprit of the Error: contract runner does not support sending transactions (operation="sendTransaction", code=UNSUPPORTED_OPERATION, version=6.7.0) error.

So to solve that problem you should connect your elections contract to a real signer like this:

const signer = new ethers.Wallet(account0, provider);
const elections = new ethers.Contract(contractAddress, abi, signer);

await ((elections.connect(signer)).vote(...pass your agruments here));

In a real-world application, you'd retrieve the signer the following way (using ethers.js to interact with Metamask):

const provider = new ethers.BrowserProvider(window.ethereum);
// It will prompt user for account connections if it isnt connected
const signer = await provider.getSigner(); // pass it to the .connect(...) function later

console.log("Account:", await signer.getAddress());
3
  • 1
    The thing is that it's a not NodeJS. As i already said, i try to build a website. I made a smart contract and i deploy it using 'hardhat-deploy' plug-in (that contract gets deployed automatically when i spin up a node using hardhat). I imported a couple of hardhat accounts to my Metamask to try to tinker with that too. But the thing is that I don't know how to make my website to be able to perform 'write' functions to that contract on my local node on a user's behalf. Anyways, thank you for your answer
    – andr0-0
    Feb 12 at 5:07
  • Aah, OK, see the updated solution that I suggest should work in your case. I updated my answer above. ⬆️
    – Mila A
    Feb 12 at 10:34
  • 1
    Thank you, kind guy, it indeed helped!
    – andr0-0
    Feb 12 at 11:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.