0

My contract

pragma solidity ^0.8.0;

contract CRUD {
 uint public maxId = 1; 
 struct User {
  uint id;
  string name;
 }

 User[] public users;

    // get index for id
    function getUserIndex(uint _id) view internal returns(uint index){
        for( uint i = 0; i < users.length ; i++){
            if(users[i].id == _id){
                return i;
            }
        }
        revert('User does not exist');
    }
    // get user 
    function getUser(uint _id) view public returns(uint, string memory){
        uint userIndex = getUserIndex(_id);
        return (users[userIndex].id,users[userIndex].name);
    }
   // get all user 
    function getAll() view public returns(User[] memory){
        return users;
    }

    // add
    function addUser(string memory _name) public returns(uint){
        users.push(User(maxId,_name));
        maxId++;
        return users.length;
    }
}

Now when I am trying to test this on remix its all working but with web UI it's throwing err

Returned error: VM Exception while processing transaction: out of gas

const initWeb3 = () => {
  console.log(window)
    return new Promise((resolve, reject) => {
    if(typeof window.ethereum !== 'undefined') {
      const web3 = new window.Web3(window.ethereum);
      window.ethereum.enable()
        .then(() => resolve(new window.Web3(window.ethereum)))
        .catch(e => reject(e));
      return;
    }
    if(typeof window.web3 !== 'undefined') {
      return resolve(
        new Web3(window.web3.currentProvider)
      );
    }
        resolve(new Web3('http://127.0.0.1:7545'))
    })
}
const initContract = () => {
    return new web3.eth.Contract(ABI,ADDRESS)
}
const getSetAccounts = () => {
  web3.eth.getAccounts()
    .then( _accounts => accounts = _accounts )
}

addUserBtn.addEventListener('click',async (e)=>{
  e.preventDefault()
  const inputUserName = usernameInput.value
  console.log(await web3.eth.getBalance(accounts[0]))
  crud.methods.addUser(inputUserName)
  .send({from: accounts[0]})
    .then( res => console.log("res",res))
    .catch(e => console.log(e.message))
})

I can see balance is 99 eth. Any idea what went wrong? I am opening HTML file so its connecting to ganace instance

6
  • Which function causes the error?
    – Ismael
    Commented May 20, 2022 at 1:31
  • addUser causes the error ( I believe all functions but I am stuck as addUser is starting point ) Commented May 20, 2022 at 4:29
  • I don't see an error in the code. Did you try setting the gas to some arbitrary value like "1000000" or something like that? If that work you may want to try using estimateGas.
    – Ismael
    Commented May 20, 2022 at 5:04
  • I have not set gas to anything.Full Code is here github.com/rajanlagah/CRUD_smart_contract. So you are saying to set estimateGas for function ? Commented May 20, 2022 at 5:20
  • Remix has a default gas limit of 3M. I don't know recent web3 version but it used to have a low gas limit. That was my very first issue with web3: to set the correct gas limit.
    – Ismael
    Commented May 20, 2022 at 15:06

1 Answer 1

1

this should help:

addUserBtn.addEventListener('click',async (e)=>{
  e.preventDefault()
  const inputUserName = usernameInput.value
  console.log(await web3.eth.getBalance(accounts[0]))
  await crud.methods.addUser(inputUserName)
  .send({
   from: accounts[0],
   gas: 3000000,
   gasPrice: web3.toWei(10, 'gwei')
   })
    .then( res => console.log("res",res))
    .catch(e => console.log(e.message))
})

for more details : Error: VM Exception while processing transaction: out of gas

3
  • Hmm. It is not throwing this error but also not adding to the blockchain. If I add user and then get user its returning null Commented May 20, 2022 at 10:53
  • just edited the answer and add await cause it an async function
    – Prosperity
    Commented May 21, 2022 at 12:24
  • It has solved this issue. Thankyou Commented May 21, 2022 at 14:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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