-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
107 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
pragma solidity ^0.8.20; | ||
|
||
import "OZ/token/ERC20/utils/SafeERC20.sol"; | ||
import "./IncomeVaultInvariantStorage.sol"; | ||
import "CMTAT/interfaces/ICMTATSnapshot.sol"; | ||
/** | ||
* @title Internal functions | ||
*/ | ||
abstract contract IncomeVaultInternal is IncomeVaultInvariantStorage { | ||
// CMTAT token | ||
ICMTATSnapshot public CMTAT_TOKEN; | ||
IERC20 public ERC20TokenPayment; | ||
mapping(address => mapping (uint256 => bool)) public claimedDividend; | ||
mapping(uint256 => uint256) public segregatedDividend; | ||
mapping(uint256 => bool) public segregatedClaim; | ||
uint256 public timeLimitToWithdraw; | ||
|
||
// Security | ||
using SafeERC20 for IERC20; | ||
|
||
/** | ||
* @param time dividend time | ||
* @param tokenHolders addresses to compute dividend | ||
* @param tokenHoldersBalance the sender balance | ||
* @param tokenTotalSupply the total supply | ||
*/ | ||
function _computeDividendBatch(uint256 time, address[] calldata tokenHolders, uint256[] memory tokenHoldersBalance, uint256 tokenTotalSupply) internal view returns(uint256[] memory tokenHolderDividend){ | ||
tokenHolderDividend = new uint256[](tokenHolders.length); | ||
uint256 dividendTotalSupply = segregatedDividend[time]; | ||
for(uint256 i = 0; i < tokenHolders.length; ++i){ | ||
if(tokenHoldersBalance[i] > 0) { | ||
tokenHolderDividend[i] = (tokenHoldersBalance[i] * dividendTotalSupply) / tokenTotalSupply; | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* @param time dividend time | ||
* @param senderBalance token holder balance | ||
* @param tokenTotalSupply the total supply | ||
*/ | ||
function _computeDividend(uint256 time, uint256 senderBalance, uint256 tokenTotalSupply) internal view returns(uint256 tokenHolderDividend){ | ||
if (senderBalance == 0){ | ||
revert IncomeVault_NoDividendToClaim(); | ||
} | ||
/** | ||
SenderBalance = 300 | ||
totalSupply = 900 | ||
Dividend total supply= 200 | ||
dividend = (300 * 200) / 900 = 60000 / 900 = 600/9 = 66.6 = 66 | ||
*/ | ||
uint256 dividendTotalSupply = segregatedDividend[time]; | ||
|
||
tokenHolderDividend = (senderBalance * dividendTotalSupply) / tokenTotalSupply; | ||
} | ||
|
||
/** | ||
* @param time dividend time | ||
* @param tokenHolder addresses to send the dividends | ||
* @param tokenHolderDividend the computed dividends | ||
*/ | ||
function _transferDividend(uint256 time, address tokenHolder, uint256 tokenHolderDividend) internal{ | ||
// Before ERC-20 transfer to avoid re-entrancy attack | ||
claimedDividend[tokenHolder][time] = true; | ||
emit DividendClaimed(time, tokenHolder, tokenHolderDividend); | ||
// transfer | ||
// We don't revert if SenderBalance == 0 to record the claim | ||
if(tokenHolderDividend != 0){ | ||
// Will revert in case of failure | ||
// We should put that in a try catch for the batch version ??? | ||
ERC20TokenPayment.safeTransfer(tokenHolder, tokenHolderDividend); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
pragma solidity ^0.8.20; | ||
|
||
abstract contract IncomeVaultInvariantStorage { | ||
// Role | ||
bytes32 public constant INCOME_VAULT_OPERATOR_ROLE = keccak256("INCOME_VAULT_OPERATOR_ROLE"); | ||
bytes32 public constant INCOME_VAULT_DEPOSIT_ROLE = keccak256("INCOME_VAULT_DEPOSIT_ROLE"); | ||
bytes32 public constant INCOME_VAULT_DISTRIBUTE_ROLE = keccak256("INCOME_VAULT_DEPOSIT_ROLE"); | ||
bytes32 public constant INCOME_VAULT_WITHDRAW_ROLE = keccak256("INCOME_VAULT_WITHDRAW_ROLE"); | ||
|
||
// errors | ||
error IncomeVault_ClaimNotActivated(); | ||
error IncomeVault_DividendAlreadyClaimed(); | ||
error IncomeVault_NoDividendToClaim(); | ||
error IncomeVault_AdminWithAddressZeroNotAllowed(); | ||
error IncomeVault_TokenPaymentWithAddressZeroNotAllowed(); | ||
error IncomeVault_CMTATWithAddressZeroNotAllowed(); | ||
error IncomeVault_FailApproval(); | ||
error IncomeVault_NoAmountSend(); | ||
error IncomeVault_NotEnoughAmount(); | ||
error IncomeVault_TokenBalanceIsZero(); | ||
error IncomeVault_TooLateToWithdraw(uint256 currentTime); | ||
error IncomeVault_TooEarlyToWithdraw(uint256 currentTime); | ||
|
||
// event | ||
event newDeposit(uint256 indexed time, address indexed sender, uint256 dividend); | ||
event DividendClaimed(uint256 indexed time, address indexed sender, uint256 dividend); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters