1

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;


    **constructor(){
        token = new PromoterToken(100000000000000000000000000, thisContract);
        lastOwnership = block.timestamp;
        setContractPromoter(address(this));
    }**

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){
            whitelist.push(msg.sender);
            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){
            removeMemberOfWhitelist(msg.sender);
        }
    }

Do someone see the issue ?

1 Answer 1

0

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));

console.log(ethers.utils.formatEther(BUSDamountIn));
console.log(ethers.utils.formatEther(WBNBamountOutMin));

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

receipt = await swapTx.wait();
console.log(receipt);
0

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.