mirror of
https://github.com/th30d4y/OpenLearnX.git
synced 2026-05-26 19:26:33 +00:00
Fix .gitignore: stop tracking ignored files
This commit is contained in:
@@ -0,0 +1,214 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
// OpenZeppelin Contracts (last updated v4.8.0) (finance/PaymentSplitter.sol)
|
||||
|
||||
pragma solidity ^0.8.0;
|
||||
|
||||
import "../token/ERC20/utils/SafeERC20.sol";
|
||||
import "../utils/Address.sol";
|
||||
import "../utils/Context.sol";
|
||||
|
||||
/**
|
||||
* @title PaymentSplitter
|
||||
* @dev This contract allows to split Ether payments among a group of accounts. The sender does not need to be aware
|
||||
* that the Ether will be split in this way, since it is handled transparently by the contract.
|
||||
*
|
||||
* The split can be in equal parts or in any other arbitrary proportion. The way this is specified is by assigning each
|
||||
* account to a number of shares. Of all the Ether that this contract receives, each account will then be able to claim
|
||||
* an amount proportional to the percentage of total shares they were assigned. The distribution of shares is set at the
|
||||
* time of contract deployment and can't be updated thereafter.
|
||||
*
|
||||
* `PaymentSplitter` follows a _pull payment_ model. This means that payments are not automatically forwarded to the
|
||||
* accounts but kept in this contract, and the actual transfer is triggered as a separate step by calling the {release}
|
||||
* function.
|
||||
*
|
||||
* NOTE: This contract assumes that ERC20 tokens will behave similarly to native tokens (Ether). Rebasing tokens, and
|
||||
* tokens that apply fees during transfers, are likely to not be supported as expected. If in doubt, we encourage you
|
||||
* to run tests before sending real value to this contract.
|
||||
*/
|
||||
contract PaymentSplitter is Context {
|
||||
event PayeeAdded(address account, uint256 shares);
|
||||
event PaymentReleased(address to, uint256 amount);
|
||||
event ERC20PaymentReleased(IERC20 indexed token, address to, uint256 amount);
|
||||
event PaymentReceived(address from, uint256 amount);
|
||||
|
||||
uint256 private _totalShares;
|
||||
uint256 private _totalReleased;
|
||||
|
||||
mapping(address => uint256) private _shares;
|
||||
mapping(address => uint256) private _released;
|
||||
address[] private _payees;
|
||||
|
||||
mapping(IERC20 => uint256) private _erc20TotalReleased;
|
||||
mapping(IERC20 => mapping(address => uint256)) private _erc20Released;
|
||||
|
||||
/**
|
||||
* @dev Creates an instance of `PaymentSplitter` where each account in `payees` is assigned the number of shares at
|
||||
* the matching position in the `shares` array.
|
||||
*
|
||||
* All addresses in `payees` must be non-zero. Both arrays must have the same non-zero length, and there must be no
|
||||
* duplicates in `payees`.
|
||||
*/
|
||||
constructor(address[] memory payees, uint256[] memory shares_) payable {
|
||||
require(payees.length == shares_.length, "PaymentSplitter: payees and shares length mismatch");
|
||||
require(payees.length > 0, "PaymentSplitter: no payees");
|
||||
|
||||
for (uint256 i = 0; i < payees.length; i++) {
|
||||
_addPayee(payees[i], shares_[i]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev The Ether received will be logged with {PaymentReceived} events. Note that these events are not fully
|
||||
* reliable: it's possible for a contract to receive Ether without triggering this function. This only affects the
|
||||
* reliability of the events, and not the actual splitting of Ether.
|
||||
*
|
||||
* To learn more about this see the Solidity documentation for
|
||||
* https://solidity.readthedocs.io/en/latest/contracts.html#fallback-function[fallback
|
||||
* functions].
|
||||
*/
|
||||
receive() external payable virtual {
|
||||
emit PaymentReceived(_msgSender(), msg.value);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Getter for the total shares held by payees.
|
||||
*/
|
||||
function totalShares() public view returns (uint256) {
|
||||
return _totalShares;
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Getter for the total amount of Ether already released.
|
||||
*/
|
||||
function totalReleased() public view returns (uint256) {
|
||||
return _totalReleased;
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Getter for the total amount of `token` already released. `token` should be the address of an IERC20
|
||||
* contract.
|
||||
*/
|
||||
function totalReleased(IERC20 token) public view returns (uint256) {
|
||||
return _erc20TotalReleased[token];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Getter for the amount of shares held by an account.
|
||||
*/
|
||||
function shares(address account) public view returns (uint256) {
|
||||
return _shares[account];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Getter for the amount of Ether already released to a payee.
|
||||
*/
|
||||
function released(address account) public view returns (uint256) {
|
||||
return _released[account];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Getter for the amount of `token` tokens already released to a payee. `token` should be the address of an
|
||||
* IERC20 contract.
|
||||
*/
|
||||
function released(IERC20 token, address account) public view returns (uint256) {
|
||||
return _erc20Released[token][account];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Getter for the address of the payee number `index`.
|
||||
*/
|
||||
function payee(uint256 index) public view returns (address) {
|
||||
return _payees[index];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Getter for the amount of payee's releasable Ether.
|
||||
*/
|
||||
function releasable(address account) public view returns (uint256) {
|
||||
uint256 totalReceived = address(this).balance + totalReleased();
|
||||
return _pendingPayment(account, totalReceived, released(account));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Getter for the amount of payee's releasable `token` tokens. `token` should be the address of an
|
||||
* IERC20 contract.
|
||||
*/
|
||||
function releasable(IERC20 token, address account) public view returns (uint256) {
|
||||
uint256 totalReceived = token.balanceOf(address(this)) + totalReleased(token);
|
||||
return _pendingPayment(account, totalReceived, released(token, account));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Triggers a transfer to `account` of the amount of Ether they are owed, according to their percentage of the
|
||||
* total shares and their previous withdrawals.
|
||||
*/
|
||||
function release(address payable account) public virtual {
|
||||
require(_shares[account] > 0, "PaymentSplitter: account has no shares");
|
||||
|
||||
uint256 payment = releasable(account);
|
||||
|
||||
require(payment != 0, "PaymentSplitter: account is not due payment");
|
||||
|
||||
// _totalReleased is the sum of all values in _released.
|
||||
// If "_totalReleased += payment" does not overflow, then "_released[account] += payment" cannot overflow.
|
||||
_totalReleased += payment;
|
||||
unchecked {
|
||||
_released[account] += payment;
|
||||
}
|
||||
|
||||
Address.sendValue(account, payment);
|
||||
emit PaymentReleased(account, payment);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Triggers a transfer to `account` of the amount of `token` tokens they are owed, according to their
|
||||
* percentage of the total shares and their previous withdrawals. `token` must be the address of an IERC20
|
||||
* contract.
|
||||
*/
|
||||
function release(IERC20 token, address account) public virtual {
|
||||
require(_shares[account] > 0, "PaymentSplitter: account has no shares");
|
||||
|
||||
uint256 payment = releasable(token, account);
|
||||
|
||||
require(payment != 0, "PaymentSplitter: account is not due payment");
|
||||
|
||||
// _erc20TotalReleased[token] is the sum of all values in _erc20Released[token].
|
||||
// If "_erc20TotalReleased[token] += payment" does not overflow, then "_erc20Released[token][account] += payment"
|
||||
// cannot overflow.
|
||||
_erc20TotalReleased[token] += payment;
|
||||
unchecked {
|
||||
_erc20Released[token][account] += payment;
|
||||
}
|
||||
|
||||
SafeERC20.safeTransfer(token, account, payment);
|
||||
emit ERC20PaymentReleased(token, account, payment);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev internal logic for computing the pending payment of an `account` given the token historical balances and
|
||||
* already released amounts.
|
||||
*/
|
||||
function _pendingPayment(
|
||||
address account,
|
||||
uint256 totalReceived,
|
||||
uint256 alreadyReleased
|
||||
) private view returns (uint256) {
|
||||
return (totalReceived * _shares[account]) / _totalShares - alreadyReleased;
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Add a new payee to the contract.
|
||||
* @param account The address of the payee to add.
|
||||
* @param shares_ The number of shares owned by the payee.
|
||||
*/
|
||||
function _addPayee(address account, uint256 shares_) private {
|
||||
require(account != address(0), "PaymentSplitter: account is the zero address");
|
||||
require(shares_ > 0, "PaymentSplitter: shares are 0");
|
||||
require(_shares[account] == 0, "PaymentSplitter: account already has shares");
|
||||
|
||||
_payees.push(account);
|
||||
_shares[account] = shares_;
|
||||
_totalShares = _totalShares + shares_;
|
||||
emit PayeeAdded(account, shares_);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
= Finance
|
||||
|
||||
[.readme-notice]
|
||||
NOTE: This document is better viewed at https://docs.openzeppelin.com/contracts/api/finance
|
||||
|
||||
This directory includes primitives for financial systems:
|
||||
|
||||
- {PaymentSplitter} allows to split Ether and ERC20 payments among a group of accounts. The sender does not need to be
|
||||
aware that the assets will be split in this way, since it is handled transparently by the contract. The split can be
|
||||
in equal parts or in any other arbitrary proportion.
|
||||
|
||||
- {VestingWallet} handles the vesting of Ether and ERC20 tokens for a given beneficiary. Custody of multiple tokens can
|
||||
be given to this contract, which will release the token to the beneficiary following a given, customizable, vesting
|
||||
schedule.
|
||||
|
||||
== Contracts
|
||||
|
||||
{{PaymentSplitter}}
|
||||
|
||||
{{VestingWallet}}
|
||||
@@ -0,0 +1,145 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
// OpenZeppelin Contracts (last updated v4.9.0) (finance/VestingWallet.sol)
|
||||
pragma solidity ^0.8.0;
|
||||
|
||||
import "../token/ERC20/utils/SafeERC20.sol";
|
||||
import "../utils/Address.sol";
|
||||
import "../utils/Context.sol";
|
||||
|
||||
/**
|
||||
* @title VestingWallet
|
||||
* @dev This contract handles the vesting of Eth and ERC20 tokens for a given beneficiary. Custody of multiple tokens
|
||||
* can be given to this contract, which will release the token to the beneficiary following a given vesting schedule.
|
||||
* The vesting schedule is customizable through the {vestedAmount} function.
|
||||
*
|
||||
* Any token transferred to this contract will follow the vesting schedule as if they were locked from the beginning.
|
||||
* Consequently, if the vesting has already started, any amount of tokens sent to this contract will (at least partly)
|
||||
* be immediately releasable.
|
||||
*/
|
||||
contract VestingWallet is Context {
|
||||
event EtherReleased(uint256 amount);
|
||||
event ERC20Released(address indexed token, uint256 amount);
|
||||
|
||||
uint256 private _released;
|
||||
mapping(address => uint256) private _erc20Released;
|
||||
address private immutable _beneficiary;
|
||||
uint64 private immutable _start;
|
||||
uint64 private immutable _duration;
|
||||
|
||||
/**
|
||||
* @dev Set the beneficiary, start timestamp and vesting duration of the vesting wallet.
|
||||
*/
|
||||
constructor(address beneficiaryAddress, uint64 startTimestamp, uint64 durationSeconds) payable {
|
||||
require(beneficiaryAddress != address(0), "VestingWallet: beneficiary is zero address");
|
||||
_beneficiary = beneficiaryAddress;
|
||||
_start = startTimestamp;
|
||||
_duration = durationSeconds;
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev The contract should be able to receive Eth.
|
||||
*/
|
||||
receive() external payable virtual {}
|
||||
|
||||
/**
|
||||
* @dev Getter for the beneficiary address.
|
||||
*/
|
||||
function beneficiary() public view virtual returns (address) {
|
||||
return _beneficiary;
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Getter for the start timestamp.
|
||||
*/
|
||||
function start() public view virtual returns (uint256) {
|
||||
return _start;
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Getter for the vesting duration.
|
||||
*/
|
||||
function duration() public view virtual returns (uint256) {
|
||||
return _duration;
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Amount of eth already released
|
||||
*/
|
||||
function released() public view virtual returns (uint256) {
|
||||
return _released;
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Amount of token already released
|
||||
*/
|
||||
function released(address token) public view virtual returns (uint256) {
|
||||
return _erc20Released[token];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Getter for the amount of releasable eth.
|
||||
*/
|
||||
function releasable() public view virtual returns (uint256) {
|
||||
return vestedAmount(uint64(block.timestamp)) - released();
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Getter for the amount of releasable `token` tokens. `token` should be the address of an
|
||||
* IERC20 contract.
|
||||
*/
|
||||
function releasable(address token) public view virtual returns (uint256) {
|
||||
return vestedAmount(token, uint64(block.timestamp)) - released(token);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Release the native token (ether) that have already vested.
|
||||
*
|
||||
* Emits a {EtherReleased} event.
|
||||
*/
|
||||
function release() public virtual {
|
||||
uint256 amount = releasable();
|
||||
_released += amount;
|
||||
emit EtherReleased(amount);
|
||||
Address.sendValue(payable(beneficiary()), amount);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Release the tokens that have already vested.
|
||||
*
|
||||
* Emits a {ERC20Released} event.
|
||||
*/
|
||||
function release(address token) public virtual {
|
||||
uint256 amount = releasable(token);
|
||||
_erc20Released[token] += amount;
|
||||
emit ERC20Released(token, amount);
|
||||
SafeERC20.safeTransfer(IERC20(token), beneficiary(), amount);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Calculates the amount of ether that has already vested. Default implementation is a linear vesting curve.
|
||||
*/
|
||||
function vestedAmount(uint64 timestamp) public view virtual returns (uint256) {
|
||||
return _vestingSchedule(address(this).balance + released(), timestamp);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Calculates the amount of tokens that has already vested. Default implementation is a linear vesting curve.
|
||||
*/
|
||||
function vestedAmount(address token, uint64 timestamp) public view virtual returns (uint256) {
|
||||
return _vestingSchedule(IERC20(token).balanceOf(address(this)) + released(token), timestamp);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Virtual implementation of the vesting formula. This returns the amount vested, as a function of time, for
|
||||
* an asset given its total historical allocation.
|
||||
*/
|
||||
function _vestingSchedule(uint256 totalAllocation, uint64 timestamp) internal view virtual returns (uint256) {
|
||||
if (timestamp < start()) {
|
||||
return 0;
|
||||
} else if (timestamp > start() + duration()) {
|
||||
return totalAllocation;
|
||||
} else {
|
||||
return (totalAllocation * (timestamp - start())) / duration();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user