Rajendra Bisoi
Rajendra Bisoi

Reputation: 29

Uncaught (in promise) Error: sending a transaction requires a signer

Hey I am getting this error

                                      `Uncaught (in promise) Error: sending a transaction requires a signer (operation="sendTransaction", code=UNSUPPORTED_OPERATION, version=contracts/5.2.0)
at Logger.makeError (ethers-5.2.umd.min.js:1:59669)
at Logger.throwError (ethers-5.2.umd.min.js:1:59874)
at Contract.<anonymous> (ethers-5.2.umd.min.js:1:312289)
at step (ethers-5.2.umd.min.js:1:305047)
at Object.next (ethers-5.2.umd.min.js:1:304307)
at ethers-5.2.umd.min.js:1:303953
at new Promise (<anonymous>)
at __awaiter (ethers-5.2.umd.min.js:1:303591)
at Contract.<anonymous> (ethers-5.2.umd.min.js:1:312138)
at vote (index.html:338:34)`

On this code

function vote(){ provider = new ethers.providers.Web3Provider(window.ethereum); signer = provider.getSigner(0); const contract = new ethers.Contract("0xF1bFB2277C269DC90D8726DDf60A680aeffA2AbF", abi, provider); console.log("workin"); var propval = document.getElementById("select").value; var castvote = contract.vote(propval); castvote.then(function(){ document.getElementById("mp").innerHTML = transaction; }) }

Anyone pls help me in fixing this

Upvotes: 1

Views: 5189

Answers (1)

Xavier Hamel
Xavier Hamel

Reputation: 564

In ethers.js, providers allow you to query data from the blockchain. They represent the way you connect to the blockchain. With them you can only call view methods on contracts and get data from those contract.

Signers are providers but with access to an ethereum account. Therefore, they can sign transaction that modify the state of the blockchain (Transaction where you store or change information on the blockchain).

When you instantiate your contract you pass a provider and not a signer. So, on this contract, you should only be able to call view methods. Because the vote method modify the state on the blockchain, you get the error that you get.

To resolve this the only change you need to do is to pass the signer instead of your provider in your contract instanciation:

const contract = new ethers.Contract("0xF1bFB2277C269DC90D8726DDf60A680aeffA2AbF", abi, signer);

Upvotes: 3

Related Questions