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

build: dumper #47

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
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
8 changes: 8 additions & 0 deletions contracts/Mocks/MockTradeFactory.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.18;

contract MockTradeFactory {
function enable(address, address) external {}

function disable(address, address) external {}
}
94 changes: 94 additions & 0 deletions contracts/splitter/Dumper.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// SPDX-License-Identifier: GNU AGPLv3
pragma solidity >=0.8.18;

import {Governance} from "@periphery/utils/Governance.sol";
import {TradeFactorySwapper} from "@periphery/swappers/TradeFactorySwapper.sol";
import {Accountant, ERC20, SafeERC20} from "../accountants/Accountant.sol";

contract Dumper is TradeFactorySwapper, Governance {
using SafeERC20 for ERC20;

Accountant public immutable accountant;

address public immutable splitter;

address public splitToken;

constructor(
address _governance,
address _accountant,
address _splitter,
address _tf,
address _splitToken
) Governance(_governance) {
require(_accountant != address(0), "ZERO ADDRESS");
require(_splitter != address(0), "ZERO ADDRESS");
require(_splitToken != address(0), "ZERO ADDRESS");
accountant = Accountant(_accountant);
splitter = _splitter;
_setTradeFactory(_tf, _splitToken);
splitToken = _splitToken;
}

function setTradeFactory(address _tf) external onlyGovernance {
_setTradeFactory(_tf, splitToken);
}

function addToken(address _tokenFrom) external onlyGovernance {
_addToken(_tokenFrom, splitToken);
}

function addTokens(address[] calldata _tokens) external onlyGovernance {
address _splitToken = splitToken;
for (uint256 i; i < _tokens.length; ++i) {
_addToken(_tokens[i], _splitToken);
}
}

function removeTokens(address[] calldata _tokens) external onlyGovernance {
address _splitToken = splitToken;
for (uint256 i; i < _tokens.length; ++i) {
_removeToken(_tokens[i], _splitToken);
}
}

function setSplitToken(address _splitToken) external onlyGovernance {
require(_splitToken != address(0), "ZERO ADDRESS");
// Set to same Trade Factory address but new split token
_setTradeFactory(tradeFactory(), _splitToken);
splitToken = _splitToken;
}

// Send the split token to the Splitter contract.
function distribute() external {
ERC20(splitToken).safeTransfer(
splitter,
ERC20(splitToken).balanceOf(address(this)) - 1
);
}

function _claimRewards() internal override {}

// Claim the fees from the accountant
function claim(address _token) external onlyGovernance {
accountant.distribute(_token);
}

function claim(address[] calldata _tokens) external onlyGovernance {
for (uint256 i; i < _tokens.length; ++i) {
accountant.distribute(_tokens[i]);
}
}

function claim(address _token, uint256 _amount) external onlyGovernance {
accountant.distribute(_token, _amount);
}

function sweep(address _token) external onlyGovernance {
address daddy = accountant.feeManager();
ERC20(_token).safeTransfer(
daddy,
ERC20(_token).balanceOf(address(this))
);
}
}
4 changes: 2 additions & 2 deletions scripts/Deploy.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ contract Deploy is Script {

// Append constructor args to the bytecode
bytes memory bytecode = abi.encodePacked(
vm.getCode("registry/ReleaseRegistry.sol:ReleaseRegistry"),
abi.encode(initGov)
vm.getCode("splitter/Dumper.sol:Dumper"),
abi.encode()
);

// Use salt of 0.
Expand Down
112 changes: 112 additions & 0 deletions src/splitter/Dumper.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
// SPDX-License-Identifier: GNU AGPLv3
pragma solidity >=0.8.18;

import {Governance} from "@periphery/utils/Governance.sol";
import {Accountant, ERC20, SafeERC20} from "../accountants/Accountant.sol";

interface IAuction {
function kick(address _token) external returns (uint256);
}

contract Dumper is Governance {
using SafeERC20 for ERC20;

modifier onlyAllowed() {
require(msg.sender == governance || allowed[msg.sender], "NOT ALLOWED");
_;
}

Accountant public immutable accountant;

address public immutable splitter;

address public splitToken;

address public auction;

mapping(address => bool) public allowed;

constructor(
address _governance,
address _accountant,
address _splitter,
address _splitToken
) Governance(_governance) {
require(_accountant != address(0), "ZERO ADDRESS");
require(_splitter != address(0), "ZERO ADDRESS");
require(_splitToken != address(0), "ZERO ADDRESS");
accountant = Accountant(_accountant);
splitter = _splitter;
splitToken = _splitToken;
}

// Send the split token to the Splitter contract.
function distribute() external {
ERC20(splitToken).safeTransfer(
splitter,
ERC20(splitToken).balanceOf(address(this)) - 1
);
}

function dumpToken(address _token) external onlyAllowed {
_dumpToken(_token);
}

function dumpTokens(address[] calldata _tokens) external onlyAllowed {
for (uint256 i; i < _tokens.length; ++i) {
_dumpToken(_tokens[i]);
}
}

function _dumpToken(address _token) internal {
uint256 accountantBalance = ERC20(_token).balanceOf(
address(accountant)
);
if (accountantBalance > 0) {
accountant.distribute(_token);
}
ERC20(_token).safeTransfer(
auction,
ERC20(_token).balanceOf(address(this)) - 1
);
IAuction(auction).kick(_token);
}

// Claim the fees from the accountant
function claimToken(address _token) external onlyAllowed {
accountant.distribute(_token);
}

function claimTokens(address[] calldata _tokens) external onlyAllowed {
for (uint256 i; i < _tokens.length; ++i) {
accountant.distribute(_tokens[i]);
}
}

function claimToken(address _token, uint256 _amount) external onlyAllowed {
accountant.distribute(_token, _amount);
}

function sweep(address _token) external onlyGovernance {
ERC20(_token).safeTransfer(
governance,
ERC20(_token).balanceOf(address(this))
);
}

function setSplitToken(address _splitToken) external onlyGovernance {
require(_splitToken != address(0), "ZERO ADDRESS");
splitToken = _splitToken;
}

function setAuction(address _auction) external onlyGovernance {
auction = _auction;
}

function setAllowed(
address _person,
bool _allowed
) external onlyGovernance {
allowed[_person] = _allowed;
}
}
4 changes: 2 additions & 2 deletions src/test/splitter/TestSplitter.t.sol
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.18;

import {Setup, ISplitter, ISplitterFactory, IVault, MockTokenized} from "../utils/Setup.sol";
import {Setup, ISplitter, ISplitterFactory, IVault, Accountant, MockTokenized} from "../utils/Setup.sol";


contract TestSplitter is Setup {
event UpdateManagerRecipient(address indexed newManagerRecipient);
Expand All @@ -12,7 +13,6 @@ contract TestSplitter is Setup {

IVault public vault;
MockTokenized public mockTokenized;

function setUp() public override {
super.setUp();
(splitterFactory, splitter) = setupSplitter();
Expand Down
78 changes: 78 additions & 0 deletions tests/splitter/test_dumper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import ape
from ape import project


def test_dumper(asset, vault, accountant, daddy, brain, amount):
splitter = daddy
tf = brain.deploy(project.MockTradeFactory)
dumper = brain.deploy(
project.Dumper, brain, accountant.address, splitter, tf, vault
)
accountant.setFeeRecipient(dumper, sender=daddy)

assert dumper.governance() == brain
assert dumper.accountant() == accountant
assert dumper.tradeFactory() == tf
assert dumper.splitToken() == vault
assert dumper.rewardTokens() == []

with ape.reverts("!governance"):
dumper.addTokens([asset], sender=daddy)

with ape.reverts("!governance"):
dumper.addToken(asset, sender=daddy)

dumper.addTokens([asset], sender=brain)

assert dumper.rewardTokens() == [asset]

asset.mint(accountant, amount, sender=brain)

assert asset.balanceOf(accountant) == amount
assert asset.balanceOf(dumper) == 0
assert asset.balanceOf(splitter) == 0

with ape.reverts("!governance"):
dumper.claim([asset], sender=daddy)

dumper.claim([asset], sender=brain)

assert asset.balanceOf(accountant) == 0
assert asset.balanceOf(dumper) == amount
assert vault.balanceOf(dumper) == 0
assert asset.balanceOf(splitter) == 0
assert vault.balanceOf(splitter) == 0

asset.transferFrom(dumper, tf, amount, sender=tf)

assert asset.balanceOf(accountant) == 0
assert asset.balanceOf(dumper) == 0
assert asset.balanceOf(tf) == amount

asset.approve(vault, amount, sender=tf)
vault.deposit(amount, dumper, sender=tf)

assert asset.balanceOf(accountant) == 0
assert asset.balanceOf(dumper) == 0
assert vault.balanceOf(dumper) == amount
assert asset.balanceOf(splitter) == 0
assert vault.balanceOf(splitter) == 0

dumper.distribute(sender=daddy)

assert asset.balanceOf(accountant) == 0
assert asset.balanceOf(dumper) == 0
assert vault.balanceOf(dumper) == 1
assert asset.balanceOf(splitter) == 0
assert vault.balanceOf(splitter) == amount - 1

with ape.reverts("!governance"):
dumper.setSplitToken(asset, sender=daddy)

dumper.setSplitToken(asset, sender=brain)

assert dumper.governance() == brain
assert dumper.accountant() == accountant
assert dumper.tradeFactory() == tf
assert dumper.splitToken() == asset
assert dumper.rewardTokens() == [asset]
Loading