Skip to content

Commit

Permalink
Optimize gas when depositing or withdrawing
Browse files Browse the repository at this point in the history
  • Loading branch information
jishankai committed Sep 2, 2020
1 parent d76898b commit 7136f54
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions contracts/MasterChef.sol
Original file line number Diff line number Diff line change
@@ -205,10 +205,14 @@ contract MasterChef is Ownable {
updatePool(_pid);
if (user.amount > 0) {
uint256 pending = user.amount.mul(pool.accSushiPerShare).div(1e12).sub(user.rewardDebt);
safeSushiTransfer(msg.sender, pending);
if(pending > 0) {
safeSushiTransfer(msg.sender, pending);
}
}
if(_amount > 0) {
pool.lpToken.safeTransferFrom(address(msg.sender), address(this), _amount);
user.amount = user.amount.add(_amount);
}
pool.lpToken.safeTransferFrom(address(msg.sender), address(this), _amount);
user.amount = user.amount.add(_amount);
user.rewardDebt = user.amount.mul(pool.accSushiPerShare).div(1e12);
emit Deposit(msg.sender, _pid, _amount);
}
@@ -220,10 +224,14 @@ contract MasterChef is Ownable {
require(user.amount >= _amount, "withdraw: not good");
updatePool(_pid);
uint256 pending = user.amount.mul(pool.accSushiPerShare).div(1e12).sub(user.rewardDebt);
safeSushiTransfer(msg.sender, pending);
user.amount = user.amount.sub(_amount);
if(pending > 0) {
safeSushiTransfer(msg.sender, pending);
}
if(_amount > 0) {
user.amount = user.amount.sub(_amount);
pool.lpToken.safeTransfer(address(msg.sender), _amount);
}
user.rewardDebt = user.amount.mul(pool.accSushiPerShare).div(1e12);
pool.lpToken.safeTransfer(address(msg.sender), _amount);
emit Withdraw(msg.sender, _pid, _amount);
}

@@ -252,4 +260,4 @@ contract MasterChef is Ownable {
require(msg.sender == devaddr, "dev: wut?");
devaddr = _devaddr;
}
}
}

0 comments on commit 7136f54

Please sign in to comment.