Created
June 7, 2021 19:27
-
-
Save KlonD90/5f63bdacc176e662ce4c245ffd9b2a75 to your computer and use it in GitHub Desktop.
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
pragma solidity 0.8.4; | |
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC20/IERC20.sol"; | |
contract AmirSender { | |
address public owner; | |
mapping(uint24 => uint256) public eth_transaction_map; | |
mapping(uint24 => uint256) public tokens_transaction_map; | |
constructor() { | |
owner = msg.sender; | |
} | |
modifier onlyOwner() { | |
require(msg.sender == owner, "Owner"); | |
_; | |
} | |
function position(uint24 transaction_num) private pure returns (uint16 wordPos, uint8 bitPos) { | |
wordPos = uint16(transaction_num >> 8); | |
bitPos = uint8(transaction_num % 256); | |
} | |
function _transferFrom(IERC20 token, address from, address to, uint256 amount) internal { | |
token.transferFrom(from, to, amount); | |
} | |
function sendEthToMany( | |
address payable[] memory targets, | |
uint24[] memory ids, | |
uint256[] memory amounts | |
) external payable { | |
uint arrayLength = targets.length; | |
require(targets.length > 0, "should have targets"); | |
require(targets.length == ids.length, "should be targets and ids same length"); | |
require(amounts.length == ids.length, "should be amounts and ids same length"); | |
uint256 sum = 0; | |
uint i=0; | |
for (; i<arrayLength; i++) { | |
(uint16 wordPos, uint8 bitPos) = position(ids[i]); | |
uint256 mask = 1 << bitPos; | |
uint256 masked = eth_transaction_map[wordPos] & mask; | |
require(masked == 0, "Already sent transaction"); | |
targets[i].send(amounts[i]); | |
sum += amounts[i]; | |
eth_transaction_map[wordPos] ^= mask; | |
} | |
require(msg.value == sum, "All money sent"); | |
} | |
function sendTokensToMany( | |
IERC20[] memory tokens, | |
address[] memory targets, | |
uint24[] memory ids, | |
uint256[] memory amounts | |
) external { | |
uint arrayLength = targets.length; | |
require(targets.length > 0, "should have targets"); | |
require(targets.length == tokens.length, "should have targets and tokens same length"); | |
require(targets.length == ids.length, "should be targets and ids same length"); | |
require(amounts.length == ids.length, "should be amounts and ids same length"); | |
uint i=0; | |
for (; i<arrayLength; i++) { | |
(uint16 wordPos, uint8 bitPos) = position(ids[i]); | |
uint256 mask = 1 << bitPos; | |
uint256 masked = tokens_transaction_map[wordPos] & mask; | |
require(masked == 0, "Already sent transaction"); | |
_transferFrom(tokens[i], msg.sender, targets[i], amounts[i]); | |
tokens_transaction_map[wordPos] ^= mask; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment