Hi im using an instance of a pool contract where you can call the function "addliquidity" from. But when i call the "addliquidity" function which has a transferfrom, i get "ERC20: insufficient allowance", even though i have approved the contract with the token amount from the owner before calling it. Maybe its not possible to approve a child contract?
Contract A "Dex" ->
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
import "./Pool.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
contract DexV3 {
Pool public pool;
event poolAddress (address indexed _pooladdress);
event userAddress (address indexed _useraddress);
event userNumber (uint256 indexed _userNumber);
event userMessage (string indexed _message);
mapping (uint256 => Pool) public PoolMapping;
uint256 public counter;
function createPool(address liquidityToken, address tokenA) public payable {
pool = new Pool(liquidityToken, tokenA);
PoolMapping[counter] = pool;
emit poolAddress(address(pool));
counter += 1;
}
function _addLiquidity(uint256 _pool, uint256 _amount) public payable {
pool = Pool(address(PoolMapping[_pool]));
// tokenA.transferFrom(msg.sender, address(this), _amount);
emit userAddress(address(pool));
pool.addLiquidity(_amount, msg.sender);
// pool.addLiquidity(_amount).send({from: msg.sender});
}
function _removeLiquidity(uint256 _pool, uint256 _amount) public payable{
pool = PoolMapping[_pool];
pool.removeLiquidity(_amount);
}
}
Contract B "Pool" ->
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "./LiquidityToken.sol";
contract Pool is IERC20, LiquidityToken {
LiquidityToken public lpToken;
address public lpTokenAddress;
address public erc20TokenAddress;
mapping(address => uint256) WethMapping;
event tokenSwap(address indexed token, address indexed swapper, string indexed swap);
event liquidityPool( uint256 indexed amount, address indexed provider);
event liquidityWidthdraw( uint256 indexed _amount, address indexed _to);
event addressBalance(address indexed token, uint256 indexed _amount, address indexed _address);
event balanceCall( uint256 indexed _amount);
event userInPoolAddress(address indexed _caller);
constructor(address _lpToken, address _erc20TokenAddress) {
require(_lpToken != address(0));
lpTokenAddress = _lpToken;
erc20TokenAddress = _erc20TokenAddress;
}
function getReserve() public payable returns(uint256) {
uint256 balaceGet = IERC20(erc20TokenAddress).balanceOf(address(this));
emit balanceCall(balaceGet);
return balaceGet;
}
function getEthReserve() public payable returns(uint256) {
uint256 balaceGet = address(this).balance;
emit balanceCall(balaceGet);
return address(this).balance;
}
function addLiquidity (uint _amount, address _sender) public payable{
uint256 daiReserve = getReserve();
uint256 ethReserve = address(this).balance;
emit userInPoolAddress(msg.sender);
if(daiReserve == 0){
IERC20(erc20TokenAddress).transferFrom(_sender, address(this), _amount);
_mint(_sender, ethReserve);
emit liquidityPool( _amount, _sender);
}else{
uint256 _ethReserve = address(this).balance - msg.value;
uint256 acceptedLiquidityAmount = (_amount * daiReserve) / (_ethReserve);
require(_amount >= acceptedLiquidityAmount, "not accepted liquidity less then the minimum amount accepted");
IERC20(erc20TokenAddress).transferFrom(msg.sender, address(this), acceptedLiquidityAmount);
uint256 mintokens = (IERC20(erc20TokenAddress).totalSupply() * msg.value) / (_ethReserve);
_mint(msg.sender, mintokens);
emit liquidityPool( _amount, msg.sender);
}
}
}