Web3js
與智能合約互動時出現錯誤
首先這裡是程式碼
var web3 = new Web3(Web3.givenProvider); var contractInstance; var x = 0 $(document).ready(function() { window.ethereum.enable().then(function(accounts){ contractInstance = new web3.eth.Contract(window.abi, "0xe6f4FDF7110B0412183F46896666CB55fab1d85A", {from: accounts[0]}); }); $("#get_data_button").click(fetchAndDisplay); }); function fetchAndDisplay(){ contractInstance.methods.betxc(x).send({value: web3.utils.toWei("1", "ether")}).then(function(res){ if(res==0) console.log("you won!!!!!") else { console.log("you lost") } }) };
這裡是智能合約程式碼(我嘗試使用 remix 來查找錯誤,但那裡一切正常,我可以完美地與合約互動。這讓我認為錯誤可能在 main.js 文件中,即上面的那個)
pragma solidity >=0.4.22 <0.9.0; contract cointoss { event Won(string); event Def(string); uint monysent; uint tot; modifier costs(uint cost){ require(msg.value >= cost); _; } string[] public data; function random() public view returns(uint){ return block.timestamp % 2; } function betxc(uint numb) payable public costs(0.01 ether) returns(uint){ uint max= random(); tot = tot + msg.value; if (max==numb){ msg.sender.transfer(msg.value*2); tot -= msg.value*2; emit Won("somebody won"); } emit Def("the contact interaction finished"); return max; } }
這是我在錯誤中遇到的錯誤(我在元遮罩中也出現錯誤“ALERT:交易錯誤。合約程式碼中引發異常”,我無法直接將資金發送到合約地址,如果最後的資訊對您有幫助)
MetaMask - RPC Error: Error: [ethjs-query] while formatting outputs from RPC '{"value":{"code":-32603,"data":{"message":"VM Exception while processing transaction: revert","code":-32000,"data":{"0xccb22b990e1c56d2f45ad4ada4143c8ea721c924e875cd62601313d7402d3ea8":{"error":"revert","program_counter":46,"return":"0x"},"stack":"RuntimeError: VM Exception while processing transaction: revert\n at Function.RuntimeError.fromResults (/tmp/.mount_ganachYMHaCq/resources/static/node/node_modules/ganache-core/lib/utils/runtimeerror.js:94:13)\n at BlockchainDouble.processBlock (/tmp/.mount_ganachYMHaCq/resources/static/node/node_modules/ganache-core/lib/blockchain_double.js:627:24)\n at runMicrotasks (<anonymous>)\n at processTicksAndRejections (internal/process/task_queues.js:93:5)","name":"RuntimeError"}}}}' Object { code: -32603, message: "Error: [ethjs-query] while formatting outputs from RPC '{\"value\":{\"code\":-32603,\"data\":{\"message\":\"VM Exception while processing transaction: revert\",\"code\":-32000,\"data\":{\"0xccb22b990e1c56d2f45ad4ada4143c8ea721c924e875cd62601313d7402d3ea8\":{\"error\":\"revert\",\"program_counter\":46,\"return\":\"0x\"},\"stack\":\"RuntimeError: VM Exception while processing transaction: revert\\n at Function.RuntimeError.fromResults (/tmp/.mount_ganachYMHaCq/resources/static/node/node_modules/ganache-core/lib/utils/runtimeerror.js:94:13)\\n at BlockchainDouble.processBlock (/tmp/.mount_ganachYMHaCq/resources/static/node/node_modules/ganache-core/lib/blockchain_double.js:627:24)\\n at runMicrotasks (<anonymous>)\\n at processTicksAndRejections (internal/process/task_queues.js:93:5)\",\"name\":\"RuntimeError\"}}}}'" } inpage.js:1:24326 Uncaught (in promise) Object { code: -32603, message: "Error: [ethjs-query] while formatting outputs from RPC '{\"value\":{\"code\":-32603,\"data\":{\"message\":\"VM Exception while processing transaction: revert\",\"code\":-32000,\"data\":{\"0xccb22b990e1c56d2f45ad4ada4143c8ea721c924e875cd62601313d7402d3ea8\":{\"error\":\"revert\",\"program_counter\":46,\"return\":\"0x\"},\"stack\":\"RuntimeError: VM Exception while processing transaction: revert\\n at Function.RuntimeError.fromResults (/tmp/.mount_ganachYMHaCq/resources/static/node/node_modules/ganache-core/lib/utils/runtimeerror.js:94:13)\\n at BlockchainDouble.processBlock (/tmp/.mount_ganachYMHaCq/resources/static/node/node_modules/ganache-core/lib/blockchain_double.js:627:24)\\n at runMicrotasks (<anonymous>)\\n at processTicksAndRejections (internal/process/task_queues.js:93:5)\",\"name\":\"RuntimeError\"}}}}'" }
如您所見,合約是一個簡單的拋硬幣接觸,您可以推斷我是這方面的絕對初學者
我有同樣的錯誤,我現在通過在發送交易時提高氣體限制來解決它!
我建議你提高你的氣體限制,然後再試一次。
可能導致此問題的一件事
前端發送 1 個乙太幣
contractInstance.methods.betxc(x).send({value: web3.utils.toWei("1", "ether")})
和獲勝時的契約發送兩倍的金額
msg.sender.transfer(msg.value*2);
為了使轉賬成功,合約需要有大於 2 個乙太幣的餘額。
注意:
block.timestamp
不是隨機性的好來源。