Solidity ParseError: Expected primary expression - solidity

When I am compiling with truffle it is giving me this error
I got this error after adding payable
prior to that it was
winners[j].transfer(betwa*(10000+(LoserBet*10000/WinnerBet))/10000);
I had to add it because I was getting another error
Which was
TypeError: "send" and "transfer" are only available for objects of type "address payable", not "address".
My Complete Contract :
pragma solidity ^0.5.16;
contract Betting {
address public owner;
uint256 public minimumBet;
uint256 public totalBetsOne;
uint256 public totalBetsTwo;
address[] public players;
struct Player {
uint256 amountBet;
uint16 teamSelected;
}
// The address of the player and => the user info
mapping(address => Player) public playerInfo;
function() external payable {}
constructor() public {
owner = msg.sender;
minimumBet = 100000000000000;
}
function kill() public {
if(msg.sender == owner) selfdestruct(msg.sender);
}
function checkPlayerExists(address player) public view returns(bool){
for(uint256 i = 0; i < players.length; i++){
if(players[i] == player) return true;
}
return false;
}
function bet(uint8 _teamSelected) public payable {
//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(msg.value >= minimumBet);
//We set the player informations : amount of the bet and selected team
playerInfo[msg.sender].amountBet = msg.value;
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 += msg.value;
}
else{
totalBetsTwo += msg.value;
}
}
// Generates a number between 1 and 10 that will be the winner
function distributePrizes(uint16 teamWinner) public {
address[1000] memory winners;
//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
address playerAddress;
//We loop through the player array to check who selected the winner team
for(uint256 i = 0; i < players.length; i++){
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;
}
else{
LoserBet = totalBetsOne;
WinnerBet = totalBetsTwo;
}
//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)){
address add = winners[j];
uint256 betwa = playerInfo[add].amountBet;
//Transfer the money to the user
payable(winners[j]).transfer( (betwa*(10000+(LoserBet*10000/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;
}
function AmountOne() public view returns(uint256){
return totalBetsOne;
}
function AmountTwo() public view returns(uint256){
return totalBetsTwo;
}
}
What I have tried is making the address payable in this function but It is not working I have tried to replace memory with payable but still it isn't working
My versions
Truffle v5.4.18 (core: 5.4.18)
Solidity v0.5.16 (solc-js)
Node v14.15.1
Web3.js v1.5.3

The payable() conversion from address to address payable was introduced in Solidity 0.6.
Source: https://docs.soliditylang.org/en/latest/060-breaking-changes.html#new-features
Since your contract is using (deprecated) version 0.5.16, it doesn't allow this conversion. So you need to define the winners array already as address payable:
// added `payable`
address payable[1000] memory winners;
Then you'll be able to use the .transfer() method of the address payable type:
// `winners[j]` is payable
winners[j].transfer(...);

Related

TypeError: Explicit type conversion not allowed from "uint256" to "address"

i make simple code, but have error "TypeError: Explicit type conversion not allowed from "uint256" to "address"." Can you help me? I even asked the gpt bot about this error, but it confused me even more)))
in the beginning there was a simple idea - 10k nft, 30 of them immediately after the deposit, the minimum price is indicated and everyone can buy without restriction
`pragma solidity ^0.8.4;
import "#openzeppelin/contracts/token/ERC721/ERC721.sol";
contract Sptzrawef is ERC721 {
// Define the token name, symbol, and maximum supply
uint256 public totalSupply = 10000;
string public tokenURI;
// Define the owner of the contract
address public owner;
// Define the minimum price of the token
uint256 public minPrice;
// Define the mapping for the token ownership
mapping(address => mapping(uint256 => bool)) public tokenOwnership;
// The constructor function
constructor() ERC721("Sptzrawef", "SP") {
tokenURI = "https://nftstorage.link/ipfs/ba.................q3oq/";
owner = msg.sender;
minPrice = 0.025 ether;
totalSupply = 10000;
// Mint 30 tokens immediately upon deployment
for (uint256 i = 1; i <= 30; i++) {
tokenOwnership[msg.sender][i] = true;
}
}
// Function to mint new tokens
function mint(address _to, uint256 _tokenId) public {
require(msg.sender == owner);
require(_tokenId <= totalSupply);
require(tokenOwnership[_to][_tokenId] == false);
tokenOwnership[_to][_tokenId] = true;
totalSupply++;
}
// Function to buy the token
function buy(uint256 _tokenId) public payable {
require(msg.value >= minPrice);
require(tokenOwnership[msg.sender][_tokenId] == false);
require(_tokenId <= totalSupply);
tokenOwnership[msg.sender][_tokenId] = true;
}
function balanceOf() public view returns (uint256) {
return totalSupply;
}
function tokenOfOwnerByIndex(address _owner, uint256 _index) public view returns (uint256) {
uint256 count = 0;
for (uint256 i = 1; i <= totalSupply; i++) {
if (tokenOwnership[_owner][i]) {
if (count == _index) {
return i;
}
count++;
}
}
return 0;
}
function ownerOf(uint256 _tokenId) public view override returns (address) {
require(_tokenId <= totalSupply);
for (uint256 i = 0; i < totalSupply; i++) {
address wallet = address(i); - error at this line
if (tokenOwnership[wallet][_tokenId]) {
return wallet;
}
}
return address(0);
}
}`
I think you are using ownerOf function in the wrong way.
try to create a new "getWalletAddress" function, for example, and use it in this way:
function getWalletAddress(uint256 _tokenId) public view returns (address) {
require(_tokenId <= totalSupply);
for (uint256 i = 0; i < totalSupply; i++) {
address wallet = ownerOf(i);
if (tokenOwnership[wallet][_tokenId]) {
return wallet;
}
}
return address(0);
}
}
Or if you want to override the "ownerOf" function then you should go for this code:
function ownerOf(uint256 _tokenId) public view overrides returns (address) {
require(_tokenId <= totalSupply);
for (uint256 i = 0; i < totalSupply; i++) {
address wallet = _ownerOf(i);
if (tokenOwnership[wallet][_tokenId]) {
return wallet;
}
}
return address(0);
}
}
I hope this help resolve your issue
address is 20bytes and uint256 is 32bytes.
Therefore, you need to use uint160 for the type of i to convert it to address.
Or please convert i into uint160 before converting it to address
address wallet = address(uint160(i));

Transfer ether to smart contract user SOLIDITY

I am coding a coin flip contract. I have an issue with the amount sent by the smart contract to a player who wins the game.
What I want to do is: when you loose the coin flip, you loose your bet and when you win, you get 2 x the bet amount - our royalty. Yet, when I deploy the contract on Remix and test the PayWinner() function, I do not get 2x what I bet - royalty.
Thanks for your help!
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.16;
contract CoinFlip {
uint roundId;
uint betAmount;
uint royalty;
address owner;
address payable[] players;
struct round {
address payable player;
uint betAmount;
bool playerChoice; // 0 = heads; 1 = tails
bool draw;
bool win;
uint winAmount;
}
round public myRound;
mapping (uint => round) public flipHistory; // map the roundId with the round data
constructor() {
owner = msg.sender;
betAmount = 0;
roundId = 0;
}
function setRoyalty(uint _royalty) public onlyOwner returns (uint) {
return royalty = _royalty / 100;
}
function getRandomNumber(uint _bet, bool _playerChoice) public payable {
// minimum amount to be bet
require(msg.value > .001 ether);
// update the array players with the latest player
players.push(payable(msg.sender));
// update roundId
roundId = roundId+1;
// update player in Struct
myRound.player = payable(msg.sender);
// update betAmount in Struct
myRound.betAmount = _bet;
// update playerChoice in Struct
myRound.playerChoice = _playerChoice;
myRound.draw = uint256(keccak256(abi.encodePacked(
msg.sender,
)
)
)
% 2 == 1;
payWinner();
}
function payWinner() private {
if (myRound.draw == myRound.playerChoice) { // if else statement
myRound.win = true;
// compute amount to be transferred to winner
myRound.winAmount = myRound.betAmount * (2 - royalty);
// update mapping
flipHistory[roundId] = myRound;
payable(myRound.player).transfer(myRound.winAmount);
}
else {
myRound.win = false;
// update mapping
flipHistory[roundId] = myRound;
}
}
modifier onlyOwner() {
require(msg.sender == owner);
_;
}
}

How to return a struct inside a nested mapping in solidity

I'm working on a smart contract that allows users to subscribe to different Plans.
I have a nested mapping that returns a struct(Subscription). I wanted to create a function that returns all the subscriptions that exist in a specific plan(0,1,2..).
The function that I wrote doesn't return all structs it returns only the first one after that it returns only zeros.
totalSubscriptions increase when a new user creates a new subscription.
address[] allSubcribers contains all the addresses of subscribers.
currentId represents the id of the plan that a subscriber signed up for.
struct Subscription {
address subscriber;
uint start;
uint nextPayment;
}
mapping(address => mapping(uint => Subscription)) public subscriptions;
function getAllsubscriptions() external view returns (Subscription[] memory) {
Subscription[] memory items = new Subscription[](totalLoans);
for(uint256 i = 0; i < totalSubscriptions; i++) {
uint256 currentId = i;
LoanRequest storage currentItem = subscriptions[allSubcribers[currentId]][currentId];
items[currentId] = currentItem;
currentId += 1;
}
return items;
}
What do you think?

How to store unique value in an array using for loop in Solidity?

I want to store unique addresses in the array and then increment the count. But I am not able to do it. The count variable is not incremented and returns 0. Also redundant addresses are stored in the array.
Can you please help?
function placeBid() public payable notOwner afterStart beforeEnd returns(bool){
require(auctionState == State.Running);
uint currentBid = 0;
if(totalBidder>0)
{
currentBid = bids[msg.sender] + msg.value;
require(currentBid > highestBindingBid);
}
else{
require(msg.value > startBid && msg.value > highestBindingBid);
currentBid = msg.value;
}
bids[msg.sender] = currentBid;
for(uint i=0; i<bidders.length; i++){
if(msg.sender!=bidders[i])
bidders.push(payable(msg.sender));
totalBidder++;
}
}
highestBindingBid = currentBid;
highestBidder = payable(msg.sender);
return true;
}
An easy (and cheaper compared to your snippet) way to push only unique items to a storage array is to duplicate the values in a mapping, and then validate against the mapping.
pragma solidity ^0.8;
contract MyContract {
address payable[] public bidders;
mapping (address => bool) isBidder; // default `false`
// only add `msg.sender` to `bidders` if it's not there yet
function placeBid() public {
// check against the mapping
if (isBidder[msg.sender] == false) {
// push the unique item to the array
bidders.push(payable(msg.sender));
// don't forget to set the mapping value as well
isBidder[msg.sender] = true;
}
}
}

how to add function for closing and opening bet per match smart contract?

source code:https://github.com/laronlineworld/bettingMatch/blob/main/bettingMatch.sol
How to open and close bet per match
function bet(uint16 _matchSelected, uint16 _resultSelected) public payable {
//Check if the player already exist
// require(!checkIfPlayerExists(msg.sender));
//Check if the value sended by the player is higher than the min value
require(msg.value >= minimumBet);
//Set the player informations : amount of the bet, match and result selected
playerInfo[msg.sender].amountBet = msg.value;
playerInfo[msg.sender].matchSelected = _matchSelected;
playerInfo[msg.sender].resultSelected = _resultSelected;
//Add the address of the player to the players array
players.push(msg.sender);
//Finally increment the stakes of the team selected with the player bet
if ( _resultSelected == 1){
totalBetHome[_matchSelected] += msg.value;
}
else if( _resultSelected == 2){
totalBetAway[_matchSelected] += msg.value;
}
else{
totalBetDraw[_matchSelected] += msg.value;
}
}
This is the code for opening the betting
/* Function to enable betting */
function beginVotingPeriod() public onlyOwner returns(bool) {
bettingActive = true;
return true;
}
how about opening the bet per match?
also closing the bet per match
/* Function to close voting and handle payout. Can only be called by the owner. */
function closeVoting() public onlyOwner returns (bool) {
// Close the betting period
bettingActive = false;
return true;
}
The linked code recognizes a match as index of these arrays: totalBetHome, totalBetAway and totalBetDraw.
You can add a mapping where the key is the match ID, and the value is a flag signalizing whether there's a betting enabled on the match or not.
// default value for each is `false`
mapping (uint256 => bool) isMatchEnabled;
function enableBetting(uint256 _matchId) external onlyOwner {
isBettingEnabled[_matchId] = true;
}
function disableBetting(uint256 _matchId) external onlyOwner {
isBettingEnabled[_matchId] = false;
}
Then you can amend the bet() function - add a condition requiring betting for this match to be enabled.
function bet(uint16 _matchSelected, uint16 _resultSelected) public payable {
require(isBettingEnabled[_matchSelected] == true);
// rest of your code
}
Note: Solidity v0.4.2 that you're using in the linked code, is few years old and has few security issues. The current version (August 2021) is 0.8.7. Consider upgrading to the latest version.