Skip to content

Commit

Permalink
tests back to 100
Browse files Browse the repository at this point in the history
  • Loading branch information
mejango committed Nov 30, 2024
1 parent 793dd2d commit 291401f
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 37 deletions.
34 changes: 20 additions & 14 deletions test/Fork.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -380,12 +380,13 @@ contract TestJBBuybackHook_Fork is TestBaseWorkflow, JBTest, UniswapV3ForgeQuote
);

// Check: token received by the multisig()
assertApproxEqAbs(
jbx.balanceOf(multisig()) - _balBeforePayment,
_amountOutQuoted - (_amountOutQuoted * _reservedPercent / 10_000),
1,
"wrong balance"
);
// rm vesting:assertApproxEqAbs(
// jbx.balanceOf(multisig()) - _balBeforePayment,
// _amountOutQuoted - (_amountOutQuoted * _reservedPercent / 10_000),
// 1,
// "wrong balance"
// );
assertApproxEqAbs(jbx.balanceOf(multisig()) - _balBeforePayment, 0, 1, "wrong balance");

// Check: token added to the reserve - 1 wei sensitivity for rounding errors
assertApproxEqAbs(
Expand Down Expand Up @@ -467,7 +468,8 @@ contract TestJBBuybackHook_Fork is TestBaseWorkflow, JBTest, UniswapV3ForgeQuote
);

// Check: token received by the multisig()
assertEq(jbx.balanceOf(multisig()), _balanceBene + amountOutQuoted / 2);
// rm vesting:assertEq(jbx.balanceOf(multisig()), _balanceBene + amountOutQuoted / 2);
assertEq(jbx.balanceOf(multisig()), 0);

// Check: token added to the reserve - 1 wei sensitivity for rounding errors
assertApproxEqAbs(jbController().pendingReservedTokenBalanceOf(1), _reserveBalance + amountOutQuoted / 2, 1);
Expand Down Expand Up @@ -523,7 +525,8 @@ contract TestJBBuybackHook_Fork is TestBaseWorkflow, JBTest, UniswapV3ForgeQuote
uint256 _diff = _balAfterPayment - _balBeforePayment;

// Check: token received by the multisig()
assertEq(_diff, _quote);
// rm vesting: assertEq(_diff, _quote);
assertEq(_diff, 0);

// Check: reserve unchanged
assertEq(jbController().pendingReservedTokenBalanceOf(1), _reservedBalanceBefore);
Expand Down Expand Up @@ -579,7 +582,9 @@ contract TestJBBuybackHook_Fork is TestBaseWorkflow, JBTest, UniswapV3ForgeQuote
// 1 wei sensitivity for rounding errors
if (_twap > _tokenCount) {
// Path is picked based on twap, but the token received are the one quoted
assertApproxEqAbs(_tokenReceived, _quote - (_quote * _reservedPercent) / 10_000, 1, "wrong swap");
// rm for vesting: assertApproxEqAbs(_tokenReceived, _quote - (_quote * _reservedPercent) / 10_000, 1,
// "wrong swap");
assertApproxEqAbs(_tokenReceived, 0, 1, "wrong swap");
assertApproxEqAbs(
jbController().pendingReservedTokenBalanceOf(1),
_reservedBalanceBefore + (_quote * _reservedPercent) / 10_000,
Expand Down Expand Up @@ -725,11 +730,12 @@ contract TestJBBuybackHook_Fork is TestBaseWorkflow, JBTest, UniswapV3ForgeQuote
);

// Check: token received by the multisig()
assertApproxEqAbs(
jbx.balanceOf(multisig()) - _balBeforePayment,
amountOutQuoted / 2 + mulDiv18(_amountInExtra, _weight) / 2,
10
);
// rm vesting: assertApproxEqAbs(
// jbx.balanceOf(multisig()) - _balBeforePayment,
// amountOutQuoted / 2 + mulDiv18(_amountInExtra, _weight) / 2,
// 10
// );
assertApproxEqAbs(jbx.balanceOf(multisig()) - _balBeforePayment, 0, 10);

// Check: token added to the reserve
assertApproxEqAbs(
Expand Down
85 changes: 62 additions & 23 deletions test/Unit.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ contract Test_BuybackHook_Unit is TestBaseWorkflow, JBTest {
ForTest_JBBuybackHook hook;

event Swap(uint256 indexed projectId, uint256 amountIn, IUniswapV3Pool pool, uint256 amountOut, address caller);
event StartVestingBuyback(
uint256 indexed projectId,
address indexed beneficiary,
uint256 amount,
uint256 startsAt,
uint256 endsAt,
address caller
);
event Mint(uint256 indexed projectId, uint256 amount, uint256 tokenCount, address caller);
event TwapWindowChanged(uint256 indexed projectId, uint256 oldSecondsAgo, uint256 newSecondsAgo, address caller);
event TwapSlippageToleranceChanged(
Expand Down Expand Up @@ -742,21 +750,20 @@ contract Test_BuybackHook_Unit is TestBaseWorkflow, JBTest {
abi.encodeCall(controller.burnTokensOf, (address(hook), afterPayRecordedContext.projectId, twapQuote, ""))
);

uint256 amountToVest = 123;

// Mock and expect the call to mint tokens to the beneficiary.
vm.mockCall(
address(controller),
abi.encodeCall(
controller.mintTokensOf, (afterPayRecordedContext.projectId, twapQuote, address(dude), "", true)
controller.mintTokensOf, (afterPayRecordedContext.projectId, twapQuote, address(hook), "", true)
),
abi.encode(true)
);
vm.expectCall(
address(controller),
abi.encodeCall(
controller.mintTokensOf, (afterPayRecordedContext.projectId, twapQuote, address(dude), "", true)
)
abi.encode(amountToVest)
);

vm.mockCall(address(controller), abi.encodeCall(controller.TOKENS, ()), abi.encode(tokens));
vm.mockCall(address(tokens), abi.encodeCall(tokens.tokenOf, (projectId)), abi.encode(projectToken));

// Package data for ruleset call.
JBRulesetMetadata memory _rulesMetadata = JBRulesetMetadata({
reservedPercent: JBConstants.MAX_RESERVED_PERCENT,
Expand Down Expand Up @@ -811,6 +818,17 @@ contract Test_BuybackHook_Unit is TestBaseWorkflow, JBTest {
address(multiTerminal)
);

// Expect the vesting buyback event.
vm.expectEmit(true, true, true, true);
emit StartVestingBuyback(
afterPayRecordedContext.projectId,
address(dude),
amountToVest,
block.timestamp,
block.timestamp + 180 days,
address(multiTerminal)
);

vm.prank(address(multiTerminal));

// Test: call `afterPayRecordedWith`.
Expand Down Expand Up @@ -893,21 +911,19 @@ contract Test_BuybackHook_Unit is TestBaseWorkflow, JBTest {
abi.encodeCall(controller.burnTokensOf, (address(hook), afterPayRecordedContext.projectId, twapQuote, ""))
);

uint256 amountToVest = 123;
// Mock and expect the call to mint tokens to the beneficiary.
vm.mockCall(
address(controller),
abi.encodeCall(
controller.mintTokensOf, (afterPayRecordedContext.projectId, twapQuote, address(dude), "", true)
controller.mintTokensOf, (afterPayRecordedContext.projectId, twapQuote, address(hook), "", true)
),
abi.encode(true)
);
vm.expectCall(
address(controller),
abi.encodeCall(
controller.mintTokensOf, (afterPayRecordedContext.projectId, twapQuote, address(dude), "", true)
)
abi.encode(amountToVest)
);

vm.mockCall(address(controller), abi.encodeCall(controller.TOKENS, ()), abi.encode(tokens));
vm.mockCall(address(tokens), abi.encodeCall(tokens.tokenOf, (projectId)), abi.encode(projectToken));

// Package data for ruleset call.
JBRulesetMetadata memory _rulesMetadata = JBRulesetMetadata({
reservedPercent: JBConstants.MAX_RESERVED_PERCENT,
Expand Down Expand Up @@ -962,6 +978,16 @@ contract Test_BuybackHook_Unit is TestBaseWorkflow, JBTest {
address(multiTerminal)
);

// Expect the vesting buyback event.
vm.expectEmit(true, true, true, true);
emit StartVestingBuyback(
afterPayRecordedContext.projectId,
address(dude),
amountToVest,
block.timestamp,
block.timestamp + 180 days,
address(multiTerminal)
);
vm.prank(address(multiTerminal));

// Test: call `afterPayRecordedWith`.
Expand Down Expand Up @@ -1061,19 +1087,21 @@ contract Test_BuybackHook_Unit is TestBaseWorkflow, JBTest {
abi.encodeCall(controller.burnTokensOf, (address(hook), afterPayRecordedContext.projectId, twapQuote, ""))
);

uint256 amountToVest = 123;
// Mock and expect the call to mint tokens to the beneficiary.
vm.mockCall(
address(controller),
abi.encodeCall(
controller.mintTokensOf, (afterPayRecordedContext.projectId, twapQuote, address(dude), "", true)
controller.mintTokensOf, (afterPayRecordedContext.projectId, twapQuote, address(hook), "", true)
),
abi.encode(true)
abi.encode(amountToVest)
);
vm.expectCall(
address(controller),
abi.encodeCall(
controller.mintTokensOf, (afterPayRecordedContext.projectId, twapQuote, address(dude), "", true)
)

vm.mockCall(address(controller), abi.encodeCall(controller.TOKENS, ()), abi.encode(tokens));
vm.mockCall(
address(tokens),
abi.encodeCall(tokens.tokenOf, (afterPayRecordedContext.projectId)),
abi.encode(projectToken)
);

// Mock and expect the call to check the balance of the hook. There should be no tokens left over.
Expand Down Expand Up @@ -1143,6 +1171,17 @@ contract Test_BuybackHook_Unit is TestBaseWorkflow, JBTest {
address(multiTerminal)
);

// Expect the vesting buyback event.
vm.expectEmit(true, true, true, true);
emit StartVestingBuyback(
afterPayRecordedContext.projectId,
address(dude),
amountToVest,
block.timestamp,
block.timestamp + 180 days,
address(multiTerminal)
);

vm.prank(address(multiTerminal));

// Test: call `afterPayRecordedWith`.
Expand Down

0 comments on commit 291401f

Please sign in to comment.