Solidity

如何從已部署的合約中將 ERC-20 代幣穩定地發送到多個地址?

  • August 24, 2022

這是我的 mulsend 合約程式碼,在這裡我想將令牌從另一個已部署的合約發送到多個地址,這是我已部署的合約:

https://rinkeby.etherscan.io/token/0x8fd22db75d8bc9a95c08bb4907f45e411a7b6251

提前謝謝。

pragma solidity ^0.5.0;
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v2.4.0/contracts/token/ERC20/IERC20.sol";
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v2.4.0/contracts/token/ERC20/SafeERC20.sol";
contract BatchSendERC20 {
   using SafeMath for uint256;
   using SafeERC20 for IERC20;
   
   address public owner;
   
   modifier onlyOwner(){
       require(msg.sender == owner);
       _;
   }
   
   constructor() public{
       owner = msg.sender;
   }
  
   //getowner
   function getOwner() public view returns (address) {
       return owner;
   }
   
   //get token balance
   function getTokenBalance(IERC20 token) public view returns (uint256) {
       return token.balanceOf(address(this));
   }
   
   //batch send different token amount from contract
   function multiSendDiffTokenFromContract(IERC20 token, address[] memory recipients, uint256[] memory amounts) public onlyOwner {
       
       require(recipients.length > 0);
       require(recipients.length == amounts.length);
       
       uint256 length = recipients.length;
       uint256 currentSum = 0;
       uint256 currentTokenBalance = token.balanceOf(address(this));
       
       for (uint256 i = 0; i < length; i++) {
           uint256 amount = amounts[i];
           require(amount > 0);
           currentSum = currentSum.add(amount);
           require(currentSum <= currentTokenBalance);
           
           token.safeTransfer(recipients[i], amount);
       }
   }
   
}

當我在部署合約後呼叫 multiSendDiffTokenFromContract 並給出顯示此錯誤消息的函式參數值“氣體估計錯誤並顯示以下消息(見下文)。交易執行可能會失敗。你想強制發送嗎?執行已恢復”。

嘿,我編寫了您的程式碼,它在本地混音環境中執行良好,正在嘗試部署然後呼叫它??,請發送錯誤截圖真的很有幫助

您必須計算您的合約中有足夠的代幣,然後將代幣發送給收件人,因此我為此添加了 totalSend 功能,請檢查此合約。

contract BatchSendERC20 {
   using SafeMath for uint256;
   using SafeERC20 for IERC20;

   address public owner;

   modifier onlyOwner(){
      require(msg.sender == owner);
      _;
   }

   constructor() public{
      owner = msg.sender;
   }

   //getowner
   function getOwner() public view returns (address) {
       return owner;
   }

   //get token balance
   function getTokenBalance(IERC20 token) public view returns (uint256) {
       return token.balanceOf(address(this));
   }

   function totalSend(uint256[] memory amounts) internal pure returns(uint256) {
         uint256 length = amounts.lebngth;
         uint256 totalAmount;
     for(uint256 i=0; i < length; i++) {
       totalAmount += amounts[i];
     }
     return totalAmount
   }
   //batch send different token amount from contract
   function multiSendDiffTokenFromContract(IERC20 token, address[]     memory recipients, uint256[] memory amounts) public onlyOwner {
   
       require(recipients.length > 0);
       require(recipients.length == amounts.length);
   
       uint256 length = recipients.length;
       uint256 totalToSend = totalSend(amounts);
       uint256 currentTokenBalance = token.balanceOf(address(this));
       require(currentTokenBalance >= totalToSend)
       for (uint256 i = 0; i < length; i++) {
           uint256 amount = amounts[i];
           require(amount > 0);
           token.safeTransfer(recipients[i], amount);
       }
   }

}

引用自:https://ethereum.stackexchange.com/questions/134220