-
Notifications
You must be signed in to change notification settings - Fork 0
sidechain distribution contract #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
gfe3wo9yt8
wants to merge
3
commits into
main
Choose a base branch
from
sidechain_distribution
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,83 @@ | ||
| // SPDX-License-Identifier: bsl-1.1 | ||
| /** | ||
| * Copyright 2022 Unit Protocol V2: Artem Zakharov (hello@unit.xyz). | ||
| */ | ||
| pragma solidity ^0.8.0; | ||
|
|
||
| import "@openzeppelin/contracts/token/ERC20/IERC20.sol"; | ||
| import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol"; | ||
| import "@openzeppelin/contracts/access/Ownable.sol"; | ||
| import "@openzeppelin/contracts/security/ReentrancyGuard.sol"; | ||
|
|
||
| /** | ||
| * @notice Rewards distribution in sidechains to veDUCK holders | ||
| * Snapshot of holders in mainnet is taken once | ||
| */ | ||
| contract veDistributionSnapshot is Ownable, ReentrancyGuard { | ||
| using SafeERC20 for IERC20; | ||
|
|
||
| /** @notice balance of user from snapshot */ | ||
| mapping(address => uint) public balanceOf; | ||
| /** @notice users list */ | ||
| address[] public users; | ||
| /** @notice sum of users' balances from snapshot */ | ||
| uint public totalSupply; | ||
|
|
||
| /** @notice amounts of reward token already sent to all users */ | ||
| mapping(IERC20 => uint) public rewardsSent; | ||
| /** @notice amounts of reward token already sent to user */ | ||
| mapping(IERC20 => mapping (address => uint)) public rewardsSentToUser; | ||
|
|
||
| event RewardSent(IERC20 token, address user, uint amount); | ||
Eenae marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| /** | ||
| * @notice add users' balances from snapshot | ||
| * @dev after all balances added `renounceOwnership` must be called | ||
| */ | ||
| function addBalances(address[] calldata users_, uint[] calldata balances_) public onlyOwner { | ||
| require(users_.length > 0, "DISTRIBUTION: EMPTY_ARRAYS"); | ||
| require(users_.length == balances_.length, "DISTRIBUTION: INVALID_ARRAYS_LENGTH"); | ||
|
|
||
| for (uint i; i < users_.length; i++) { | ||
| require(balances_[i] > 0, "DISTRIBUTION: INVALID_AMOUNT"); | ||
| require(balanceOf[users_[i]] == 0, "DISTRIBUTION: USER_ALREADY_ADDED"); | ||
|
|
||
| balanceOf[users_[i]] = balances_[i]; | ||
| totalSupply += balances_[i]; | ||
| users.push(users_[i]); | ||
| } | ||
| } | ||
|
|
||
| function usersCount() public view returns (uint) { | ||
| return users.length; | ||
| } | ||
|
|
||
| function allUsers() public view returns (address[] memory) { | ||
| return users; | ||
| } | ||
|
|
||
| function withdrawReward(IERC20[] calldata tokens_) public { | ||
Eenae marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| require(owner() == address(0), 'DISTRIBUTION: CONTRACT_IS_NOT_FINALIZED'); | ||
|
|
||
| for (uint i; i < tokens_.length; i++) { | ||
| _withdrawReward(tokens_[i]); | ||
| } | ||
| } | ||
|
|
||
| function _withdrawReward(IERC20 token_) internal nonReentrant { | ||
| uint userBalance = balanceOf[msg.sender]; | ||
| require(userBalance > 0, 'DISTRIBUTION: AUTH_FAILED'); | ||
|
|
||
| uint totalRewardsReceived = rewardsSent[token_] + token_.balanceOf(address(this)); | ||
|
|
||
| uint totalUserReward = totalRewardsReceived * userBalance / totalSupply; | ||
| require(totalUserReward > rewardsSentToUser[token_][msg.sender], 'DISTRIBUTION: NOTHING_TO_WITHDRAW'); | ||
|
|
||
| uint amountToSend = totalUserReward - rewardsSentToUser[token_][msg.sender]; | ||
| rewardsSentToUser[token_][msg.sender] += amountToSend; | ||
| rewardsSent[token_] += amountToSend; | ||
|
|
||
| token_.safeTransfer(msg.sender, amountToSend); | ||
| emit RewardSent(token_, msg.sender, amountToSend); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.