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

Add FeeDistributorBALClaimer contract #1212

Closed
wants to merge 14 commits into from
Closed
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
[
{
"inputs": [
{
"internalType": "contract IFeeDistributor",
"name": "feeDistributor",
"type": "address"
},
{
"internalType": "contract ISingleRecipientGauge",
"name": "gauge",
"type": "address"
},
{
"internalType": "contract IAuthorizerAdaptor",
"name": "authorizerAdaptor",
"type": "address"
}
],
"stateMutability": "nonpayable",
"type": "constructor"
},
{
"inputs": [],
"name": "distributeBAL",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [],
"name": "getAuthorizerAdaptor",
"outputs": [
{
"internalType": "contract IAuthorizerAdaptor",
"name": "",
"type": "address"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "getBALTokenHolder",
"outputs": [
{
"internalType": "contract IBALTokenHolder",
"name": "",
"type": "address"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "getBalancerToken",
"outputs": [
{
"internalType": "contract IERC20",
"name": "",
"type": "address"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "getFeeDistributor",
"outputs": [
{
"internalType": "contract IFeeDistributor",
"name": "",
"type": "address"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "getGauge",
"outputs": [
{
"internalType": "contract ISingleRecipientGauge",
"name": "",
"type": "address"
}
],
"stateMutability": "view",
"type": "function"
}
]

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"creationCode": "0x61012060405234801561001157600080fd5b50604051610a13380380610a1383398101604081905261003091610170565b6000826001600160a01b0316631b88094d6040518163ffffffff1660e01b815260040160206040518083038186803b15801561006b57600080fd5b505afa15801561007f573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906100a3919061014d565b9050806001600160a01b031663c00396996040518163ffffffff1660e01b815260040160206040518083038186803b1580156100de57600080fd5b505afa1580156100f2573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190610116919061014d565b6001600160601b0319606091821b811660805292811b831660a05293841b821660c05291831b811660e052911b16610100526101d4565b60006020828403121561015e578081fd5b8151610169816101bc565b9392505050565b600080600060608486031215610184578182fd5b835161018f816101bc565b60208501519093506101a0816101bc565b60408501519092506101b1816101bc565b809150509250925092565b6001600160a01b03811681146101d157600080fd5b50565b60805160601c60a05160601c60c05160601c60e05160601c6101005160601c6107c16102526000398060c1528061023e52806102d7525080610154528061047952508061010952806101b5528061027a52806103ec52508061012d528061052e52508060e552806101e2528061029b528061041b52506107c16000f3fe608060405234801561001057600080fd5b50600436106100725760003560e01c8063e758d36b11610050578063e758d36b146100a5578063eeed85fa146100ad578063f2803f03146100b757610072565b80630a08832d14610077578063c003969914610095578063c89131731461009d575b600080fd5b61007f6100bf565b60405161008c91906106aa565b60405180910390f35b61007f6100e3565b61007f610107565b61007f61012b565b6100b561014f565b005b61007f610477565b7f000000000000000000000000000000000000000000000000000000000000000090565b7f000000000000000000000000000000000000000000000000000000000000000090565b7f000000000000000000000000000000000000000000000000000000000000000090565b7f000000000000000000000000000000000000000000000000000000000000000090565b6101787f000000000000000000000000000000000000000000000000000000000000000061049b565b6040517f3902b9bc00000000000000000000000000000000000000000000000000000000815273ffffffffffffffffffffffffffffffffffffffff7f00000000000000000000000000000000000000000000000000000000000000001690633902b9bc9061020a907f0000000000000000000000000000000000000000000000000000000000000000906004016106aa565b600060405180830381600087803b15801561022457600080fd5b505af1158015610238573d6000803e3d6000fd5b505050507f000000000000000000000000000000000000000000000000000000000000000073ffffffffffffffffffffffffffffffffffffffff1663c10753297f00000000000000000000000000000000000000000000000000000000000000007f000000000000000000000000000000000000000000000000000000000000000073ffffffffffffffffffffffffffffffffffffffff166370a082317f00000000000000000000000000000000000000000000000000000000000000006040518263ffffffff1660e01b815260040161031291906106aa565b60206040518083038186803b15801561032a57600080fd5b505afa15801561033e573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906103629190610692565b6040518363ffffffff1660e01b815260040161037f929190610739565b600060405180830381600087803b15801561039957600080fd5b505af11580156103ad573d6000803e3d6000fd5b50506040517f3902b9bc00000000000000000000000000000000000000000000000000000000815273ffffffffffffffffffffffffffffffffffffffff7f0000000000000000000000000000000000000000000000000000000000000000169250633902b9bc9150610443907f0000000000000000000000000000000000000000000000000000000000000000906004016106aa565b600060405180830381600087803b15801561045d57600080fd5b505af1158015610471573d6000803e3d6000fd5b50505050565b7f000000000000000000000000000000000000000000000000000000000000000090565b6040805160048082526024820183526020820180517bffffffffffffffffffffffffffffffffffffffffffffffffffffffff167fc2c4c5c10000000000000000000000000000000000000000000000000000000017905291517f4036176a00000000000000000000000000000000000000000000000000000000815273ffffffffffffffffffffffffffffffffffffffff7f00000000000000000000000000000000000000000000000000000000000000001692634036176a92610561928692016106cb565b600060405180830381600087803b15801561057b57600080fd5b505af115801561058f573d6000803e3d6000fd5b505050506040513d6000823e601f3d9081017fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe01682016040526105d591908101906105d9565b5050565b6000602082840312156105ea578081fd5b815167ffffffffffffffff80821115610601578283fd5b818401915084601f830112610614578283fd5b815181811115610622578384fd5b60405160207fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe0601f8401168201018181108482111715610660578586fd5b604052818152838201602001871015610677578485fd5b61068882602083016020870161075f565b9695505050505050565b6000602082840312156106a3578081fd5b5051919050565b73ffffffffffffffffffffffffffffffffffffffff91909116815260200190565b600073ffffffffffffffffffffffffffffffffffffffff8416825260406020830152825180604084015261070681606085016020870161075f565b601f017fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe016919091016060019392505050565b73ffffffffffffffffffffffffffffffffffffffff929092168252602082015260400190565b60005b8381101561077a578181015183820152602001610762565b83811115610471575050600091015256fea264697066735822122022ef52312e84e78a4f93dc70d3394474e7cd7e5604cf6c42c006a69e71b7327264736f6c63430007010033"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import Task from '../../src/task';
import { TaskRunOptions } from '../../src/types';
import { FeeDistributorBALClaimerDeployment } from './input';

export default async (task: Task, { force, from }: TaskRunOptions = {}): Promise<void> => {
const input = task.input() as FeeDistributorBALClaimerDeployment;

const args = [input.FeeDistributor, input.Gauge, input.AuthorizerAdaptor];
await task.deployAndVerify('FeeDistributorBALClaimer', args, from, force);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import Task, { TaskMode } from '../../src/task';

export type FeeDistributorBALClaimerDeployment = {
AuthorizerAdaptor: string;
FeeDistributor: string;
Gauge: string;
};

const AuthorizerAdaptor = new Task('20220325-authorizer-adaptor', TaskMode.READ_ONLY);
const FeeDistributor = new Task('20220420-fee-distributor', TaskMode.READ_ONLY);

export default {
mainnet: {
AuthorizerAdaptor,
FeeDistributor,
Gauge: '0xE867AD0a48e8f815DC0cda2CDb275e0F163A480b', // veBAL SingleRecipientGauge
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# 2022-05-17 - Fee Distributor BAL Claimer

Deployment of the Fee Distributor BAL Claimer, which allows anyone to checkpoint the veBAL emissions gauge and direct those funds into the Fee Distributor. This gives assurances to veBAL holders that they will receive their BAL from this gauge without having to rely on a multisig to send these funds to the Fee Distributor.

## Useful Files

- [Ethereum mainnet addresses](./output/mainnet.json)
- [`FeeDistributorBALClaimer` ABI](./abi/FeeDistributorBALClaimer.json)
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import hre from 'hardhat';
import { expect } from 'chai';
import { BigNumber, Contract } from 'ethers';

import { fp } from '@balancer-labs/v2-helpers/src/numbers';
import { currentWeekTimestamp } from '@balancer-labs/v2-helpers/src/time';
import * as expectEvent from '@balancer-labs/v2-helpers/src/test/expectEvent';

import Task, { TaskMode } from '../../../src/task';
import { getForkedNetwork } from '../../../src/test';
import { impersonate } from '../../../src/signers';
import { actionId } from '@balancer-labs/v2-helpers/src/models/misc/actions';

describe('FeeDistributorBALClaimer', function () {
let authorizerAdaptor: Contract;
let balTokenHolder: Contract;
let feeDistributor: Contract;
let feeDistributorBALClaimer: Contract;

let BAL: Contract;

const task = new Task('20220518-fee-distributor-BAL-claimer', TaskMode.TEST, getForkedNetwork(hre));

const GOV_MULTISIG = '0x10A19e7eE7d7F8a52822f6817de8ea18204F2e4f';
const BAL_ADDRESS = '0xba100000625a3754423978a60c9317c58a424e3D';

const EXPECTED_GAUGE_EMISSIONS = BigNumber.from('14375600035874545749391');

before('run task', async () => {
await task.run({ force: true });
feeDistributorBALClaimer = await task.instanceAt(
'FeeDistributorBALClaimer',
task.output({ network: 'test' }).FeeDistributorBALClaimer
);
});

before('setup contracts', async () => {
const authorizerAdaptorTask = new Task('20220325-authorizer-adaptor', TaskMode.READ_ONLY, getForkedNetwork(hre));
authorizerAdaptor = await authorizerAdaptorTask.deployedInstance('AuthorizerAdaptor');

const feeDistributorTask = new Task('20220420-fee-distributor', TaskMode.READ_ONLY, getForkedNetwork(hre));
feeDistributor = await feeDistributorTask.deployedInstance('FeeDistributor');

const balTokenHolderFactoryTask = new Task(
'20220325-bal-token-holder-factory',
TaskMode.READ_ONLY,
getForkedNetwork(hre)
);
balTokenHolder = await balTokenHolderFactoryTask.instanceAt(
'BALTokenHolder',
await feeDistributorBALClaimer.getBALTokenHolder()
);

// We reuse this task as it contains an ABI similar to the one in real ERC20 tokens
const testBALTokenTask = new Task('20220325-test-balancer-token', TaskMode.READ_ONLY, getForkedNetwork(hre));
BAL = await testBALTokenTask.instanceAt('TestBalancerToken', BAL_ADDRESS);
});

before('grant permissions to feeDistributorBALClaimer', async () => {
const govMultisig = await impersonate(GOV_MULTISIG, fp(100));

const vaultTask = new Task('20210418-vault', TaskMode.READ_ONLY, getForkedNetwork(hre));
const vault = await vaultTask.deployedInstance('Vault');
const authorizer = await vaultTask.instanceAt('Authorizer', await vault.getAuthorizer());

const singleRecipientGaugeFactoryTask = new Task(
'20220325-single-recipient-gauge-factory',
TaskMode.READ_ONLY,
getForkedNetwork(hre)
);
const veBALGauge = await singleRecipientGaugeFactoryTask.instanceAt(
'SingleRecipientGauge',
feeDistributorBALClaimer.getGauge()
);

const checkpointRole = await actionId(authorizerAdaptor, 'checkpoint', veBALGauge.interface);
const withdrawFundsRole = await actionId(balTokenHolder, 'withdrawFunds');
await authorizer
.connect(govMultisig)
.grantRoles([checkpointRole, withdrawFundsRole], feeDistributorBALClaimer.address);
});

it('sends BAL to the FeeDistributor', async () => {
const currentWeek = currentWeekTimestamp();

const balToBeDistributedBefore = await feeDistributor.getTokensDistributedInWeek(BAL.address, currentWeek);
const tx = await feeDistributorBALClaimer.distributeBAL();
const balToBeDistributedAfter = await feeDistributor.getTokensDistributedInWeek(BAL.address, currentWeek);

expect(balToBeDistributedAfter).to.be.eq(balToBeDistributedBefore.add(EXPECTED_GAUGE_EMISSIONS));

expectEvent.inIndirectReceipt(
await tx.wait(),
feeDistributor.interface,
'TokenCheckpointed',
{
token: BAL.address,
amount: EXPECTED_GAUGE_EMISSIONS,
},
feeDistributor.address
);
});
});
3 changes: 3 additions & 0 deletions pkg/interfaces/contracts/standalone-utils/IBALTokenHolder.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@ pragma solidity >=0.7.0 <0.9.0;

import "../solidity-utils/helpers/IAuthentication.sol";
import "../solidity-utils/openzeppelin/IERC20.sol";
import "../liquidity-mining/IBalancerToken.sol";

interface IBALTokenHolder is IAuthentication {
function getBalancerToken() external view returns (IBalancerToken);

function getName() external view returns (string memory);

function withdrawFunds(address recipient, uint256 amount) external;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
// SPDX-License-Identifier: GPL-3.0-or-later
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

pragma solidity ^0.7.0;
pragma experimental ABIEncoderV2;

import "@balancer-labs/v2-interfaces/contracts/liquidity-mining/ISingleRecipientGauge.sol";
import "@balancer-labs/v2-interfaces/contracts/liquidity-mining/IFeeDistributor.sol";
import "@balancer-labs/v2-interfaces/contracts/standalone-utils/IBALTokenHolder.sol";

/**
* @title FeeDistributorBALClaimer
* @notice Atomically mints any outstanding BAL from a SingleRecipientGauge and transfers it to the FeeDistributor
* in order for it to be distributed among veBAL holders.
*/
contract FeeDistributorBALClaimer {
IERC20 private immutable _balToken;
IAuthorizerAdaptor private immutable _authorizerAdaptor;
IFeeDistributor private immutable _feeDistributor;
ISingleRecipientGauge private immutable _gauge;
IBALTokenHolder private immutable _balTokenHolder;

constructor(
IFeeDistributor feeDistributor,
ISingleRecipientGauge gauge,
IAuthorizerAdaptor authorizerAdaptor
) {
IBALTokenHolder balTokenHolder = IBALTokenHolder(gauge.getRecipient());

_balToken = balTokenHolder.getBalancerToken();
_authorizerAdaptor = authorizerAdaptor;
_feeDistributor = feeDistributor;
_gauge = gauge;
_balTokenHolder = balTokenHolder;
}

/**
* @notice Returns the address of the Balancer token contract.
*/
function getBalancerToken() external view returns (IERC20) {
return _balToken;
}

/**
* @notice Returns the address of the AuthorizerAdaptor contract.
*/
function getAuthorizerAdaptor() external view returns (IAuthorizerAdaptor) {
return _authorizerAdaptor;
}

/**
* @notice Returns the address of the FeeDistributor contract.
*/
function getFeeDistributor() external view returns (IFeeDistributor) {
return _feeDistributor;
}

/**
* @notice Returns the address of the associated SingleRecipientGauge contract.
*/
function getGauge() external view returns (ISingleRecipientGauge) {
return _gauge;
}

/**
* @notice Returns the address of the associated BALTokenHolder contract.
*/
function getBALTokenHolder() external view returns (IBALTokenHolder) {
return _balTokenHolder;
}

/**
* @notice Mint any outstanding BAL emissions and send them to the FeeDistributor
TomAFrench marked this conversation as resolved.
Show resolved Hide resolved
* @dev In order to call this function the `FeeDistributorBALClaimer` must be authorized to:
* - Withdraw BAL from the linked BALTokenHolder
* - Checkpoint the associated SingleRecipientGauge in order to mint BAL.
*/
function distributeBAL() external {
_checkpointGauge(_gauge);

// We checkpoint before and after depositing tokens to ensure that the BAL is assigned to the correct week.
_feeDistributor.checkpointToken(_balToken);
_balTokenHolder.withdrawFunds(address(_feeDistributor), _balToken.balanceOf(address(_balTokenHolder)));
_feeDistributor.checkpointToken(_balToken);
nventuro marked this conversation as resolved.
Show resolved Hide resolved
}

function _checkpointGauge(IStakelessGauge gauge) private {
_authorizerAdaptor.performAction(address(gauge), abi.encodeWithSelector(IStakelessGauge.checkpoint.selector));
}
}
6 changes: 1 addition & 5 deletions pkg/pool-linear/contracts/test/MockUnbuttonERC20.sol
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,7 @@ contract MockUnbuttonERC20 is ERC20, IButtonWrapper {

function initialize(uint256 initialRate) public {
uint256 mintAmount = INITIAL_DEPOSIT * initialRate;
IERC20(_underlying).safeTransferFrom(
msg.sender,
address(this),
INITIAL_DEPOSIT
);
IERC20(_underlying).safeTransferFrom(msg.sender, address(this), INITIAL_DEPOSIT);
_mint(address(this), mintAmount);
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/standalone-utils/contracts/BALTokenHolder.sol
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ contract BALTokenHolder is IBALTokenHolder, SingletonAuthentication {
_name = name;
}

function getBalancerToken() external view returns (IBalancerToken) {
function getBalancerToken() external view override returns (IBalancerToken) {
return _balancerToken;
}

Expand Down