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

Allow the owner to burn the synth only if allowed or burning from the synth contract #63

Merged
merged 3 commits into from
Mar 21, 2024
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
3 changes: 2 additions & 1 deletion src/Synths/ESynth.sol
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ contract ESynth is ERC20Collateral, Ownable {
address sender = _msgSender();
MinterData storage minterCache = minters[sender];

if (account != sender && sender != owner()) {
// The allowance check should be performed if the spender is not the account with the exception of the owner burning from this contract.
if (account != sender && !(account == address(this) && sender == owner())) {
_spendAllowance(account, sender, amount);
}

Expand Down
23 changes: 19 additions & 4 deletions test/unit/esynth/ESynthGeneral.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import {ESynth} from "src/Synths/ESynth.sol";
contract ESynthGeneralTest is ESynthTest {
uint128 constant MAX_ALLOWED = type(uint128).max;

error ERC20InsufficientAllowance(address spender, uint256 allowance, uint256 needed);

function testFuzz_mintShouldIncreaseTotalSupplyAndBalance(uint128 amount) public {
amount = uint128(bound(amount, 0, MAX_ALLOWED));
uint256 balanceBefore = esynth.balanceOf(user1);
Expand All @@ -21,10 +23,14 @@ contract ESynthGeneralTest is ESynthTest {
}

function testFuzz_burnShouldDecreaseTotalSupplyAndBalance(uint128 initialAmount, uint128 burnAmount) public {
initialAmount = uint128(bound(initialAmount, 0, MAX_ALLOWED));
initialAmount = uint128(bound(initialAmount, 1, MAX_ALLOWED));
esynth.setCapacity(address(this), MAX_ALLOWED);
esynth.mint(user1, initialAmount);
burnAmount = uint128(bound(burnAmount, 0, initialAmount));
burnAmount = uint128(bound(burnAmount, 1, initialAmount));

vm.expectRevert(abi.encodeWithSelector(ERC20InsufficientAllowance.selector, user2, 0, burnAmount));
vm.prank(user2);
esynth.burn(user1, burnAmount);

vm.prank(user1);
esynth.approve(user2, burnAmount);
Expand Down Expand Up @@ -55,7 +61,7 @@ contract ESynthGeneralTest is ESynthTest {
}

// burn of amount more then minted shoud reset minterCache.minted to 0
function testFuzz_burnMoreThenMinted(uint128 amount) public {
function testFuzz_burnMoreThanMinted(uint128 amount) public {
amount = uint128(bound(amount, 0, MAX_ALLOWED / 2));
// one minter mints
esynth.setCapacity(user2, amount); // we set the cap to less then
Expand All @@ -67,18 +73,27 @@ contract ESynthGeneralTest is ESynthTest {
vm.prank(user1);
esynth.mint(address(esynth), amount);

// the owner of the synth can always burn from synth
esynth.burn(address(esynth), amount * 2);

(, uint128 minted) = esynth.minters(address(this));
assertEq(minted, 0);
}

function testFuzz_burnFromOwner(uint128 amount) public {
amount = uint128(bound(amount, 0, MAX_ALLOWED));
amount = uint128(bound(amount, 1, MAX_ALLOWED));
esynth.setCapacity(user1, MAX_ALLOWED);
vm.prank(user1);
esynth.mint(user1, amount);

// the owner of the synth can always burn from synth but cannot from other accounts without allowance
vm.expectRevert(abi.encodeWithSelector(ERC20InsufficientAllowance.selector, address(this), 0, amount));
esynth.burn(user1, amount);

vm.prank(user1);
esynth.approve(address(this), amount);
esynth.burn(user1, amount);

assertEq(esynth.balanceOf(user1), 0);
}

Expand Down
Loading