Hello, Using ERC20 from OpenZepplin Wizard to create an ERC20 token.
We want the ability to send this token to several addresses at once on occasion.
Will this function sendBatchMyToken accomplish the goal of reducing gas costs?
Any other suggestions to improve this code are very welcome. Thank you
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.2;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
contract MyToken is ERC20 {
constructor() ERC20("MyToken", "MTC") payable {
_mint(msg.sender, 1000 * 10 ** decimals());
// test wallet1
_transfer(msg.sender, 0xAb8483F64d9C6d1EcF9b859Ae677dD3315835cb2, 50 * 10 ** 18);
}
event BatchTransferMyToken(address indexed _caller, uint256 _recipientCount, uint256 _totalTokensSent);
//
function sendBatchMyToken(address[] calldata walletAddresses, uint256[] calldata sendAmounts) payable external returns (bool) {
uint256 walletCount = walletAddresses.length;
require(walletCount == sendAmounts.length, "Check length of walletAddresses / sendAmounts arrays.");
uint256 senderBalance = balanceOf(msg.sender);
uint256 totalSentMyToken = 0;
uint256 sendAmount;
uint256 decimalPlaces = 10 ** 18;
for(uint i = 0; i < walletCount; i++) {
sendAmount = sendAmounts[i] * decimalPlaces;
require(sendAmount <= senderBalance, "Sending more then sender has remaining.");
_transfer(msg.sender, walletAddresses[i], sendAmount);
totalSentMyToken = totalSentMyToken + sendAmount;
senderBalance = senderBalance - sendAmount;
}
emit BatchTransferMyToken(msg.sender, walletAddresses.length, totalSentMyToken);
return true;
}
}