Skip to content

Commit

Permalink
Add events and fix emergency withdraw bug
Browse files Browse the repository at this point in the history
  • Loading branch information
chefnomi committed Aug 26, 2020
1 parent 5a4fa05 commit 3ccb11d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
9 changes: 8 additions & 1 deletion contracts/MasterChef.sol
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ contract MasterChef is Ownable {
// The block number when SUSHI mining starts.
uint256 public startBlock;

event Deposit(address indexed user, uint256 indexed pid, uint256 amount);
event Withdraw(address indexed user, uint256 indexed pid, uint256 amount);
event EmergencyWithdraw(address indexed user, uint256 indexed pid, uint256 amount);

constructor(
SushiToken _sushi,
address _devaddr,
Expand Down Expand Up @@ -206,6 +210,7 @@ contract MasterChef is Ownable {
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);
}

// Withdraw LP tokens from MasterChef.
Expand All @@ -219,15 +224,17 @@ contract MasterChef is Ownable {
user.amount = user.amount.sub(_amount);
user.rewardDebt = user.amount.mul(pool.accSushiPerShare).div(1e12);
pool.lpToken.safeTransfer(address(msg.sender), _amount);
emit Withdraw(msg.sender, _pid, _amount);
}

// Withdraw without caring about rewards. EMERGENCY ONLY.
function emergencyWithdraw(uint256 _pid) public {
PoolInfo storage pool = poolInfo[_pid];
UserInfo storage user = userInfo[_pid][msg.sender];
pool.lpToken.safeTransfer(address(msg.sender), user.amount);
emit EmergencyWithdraw(msg.sender, _pid, user.amount);
user.amount = 0;
user.rewardDebt = 0;
pool.lpToken.safeTransfer(address(msg.sender), user.amount);
}

// Safe sushi transfer function, just in case if rounding error causes pool to not have enough SUSHIs.
Expand Down
11 changes: 11 additions & 0 deletions test/AMasterChef.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,17 @@ contract('MasterChef', ([alice, bob, carol, dev, minter]) => {
await this.lp2.transfer(carol, '1000', { from: minter });
});

it('should allow emergency withdraw', async () => {
// 100 per block farming rate starting at block 100 with bonus until block 1000
this.chef = await MasterChef.new(this.sushi.address, dev, '100', '100', '1000', { from: alice });
await this.chef.add('100', this.lp.address, true);
await this.lp.approve(this.chef.address, '1000', { from: bob });
await this.chef.deposit(0, '100', { from: bob });
assert.equal((await this.lp.balanceOf(bob)).valueOf(), '900');
await this.chef.emergencyWithdraw(0, { from: bob });
assert.equal((await this.lp.balanceOf(bob)).valueOf(), '1000');
});

it('should give out SUSHIs only after farming time', async () => {
// 100 per block farming rate starting at block 100 with bonus until block 1000
this.chef = await MasterChef.new(this.sushi.address, dev, '100', '100', '1000', { from: alice });
Expand Down

0 comments on commit 3ccb11d

Please sign in to comment.