简体   繁体   中英

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

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM