0

I have a smart contract running on BSC test net. Mainly the contract has 10 tokens, which are bought from random addresses. Once all of the tokens are bought, the contract will select one address and send the balance received to that one address excluding the gas fees. To make sure that there is enough gas, I'm sending only 80% of the balance received. The amount receive is at least 0.01BNB per transfer resulting in 0.1BNB minimum which should cover any fees.

address payable winner;
event endContract(address indexed winner);
uint256 private ticketCount;

function endContract() private {
        require(ticketCount == MAX_TICKETS, "All tickets must be sold to select a winner");
        
        uint256 winnerTicketId = random() % MAX_TICKETS + 1;
        winner = ticketOwners[winnerTicketId];  //The value of the winner is selected correct
        
        //transfer only 80% of the pot
        uint256 transferAmount = (totalPot*80)/100;
        winner.transfer(transferAmount);
        emit endContract(winner);
        
    }   

The value of the winner is selected correctly. The value of the contract balance (totalPot) is also displayed correctly.

Is there any specific function other than transfer to issue a transfer to a selected address?

I've been going through this for a day now and could not figure it out. Contract address - 0xb39E6A3499DFA04a3eD5ed39ac9F57CF44E321fA

Thanks.

5
  • What do you mean by not issuing a transfer? Does it throws an error? Does the transaction fail? Is it doing something else? How is the private function endContract called? Why are you using the same name for a function and an event?
    – Ismael
    Commented Mar 24, 2023 at 3:32
  • @Ismael basically the function is being called, a random winner is selected (i can see the selected address) however on the network, there is no transaction being issue - meaning the winner.transfer is not doing its job. There is no error in the code and it ends normally. I believe either the function type used is wrong or the winner.transfer has to be replaced with another function
    – keeks
    Commented Mar 24, 2023 at 8:12
  • A contract doesn't generate a regular transaction. Ether transfer are done through an "internal transaction". If the transfer had failed the transaction should have failed. In order to be sure the transfer was correct externally could be verified checking the destination balance before and after the transaction.
    – Ismael
    Commented Mar 24, 2023 at 18:52
  • The transaction was not issued for it to fail or succeed. On the test network you cannot see any transaction occurring in the first place. I am asking why the transaction did not occur, not if it was correct.
    – keeks
    Commented Mar 25, 2023 at 10:38
  • Please, provide as much information as possible to try reproducing an issue: network used, framework, versions, how transaction are created, etc. You can edit the question to add more details. A transaction can fail for many reasons: not enough ether, wrong nonce, wrong sender, a smart contract bug, wrong rpc endpoint, etc.
    – Ismael
    Commented Mar 25, 2023 at 17:51

1 Answer 1

0

Check out this discussion here <address>.send vs <address>.transfer best practice usage? if you are using rawBnb. If you want to use ERC20 tokens, you need to do that transfer differently than native tokens.

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.