My smart contract create a new token when it's deployed, and people can buy or sell this token. The thing is the sell function don't work and I don't know what I did wrong or not understood.

Variables and constructor :

contract Promoter is Owner{

    **PromoterToken public token;**
    uint public lastOwnership;
    uint public rate = 1000000; 
    **address payable public thisContract = payable(address(this));**
    struct Voter{
        bool isRegistered;
        bool hasVoted;
        uint votedFor;
    mapping(address => Voter) Member;
    address[] whitelist;

        token = new PromoterToken(100000000000000000000000000, thisContract);
        lastOwnership = block.timestamp;

Buy and Sell functions :

//this one works
function buyPromoter()external payable{
        uint PromoterAmount = msg.value * rate;
        require(msg.value <= 15 ether, "You can't have more than 15% of the supply");
        require(token.balanceOf(msg.sender) + PromoterAmount <= 15000000 * 10 ** 18);
        require (getFundPromoterTokenBalance() >= msg.value, "");
        (bool sent, ) = thisContract.call{value: msg.value}("");
        require(sent, "Failed to send Ether"); 
        (bool transferOk) = token.transfer(msg.sender, (PromoterAmount));
        require(transferOk, "Failed to send PROMOTER");
        if(seeIfMemberExist(msg.sender) == false){
            Member[msg.sender].isRegistered = true;

//stuck on that one
    function sellPromoter(uint amount)external payable{
        require(amount <= token.balanceOf(msg.sender), "you can't sell token you don't own");
        token.approve(thisContract, amount);
        (bool transferOk) = token.transferFrom(msg.sender, thisContract, amount);
        require(transferOk, "Failed to send PROMOTER");
        (bool sent, ) = msg.sender.call{value: amount / rate}("");
        require(sent, "Failed to send Ether");
        if(token.balanceOf(msg.sender) < 1){

Do someone see the issue ?

1 Answer 1


Before swapping you need to call 'approve' function an example would be. You need to do this with most ERC20 tokens here is a full example: This example shows that you need to send aprove tx wait for it and then send buy or sell one.

const BUSDamountIn = ethers.utils.parseUnits('100', 18);
let amounts = await routerContract.getAmountsOut(BUSDamountIn, [BUSD, WBNB]);
const WBNBamountOutMin = amounts[1].sub(amounts[1].div(10));


const approveTx = await busdContract.approve(
let reciept = await approveTx.wait();
// call waht you need now
const swapTx = await routerContract.swapExactTokensForTokens(
    [BUSD, WBNB],
    Date.now() + 1000 * 60 * 10,
    {gasLimit: 250000}

receipt = await swapTx.wait();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.