I tried calling this function :
function createBorrowerCentricService (address _borrowToken) public returns (uint256) {
BorrowService storage borrowService = borrowingServices[numberOfBorrowingServices];
++numberOfBorrowingServices;
borrowService.lenderAddress = msg.sender;
borrowService.borrowToken = _borrowToken;
if (borrowService.borrowToken != 0x0000000000000000000000000000000000000000) {
borrowService.lenderBalance = IERC20(_borrowToken).balanceOf(msg.sender);
} else {
borrowService.lenderBalance = (msg.sender).balance;
}
borrowService.borrowAmount = 0;
borrowService.nftCollateral = 0x0000000000000000000000000000000000000000;
borrowService.borrowPeriod = 0;
borrowService.serviceID = numberOfBorrowingServices - 1;
return numberOfBorrowingServices - 1;
}
For _borrowToken
, when I passed in 0x0000000000000000000000000000000000000000 (the Ethereum null address), obviously, borrowService.lenderBalance = (msg.sender).balance;
is invoked, and the function is succesfully called
But when I passed in the address of an ERC20 token (DAI), I got the error :
revert - The transaction has been reverted to the initial state.
Note: The called function should be payable if you send value and the value you send should be less than your current balance.
I figured out that there might be an error with the way I'm doing things in
borrowService.lenderBalance = IERC20(_borrowToken).balanceOf(msg.sender);
What is the way to find the balance of a specific ERC20 token for a wallet address in solidity?
Here is the entire code for refrence :
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.14;
import "@openzeppelin/contracts/interfaces/IERC20.sol";
contract Popcorn {
struct BorrowService {
address lenderAddress;
address borrowToken;
uint256 lenderBalance;
uint256 borrowAmount;
address nftCollateral;
uint256 borrowPeriod;
uint256 serviceID;
}
mapping(uint256 => BorrowService) public borrowingServices;
uint256 public numberOfBorrowingServices = 0;
function createBorrowerCentricService (address _borrowToken) public returns (uint256) {
BorrowService storage borrowService = borrowingServices[numberOfBorrowingServices];
++numberOfBorrowingServices;
borrowService.lenderAddress = msg.sender;
borrowService.borrowToken = _borrowToken;
if (borrowService.borrowToken != 0x0000000000000000000000000000000000000000) {
borrowService.lenderBalance = IERC20(_borrowToken).balanceOf(msg.sender);
} else {
borrowService.lenderBalance = (msg.sender).balance;
}
borrowService.borrowAmount = 0;
borrowService.nftCollateral = 0x0000000000000000000000000000000000000000;
borrowService.borrowPeriod = 0;
borrowService.serviceID = numberOfBorrowingServices - 1;
return numberOfBorrowingServices - 1;
}
function getServices() public view returns (BorrowService[] memory) {
BorrowService[] memory borrowService = new BorrowService[](numberOfBorrowingServices);
for (uint256 i = 0; i < numberOfBorrowingServices; ++i) {
BorrowService storage item = borrowingServices[i];
borrowService[i] = item;
}
return borrowService;
}
}
Any help would be appreciated, thanks.