Skip to content

Commit

Permalink
Restructure contracts inheritance
Browse files Browse the repository at this point in the history
  • Loading branch information
MiniRoman committed Jun 18, 2024
1 parent f78b4ac commit eee2822
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 829 deletions.
6 changes: 3 additions & 3 deletions contracts/BackedTokenImplementation.sol
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ contract BackedTokenImplementation is OwnableUpgradeable, ERC20PermitDelegateTra
initialize("Backed Token Implementation", "BTI");
}

function initialize(string memory name_, string memory symbol_) public initializer {
function initialize(string memory name_, string memory symbol_) public virtual initializer {
__ERC20_init(name_, symbol_);
__Ownable_init();
_buildDomainSeparator();
Expand Down Expand Up @@ -149,7 +149,7 @@ contract BackedTokenImplementation is OwnableUpgradeable, ERC20PermitDelegateTra
uint8 v,
bytes32 r,
bytes32 s
) public override allowedDelegate {
) public virtual override allowedDelegate {
super.delegatedTransfer(owner, to, value, deadline, v, r, s);
}

Expand All @@ -172,7 +172,7 @@ contract BackedTokenImplementation is OwnableUpgradeable, ERC20PermitDelegateTra
* @param account The account from which the tokens will be burned
* @param amount The amount of tokens to be burned
*/
function burn(address account, uint256 amount) external {
function burn(address account, uint256 amount) virtual external {
require(_msgSender() == burner, "BackedToken: Only burner");
require(account == _msgSender() || account == address(this), "BackedToken: Cannot burn account");
_burn(account, amount);
Expand Down
244 changes: 9 additions & 235 deletions contracts/BackedTokenImplementationWithMultiplierAndAutoFeeAccrual.sol
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@

pragma solidity 0.8.9;

import "@openzeppelin/contracts-upgradeable-new/access/OwnableUpgradeable.sol";
import "@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol";
import "./ERC20PermitDelegateTransferWithMultiplier.sol";
import "./SanctionsList.sol";

Expand All @@ -56,30 +56,8 @@ import "./SanctionsList.sol";
*
*/

contract BackedTokenImplementationWithMultiplierAndAutoFeeAccrual is
OwnableUpgradeable,
ERC20PermitDelegateTransferWithMultiplier
contract BackedTokenImplementationWithMultiplierAndAutoFeeAccrual is ERC20PermitDelegateTransferWithMultiplier
{
string public constant VERSION = "1.1.0";

// Roles:
address public minter;
address public burner;
address public pauser;

// EIP-712 Delegate Functionality:
bool public delegateMode;
mapping(address => bool) public delegateWhitelist;

// Pause:
bool public isPaused;

// SanctionsList:
SanctionsList public sanctionsList;

// Terms:
string public terms;

// V2

// Roles:
Expand All @@ -91,26 +69,7 @@ contract BackedTokenImplementationWithMultiplierAndAutoFeeAccrual is
uint256 public periodLength;

// Events:
event NewMinter(address indexed newMinter);
event NewBurner(address indexed newBurner);
event NewPauser(address indexed newPauser);
event NewMultiplierUpdater(address indexed newMultiplierUpdater);
event NewSanctionsList(address indexed newSanctionsList);
event DelegateWhitelistChange(
address indexed whitelistAddress,
bool status
);
event DelegateModeChange(bool delegateMode);
event PauseModeChange(bool pauseMode);
event NewTerms(string newTerms);

modifier allowedDelegate() {
require(
delegateMode || delegateWhitelist[_msgSender()],
"BackedToken: Unauthorized delegate"
);
_;
}

modifier updateMultiplier() {
(uint256 newMultiplier, uint256 periodsPassed) = getCurrentMultiplier();
Expand All @@ -121,23 +80,16 @@ contract BackedTokenImplementationWithMultiplierAndAutoFeeAccrual is
_;
}

// constructor, call initializer to lock the implementation instance.
constructor() {
initialize(
"Backed Token Implementation",
"BTI"
);
}

function initialize(
string memory name_,
string memory symbol_
) public initializer {
) override public virtual initializer {
__ERC20_init(name_, symbol_);
__Multiplier_init();
__Ownable_init();
_buildDomainSeparator();
_setTerms("https://www.backedassets.fi/legal-documentation"); // Default Terms
__Multiplier_init();

periodLength = 24 * 3600; // Set to 24h by default
lastTimeFeeApplied = block.timestamp;
}
Expand Down Expand Up @@ -181,30 +133,6 @@ contract BackedTokenImplementationWithMultiplierAndAutoFeeAccrual is
return _totalShares * multiplier / 1e18;
}

/**
* @dev Update allowance with a signed permit. Allowed only if
* the sender is whitelisted, or the delegateMode is set to true
*
* @param owner Token owner's address (Authorizer)
* @param spender Spender's address
* @param value Amount of allowance
* @param deadline Expiration time, seconds since the epoch
* @param v v part of the signature
* @param r r part of the signature
* @param s s part of the signature
*/
function permit(
address owner,
address spender,
uint256 value,
uint256 deadline,
uint8 v,
bytes32 r,
bytes32 s
) public override allowedDelegate {
super.permit(owner, spender, value, deadline, v, r, s);
}

/**
* @dev Perform an intended transfer on one account's behalf, from another account,
* who actually pays fees for the transaction. Allowed only if the sender
Expand All @@ -226,7 +154,7 @@ contract BackedTokenImplementationWithMultiplierAndAutoFeeAccrual is
uint8 v,
bytes32 r,
bytes32 s
) public override allowedDelegate updateMultiplier {
) public override updateMultiplier {
super.delegatedTransfer(owner, to, value, deadline, v, r, s);
}

Expand All @@ -251,7 +179,7 @@ contract BackedTokenImplementationWithMultiplierAndAutoFeeAccrual is
uint8 v,
bytes32 r,
bytes32 s
) public virtual override updateMultiplier {
) public virtual override allowedDelegate updateMultiplier {
super.delegatedTransferShares(owner, to, value, deadline, v, r, s);
}

Expand Down Expand Up @@ -318,7 +246,7 @@ contract BackedTokenImplementationWithMultiplierAndAutoFeeAccrual is
function mint(
address account,
uint256 amount
) external virtual updateMultiplier {
) override external virtual updateMultiplier {
require(_msgSender() == minter, "BackedToken: Only minter");
uint256 sharesAmount = getSharesByUnderlyingAmount(amount);
_mintShares(account, sharesAmount);
Expand All @@ -331,7 +259,7 @@ contract BackedTokenImplementationWithMultiplierAndAutoFeeAccrual is
* @param account The account from which the tokens will be burned
* @param amount The amount of tokens to be burned
*/
function burn(address account, uint256 amount) external updateMultiplier {
function burn(address account, uint256 amount) override external virtual updateMultiplier {
require(_msgSender() == burner, "BackedToken: Only burner");
require(
account == _msgSender() || account == address(this),
Expand All @@ -350,56 +278,6 @@ contract BackedTokenImplementationWithMultiplierAndAutoFeeAccrual is
feePerPeriod = newFeePerPeriod;
}

/**
* @dev Function to set the pause in order to block or restore all
* transfers. Allowed only for pauser
*
* Emits a { PauseModeChange } event
*
* @param newPauseMode The new pause mode
*/
function setPause(bool newPauseMode) external {
require(_msgSender() == pauser, "BackedToken: Only pauser");
isPaused = newPauseMode;
emit PauseModeChange(newPauseMode);
}

/**
* @dev Function to change the contract minter. Allowed only for owner
*
* Emits a { NewMinter } event
*
* @param newMinter The address of the new minter
*/
function setMinter(address newMinter) external onlyOwner {
minter = newMinter;
emit NewMinter(newMinter);
}

/**
* @dev Function to change the contract burner. Allowed only for owner
*
* Emits a { NewBurner } event
*
* @param newBurner The address of the new burner
*/
function setBurner(address newBurner) external onlyOwner {
burner = newBurner;
emit NewBurner(newBurner);
}

/**
* @dev Function to change the contract pauser. Allowed only for owner
*
* Emits a { NewPauser } event
*
* @param newPauser The address of the new pauser
*/
function setPauser(address newPauser) external onlyOwner {
pauser = newPauser;
emit NewPauser(newPauser);
}

/**
* @dev Function to change the contract multiplier updater. Allowed only for owner
*
Expand Down Expand Up @@ -434,66 +312,6 @@ contract BackedTokenImplementationWithMultiplierAndAutoFeeAccrual is
_updateMultiplier(newMultiplier);
}

/**
* @dev Function to change the contract Senctions List. Allowed only for owner
*
* Emits a { NewSanctionsList } event
*
* @param newSanctionsList The address of the new Senctions List following the Chainalysis standard
*/
function setSanctionsList(address newSanctionsList) external onlyOwner {
// Check the proposed sanctions list contract has the right interface:
require(
!SanctionsList(newSanctionsList).isSanctioned(address(this)),
"BackedToken: Wrong List interface"
);

sanctionsList = SanctionsList(newSanctionsList);
emit NewSanctionsList(newSanctionsList);
}

/**
* @dev EIP-712 Function to change the delegate status of account.
* Allowed only for owner
*
* Emits a { DelegateWhitelistChange } event
*
* @param whitelistAddress The address for which to change the delegate status
* @param status The new delegate status
*/
function setDelegateWhitelist(
address whitelistAddress,
bool status
) external onlyOwner {
delegateWhitelist[whitelistAddress] = status;
emit DelegateWhitelistChange(whitelistAddress, status);
}

/**
* @dev EIP-712 Function to change the contract delegate mode. Allowed
* only for owner
*
* Emits a { DelegateModeChange } event
*
* @param _delegateMode The new delegate mode for the contract
*/
function setDelegateMode(bool _delegateMode) external onlyOwner {
delegateMode = _delegateMode;

emit DelegateModeChange(_delegateMode);
}

/**
* @dev Function to change the contract terms. Allowed only for owner
*
* Emits a { NewTerms } event
*
* @param newTerms A string with the terms. Usually a web or IPFS link.
*/
function setTerms(string memory newTerms) external onlyOwner {
_setTerms(newTerms);
}

/**
* @dev Function to change the time of last fee accrual. Allowed only for owner
*
Expand All @@ -512,50 +330,6 @@ contract BackedTokenImplementationWithMultiplierAndAutoFeeAccrual is
periodLength = newPeriodLength;
}

// Implement setTerms, to allow also to use from initializer:
function _setTerms(string memory newTerms) internal virtual {
terms = newTerms;
emit NewTerms(newTerms);
}

// Implement the pause and SanctionsList functionality before transfer:
function _beforeTokenTransfer(
address from,
address to,
uint256 amount
) internal virtual override {
// Check not paused:
require(!isPaused, "BackedToken: token transfer while paused");

// Check Sanctions List, but do not prevent minting burning:
if (from != address(0) && to != address(0)) {
require(
!sanctionsList.isSanctioned(from),
"BackedToken: sender is sanctioned"
);
require(
!sanctionsList.isSanctioned(to),
"BackedToken: receiver is sanctioned"
);
}

super._beforeTokenTransfer(from, to, amount);
}

// Implement the SanctionsList functionality for spender:
function _spendAllowance(
address owner,
address spender,
uint256 amount
) internal virtual override {
require(
!sanctionsList.isSanctioned(spender),
"BackedToken: spender is sanctioned"
);

super._spendAllowance(owner, spender, amount);
}

/**
* @dev This empty reserved space is put in place to allow future versions to add new
* variables without shifting down storage in the inheritance chain.
Expand Down
Loading

0 comments on commit eee2822

Please sign in to comment.