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 ?