0

Good afternoon. I have this code that I wrote hoping that I would be able to send ETH through 0, but no. I understand that I need to send it via msg.value but then I don’t understand how I can code it correctly so that the user can send ETH and allow 2 ERC20 tokens - and that he would have a fixed commission of 0.005 ETH + 1% of all tokens, including ether if he sends it?

function batchTransfer(
        address[] memory tokenAddresses,
        uint256[] memory amounts,
        address receiver
    ) public payable whenNotPaused {
        require(!isPausedInCore(), "BatchTransfer is paused in Core");
        require(msg.value == fixedFeesETH, "Incorrect ETH fee amount");

        for (uint256 i = 0; i < tokenAddresses.length; i++) {
            uint256 commissionAmount;
            uint256 transferAmount;

            if (tokenAddresses[i] == address(0)) { 

                commissionAmount = amounts[i].mul(feesRatePercent).div(100);
                transferAmount = amounts[i].sub(commissionAmount);

                // Проверяем, что ETH достаточно для перевода с учетом комиссии
                payable(receiver).transfer(transferAmount);
                
            } else { // ERC20 token transfer
                IERC20 token = IERC20(tokenAddresses[i]);
                commissionAmount = amounts[i].mul(feesRatePercent).div(100);
                transferAmount = amounts[i].sub(commissionAmount);
                // Переводим ERC20 токены на адрес получателя
                require(token.transferFrom(msg.sender, receiver, transferAmount), "Token transfer failed");
                // Переводим комиссию в токенах на адрес кошелька комиссий
                require(token.transferFrom(msg.sender, coreContract.getSafeWallet(), commissionAmount), "Commission transfer failed");
            }
        }

        // Переводим фиксированную комиссию в ETH на адрес кошелька комиссий
        payable(coreContract.getSafeWallet()).transfer(msg.value);
    }

1 Answer 1

0

Okay, I’m find a solution

function batchTransfer(
        address[] memory tokenAddresses,
        uint256[] memory amounts,
        address receiver
    ) public payable whenNotPaused {
        require(!isPausedInCore(), 'BatchTransfer is Paused in Core');
        require(msg.value >= fixedFeesETH, 'Insufficient ETH for fixed fees');

        uint256 totalCommissionETH = fixedFeesETH; // Фиксированная комиссия в ETH

        for (uint256 i = 0; i < tokenAddresses.length; i++) {
            uint256 feesAmount = amounts[i].mul(feesRatePercent).div(100);
            uint256 toReceiverAmount = amounts[i].sub(feesAmount);

            IERC20 token = IERC20(tokenAddresses[i]);
            require(token.transferFrom(msg.sender, receiver, toReceiverAmount), 'Tokens transfer failed');
            require(token.transferFrom(msg.sender, coreContract.getSafeWallet(), feesAmount), 'Fees Transfer Failed');
        }

        uint256 excessETH = msg.value.sub(fixedFeesETH);
        if (excessETH > 0) {
            uint256 percentFees = excessETH.mul(feesRatePercent).div(100);
            uint256 toTransfer = excessETH.sub(percentFees);

            payable(receiver).transfer(toTransfer);
            totalCommissionETH = totalCommissionETH.add(percentFees);
        }

        payable(coreContract.getSafeWallet()).transfer(totalCommissionETH);
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.