Skip to content

Commit

Permalink
rating agency v2 distributor (#504)
Browse files Browse the repository at this point in the history
Co-authored-by: Hal Hyatt <[email protected]>
  • Loading branch information
hal909 and feewet committed Feb 19, 2021
1 parent 882f065 commit 0cfcd50
Showing 1 changed file with 90 additions and 0 deletions.
90 changes: 90 additions & 0 deletions contracts/truefi/distributors/RatingAgencyV2Distributor.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.6.10;

import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import {SafeMath} from "@openzeppelin/contracts/math/SafeMath.sol";

import {Ownable} from "../common/UpgradeableOwnable.sol";
import {IArbitraryDistributor} from "../interface/IArbitraryDistributor.sol";

/**
* @title ArbitraryTrueDistributor
* @notice Distribute TRU to a smart contract
* @dev Allows for arbitrary claiming of TRU by a farm contract
*
* Contracts are registered to receive distributions. Once registered,
* a farm contract can claim TRU from the distributor.
* - Owner can withdraw funds in case distribution need to be re-allocated
*/
contract RatingAgencyV2Distributor is IArbitraryDistributor, Ownable {
using SafeMath for uint256;

// ================ WARNING ==================
// ===== THIS CONTRACT IS INITIALIZABLE ======
// === STORAGE VARIABLES ARE DECLARED BELOW ==
// REMOVAL OR REORDER OF VARIABLES WILL RESULT
// ========= IN STORAGE CORRUPTION ===========

IERC20 public trustToken;
address public override beneficiary;
uint256 public override amount;
uint256 public override remaining;
mapping(address => bool) public beneficiaries;

// ======= STORAGE DECLARATION END ============

event Distributed(uint256 amount);
event BeneficiaryStatusChanged(address beneficiary, bool status);

/**
* @dev Initialize distributor
* @param _beneficiary Address for distribution
* @param _trustToken TRU address
* @param _amount Amount to distribute
*/
function initialize(
address _beneficiary,
IERC20 _trustToken,
uint256 _amount
) public initializer {
Ownable.initialize();
trustToken = _trustToken;
beneficiary = _beneficiary;
amount = 500000000000000; // 5M
remaining = 30000000000000000; // 300M
}

/**
* @dev Only beneficiary can receive TRU
*/
modifier onlyBeneficiary {
// prettier-ignore
require(msg.sender == beneficiary || beneficiaries[msg.sender],
"ArbitraryDistributor: Only beneficiary can receive tokens");
_;
}

function setBeneficiaryStatus(address _beneficiary, bool _status) public {
beneficiaries[_beneficiary] = _status;
emit BeneficiaryStatusChanged(_beneficiary, _status);
}

/**
* @dev Distribute arbitrary number of tokens
* @param _amount Amount of TRU to distribute
*/
function distribute(uint256 _amount) public override onlyBeneficiary {
remaining = remaining.sub(_amount);
require(trustToken.transfer(msg.sender, _amount));

emit Distributed(_amount);
}

/**
* @dev Withdraw funds (for instance if owner decides to create a new distribution) and end distribution cycle
*/
function empty() public override onlyOwner {
remaining = 0;
require(trustToken.transfer(msg.sender, trustToken.balanceOf(address(this))));
}
}

0 comments on commit 0cfcd50

Please sign in to comment.