Im currently creating a DEX where users swap tokens. However I have to use IERC20 transferfrom( address sender, address recipient, uint256 amount). But for this to happen the 3rd party(1inch) will need permission to approve and withdraw.
Questions/Problems:
- What address should be sender/from? -(mine or 1inch?)
- what address should be the recipient/to?- (mine or 1inch?)
- _tokenAddress is token that the user wants. Is this correct?
**My Current Code:**
pragma solidity ^0.8.0;
// SPDX-License-Identifier: MIT
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/utils/math/SafeMath.sol";
contract testTransfer{
using SafeMath for uint256;
mapping(address => uint) balances;
address private Commowner = payable (0xAb8483F64d9C6d1EcF9b849Ae677dD3315835cb2) ;// account that take commission
uint256 public commission = 1; // we want 1%
function transferFrom(address from, address to, uint amount, address _tokenAddress ) public payable {
uint fee = amount * commission / 10000; // we want 1%
// 0x11 is 1inch (sender/from), msg.sender/account (to) the address trying to recieve tokens, 1 is the amount,
//_tokenAddress the erc20 token that we want.
IERC20(_tokenAddress).transferFrom(from, to, amount);
balances[from] = balances[from].sub(fee);
balances[Commowner] = balances[Commowner].add(fee); // we get the commission.
// emit transfer(from,to,value);
}}
Thanks for your help Dee
transferFrombehavior https://ethereum.stackexchange.com/questions/46457/send-tokens-using-approve-and-transferfrom-vs-only-transfer. In some cases you need to usetransferand in other casestransferFrom. – Ismael Aug 14 '22 at 15:44