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);
}
});