I have the following stake function that whenever I call it uses almost 100.000 gas. I did the usual stuff optimizing the code to use less gas like using struct
s and uint256
, but the gas price is still abnormally high.
Gas:101721 Transaction cost:101721
function stake(uint256 amount) external updateReward(_msgSender()) {
require(amount > 0, "Cannot stake 0");
totalStakeAmount = totalStakeAmount.add(amount);
User storage _user = userInfo[_msgSender()];
_user.stakeAmount = _user.stakeAmount.add(amount);
emit Staked(_msgSender(), amount);
}
function unstake(uint256 amount) public updateReward(_msgSender()) {
require(amount > 0, "Cannot withdraw 0");
User storage _user = userInfo[_msgSender()];
totalStakeAmount = totalStakeAmount.sub(amount);
_user.stakeAmount = _user.stakeAmount.sub(amount);
IERC20(stakeToken).safeTransfer(_msgSender(), amount);
emit Unstaked(_msgSender(), amount);
}
struct User { uint256 stakeAmount; uint256 rewards; uint256 userRewardPerTokenPaid; }
updateReward
do?