I'm doing a flash loan request and trying to emit some debug information using events out of the function, but I haven't seen those messages emitted on the arbitrum block explorer or been able to retrieve them. It's almost like they're not being sent. In most of my test cases, the flashloan has failed in the payback part--and my understanding is that the transaction gets rolled back, but does that include previous emits? And if so, is there a way to get this information on what was done prior to the rollback. I'm struggling for a way to debug this while on the arbitrum mainnet. console.log doesn't seem to work either.
// SPDX-License-Identifier: MIT
pragma solidity 0.8.24;
import {FlashLoanSimpleReceiverBase} from "@aave/core-
v3/contracts/flashloan/base/FlashLoanSimpleReceiverBase.sol";
import {IPoolAddressesProvider} from "@aave/core-
v3/contracts/interfaces/IPoolAddressesProvider.sol";
import {IERC20} from "@aave/core-
v3/contracts/dependencies/openzeppelin/contracts/IERC20.sol";
contract FLASend is FlashLoanSimpleReceiverBase {
address payable owner;
constructor(address _addressProviderFlashLoanProviderPool)
FlashLoanSimpleReceiverBase(IPoolAddressesProvider(_addressProviderFlashLoanProviderPool)) {
owner = payable(msg.sender);
}
function requestFlashLoan(address _loanTokenAddress, uint256 _loanTokenAmount) public {
address receiverAddress = address(this);
address asset = _loanTokenAddress;
uint256 amount = _loanTokenAmount;
bytes memory params = "";
uint16 referralCode = 0;
emit LendAddress(block.timestamp, address(this), "Flash Loan Requested--Loan Token Address", asset);
emit LendNumeric(block.timestamp, address(this), "Flash Loan Requested--Loan Amount", amount);
emit LendAddress(block.timestamp, address(this), "Flash Loan Requested--Loan Amount", receiverAddress);
POOL.flashLoanSimple(
receiverAddress,
asset,
amount,
params,
referralCode
);
emit LendString(block.timestamp, address(this), "Flash Loan Received", "INFO");
}
/**
This function is called after your contract has received the flash loaned amount
*/
function executeOperation(
address asset,
uint256 amount,
uint256 premium,
address initiator,
bytes calldata params
) external override returns (bool) {
uint256 token1Balance = 0;
uint256 i = 0;
bool lastSwap = false;
// At the end of your logic above, this contract owes
// the flashloaned amount + premiums.
// Therefore ensure your contract has enough to repay
// these amounts.
// I have logic here that is currently failing to payback the loan.
emit LendString(block.timestamp, address(this), "Flash will fail on payback", "INFO");
// Approve the Pool contract allowance to *pull* the owed amount
uint256 amountOwed = amount + premium;
IERC20(asset).approve(address(POOL), amountOwed);
return true;
}
function getBalance(address _tokenAddress) external view returns (uint256) {
return IERC20(_tokenAddress).balanceOf(address(this));
}
function withdraw(address _tokenAddress) external onlyOwner {
IERC20 token = IERC20(_tokenAddress);
token.transfer(msg.sender, token.balanceOf(address(this)));
}
modifier onlyOwner() {
require(
msg.sender == owner,
"Only the contract owner can call this function"
);
_;
}
event LendAddress(uint256 timestamp, address sourceAddress, string message, address addressValue);
event LendNumeric(uint256 timestamp, address sourceAddress, string message, uint numericValue);
event LendString(uint256 timestamp, address sourceAddress, string message, string stringValue);
receive() external payable {}
}