Skip to content

Commit

Permalink
Add transfer mint burn events (#7)
Browse files Browse the repository at this point in the history
* remove test.sh in favour of running forge test; add transfer mint/burn events

* forge install: forge-std

v1.6.1

* remove trues from expectEmit; add transfer event burn/mint checks

* set optimizer runs explicitly
  • Loading branch information
hexonaut authored Nov 2, 2023
1 parent 0377fa3 commit 2b7acb9
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 35 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@
[submodule "lib/erc4626-tests"]
path = lib/erc4626-tests
url = https://github.com/a16z/erc4626-tests
[submodule "lib/forge-std"]
path = lib/forge-std
url = https://github.com/foundry-rs/forge-std
3 changes: 1 addition & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
all :; forge build --use solc:0.8.17
clean :; forge clean
test :; ./test.sh $(match)
deploy :; forge script script/Deploy.s.sol:Deploy --use solc:0.8.17 --rpc-url $(ETH_RPC_URL) --sender $(ETH_FROM) --broadcast --verify -vvvv
deploy :; forge script script/Deploy.s.sol:Deploy --rpc-url $(ETH_RPC_URL) --sender $(ETH_FROM) --broadcast --verify -vvvv
3 changes: 3 additions & 0 deletions foundry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,8 @@
src = 'src'
out = 'out'
libs = ['lib']
solc = '0.8.17'
optimizer = true
optimizer_runs = 200

# See more config options https://github.com/foundry-rs/foundry/tree/master/config
1 change: 1 addition & 0 deletions lib/forge-std
Submodule forge-std added at 1d9650
2 changes: 2 additions & 0 deletions src/SavingsDai.sol
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ contract SavingsDai {
}

emit Deposit(msg.sender, receiver, assets, shares);
emit Transfer(address(0), receiver, shares);
}

function _burn(uint256 assets, uint256 shares, address receiver, address owner) internal {
Expand All @@ -260,6 +261,7 @@ contract SavingsDai {
pot.exit(shares);
daiJoin.exit(receiver, assets);

emit Transfer(owner, address(0), shares);
emit Withdraw(msg.sender, receiver, owner, assets, shares);
}

Expand Down
72 changes: 47 additions & 25 deletions src/test/SavingsDai-integration.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ contract SavingsDaiIntegrationTest is DssTest {
keccak256("Permit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)");

function setUp() public {
vm.createSelectFork(getChain('mainnet').rpcUrl);

ChainlogAbstract chainlog = ChainlogAbstract(0xdA0Ab1e0017DEbCd72Be8599041a2aa3bA7e740F);

vat = VatAbstract(chainlog.getAddress("MCD_VAT"));
Expand Down Expand Up @@ -131,8 +133,10 @@ contract SavingsDaiIntegrationTest is DssTest {
uint256 dsrDai = vat.dai(address(pot));

uint256 pie = 1e18 * RAY / pot.chi();
vm.expectEmit(true, true, true, true);
vm.expectEmit();
emit Deposit(address(this), address(0xBEEF), 1e18, pie);
vm.expectEmit();
emit Transfer(address(0), address(0xBEEF), pie);
token.deposit(1e18, address(0xBEEF));

assertEq(token.totalSupply(), pie);
Expand All @@ -146,9 +150,11 @@ contract SavingsDaiIntegrationTest is DssTest {
uint256 dsrDai = vat.dai(address(pot));

uint256 pie = 1e18 * RAY / pot.chi();
vm.expectEmit(true, true, true, true);
vm.expectEmit();
emit Deposit(address(this), address(0xBEEF), 1e18, pie);
vm.expectEmit(true, true, true, true);
vm.expectEmit();
emit Transfer(address(0), address(0xBEEF), pie);
vm.expectEmit();
emit Referral(888, address(0xBEEF), 1e18, pie);
token.deposit(1e18, address(0xBEEF), 888);

Expand All @@ -170,8 +176,10 @@ contract SavingsDaiIntegrationTest is DssTest {
uint256 dsrDai = vat.dai(address(pot));

uint256 pie = 1e18 * RAY / pot.chi();
vm.expectEmit(true, true, true, true);
vm.expectEmit();
emit Deposit(address(this), address(0xBEEF), _divup(pie * pot.chi(), RAY), pie);
vm.expectEmit();
emit Transfer(address(0), address(0xBEEF), pie);
token.mint(pie, address(0xBEEF));

assertEq(token.totalSupply(), pie);
Expand All @@ -185,9 +193,11 @@ contract SavingsDaiIntegrationTest is DssTest {
uint256 dsrDai = vat.dai(address(pot));

uint256 pie = 1e18 * RAY / pot.chi();
vm.expectEmit(true, true, true, true);
vm.expectEmit();
emit Deposit(address(this), address(0xBEEF), _divup(pie * pot.chi(), RAY), pie);
vm.expectEmit(true, true, true, true);
vm.expectEmit();
emit Transfer(address(0), address(0xBEEF), pie);
vm.expectEmit();
emit Referral(888, address(0xBEEF), 1e18, pie);
token.mint(pie, address(0xBEEF), 888);

Expand All @@ -213,7 +223,9 @@ contract SavingsDaiIntegrationTest is DssTest {

assertEq(vat.dai(address(pot)), dsrDai + pie * pot.chi());

vm.expectEmit(true, true, true, true);
vm.expectEmit();
emit Transfer(address(0xBEEF), address(0), pie * 0.9e18 / WAD);
vm.expectEmit();
emit Withdraw(address(0xBEEF), address(this), address(0xBEEF), (pie * 0.9e18 / WAD) * pot.chi() / RAY, pie * 0.9e18 / WAD);
vm.prank(address(0xBEEF));
token.redeem(pie * 0.9e18 / WAD, address(this), address(0xBEEF));
Expand All @@ -233,7 +245,9 @@ contract SavingsDaiIntegrationTest is DssTest {

uint256 assets = (pie * 0.9e18 / WAD) * pot.chi() / RAY;
uint256 shares = _divup(assets * RAY, pot.chi());
vm.expectEmit(true, true, true, true);
vm.expectEmit();
emit Transfer(address(0xBEEF), address(0), shares);
vm.expectEmit();
emit Withdraw(address(0xBEEF), address(this), address(0xBEEF), assets, shares);
vm.prank(address(0xBEEF));
token.withdraw(assets, address(this), address(0xBEEF));
Expand Down Expand Up @@ -266,15 +280,15 @@ contract SavingsDaiIntegrationTest is DssTest {
}

function testApprove() public {
vm.expectEmit(true, true, true, true);
vm.expectEmit();
emit Approval(address(this), address(0xBEEF), 1e18);
assertTrue(token.approve(address(0xBEEF), 1e18));

assertEq(token.allowance(address(this), address(0xBEEF)), 1e18);
}

function testIncreaseAllowance() public {
vm.expectEmit(true, true, true, true);
vm.expectEmit();
emit Approval(address(this), address(0xBEEF), 1e18);
assertTrue(token.increaseAllowance(address(0xBEEF), 1e18));

Expand All @@ -283,7 +297,7 @@ contract SavingsDaiIntegrationTest is DssTest {

function testDecreaseAllowance() public {
assertTrue(token.increaseAllowance(address(0xBEEF), 3e18));
vm.expectEmit(true, true, true, true);
vm.expectEmit();
emit Approval(address(this), address(0xBEEF), 2e18);
assertTrue(token.decreaseAllowance(address(0xBEEF), 1e18));

Expand All @@ -300,7 +314,7 @@ contract SavingsDaiIntegrationTest is DssTest {
uint256 pie = 1e18 * RAY / pot.chi();
token.deposit(1e18, address(this));

vm.expectEmit(true, true, true, true);
vm.expectEmit();
emit Transfer(address(this), address(0xBEEF), pie);
assertTrue(token.transfer(address(0xBEEF), pie));
assertEq(token.totalSupply(), pie);
Expand Down Expand Up @@ -328,7 +342,7 @@ contract SavingsDaiIntegrationTest is DssTest {
vm.prank(from);
token.approve(address(this), pie);

vm.expectEmit(true, true, true, true);
vm.expectEmit();
emit Transfer(from, address(0xBEEF), pie);
assertTrue(token.transferFrom(from, address(0xBEEF), pie));
assertEq(token.totalSupply(), pie);
Expand Down Expand Up @@ -356,11 +370,11 @@ contract SavingsDaiIntegrationTest is DssTest {
token.deposit(1e18, from);

vm.prank(from);
vm.expectEmit(true, true, true, true);
vm.expectEmit();
emit Approval(from, address(this), type(uint256).max);
token.approve(address(this), type(uint256).max);

vm.expectEmit(true, true, true, true);
vm.expectEmit();
emit Transfer(from, address(0xBEEF), pie);
assertTrue(token.transferFrom(from, address(0xBEEF), pie));
assertEq(token.totalSupply(), pie);
Expand All @@ -386,7 +400,7 @@ contract SavingsDaiIntegrationTest is DssTest {
)
);

vm.expectEmit(true, true, true, true);
vm.expectEmit();
emit Approval(owner, address(0xCAFE), 1e18);
token.permit(owner, address(0xCAFE), 1e18, block.timestamp, v, r, s);

Expand Down Expand Up @@ -425,7 +439,7 @@ contract SavingsDaiIntegrationTest is DssTest {
);

bytes memory signature = abi.encode(r, s, bytes32(uint256(v) << 248), r2, s2, bytes32(uint256(v2) << 248));
vm.expectEmit(true, true, true, true);
vm.expectEmit();
emit Approval(mockMultisig, address(0xCAFE), 1e18);
token.permit(mockMultisig, address(0xCAFE), 1e18, block.timestamp, signature);

Expand Down Expand Up @@ -592,8 +606,10 @@ contract SavingsDaiIntegrationTest is DssTest {
vm.warp(block.timestamp + warp % 365 days);
uint256 shares = token.previewDeposit(amount);
if (to != address(0) && to != address(token)) {
vm.expectEmit(true, true, true, true);
vm.expectEmit();
emit Deposit(address(this), to, amount, shares);
vm.expectEmit();
emit Transfer(address(0), to, shares);
} else {
vm.expectRevert("SavingsDai/invalid-address");
}
Expand All @@ -611,8 +627,10 @@ contract SavingsDaiIntegrationTest is DssTest {
vm.warp(block.timestamp + warp % 365 days);
uint256 assets = token.previewMint(shares);
if (to != address(0) && to != address(token)) {
vm.expectEmit(true, true, true, true);
vm.expectEmit();
emit Deposit(address(this), to, assets, shares);
vm.expectEmit();
emit Transfer(address(0), to, shares);
} else {
vm.expectRevert("SavingsDai/invalid-address");
}
Expand Down Expand Up @@ -645,7 +663,9 @@ contract SavingsDaiIntegrationTest is DssTest {
token.deposit(mintAmount, from);

uint256 assets = token.previewRedeem(burnAmount);
vm.expectEmit(true, true, true, true);
vm.expectEmit();
emit Transfer(address(from), address(0), burnAmount);
vm.expectEmit();
emit Withdraw(address(from), TEST_ADDRESS, address(from), assets, burnAmount);
vm.prank(from);
uint256 aassets = token.redeem(burnAmount, TEST_ADDRESS, from);
Expand Down Expand Up @@ -675,7 +695,9 @@ contract SavingsDaiIntegrationTest is DssTest {
token.deposit(mintAmount, from);

uint256 shares = token.previewWithdraw(burnAmount);
vm.expectEmit(true, true, true, true);
vm.expectEmit();
emit Transfer(address(from), address(0), shares);
vm.expectEmit();
emit Withdraw(address(from), TEST_ADDRESS, address(from), burnAmount, shares);
vm.prank(from);
uint256 ashares = token.withdraw(burnAmount, TEST_ADDRESS, from);
Expand All @@ -688,7 +710,7 @@ contract SavingsDaiIntegrationTest is DssTest {
}

function testApprove(address to, uint256 amount) public {
vm.expectEmit(true, true, true, true);
vm.expectEmit();
emit Approval(address(this), to, amount);
assertTrue(token.approve(to, amount));

Expand All @@ -702,7 +724,7 @@ contract SavingsDaiIntegrationTest is DssTest {
uint256 pie = amount * RAY / pot.chi();
token.deposit(amount, address(this));

vm.expectEmit(true, true, true, true);
vm.expectEmit();
emit Transfer(address(this), to, pie);
assertTrue(token.transfer(to, pie));
assertEq(token.totalSupply(), pie);
Expand Down Expand Up @@ -735,7 +757,7 @@ contract SavingsDaiIntegrationTest is DssTest {
vm.prank(from);
token.approve(address(this), approval);

vm.expectEmit(true, true, true, true);
vm.expectEmit();
emit Transfer(from, to, pie);
assertTrue(token.transferFrom(from, to, pie));
assertEq(token.totalSupply(), pie);
Expand Down Expand Up @@ -774,7 +796,7 @@ contract SavingsDaiIntegrationTest is DssTest {
)
);

vm.expectEmit(true, true, true, true);
vm.expectEmit();
emit Approval(owner, to, amount);
token.permit(owner, to, amount, deadline, v, r, s);

Expand Down
8 changes: 0 additions & 8 deletions test.sh

This file was deleted.

0 comments on commit 2b7acb9

Please sign in to comment.