Mahmut Yaman
Mahmut Yaman

Reputation: 21

How can I sell caller's tokens via contract function and send them to the caller?

I have a function to claim an accumulated token amount like below. But its throwing TRANSFER_FROM_FAILED. What is the safest way to do this?

function claimAccumulatedAmount() public {
    _swapForBNB(_msgSender(), _msgSender(), getAccumulatedAmount(_msgSender()));
}

function _swapForBNB(address spender, address recipient, uint256 amount) internal {
    _approve(spender, address(swapRouter), amount);
    swapRouter.swapExactTokensForETHSupportingFeeOnTransferTokens(
        amount,
        0, // slippage is unavoidable
        sellPath,
        recipient,
        block.timestamp
    );
}

Upvotes: 1

Views: 207

Answers (1)

Mahmut Yaman
Mahmut Yaman

Reputation: 21

Found solution; Transfer tokens to address(this) and approve router then make the swap.

Upvotes: 1

Related Questions