0
pragma solidity^0.4.18;

contract project3{

address owner;

struct Users{
string companyName;
string role; 
string country; 
string city;
uint phoneNo;
string physicalAddress;}

constructor (uint _tokens)public{
owner=msg.sender;
}

modifier only_admin{
require(msg.sender==owner);
_;}

mapping(address=>Users)users;

address[] private stakeholders;

function addUsers(address _address, string _companyName,string _role, string 
_country,
string _city, uint _phoneNo, string _physicalAddress)only_admin public{
var user= users[_address];
user.companyName=_companyName;
user.role=_role;
user.country=_country;
user.city=_city;
user.phoneNo=_phoneNo;
user.physicalAddress=_physicalAddress;
stakeholders.push(_address);}

function getUser(address _address)public constant 
returns(string,string,string,string,uint,string){
return(users[_address].companyName,
users[_address].role,users[_address].country,
users[_address].city,users[_address].phoneNo,
users[_address].physicalAddress);}
}

Above is my smart contract on the remix IDE, i have installed web3.js and are able to connect to the smart contract. I also created a user interface to interact with the smart contract. I was always get: Uncaught error: VM exception while processing transaction:out of gas, whenever im using web3 but its fine on EVM. i am hoping you can help me decrease the gas cost for this contract tell me how i can fix this error. Your help would be highly appreciated.

0

Your contract does not seem complex enough to run out of gas.

Perhaps increase the values in truffle.js or truffle-config.js and make sure your gas limits are sufficiently high enough based on the current limits.

In general, in order to optimize contracts you eliminate what is expensive, mappings are good because they don't cost a lot of gas to deploy, you could change getUser to external instead of public that will save some gas, again change addUsers to external as well and remove your strings, strings are expensive.

pragma solidity^0.4.18;

contract project3{

address owner;

struct Users{
bytes32 companyName;
bytes32 role; 
bytes32 country; 
bytes32 city;
uint phoneNo;
bytes32 physicalAddress;}

constructor (uint _tokens)public{
owner=msg.sender;
}

modifier only_admin{
require(msg.sender==owner);
_;}

mapping(address=>Users)users;

address[] private stakeholders;

function addUsers(address _address, bytes32 _companyName,bytes32 _role, bytes32 
_country,
bytes32 _city, uint _phoneNo, bytes32 _physicalAddress)only_admin external{
var user= users[_address];
user.companyName=_companyName;
user.role=_role;
user.country=_country;
user.city=_city;
user.phoneNo=_phoneNo;
user.physicalAddress=_physicalAddress;
stakeholders.push(_address);}

function getUser(address _address)external constant 
returns(bytes32,bytes32,bytes32,bytes32,uint,bytes32){
return(users[_address].companyName,
users[_address].role,users[_address].country,
users[_address].city,users[_address].phoneNo,
users[_address].physicalAddress);}
}

Disclaimer: didn't test the code above, but that's the rough idea, also another way to further reduce gas costs is to manually get an user struct instead of writing your own getters.

Could be like How to return array from struct using drizzle?.

  • It worked. Thank you. I just had to raised the gas limit on solidity and specify how much gas my function can use. – Lonwabo Zaula Nov 5 '18 at 17:51

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.