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

Rajan Lagah
  • 113
  • 5
  • Which function causes the error? – Ismael May 20 '22 at 01:31
  • addUser causes the error ( I believe all functions but I am stuck as addUser is starting point ) – Rajan Lagah May 20 '22 at 04: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 May 20 '22 at 05:04
  • I have not set gas to anything.Full Code is here https://github.com/rajanlagah/CRUD_smart_contract. So you are saying to set estimateGas for function ? – Rajan Lagah May 20 '22 at 05: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 May 20 '22 at 15:06
  • @Ismael lol. Yea in start basics are too different but finally solved all the bugs and my first app is live now. LOL – Rajan Lagah May 21 '22 at 14:48

1 Answers1

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

Prosperity
  • 195
  • 7