Skip to content

Commit

Permalink
test + logic fix
Browse files Browse the repository at this point in the history
  • Loading branch information
gunboatsss committed Sep 30, 2024
1 parent dd5fbce commit 1620fd0
Show file tree
Hide file tree
Showing 8 changed files with 138 additions and 68 deletions.
3 changes: 2 additions & 1 deletion foundry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ libs = ["lib"]
remappings = [
"solady/=lib/solady/src"
]

evm_version = "cancun"
verbosity = 2
# See more config options https://github.com/foundry-rs/foundry/blob/master/crates/config/README.md#all-options
19 changes: 0 additions & 19 deletions script/Counter.s.sol

This file was deleted.

26 changes: 18 additions & 8 deletions src/AutoVeBribe.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.24;
pragma solidity >=0.8.27;

import {IReward} from "./interfaces/IReward.sol";
import {IVoter} from "./interfaces/IVoter.sol";
Expand All @@ -19,7 +19,9 @@ contract AutoVeBribe is Ownable {

error GaugeAlreadySet();
error NotAGauge();

error AlreadySentThisEpoch(address _token);
error ZeroToken(address _token);
error InvalidDistributionTime();

constructor(address _voter) {
Expand All @@ -34,21 +36,21 @@ contract AutoVeBribe is Ownable {
revert NotAGauge();
}
bribeVotingReward = IReward(voter.gaugeToBribe(_gauge));
// console.log("initalizing bribe with ", address(bribeVotingReward));
gauge = _gauge;
_initializeOwner(_owner);
}

function distribute(address _token) public {
// Check the last time bribe was distributed
uint256 currentTime = block.timestamp;
uint256 lastDistributed = lastBribeTimeByToken[_token];
if (
ProtocolTimeLibrary.epochVoteStart(currentTime) > lastDistributed
|| ProtocolTimeLibrary.epochVoteEnd(currentTime) < lastDistributed
ProtocolTimeLibrary.epochVoteStart(block.timestamp) > block.timestamp
|| ProtocolTimeLibrary.epochVoteEnd(block.timestamp) < block.timestamp
) {
revert InvalidDistributionTime();
}
if (currentTime - lastDistributed < ProtocolTimeLibrary.WEEK) {
if (block.timestamp - lastDistributed < ProtocolTimeLibrary.WEEK) {
revert AlreadySentThisEpoch(_token);
}

Expand All @@ -59,16 +61,24 @@ contract AutoVeBribe is Ownable {
if (cap == 0) {
amountToSend = balance;
}

lastBribeTimeByToken[_token] = currentTime;
if (amountToSend == 0) {
revert ZeroToken(_token);
}
lastBribeTimeByToken[_token] = block.timestamp;

SafeTransferLib.safeApprove(_token, address(bribeVotingReward), amountToSend);
bribeVotingReward.notifyRewardAmount(_token, balance);
bribeVotingReward.notifyRewardAmount(_token, amountToSend);
}

function distribute(address[] calldata _token) public {
for (uint256 i = 0; i < _token.length; i++) {
distribute(_token[i]);
}
}

// SETTOR

function setTokenAmountPerEpoch(address _token, uint256 _amount) external onlyOwner {
amountToBribeByTokenPerEpoch[_token] = _amount;
}
}
13 changes: 11 additions & 2 deletions src/AutoVeBribeFactory.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.24;
pragma solidity >=0.8.27;

import {LibClone} from "solady/utils/LibClone.sol";
import {AutoVeBribe} from "./AutoVeBribe.sol";
Expand All @@ -13,9 +13,18 @@ contract AutoVeBribeFactory {
implementation = address(new AutoVeBribe(_voter));
}

function deployAutoVeBribe(address _gauge, address _owner) external {
function getLength() external view returns (uint256) {
return autoBribes.length;
}

function getBribe(uint256 index) external view returns (address) {
return autoBribes[index];
}

function deployAutoVeBribe(address _gauge, address _owner) external returns (address newAutoBribe) {
AutoVeBribe newBribe = AutoVeBribe(LibClone.clone_PUSH0(implementation));
newBribe.initialize(_gauge, _owner);
autoBribes.push(address(newBribe));
return address(newBribe);
}
}
14 changes: 0 additions & 14 deletions src/Counter.sol

This file was deleted.

64 changes: 64 additions & 0 deletions test/AutoVeBribe.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity >=0.8.27;
import {AutoVeBribeFactory} from "src/AutoVeBribeFactory.sol";
import {AutoVeBribe} from "src/AutoVeBribe.sol";

import {IVoter} from "src/interfaces/IVoter.sol";

import {SafeTransferLib} from "solady/utils/SafeTransferLib.sol";

import {Test, console} from "forge-std/Test.sol";
import {AutoVeBribeFactory} from "src/AutoVeBribeFactory.sol";
import {ProtocolTimeLibrary} from "src/libraries/ProtocolTimeLibrary.sol";

contract AutoVeBribeTest is Test {
AutoVeBribe autoBribe;
AutoVeBribeFactory factory;
address token = 0x940181a94A35A4569E4529A3CDfB74e38FD98631; // AERO
address owner = address(666666666666666);
address gauge = 0x4F09bAb2f0E15e2A078A227FE1537665F55b8360; // AERO/USDC gauge

function setUp() public {
vm.createSelectFork(vm.envString("BASE_RPC_URL"));
factory = new AutoVeBribeFactory(0x16613524e02ad97eDfeF371bC883F2F5d6C480A5);
console.log("current", block.timestamp);
console.log("start time", ProtocolTimeLibrary.epochVoteStart(block.timestamp));
console.log("end time", ProtocolTimeLibrary.epochVoteEnd(block.timestamp));
vm.warp(ProtocolTimeLibrary.epochVoteStart(block.timestamp) + 1);
autoBribe = AutoVeBribe(factory.deployAutoVeBribe(gauge, owner));
console.log("voter address", address(autoBribe.voter()));
console.log("bribe address", IVoter(0x16613524e02ad97eDfeF371bC883F2F5d6C480A5).gaugeToBribe(gauge));
console.log("owner address", autoBribe.owner());
console.log("gauge address", autoBribe.gauge());
console.log("bribe in contract", address(autoBribe.bribeVotingReward()));
}

function test_bribe() public {
deal(token, address(autoBribe), 1e18);
autoBribe.distribute(token);
assertEq(SafeTransferLib.balanceOf(token, address(autoBribe)), 0);
}
function test_setAmount() public {
vm.expectRevert();
autoBribe.setTokenAmountPerEpoch(token, 0.5e18);
vm.prank(owner);
autoBribe.setTokenAmountPerEpoch(token, 0.5e18);
deal(token, address(autoBribe), 1e18);
autoBribe.distribute(token);
assertEq(SafeTransferLib.balanceOf(token, address(autoBribe)), 0.5e18);
}

function test_cannotSendMultipleTimeInSingleEpoch() public {
vm.prank(owner);
autoBribe.setTokenAmountPerEpoch(token, 0.5e18);
deal(token, address(autoBribe), 1e18);
autoBribe.distribute(token);
vm.expectRevert(abi.encodeWithSelector(AutoVeBribe.AlreadySentThisEpoch.selector, (token)));
autoBribe.distribute(token);
}

function test_cannotReinit() public {
vm.expectRevert(AutoVeBribe.GaugeAlreadySet.selector);
autoBribe.initialize(gauge, owner);
}
}
43 changes: 43 additions & 0 deletions test/AutoVeBribeFactory.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity >=0.8.27;

import {Test, console} from "forge-std/Test.sol";
import {AutoVeBribeFactory} from "src/AutoVeBribeFactory.sol";
import {ProtocolTimeLibrary} from "src/libraries/ProtocolTimeLibrary.sol";
import {AutoVeBribe} from "src/AutoVeBribe.sol";

contract AutoVeBribeFactoryTest is Test {
AutoVeBribeFactory factory;
address owner = address(666666666666666);
address gauge = 0x4F09bAb2f0E15e2A078A227FE1537665F55b8360; // AERO/USDC gauge

function setUp() public {
_setup();
}

function _setup() public {
vm.createSelectFork(vm.envString("BASE_RPC_URL"));
factory = new AutoVeBribeFactory(0x16613524e02ad97eDfeF371bC883F2F5d6C480A5);
vm.warp(ProtocolTimeLibrary.epochVoteStart(block.timestamp) + 1);
}

function test_sanity() view public {
assert(factory.implementation() != address(0));
assertEq(factory.getLength(), 0);
}

function test_createNewBribe() public {
address newBribe = factory.deployAutoVeBribe(gauge, owner);
assertEq(factory.getLength(), 1);
assertEq(factory.getBribe(0), newBribe);
// console.log("in test", address(AutoVeBribe(newBribe).bribeVotingReward()));
address anotherBribe = factory.deployAutoVeBribe(gauge, owner);
assertEq(factory.getLength(), 2);
assertEq(factory.getBribe(1), anotherBribe);
}

function test_invalidGauge() public {
vm.expectRevert();
factory.deployAutoVeBribe(address(69), owner);
}
}
24 changes: 0 additions & 24 deletions test/Counter.t.sol

This file was deleted.

0 comments on commit 1620fd0

Please sign in to comment.