I am new to Solidity and trying out this contract on a custom test-net chain:
<code>contract TokenTransfer {
using SafeERC20 for IERC20;
IERC20 private _token;
constructor() {
_token = IERC20(0x2aebcdc4F9f9149a50422Fff86198Cb0939Ea165);
}
function transferTokens() external {
// Ensure the from account has given enough allowance to the contract executing the transfer.
uint256 amount = 1 * 10 ** 6; // 1 USDC in the smallest unit
_token.approve(address(this), amount);
require(
_token.allowance(msg.sender, address(this)) >= amount,
"Transfer amount exceeds allowance"
);
// Execute the transfer from the from account to the to account.
_token.transfer(0x88b291B4C4C339F85a6cd353b3f2b9724ad53801, amount);
}
}
</code>
<code>contract TokenTransfer {
using SafeERC20 for IERC20;
IERC20 private _token;
constructor() {
_token = IERC20(0x2aebcdc4F9f9149a50422Fff86198Cb0939Ea165);
}
function transferTokens() external {
// Ensure the from account has given enough allowance to the contract executing the transfer.
uint256 amount = 1 * 10 ** 6; // 1 USDC in the smallest unit
_token.approve(address(this), amount);
require(
_token.allowance(msg.sender, address(this)) >= amount,
"Transfer amount exceeds allowance"
);
// Execute the transfer from the from account to the to account.
_token.transfer(0x88b291B4C4C339F85a6cd353b3f2b9724ad53801, amount);
}
}
</code>
contract TokenTransfer {
using SafeERC20 for IERC20;
IERC20 private _token;
constructor() {
_token = IERC20(0x2aebcdc4F9f9149a50422Fff86198Cb0939Ea165);
}
function transferTokens() external {
// Ensure the from account has given enough allowance to the contract executing the transfer.
uint256 amount = 1 * 10 ** 6; // 1 USDC in the smallest unit
_token.approve(address(this), amount);
require(
_token.allowance(msg.sender, address(this)) >= amount,
"Transfer amount exceeds allowance"
);
// Execute the transfer from the from account to the to account.
_token.transfer(0x88b291B4C4C339F85a6cd353b3f2b9724ad53801, amount);
}
}
Somehow I am always hitting the Transfer amount exceeds allowance
error. If I understand correctly, I am approving the contract to spend amount on behalf of the sender.
So what am I missing ?