From 4c45d5a1854dd14fd0c1fc8c2a0ad1ac0222646e Mon Sep 17 00:00:00 2001 From: Ivan Zhelyazkov Date: Thu, 16 Nov 2023 12:02:33 +0200 Subject: [PATCH] carbon pol eth conversion - burn bnt directly --- contracts/helpers/TestBNT.sol | 12 ++++++++++++ contracts/pol/CarbonPOL.sol | 4 ++-- test/forge/CarbonPOL.t.sol | 15 +++++++++------ 3 files changed, 23 insertions(+), 8 deletions(-) diff --git a/contracts/helpers/TestBNT.sol b/contracts/helpers/TestBNT.sol index 14172ff6..031f509d 100644 --- a/contracts/helpers/TestBNT.sol +++ b/contracts/helpers/TestBNT.sol @@ -29,6 +29,18 @@ contract TestBNT is TestERC20Token { return true; } + function transferFrom(address _from, address _to, uint256 _value) public override(ERC20) returns (bool success) { + assert(super.transferFrom(_from, _to, _value)); + + // transferring to the contract address destroys tokens + if (_to == address(this)) { + _burn(address(this), _value); + emit Destruction(_value); + } + + return true; + } + function issue(address recipient, uint256 amount) external { _mint(recipient, amount); } diff --git a/contracts/pol/CarbonPOL.sol b/contracts/pol/CarbonPOL.sol index 0e374c1d..9311a959 100644 --- a/contracts/pol/CarbonPOL.sol +++ b/contracts/pol/CarbonPOL.sol @@ -328,8 +328,8 @@ contract CarbonPOL is ICarbonPOL, Upgradeable, ReentrancyGuardUpgradeable, Utils if (ethAmountRequired == 0) { revert InvalidTrade(); } - // transfer the tokens from the user - _bnt.safeTransferFrom(msg.sender, address(this), amount); + // transfer the tokens from the user to the bnt address (burn them directly) + _bnt.safeTransferFrom(msg.sender, Token.unwrap(_bnt), amount); // transfer the eth to the user payable(msg.sender).sendValue(ethAmountRequired); diff --git a/test/forge/CarbonPOL.t.sol b/test/forge/CarbonPOL.t.sol index 8423a34f..6a37b1f5 100644 --- a/test/forge/CarbonPOL.t.sol +++ b/test/forge/CarbonPOL.t.sol @@ -7,7 +7,7 @@ import { TestFixture } from "./TestFixture.t.sol"; import { POLTestCaseParser } from "./POLTestCaseParser.t.sol"; import { AccessDenied, ZeroValue, InvalidAddress } from "../../contracts/utility/Utils.sol"; -import { Token, NATIVE_TOKEN } from "../../contracts/token/Token.sol"; +import { Token, toERC20, NATIVE_TOKEN } from "../../contracts/token/Token.sol"; import { TestReenterCarbonPOL } from "../../contracts/helpers/TestReenterCarbonPOL.sol"; import { ICarbonPOL } from "../../contracts/pol/interfaces/ICarbonPOL.sol"; @@ -811,8 +811,8 @@ contract CarbonPOLTest is TestFixture { vm.stopPrank(); } - /// @dev test trading should increase the contract's bnt balance - function testTradingETHShouldIncreaseContractBNTBalance() public { + /// @dev test trading eth should burn bnt + function testTradingETHShouldBurnBNT() public { // enable trading and set price for the native token vm.prank(admin); Token token = NATIVE_TOKEN; @@ -830,7 +830,8 @@ contract CarbonPOLTest is TestFixture { uint128 tradeAmount = 1000 * 1e18; uint128 expectedTradeInput = carbonPOL.expectedTradeInput(token, tradeAmount); - uint256 bntBalanceBefore = bnt.balanceOf(address(carbonPOL)); + uint256 bntBalanceBefore = bnt.balanceOf(user1); + uint256 bntSupplyBefore = toERC20(bnt).totalSupply(); // approve bnt for eth -> bnt trades bnt.safeApprove(address(carbonPOL), tradeAmount); @@ -838,9 +839,11 @@ contract CarbonPOLTest is TestFixture { // trade carbonPOL.trade{ value: expectedTradeInput }(token, tradeAmount); - uint256 bntBalanceAfter = bnt.balanceOf(address(carbonPOL)); + uint256 bntBalanceAfter = bnt.balanceOf(user1); + uint256 bntSupplyAfter = toERC20(bnt).totalSupply(); - assertEq(bntBalanceAfter - bntBalanceBefore, tradeAmount); + assertEq(bntBalanceBefore - bntBalanceAfter, tradeAmount); + assertEq(bntSupplyBefore - bntSupplyAfter, tradeAmount); vm.stopPrank(); }