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()),
}
: {},
});