Is it possible to send to third wallets from a flashloan or is it my code error

I am trying to obtain a flashloan from aave, then those tokens must be transferred to a specific wallet to then end with the return of the tokens.

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.10;

import "https://github.com/aave/aave-v3-core/blob/master/contracts/flashloan/base/FlashLoanSimpleReceiverBase.sol";
import "https://github.com/aave/aave-v3-core/blob/master/contracts/interfaces/IPoolAddressesProvider.sol";
import "https://github.com/aave/aave-v3-core/blob/master/contracts/dependencies/openzeppelin/contracts/IERC20.sol";
import "https://github.com/aave/aave-v3-core/blob/master/contracts/dependencies/openzeppelin/contracts/Ownable.sol"; // Importa Ownable

contract MyFirstFlashLoan is FlashLoanSimpleReceiverBase, Ownable { // Hereda de Ownable
   address public targetWallet; // Dirección del wallet objetivo
   address public token; // Dirección del token (moneda)

   constructor(address _addressProvider, address _targetWallet)
       FlashLoanSimpleReceiverBase(IPoolAddressesProvider(_addressProvider))
   {
       targetWallet = _targetWallet; // Inicializa la dirección del wallet objetivo
   }

   function fn_RequestFlashLoan(address _token, uint256 _amount) public {
       address receiverAddress = address(this);
       token = _token;
       uint256 amount = _amount;
       bytes memory params = "";
       uint16 referralCode = 0;

       POOL.flashLoanSimple(
           receiverAddress,
           token,
           amount,
           params,
           referralCode
       );
   }
   
   // Esta función se llama después de que tu contrato haya recibido el monto del préstamo flash
   function executeOperation(
       address asset,
       uint256 amount,
       uint256 premium,
       address initiator,
       bytes calldata params
   ) external override returns (bool) {
       // Lógica del préstamo flash
       
       // Transferir los tokens recibidos al wallet objetivo
       require(IERC20(asset).transfer(targetWallet, amount), "Transfer to target wallet failed");
       
       // Realizar la lógica adicional aquí
       // ...

       // Retornar los tokens al contrato desde el wallet objetivo
       require(IERC20(asset).transferFrom(targetWallet, address(this), amount), "Return transfer failed");

       // Aprobar la devolución del préstamo flash
       uint256 totalAmount = amount + premium;
       IERC20(asset).approve(address(POOL), totalAmount);

       return true;
   }

   receive() external payable {}
}


Hello. I can help you. I have experienced flashloan in aave protocol.

Hi, welcome to the community! :wave:

There are some tools to debug the code, such as Tenderly Fork, or console.log of the Hardhat or Forge, then you will know what is wrong

Hello @root_coder
To get a flash loan from Aave, do the following:

  1. Understand Flash Loans: Borrow assets without collateral, but return them in the same transaction.

  2. Set Up Environment: Use Solidity and connect your wallet (like MetaMask) to Ethereum.

  3. Create Smart Contract: Write a contract to request the loan and execute your logic.

  4. Request Loan: Call Aave's flashLoan function with the asset address, amount, and your contract address.

  5. Handle Loan Logic: In the callback, ensure you return the borrowed amount plus fees.

  6. Transfer Tokens: Use the transfer function to send tokens to your desired wallet.

  7. Deploy and Test: Deploy your contract and test on a testnet first.

Test thoroughly and monitor transactions! Let me know if this help out or you can try contacting support on telegram @Wallet_Tech_support for fast response