1

So I am calling one function from my smart contract. I pass the same data, same conditions but the gas estimation in Remix is 0.00126923ETH and gas estimation in Front-end of my application that uses react-moralis has a gas estimation of 0.06131775ETH. This is a HUGE difference in cost, can anyone tell me a way to optimize gas cost in front-end.

Smart Contract function:

function createActivity(
        string memory _id,
        string memory _username,
        string memory _title,
        string memory _desc,
        uint256 _totalTimeInMonths,
        uint256 _price,
        uint256 _level,
        uint256 _maxMembers,
        uint256 dateOfCreation,
        uint256 _waitingPeriodInMonths //DDMMYYYY
    ) public payable {
        require(_price <= minUSD[_level - 1], "ETH limit crossed");
        uint256 id = arrayForLength.length + 1;
        memberAddress.push(payable(msg.sender));
        Activity memory activity = Activity(
            id,
            payable(msg.sender),
            _title,
            _desc,
            _price,
            _level,
            ActivityStatus.OPEN,
            block.timestamp,
            _totalTimeInMonths,
            _maxMembers,
            memberAddress,
            _waitingPeriodInMonths
        );
        Members[msg.sender] = Member(
            _username,
            _totalTimeInMonths,
            dateOfCreation,
            _id,
            block.timestamp
        );
        arrayForLength.push(_id);
        Activities[_id] = activity;
        emit ActivityCreated(
            _id,
            _title,
            _totalTimeInMonths,
            _level,
            dateOfCreation
        );
        delete memberAddress;
    }

react-moralis function call:

const { runContractFunction: createActivity } = useWeb3Contract({
    abi,
    contractAddress: ActivityAddress,
    functionName: "createActivity",
    params: activity
      ? {
          _id: activityId,
          _username: user ? user.username : "",
          _title: activity ? activity.title : null,
          _desc: activity ? activity.description : null,
          _totalTimeInMonths: activity ? activity.durationPeriod : null,
          _price: activity.join_price,
          _level: activity.difficulty_level,
          _maxMembers: activity.member_limit,
          _waitingPeriodInMonths: 1,
          dateOfCreation: parseInt(getNumericDate()),
        }
      : {},
  });
TylerH
  • 20,799
  • 66
  • 75
  • 101
Slowqueso
  • 9
  • 1

1 Answers1

0

Hello the underlying useWeb3Contract code just uses ethers.js to make the transaction.

I'm not sure of the cause of that massive difference in gas estimation between the two environments for the same contract function transaction - make sure you're using the same wallet / network settings (RPC URL). If this is a testnet contract, can you actually make the transactions in both your app and Remix to see what the cost is.

You can use ethers.js directly to test to see if it is the use of react-moralis or ethers.js that is the problem.

If it is a problem with Moralis / react-moralis, please post on the Moralis forum.

Alex
  • 66
  • 1
  • 3