link for full source code:https://testnet.bscscan.com/address/0x8bbf79cbe0a23f45e52b4170a4d11e850d5ec8f5#code
The problem is how to refund the token bet by user?
user will bet
IERC20 token = IERC20(0x78867BbEeF44f2326bF8DDd1941a4439382EF2A7);
function bet(uint8 _teamSelected, uint256 amount) public {
require(bettingActive);
//The first require is used to check if the player already exist
require(!checkPlayerExists(msg.sender));
//The second one is used to see if the value sended by the player is
//Higher than the minimum value
require(amount >= minimumBet);
//To roll in the Token, this line of code is executed on the condition that the user has approved a contract to use his Token
//IERC20 is Token
token.transferFrom(msg.sender,address(this),amount);
//We set the player informations : amount of the bet and selected team
playerInfo[msg.sender].amountBet = amount;
playerInfo[msg.sender].teamSelected = _teamSelected;
//then we add the address of the player to the players array
players.push(msg.sender);
//at the end, we increment the stakes of the team selected with the player bet
if ( _teamSelected == 1){
totalBetsOne += amount;
}
else if(_teamSelected == 2){
totalBetsTwo += amount;
}
}
after betting is closed agent will distribute the prize. How about if the result is draW? How to refund the token bet by user deducted by a fee?
function for distribution of prize
// Generates a number between 1 and 10 that will be the winner
function distributePrizes(uint16 teamWinner) public onlyAgent {
require(bettingActive == false);
address[1000] memory winners;
address[1000] memory draw;
//We have to create a temporary in memory array with fixed size
//Let's choose 1000
uint256 count = 0; // This is the count for the array of winners
uint256 LoserBet = 0; //This will take the value of all losers bet
uint256 WinnerBet = 0; //This will take the value of all winners bet
//We loop through the player array to check who selected the winner team
for(uint256 i = 0; i < players.length; i++){
address playerAddress = players[i];
//If the player selected the winner team
//We add his address to the winners array
if(playerInfo[playerAddress].teamSelected == teamWinner){
winners[count] = playerAddress;
count++;
}
}
//We define which bet sum is the Loser one and which one is the winner
if ( teamWinner == 1){
LoserBet = totalBetsTwo;
WinnerBet = totalBetsOne;
//We loop through the array of winners, to give ethers to the winners
for(uint256 j = 0; j < count; j++){
// Check that the address in this fixed array is not empty
if(winners[j] != address(0))
add = winners[j];
bets = playerInfo[add].amountBet;
token.transfer(winners[j], (bets*(10000+(LoserBet*devFee /WinnerBet)))/10000 );
}
delete playerInfo[playerAddress]; // Delete all the players
players.length = 0; // Delete all the players array
LoserBet = 0; //reinitialize the bets
WinnerBet = 0;
totalBetsOne = 0;
totalBetsTwo = 0;
totalBetsDraw = 0;
}
else if(teamWinner == 2){
LoserBet = totalBetsOne;
WinnerBet = totalBetsTwo;
//We loop through the array of winners, to give ethers to the winners
for(uint256 k = 0; k < count; k++){
// Check that the address in this fixed array is not empty
if(winners[k] != address(0))
add = winners[k];
bets = playerInfo[add].amountBet;
token.transfer(winners[k], (bets*(10000+(LoserBet*devFee /WinnerBet)))/10000 );
}
delete playerInfo[playerAddress]; // Delete all the players
players.length = 0; // Delete all the players array
LoserBet = 0; //reinitialize the bets
WinnerBet = 0;
totalBetsOne = 0;
totalBetsTwo = 0;
totalBetsDraw = 0;
}
else if(teamWinner == 3){
//We loop through the player array to check who selected the winner team
uint256 num;
for(uint256 l = 0; l < players.length; l++){
address aP = players[l];
if(playerInfo[aP].teamSelected == 1||playerInfo[aP].teamSelected == 2){
draw[num] = aP;
num++;
}
}
//We loop through the array of winners, to give ethers to the winners
for(uint256 m = 0; m < num; m++){
// Check that the address in this fixed array is not empty
if(draw[m] != address(0))
add = draw[m];
bets = playerInfo[add].amountBet;
token.transfer(draw[m], (bets*(devFee/10000)));
}
delete playerInfo[playerAddress]; // Delete all the players
players.length = 0; // Delete all the players array
LoserBet = 0; //reinitialize the bets
WinnerBet = 0;
totalBetsOne = 0;
totalBetsTwo = 0;
totalBetsDraw = 0;
}