1

first of all here are the codes

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

here the smart contract code( I tried using remix to find the error but everything is fine there, I can perfectly interact with the contract.This makes me think that the error might be in the main.js file,the one above)

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

and here is the error that I ge in the error( I get an error in metamask too"ALERT: Transaction Error. Exception thrown in contract code" and I can't send funds to the contract address directly, If this last information helps you)

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\"}}}}'" }


as you can see the contract is a simple coin toss contact, you can deduct that I am an absolute beginner in this area

1
1

I had same errors and I solved it now by raising the gas limit when sending transaction!

I recommend you raise your gas limit and try it again.

0

ONe thing that maybe causing this issue

The front sends 1 ether

contractInstance.methods.betxc(x).send({value: web3.utils.toWei("1", "ether")})

and the contract when winning send twice that amount

msg.sender.transfer(msg.value*2);

For the transfer to succeed the contract needs to have a balance greater than 2 ether.

Note: block.timestamp isn't a good source of randomness.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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