Update docs
This commit is contained in:
103
docs/modules/api/examples/ERC4626Fees.sol
Normal file
103
docs/modules/api/examples/ERC4626Fees.sol
Normal file
@ -0,0 +1,103 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
pragma solidity ^0.8.20;
|
||||
|
||||
import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
|
||||
import {ERC4626} from "@openzeppelin/contracts/token/ERC20/extensions/ERC4626.sol";
|
||||
import {SafeERC20} from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
|
||||
import {Math} from "@openzeppelin/contracts/utils/math/Math.sol";
|
||||
|
||||
/// @dev ERC4626 vault with entry/exit fees expressed in https://en.wikipedia.org/wiki/Basis_point[basis point (bp)].
|
||||
abstract contract ERC4626Fees is ERC4626 {
|
||||
using Math for uint256;
|
||||
|
||||
uint256 private constant _BASIS_POINT_SCALE = 1e4;
|
||||
|
||||
// === Overrides ===
|
||||
|
||||
/// @dev Preview taking an entry fee on deposit. See {IERC4626-previewDeposit}.
|
||||
function previewDeposit(uint256 assets) public view virtual override returns (uint256) {
|
||||
uint256 fee = _feeOnTotal(assets, _entryFeeBasisPoints());
|
||||
return super.previewDeposit(assets - fee);
|
||||
}
|
||||
|
||||
/// @dev Preview adding an entry fee on mint. See {IERC4626-previewMint}.
|
||||
function previewMint(uint256 shares) public view virtual override returns (uint256) {
|
||||
uint256 assets = super.previewMint(shares);
|
||||
return assets + _feeOnRaw(assets, _entryFeeBasisPoints());
|
||||
}
|
||||
|
||||
/// @dev Preview adding an exit fee on withdraw. See {IERC4626-previewWithdraw}.
|
||||
function previewWithdraw(uint256 assets) public view virtual override returns (uint256) {
|
||||
uint256 fee = _feeOnRaw(assets, _exitFeeBasisPoints());
|
||||
return super.previewWithdraw(assets + fee);
|
||||
}
|
||||
|
||||
/// @dev Preview taking an exit fee on redeem. See {IERC4626-previewRedeem}.
|
||||
function previewRedeem(uint256 shares) public view virtual override returns (uint256) {
|
||||
uint256 assets = super.previewRedeem(shares);
|
||||
return assets - _feeOnTotal(assets, _exitFeeBasisPoints());
|
||||
}
|
||||
|
||||
/// @dev Send entry fee to {_entryFeeRecipient}. See {IERC4626-_deposit}.
|
||||
function _deposit(address caller, address receiver, uint256 assets, uint256 shares) internal virtual override {
|
||||
uint256 fee = _feeOnTotal(assets, _entryFeeBasisPoints());
|
||||
address recipient = _entryFeeRecipient();
|
||||
|
||||
super._deposit(caller, receiver, assets, shares);
|
||||
|
||||
if (fee > 0 && recipient != address(this)) {
|
||||
SafeERC20.safeTransfer(IERC20(asset()), recipient, fee);
|
||||
}
|
||||
}
|
||||
|
||||
/// @dev Send exit fee to {_exitFeeRecipient}. See {IERC4626-_deposit}.
|
||||
function _withdraw(
|
||||
address caller,
|
||||
address receiver,
|
||||
address owner,
|
||||
uint256 assets,
|
||||
uint256 shares
|
||||
) internal virtual override {
|
||||
uint256 fee = _feeOnRaw(assets, _exitFeeBasisPoints());
|
||||
address recipient = _exitFeeRecipient();
|
||||
|
||||
super._withdraw(caller, receiver, owner, assets, shares);
|
||||
|
||||
if (fee > 0 && recipient != address(this)) {
|
||||
SafeERC20.safeTransfer(IERC20(asset()), recipient, fee);
|
||||
}
|
||||
}
|
||||
|
||||
// === Fee configuration ===
|
||||
|
||||
function _entryFeeBasisPoints() internal view virtual returns (uint256) {
|
||||
return 0; // replace with e.g. 100 for 1%
|
||||
}
|
||||
|
||||
function _exitFeeBasisPoints() internal view virtual returns (uint256) {
|
||||
return 0; // replace with e.g. 100 for 1%
|
||||
}
|
||||
|
||||
function _entryFeeRecipient() internal view virtual returns (address) {
|
||||
return address(0); // replace with e.g. a treasury address
|
||||
}
|
||||
|
||||
function _exitFeeRecipient() internal view virtual returns (address) {
|
||||
return address(0); // replace with e.g. a treasury address
|
||||
}
|
||||
|
||||
// === Fee operations ===
|
||||
|
||||
/// @dev Calculates the fees that should be added to an amount `assets` that does not already include fees.
|
||||
/// Used in {IERC4626-mint} and {IERC4626-withdraw} operations.
|
||||
function _feeOnRaw(uint256 assets, uint256 feeBasisPoints) private pure returns (uint256) {
|
||||
return assets.mulDiv(feeBasisPoints, _BASIS_POINT_SCALE, Math.Rounding.Ceil);
|
||||
}
|
||||
|
||||
/// @dev Calculates the fee part of an amount `assets` that already includes fees.
|
||||
/// Used in {IERC4626-deposit} and {IERC4626-redeem} operations.
|
||||
function _feeOnTotal(uint256 assets, uint256 feeBasisPoints) private pure returns (uint256) {
|
||||
return assets.mulDiv(feeBasisPoints, feeBasisPoints + _BASIS_POINT_SCALE, Math.Rounding.Ceil);
|
||||
}
|
||||
}
|
||||
81
docs/modules/api/examples/governance/MyGovernor.sol
Normal file
81
docs/modules/api/examples/governance/MyGovernor.sol
Normal file
@ -0,0 +1,81 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
pragma solidity ^0.8.20;
|
||||
|
||||
import {IGovernor, Governor} from "@openzeppelin/contracts/governance/Governor.sol";
|
||||
import {GovernorCountingSimple} from "@openzeppelin/contracts/governance/extensions/GovernorCountingSimple.sol";
|
||||
import {GovernorVotes} from "@openzeppelin/contracts/governance/extensions/GovernorVotes.sol";
|
||||
import {GovernorVotesQuorumFraction} from "@openzeppelin/contracts/governance/extensions/GovernorVotesQuorumFraction.sol";
|
||||
import {GovernorTimelockControl} from "@openzeppelin/contracts/governance/extensions/GovernorTimelockControl.sol";
|
||||
import {TimelockController} from "@openzeppelin/contracts/governance/TimelockController.sol";
|
||||
import {IVotes} from "@openzeppelin/contracts/governance/utils/IVotes.sol";
|
||||
import {IERC165} from "@openzeppelin/contracts/interfaces/IERC165.sol";
|
||||
|
||||
contract MyGovernor is
|
||||
Governor,
|
||||
GovernorCountingSimple,
|
||||
GovernorVotes,
|
||||
GovernorVotesQuorumFraction,
|
||||
GovernorTimelockControl
|
||||
{
|
||||
constructor(
|
||||
IVotes _token,
|
||||
TimelockController _timelock
|
||||
) Governor("MyGovernor") GovernorVotes(_token) GovernorVotesQuorumFraction(4) GovernorTimelockControl(_timelock) {}
|
||||
|
||||
function votingDelay() public pure override returns (uint256) {
|
||||
return 7200; // 1 day
|
||||
}
|
||||
|
||||
function votingPeriod() public pure override returns (uint256) {
|
||||
return 50400; // 1 week
|
||||
}
|
||||
|
||||
function proposalThreshold() public pure override returns (uint256) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// The functions below are overrides required by Solidity.
|
||||
|
||||
function state(uint256 proposalId) public view override(Governor, GovernorTimelockControl) returns (ProposalState) {
|
||||
return super.state(proposalId);
|
||||
}
|
||||
|
||||
function proposalNeedsQueuing(
|
||||
uint256 proposalId
|
||||
) public view virtual override(Governor, GovernorTimelockControl) returns (bool) {
|
||||
return super.proposalNeedsQueuing(proposalId);
|
||||
}
|
||||
|
||||
function _queueOperations(
|
||||
uint256 proposalId,
|
||||
address[] memory targets,
|
||||
uint256[] memory values,
|
||||
bytes[] memory calldatas,
|
||||
bytes32 descriptionHash
|
||||
) internal override(Governor, GovernorTimelockControl) returns (uint48) {
|
||||
return super._queueOperations(proposalId, targets, values, calldatas, descriptionHash);
|
||||
}
|
||||
|
||||
function _executeOperations(
|
||||
uint256 proposalId,
|
||||
address[] memory targets,
|
||||
uint256[] memory values,
|
||||
bytes[] memory calldatas,
|
||||
bytes32 descriptionHash
|
||||
) internal override(Governor, GovernorTimelockControl) {
|
||||
super._executeOperations(proposalId, targets, values, calldatas, descriptionHash);
|
||||
}
|
||||
|
||||
function _cancel(
|
||||
address[] memory targets,
|
||||
uint256[] memory values,
|
||||
bytes[] memory calldatas,
|
||||
bytes32 descriptionHash
|
||||
) internal override(Governor, GovernorTimelockControl) returns (uint256) {
|
||||
return super._cancel(targets, values, calldatas, descriptionHash);
|
||||
}
|
||||
|
||||
function _executor() internal view override(Governor, GovernorTimelockControl) returns (address) {
|
||||
return super._executor();
|
||||
}
|
||||
}
|
||||
21
docs/modules/api/examples/governance/MyToken.sol
Normal file
21
docs/modules/api/examples/governance/MyToken.sol
Normal file
@ -0,0 +1,21 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
pragma solidity ^0.8.20;
|
||||
|
||||
import {ERC20} from "@openzeppelin/contracts/token/ERC20/ERC20.sol";
|
||||
import {ERC20Permit} from "@openzeppelin/contracts/token/ERC20/extensions/ERC20Permit.sol";
|
||||
import {ERC20Votes} from "@openzeppelin/contracts/token/ERC20/extensions/ERC20Votes.sol";
|
||||
import {Nonces} from "@openzeppelin/contracts/utils/Nonces.sol";
|
||||
|
||||
contract MyToken is ERC20, ERC20Permit, ERC20Votes {
|
||||
constructor() ERC20("MyToken", "MTK") ERC20Permit("MyToken") {}
|
||||
|
||||
// The functions below are overrides required by Solidity.
|
||||
|
||||
function _update(address from, address to, uint256 amount) internal override(ERC20, ERC20Votes) {
|
||||
super._update(from, to, amount);
|
||||
}
|
||||
|
||||
function nonces(address owner) public view virtual override(ERC20Permit, Nonces) returns (uint256) {
|
||||
return super.nonces(owner);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,32 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
pragma solidity ^0.8.20;
|
||||
|
||||
import {ERC20} from "@openzeppelin/contracts/token/ERC20/ERC20.sol";
|
||||
import {ERC20Permit} from "@openzeppelin/contracts/token/ERC20/extensions/ERC20Permit.sol";
|
||||
import {ERC20Votes} from "@openzeppelin/contracts/token/ERC20/extensions/ERC20Votes.sol";
|
||||
import {Nonces} from "@openzeppelin/contracts/utils/Nonces.sol";
|
||||
|
||||
contract MyTokenTimestampBased is ERC20, ERC20Permit, ERC20Votes {
|
||||
constructor() ERC20("MyTokenTimestampBased", "MTK") ERC20Permit("MyTokenTimestampBased") {}
|
||||
|
||||
// Overrides IERC6372 functions to make the token & governor timestamp-based
|
||||
|
||||
function clock() public view override returns (uint48) {
|
||||
return uint48(block.timestamp);
|
||||
}
|
||||
|
||||
// solhint-disable-next-line func-name-mixedcase
|
||||
function CLOCK_MODE() public pure override returns (string memory) {
|
||||
return "mode=timestamp";
|
||||
}
|
||||
|
||||
// The functions below are overrides required by Solidity.
|
||||
|
||||
function _update(address from, address to, uint256 amount) internal override(ERC20, ERC20Votes) {
|
||||
super._update(from, to, amount);
|
||||
}
|
||||
|
||||
function nonces(address owner) public view virtual override(ERC20Permit, Nonces) returns (uint256) {
|
||||
return super.nonces(owner);
|
||||
}
|
||||
}
|
||||
28
docs/modules/api/examples/governance/MyTokenWrapped.sol
Normal file
28
docs/modules/api/examples/governance/MyTokenWrapped.sol
Normal file
@ -0,0 +1,28 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
pragma solidity ^0.8.20;
|
||||
|
||||
import {IERC20, ERC20} from "@openzeppelin/contracts/token/ERC20/ERC20.sol";
|
||||
import {ERC20Permit} from "@openzeppelin/contracts/token/ERC20/extensions/ERC20Permit.sol";
|
||||
import {ERC20Votes} from "@openzeppelin/contracts/token/ERC20/extensions/ERC20Votes.sol";
|
||||
import {ERC20Wrapper} from "@openzeppelin/contracts/token/ERC20/extensions/ERC20Wrapper.sol";
|
||||
import {Nonces} from "@openzeppelin/contracts/utils/Nonces.sol";
|
||||
|
||||
contract MyTokenWrapped is ERC20, ERC20Permit, ERC20Votes, ERC20Wrapper {
|
||||
constructor(
|
||||
IERC20 wrappedToken
|
||||
) ERC20("MyTokenWrapped", "MTK") ERC20Permit("MyTokenWrapped") ERC20Wrapper(wrappedToken) {}
|
||||
|
||||
// The functions below are overrides required by Solidity.
|
||||
|
||||
function decimals() public view override(ERC20, ERC20Wrapper) returns (uint8) {
|
||||
return super.decimals();
|
||||
}
|
||||
|
||||
function _update(address from, address to, uint256 amount) internal override(ERC20, ERC20Votes) {
|
||||
super._update(from, to, amount);
|
||||
}
|
||||
|
||||
function nonces(address owner) public view virtual override(ERC20Permit, Nonces) returns (uint256) {
|
||||
return super.nonces(owner);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user