I’m creating a token where Txes are taxed, but rather than having a bunch of token sitting around in a wallet that I’d have to sell before I could use for e.g. marketing I’d like to automatically convert to BNB as I go.
I only want to do this conversion on buys, as on sells it’s useful to reduce the amount of token sold (so it’s a little like fees to an LP provider). Because I’m unimaginative it’s based on Safemoon.
I’m having an issue whereby if I try to implement this it fails with every buy, however, if I change it so it only swaps into BNB on sells (changing nothing else) it works without problem.
For those familiar with Safemoon, this process is done in the _getValues function, where fees are calculated, calling this function:
function _takeFee (uint256 tFee, address recipient, bool transferAsBNB) private {
uint256 currentRate = _getRate();
uint256 rFee = tFee.mul(currentRate);
// if it's a sell then take token as normal
if (!transferAsBNB) {
_rOwned[recipient] = _rOwned[recipient].add(rFee);
if(_isExcluded[recipient])
_tOwned[recipient] = _tOwned[recipient].add(tFee);
emit FeeTransfer (tFee, recipient);
} else {
// if it's a buy swap to BNB first
// transfer tokens to the contract then we can swap them to BNB
// the contract address can't be excluded from the award so we only need to update the reflected balance
_rOwned[address(this)] = _rOwned[address(this)].add(rFee);
uint256 bnbReceived = swapTokensForBNB (tFee);
if (bnbReceived > 0)
payable(recipient).transfer(bnbReceived);
}
}
swapTokensToBNB is the same function that is used when adding to liquidity (check contract balance, swap, check contract balance again - that’s how much BNB you have). This function works when called as part of the swap & liquify process (but that is also only done on sells).
Basically is there something that stops me from transferring tokens to BNB (i.e. selling tokens to take a fee in BNB) when I’m buying the token? Is there another way to get round this if so?
Thanks