Beginner help – ERC20 contract that adds free tokens in the _transfer function isn’t working?

I’m writing a simple ERC20 contract that allows me to store a list of addresses that should recieve 500 extra free tokens whenever they buy – the transfer is sucessfull for users not in the “bonus token” list, but when I add their address to the list then try to purchase tokens (via PancakeSwap) I get an unknown error.

I’m assuming the error is due to PancakeSwap expecting the wallet to recieve X amount of tokens, but my contract amends it to be X+500 – however I’m not sure how to go about this.


Any advice is highly appreciated, thank you! The code for the function is below:


function _transfer(address sender, address recipient, uint256 amount) private {
uint256 recAmount;

require(sender != address(0), “ERC20: transfer from the zero address”);
require(recipient != address(0), “ERC20: transfer to the zero address”);
require(amount > 0, “Transfer amount must be greater than zero”);

if (_isBonusBuyer[recipient]) {
recAmount = amount.add(500);
} else {
recAmount = amount;

_walletBalance[sender] = _walletBalance[sender].sub(recAmount);
_walletBalance[recipient] = _walletBalance[recipient].add(recAmount);
emit Transfer(sender, recipient, recAmount);

What do you think?

Leave a Reply

Your email address will not be published. Required fields are marked *

GIPHY App Key not set. Please check settings

One Comment

  1. Update:


    So I managed to get it working if the recipient gets the 500 extra free tokens, and the sender balance only has the transaction amount deducted from it. This however causes inflation as those extra 500 tokens are being minted each time, so I guess my problem is that I’m not able to remove the extra tokens from the balance of the sender (the sender in this case is PCS LP). So this is a bigger issue than I first realised.

A Guide to Algorithmic Trading in Crypto | by Steven McKie | Amentum

The Market Won’t Crash Any Time Soon Because The Crash Is Already Priced In