I am now developing a ICO smart contract which receives USDT tokens from other people and withdraw.
My withdraw function in contract is below.
function withdraw() external onlyOwner onlyContractHasMoney {
// here, contract will send usdt to _wallet
uint balance = _usdtToken.balanceOf(address(this));
_usdtToken.transfer(_wallet, balance);
}
And here is USDT transfer code that I wrote for localhost test purpose.
function transfer(address _to, uint _value) public onlyPayloadSize(2 * 32) {
console.log("msg sender of transfering is %s", msg.sender);
uint fee = (_value.mul(basisPointsRate)).div(10000);
if (fee > maximumFee) {
fee = maximumFee;
}
uint sendAmount = _value.sub(fee);
console.log("balance of msg sendger is %s and transfering value is %s, receiving address is %s", balances[msg.sender], _value, _to);
balances[msg.sender] = balances[msg.sender].sub(_value);
balances[_to] = balances[_to].add(sendAmount);
if (fee > 0) {
balances[owner] = balances[owner].add(fee);
Transfer(msg.sender, owner, fee);
}
Transfer(msg.sender, _to, sendAmount);
console.log("there is no problem until yet");
}
And my javascript withdraw scripts is below.
const hre = require("hardhat");
const ICO = require('../../clients/artifacts/contracts/ico.json');
async function main() {
const icoAddress = ICO.address;
const icoABI = ICO.abi;
const [signer] = await ethers.getSigners();
const ico = new hre.ethers.Contract(icoAddress, icoABI, signer);
await ico.withdraw();
}
main()
.then(() => process.exit(0))
.catch((error) => {
console.error(error);
process.exit(1);
});
But when I run the script, I am getting this error.
As you can see, USDT transfer transaction reverted because of UNPREDICTABLE_GAS_LIMIT, not usdt transfer code itself. I am not sure how can I resolve this issue. Do you have any idea to solve this problem? Please help me!