Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add burnFrom burning function #41

Merged
merged 1 commit into from
Mar 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/interfaces/IBurnableERC20.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ pragma solidity 0.8.15;

interface IBurnableERC20 {
function burn(uint256 amount) external;
function burnFrom(address user, uint256 amount) external;
}
15 changes: 14 additions & 1 deletion src/token/ERC20MintBurn.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
pragma solidity 0.8.15;

import "../utils/Ownable.sol";
import "../interfaces/IMintableERC20.sol";
import "./BaseERC20.sol";
import "../interfaces/IMintableERC20.sol";
import "../interfaces/IBurnableERC20.sol";
Expand Down Expand Up @@ -58,4 +57,18 @@ abstract contract ERC20MintBurn is IMintableERC20, IBurnableERC20, Ownable, Base

_burn(msg.sender, _value);
}

/**
* @dev Burns pre-approved tokens from the other address.
* Callable only by one of the burner addresses.
* @param _from account to burn tokens from.
* @param _value amount of tokens to burn. Should be less than or equal to account balance.
*/
function burnFrom(address _from, uint256 _value) external virtual {
require(isBurner(msg.sender), "ERC20MintBurn: not a burner");

_spendAllowance(_from, msg.sender, _value);

_burn(_from, _value);
}
}
24 changes: 24 additions & 0 deletions test/BobToken.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,30 @@ contract BobTokenTest is Test, EIP2470Test {
assertEq(bob.balanceOf(user2), 0 ether);
}

function testBurnFrom() public {
vm.prank(user1);
bob.mint(user1, 1 ether);

vm.expectRevert("ERC20MintBurn: not a burner");
bob.burnFrom(user1, 1 ether);

vm.prank(user2);
vm.expectRevert("ERC20: insufficient allowance");
bob.burnFrom(user1, 1 ether);

vm.prank(user1);
bob.approve(user2, 10 ether);

vm.prank(user2);
vm.expectEmit(true, true, false, true);
emit Transfer(user1, address(0), 1 ether);
bob.burnFrom(user1, 1 ether);

assertEq(bob.totalSupply(), 0);
assertEq(bob.balanceOf(user1), 0);
assertEq(bob.allowance(user1, user2), 9 ether);
}

function testMinterChange() public {
vm.expectRevert("Ownable: caller is not the owner");
bob.updateMinter(user3, true, true);
Expand Down