In the following code I am getting the following error when I declare selfdestruct() function in this code.
Error is :
Invalid implicit conversion from address to address payable requested.
pragma solidity >=0.4.22 <0.6.0;
contract owned{
address owner;
constructor() public{
owner = msg.sender;
}
modifier onlyOwner{
require(owner == msg.sender);
_;
}
}
contract mortal is owned{
function close()public onlyOwner{
selfdestruct(owner);
}
}
Assuming you're using version 0.5.x of the Solidity compiler, selfdestruct accepts an address payable, but you're passing in an address.
You can fix this by just changing the type of owner:
address payable owner;
But your pragma directive is a crazy range... there's no way to make this code work with both 0.4.x and 0.5.x compilers. There were quite a few breaking changes between those versions (including this one).
Related
I'm playing ethernaut Level 3, the original contract is here: https://ethernaut.openzeppelin.com/level/0x4dF32584890A0026e56f7535d0f2C6486753624f
When consecutiveWins is bigger or equal to 10, the player wins:
// SPDX-License-Identifier: MIT
pragma solidity ^0.6.0;
import '#openzeppelin/contracts/math/SafeMath.sol';
contract CoinFlip {
using SafeMath for uint256;
uint256 public consecutiveWins;
...
}
Now I want to read the consecutiveWins value in my contract, so I defined an interface:
interface CoinFlip {
uint256 public consecutiveWins;
function flip(bool _guess) external returns (bool);
}
But it is not allowed to have a variable in an interface, what should I do?
Solidity compiler will automatically generate getter functions for public variables, so what you need is replacing consecutiveWins variable in your interface with a getter function like this:
function consecutiveWins() public view returns (uint256);
You can read more about it in Solidity docs here.
Can someone please explain me why this works fine
import "./SimpleStorage.sol";
pragma solidity ^0.6.0;
contract storageFactoryContract {
SimpleStorage[] public asd;
function createSimpleStorageContract() public{
SimpleStorage simpleStorage = new SimpleStorage();
}
}
but this doesn't
import "./SimpleStorage.sol";
pragma solidity ^0.6.0;
contract storageFactoryContract {
function createSimpleStorageContract() public{
SimpleStorage[] public asd;
SimpleStorage simpleStorage = new SimpleStorage();
}
}
The error is:
freecodecamptutorial/FactoryContract.sol:14:26: ParserError: Expected ';' but got 'public'
SimpleStorage[] public asd;
^----^
In the second snippet, you're trying to set a visibility modifier for a regular variable. Only state variables (i.e. contract properties) and functions can have visibility modifiers.
Reference type variables (such as a contract) need to also have a data location. Since you're not working with the variable further, you can safely use the memory location. If you were to store it in storage, you might want to use the storage data location.
function createSimpleStorageContract() public {
// removed the `public` modifier
// added the `memory` data location
SimpleStorage[] memory asd;
SimpleStorage simpleStorage = new SimpleStorage();
}
This will generate two warnings about the unused variables asd and simpleStorage. It's simply because you're assigning the variables but never using them later. In this context, you can safely ignore them.
Note: I'm assuming that the SimpleStorage is a contract without a constructor or with a 0-argument constructor. Otherwise, you might get some different errors, for example wrong argument count.
im doing step by step of this article and i had a problem on truffle compile part.
I've got this error in cmd:
Error parsing #openzeppelin/contracts/token/ERC721/ERC721.sol: ParsedContract.sol:51:72: ParserError: Expected '{' but got reserved keyword 'override'
function supportsInterface(bytes4 interfaceId) public view virtual override(ERC165, IERC165) returns (bool) {
^------^
my contract :
pragma solidity ^0.6.0;
import "#openzeppelin/contracts/token/ERC721/ERC721.sol";
import "#openzeppelin/contracts/utils/Counters.sol";
contract Uniken is ERC721{
using Counters for Counters.Counter;
Counters.Counter private _tokenIds;
mapping(string => uint8) hashes;
constructor() public ERC721("Uniken", "Ukn") {
}
function awardItem(address recipient, string memory hash, string memory metadata)
public
returns (uint256)
{
require(hashes[hash] != 1);
hashes[hash] = 1;
_tokenIds.increment();
uint256 newItemId = _tokenIds.current();
_mint(recipient, newItemId);
_setTokenURI(newItemId, metadata);
return newItemId;
}
}
I'd be thankfull if anyone tell me whatis the problem?
It seems like it isn't seeing the ERC165 contract that ERC721 extends from. That function that it is getting stuck on should be overriding a function of the same name in ERC165, but the truffle compiler doesn't see a function named supportsInterface() in a class that ERC721 inherits from. So I would check to make sure everything imported in the ERC721 smart contract is in the right place in your folder structures, and that ERC721 is correctly inheriting ERC165.
After made some research , I was in truffle version 5.0 and update to the latest version of truffle -> v5.4.6, I am now able to compile
function endSale() public {
require(msg.sender == admin);
require(tokenContract.transfer(admin, tokenContract.balanceOf(address(this))));
admin.transfer(address(this).balance);
}
}
error line ---> admin.transfer(address(this).balance);
can someone help me with this thank you
Address literals have the type address instead of address payable. They can be converted to address payable by using an explicit conversion, e.g. payable(0xdCad3a6d3569DF655070DEd06cb7A1b2Ccd1D3AF).
Source: Solidity v0.8.0 Breaking Changes
What it means, is that since Solidity 0.8, address is not payable by default. And if you want to send it native currency, you need to cast it to payable first.
Example:
payable(admin).transfer(address(this).balance);
I created an array of structures and then tried to get the values of each account of an array. But I failed with an array while passing the address variable which contains msg.sender and the type is not visibly convertible to uint256. How can I do it?
As of Solidity v0.8, you can no longer cast explicitly from address to uint256.
You can now use:
uint256 i = uint256(uint160(msg.sender));
function f(address a) internal pure returns (uint256) {
return uint256(uint160(a));
}
You can cast it explicitly:
uint256 i = uint256(msg.sender);
function f(address a) constant returns (uint256) {
return uint256(a);
}
Pre v0.8.0 of Solidity you could do:
pragma <0.8.0;
return address(toUint(item));
Post v0.8.0 of Solidity you must now do:
pragma ^0.8.0
return address(uint160(toUint(item)));
References
address(uint) and uint(address): converting both type-category and width. Replace this by address(uint160(uint) and uint(uint160(address)) respectively.
see v0.8.0 breaking changes, Solidity Documentation