[英]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.在 ethers.js 中,提供者允许您从区块链中查询数据。 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).
因此,他们可以签署修改区块链 state 的交易(您在区块链上存储或更改信息的交易)。
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.
因为投票方法修改了区块链上的 state,所以你得到了你得到的错误。
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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.