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

EXP - Rational Flow for Bribes and Claims #25

Merged
merged 4 commits into from
Oct 15, 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
9 changes: 5 additions & 4 deletions src/BribeInitiative.sol
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,19 @@ contract BribeInitiative is IInitiative, IBribeInitiative {

/// @inheritdoc IBribeInitiative
function depositBribe(uint128 _boldAmount, uint128 _bribeTokenAmount, uint16 _epoch) external {
bold.safeTransferFrom(msg.sender, address(this), _boldAmount);
bribeToken.safeTransferFrom(msg.sender, address(this), _bribeTokenAmount);

uint16 epoch = governance.epoch();
require(_epoch > epoch, "BribeInitiative: only-future-epochs");
require(_epoch >= epoch, "BribeInitiative: only-future-epochs");

Bribe memory bribe = bribeByEpoch[_epoch];
bribe.boldAmount += _boldAmount;
bribe.bribeTokenAmount += _bribeTokenAmount;
bribeByEpoch[_epoch] = bribe;

emit DepositBribe(msg.sender, _boldAmount, _bribeTokenAmount, _epoch);

bold.safeTransferFrom(msg.sender, address(this), _boldAmount);
bribeToken.safeTransferFrom(msg.sender, address(this), _bribeTokenAmount);
}

function _claimBribe(
Expand All @@ -76,7 +77,7 @@ contract BribeInitiative is IInitiative, IBribeInitiative {
uint16 _prevLQTYAllocationEpoch,
uint16 _prevTotalLQTYAllocationEpoch
) internal returns (uint256 boldAmount, uint256 bribeTokenAmount) {
require(_epoch != governance.epoch(), "BribeInitiative: cannot-claim-for-current-epoch");
require(_epoch < governance.epoch(), "BribeInitiative: cannot-claim-for-current-epoch");
require(!claimedBribeAtEpoch[_user][_epoch], "BribeInitiative: already-claimed");

Bribe memory bribe = bribeByEpoch[_epoch];
Expand Down
55 changes: 55 additions & 0 deletions test/BribeInitiative.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,54 @@ contract BribeInitiativeTest is Test {
assertEq(totalLQTYAllocated, 0);
}

// forge test --match-test test_rationalFlow -vvvv
function test_rationalFlow() public {
vm.warp(block.timestamp + (EPOCH_DURATION)); // Initiative not active

// We are now at epoch

// Deposit
_stakeLQTY(user1, 1e18);

// Deposit Bribe for now
_allocateLQTY(user1, 5e17, 0); /// @audit Allocate b4 or after bribe should be irrelevant

/// @audit WTF
_depositBribe(1e18, 1e18, governance.epoch()); /// @audit IMO this should also work

_allocateLQTY(user1, 5e17, 0); /// @audit Allocate b4 or after bribe should be irrelevant

// deposit bribe for Epoch + 2
_depositBribe(1e18, 1e18, governance.epoch() + 1);


(uint88 totalLQTYAllocated,) =
bribeInitiative.totalLQTYAllocatedByEpoch(governance.epoch());
(uint88 userLQTYAllocated,) =
bribeInitiative.lqtyAllocatedByUserAtEpoch(user1, governance.epoch());
assertEq(totalLQTYAllocated, 1e18, "total allocation");
assertEq(userLQTYAllocated, 1e18, "user allocation");

vm.warp(block.timestamp + (EPOCH_DURATION));
// We are now at epoch + 1 // Should be able to claim epoch - 1

// user should receive bribe from their allocated stake
(uint256 boldAmount, uint256 bribeTokenAmount) = _claimBribe(user1, governance.epoch() - 1, governance.epoch() - 1, governance.epoch() - 1);
assertEq(boldAmount, 1e18, "bold amount");
assertEq(bribeTokenAmount, 1e18, "bribe amount");

// And they cannot claim the one that is being added currently
_claimBribe(user1, governance.epoch(), governance.epoch() - 1, governance.epoch() - 1, true);

// decrease user allocation for the initiative
_allocateLQTY(user1, -1e18, 0);

(userLQTYAllocated,) = bribeInitiative.lqtyAllocatedByUserAtEpoch(user1, governance.epoch());
(totalLQTYAllocated,) = bribeInitiative.totalLQTYAllocatedByEpoch(governance.epoch());
assertEq(userLQTYAllocated, 0, "total allocation");
assertEq(totalLQTYAllocated, 0, "user allocation");
}

/**
Revert Cases
*/
Expand Down Expand Up @@ -598,11 +646,18 @@ contract BribeInitiativeTest is Test {
}

function _claimBribe(address claimer, uint16 epoch, uint16 prevLQTYAllocationEpoch, uint16 prevTotalLQTYAllocationEpoch) public returns (uint256 boldAmount, uint256 bribeTokenAmount){
return _claimBribe(claimer, epoch, prevLQTYAllocationEpoch, prevTotalLQTYAllocationEpoch, false);
}

function _claimBribe(address claimer, uint16 epoch, uint16 prevLQTYAllocationEpoch, uint16 prevTotalLQTYAllocationEpoch, bool expectRevert) public returns (uint256 boldAmount, uint256 bribeTokenAmount){
vm.startPrank(claimer);
BribeInitiative.ClaimData[] memory epochs = new BribeInitiative.ClaimData[](1);
epochs[0].epoch = epoch;
epochs[0].prevLQTYAllocationEpoch = prevLQTYAllocationEpoch;
epochs[0].prevTotalLQTYAllocationEpoch = prevTotalLQTYAllocationEpoch;
if(expectRevert) {
vm.expectRevert();
}
(boldAmount, bribeTokenAmount) = bribeInitiative.claimBribes(epochs);
vm.stopPrank();
}
Expand Down
Loading