Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
RensR committed Oct 7, 2024
1 parent 4f7a047 commit e359ed0
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 8 deletions.
6 changes: 5 additions & 1 deletion contracts/gas-snapshots/ccip.gas-snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ BurnMintTokenPool_releaseOrMint:test_PoolMint_Success() (gas: 112391)
BurnWithFromMintTokenPool_lockOrBurn:test_ChainNotAllowed_Revert() (gas: 28842)
BurnWithFromMintTokenPool_lockOrBurn:test_PoolBurnRevertNotHealthy_Revert() (gas: 55271)
BurnWithFromMintTokenPool_lockOrBurn:test_PoolBurn_Success() (gas: 244050)
BurnWithFromMintTokenPool_lockOrBurn:test_Setup_Success() (gas: 24170)
BurnWithFromMintTokenPool_lockOrBurn:test_Setup_Success() (gas: 24168)
BurnWithFromMintTokenPool_releaseOrMint:test_Setup_Success() (gas: 24547)
BurnWithFromMintTokenPool_releaseOrMint:test_releaseOrMint_NegativeMintAmount_reverts() (gas: 93840)
BurnWithFromMintTokenPool_releaseOrMint:test_releaseOrMint_Success() (gas: 93423)
BurnWithFromMintTokenPool_releaseOrMint:test_releaseOrMint_rebasing_success(uint16) (runs: 257, μ: 95578, ~: 97505)
CCIPClientExample_sanity:test_ImmutableExamples_Success() (gas: 2052431)
CCIPHome__validateConfig:test__validateConfigLessTransmittersThanSigners_Success() (gas: 334693)
CCIPHome__validateConfig:test__validateConfigSmallerFChain_Success() (gas: 466117)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {TokenPool} from "./TokenPool.sol";
contract BurnWithFromMintRebasingTokenPool is BurnWithFromMintTokenPool {
using SafeERC20 for IBurnMintERC20;

error NegativeMintAmount(uint256 pre, uint256 post);
error NegativeMintAmount(uint256 amountBurned);

string public constant override typeAndVersion = "BurnWithFromMintRebasingTokenPool 1.5.0";

Expand All @@ -41,15 +41,15 @@ contract BurnWithFromMintRebasingTokenPool is BurnWithFromMintTokenPool {
// Mint to the receiver
IBurnMintERC20(address(i_token)).mint(releaseOrMintIn.receiver, releaseOrMintIn.amount);

emit Minted(msg.sender, releaseOrMintIn.receiver, releaseOrMintIn.amount);

uint256 balancePost = IBurnMintERC20(address(i_token)).balanceOf(releaseOrMintIn.receiver);

// Mint should not reduce the number of tokens in the receiver, if it does it will revert the call.
if (balancePost < balancePre) {
revert NegativeMintAmount(balancePre, balancePost);
revert NegativeMintAmount(balancePre - balancePost);
}

emit Minted(msg.sender, releaseOrMintIn.receiver, balancePost - balancePre);

return Pool.ReleaseOrMintOutV1({destinationAmount: balancePost - balancePre});
}
}
11 changes: 10 additions & 1 deletion contracts/src/v0.8/ccip/test/helpers/ERC20RebasingHelper.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,23 @@ import {ERC20} from "../../../vendor/openzeppelin-solidity/v4.8.3/contracts/toke

contract ERC20RebasingHelper is ERC20 {
uint16 public s_multiplierPercentage = 100;
bool public s_mintShouldBurn = false;

constructor() ERC20("Rebasing", "REB") {}

function mint(address to, uint256 amount) external {
_mint(to, amount * s_multiplierPercentage / 100);
if (!s_mintShouldBurn) {
_mint(to, amount * s_multiplierPercentage / 100);
return;
}
_burn(to, amount * s_multiplierPercentage / 100);
}

function setMultiplierPercentage(uint16 multiplierPercentage) external {
s_multiplierPercentage = multiplierPercentage;
}

function setMintShouldBurn(bool mintShouldBurn) external {
s_mintShouldBurn = mintShouldBurn;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ contract BurnWithFromMintRebasingTokenPoolSetup is BurnMintSetup {

_applyChainUpdates(address(s_pool));

deal(address(s_rebasingToken), OWNER, 1e18);

vm.startPrank(s_burnMintOffRamp);
}
}
Expand All @@ -40,22 +42,34 @@ contract BurnWithFromMintTokenPool_releaseOrMint is BurnWithFromMintRebasingToke

function test_releaseOrMint_Success() public {
uint256 amount = 1000;
uint256 balancePre = s_rebasingToken.balanceOf(address(OWNER));

Pool.ReleaseOrMintOutV1 memory releaseOrMintOut = s_pool.releaseOrMint(_getReleaseOrMintIn(amount));

assertEq(amount, releaseOrMintOut.destinationAmount);
assertEq(amount, s_rebasingToken.balanceOf(address(OWNER)));
assertEq(balancePre + amount, s_rebasingToken.balanceOf(address(OWNER)));
}

function test_releaseOrMint_rebasing_success(uint16 multiplierPercentage) public {
uint256 amount = 1000;
uint256 expectedAmount = amount * multiplierPercentage / 100;
s_rebasingToken.setMultiplierPercentage(multiplierPercentage);

uint256 balancePre = s_rebasingToken.balanceOf(address(OWNER));

Pool.ReleaseOrMintOutV1 memory releaseOrMintOut = s_pool.releaseOrMint(_getReleaseOrMintIn(amount));

assertEq(expectedAmount, releaseOrMintOut.destinationAmount);
assertEq(expectedAmount, s_rebasingToken.balanceOf(address(OWNER)));
assertEq(balancePre + expectedAmount, s_rebasingToken.balanceOf(address(OWNER)));
}

function test_releaseOrMint_NegativeMintAmount_reverts() public {
uint256 amount = 1000;
s_rebasingToken.setMintShouldBurn(true);

vm.expectRevert(abi.encodeWithSelector(BurnWithFromMintRebasingTokenPool.NegativeMintAmount.selector, amount));

s_pool.releaseOrMint(_getReleaseOrMintIn(amount));
}

function _getReleaseOrMintIn(uint256 amount) internal view returns (Pool.ReleaseOrMintInV1 memory) {
Expand Down

0 comments on commit e359ed0

Please sign in to comment.