Last active
May 14, 2022 16:31
-
-
Save m-bo-one/eba50ec5ea18e570c57d7b1f88201b38 to your computer and use it in GitHub Desktop.
Disperse tokens
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
// SPDX-License-Identifier: MIT | |
pragma solidity ^0.8.0; | |
import "@openzeppelin/contracts/token/ERC20/IERC20.sol"; | |
import "@openzeppelin/contracts/utils/Context.sol"; | |
contract Disperse is Context { | |
function disperseEther( | |
address payable[] calldata recipients, | |
uint256[] calldata values | |
) external payable { | |
for (uint256 i = 0; i < recipients.length; i++) { | |
recipients[i].transfer(values[i]); | |
} | |
uint256 balance = address(this).balance; | |
if (balance > 0) { | |
payable(_msgSender()).transfer(balance); | |
} | |
} | |
function disperseToken( | |
IERC20 token, | |
address[] calldata recipients, | |
uint256[] calldata values | |
) external { | |
uint256 total = 0; | |
for (uint256 i = 0; i < recipients.length; i++) { | |
total += values[i]; | |
} | |
require( | |
token.transferFrom(_msgSender(), address(this), total), | |
"D: Failed to get tokens" | |
); | |
for (uint256 j = 0; j < recipients.length; j++) { | |
require( | |
token.transfer(recipients[j], values[j]), | |
"D: Failed to transfer tokens" | |
); | |
} | |
} | |
function disperseTokenSimple( | |
IERC20 token, | |
address[] calldata recipients, | |
uint256[] calldata values | |
) external { | |
for (uint256 i = 0; i < recipients.length; i++) { | |
require( | |
token.transferFrom(_msgSender(), recipients[i], values[i]), | |
"D: Failed to transfer tokens" | |
); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment