English 中文(简体)
There is an error in the below mentioned line- ParserError: Expected identifier but got ( receive() external payable^
原标题:
// SPDX-License-Identifier: MIT
pragma solidity >=0.5.0 <0.9.0; 
contract Lottery 
{
address public manager;
address payable[] participants;

constructor() public
{
manager = msg.sender;
}
receive() external payable   //error on this line
{
require(msg.value==1 ether);
participants.push(payable(msg.sender));
}

function getBalance() public view returns(uint)
{
require(msg.sender==manager);
return address(this).balance;

}
function randomG() public view returns(uint)
{
return uint(keccak256(abi.encodePacked(block.difficulty, block.timestamp, participants.length)));
}

function SelectWinner() public 
{
uint r=randomG();
uint index= r % participants.length; 
address payable winner;
winner = participants[index];
winner.transfer(getBalance());

}
}

Please identify what is the error with the recieve function.This is a smart contract for a lottery system where a random winner is selected and paid. I cannot receive ether from the participant s address. what is wrong with the function? Are there any alternatives for the same?

问题回答

In remix, compiler 0.8.18. Your contract code can accept transfers from other accounts, and only 1 ether will be executed successfully.

The error occurs because the participants array was not initialized in the constructor. To solve this issue, add the following line in the constructor:

participants = new address payable[](0);

This will initialize the participants array as an empty array.





相关问题
unable to get data from blockchain using thirdweb

I am building a simple crowdfunding app using React and thirdweb , I was able to successfully create a Campaign mean data is pushed successfully , I have read docs everything works fine , but when i ...

How named accounts works in NEAR

It appears that named accounts in NEAR are being managed by the near and register smart contracts. However, this raises the question of whether NEAR natively supports named accounts. it s unclear if ...

ETH sendSignedTransaction

I Try to use @ethereumjs/tx to sign Transaction. I want to send ETH from one address to second address. .... const legacy = require( @ethereumjs/tx/dist/legacyTransaction ); const utils = require( ...

How to set a timer in solidity?

I am launching a game that should close after exactly 3 days/72 hours of initializing & deploying the contract. My current thoughts are: declare a variable, timestamp. in the constructor, set the ...

"code": -32000, "message": "execution reverted"

Hello I am trying to deploy a contract using remix.ethereum.org after linked the metamask account using mumbai network. I receive this error while making the transaction for the contract Gas ...

How to get all accounts from near protocol blockchain?

I am currently working on a program within the Near Protocol to generate user-specific Near token ownership statistics. However, I m not sure how to obtain a list of all user accounts. Is there an ...

热门标签