Array.push() throwing ParserError: Expected identifier but got '(' - solidity

I would appreciate some help/explanation on the following error since I can't understand what's wrong.
simpleStorageArray.push(simpleStorage2); is throwing ParserError: Expected identifier but got '('
pragma solidity ^0.6.0;
import "./SimpleStorage.sol";
contract StorageFactory {
SimpleStorage[] public simpleStorageArray;
SimpleStorage public simpleStorage2 = new SimpleStorage();
simpleStorageArray.push(simpleStorage2);
function createSimpleStorageContract() public {
SimpleStorage simpleStorage = new SimpleStorage();
simpleStorageArray.push(simpleStorage);
}
}

You must to put this statement inside a function:
simpleStorageArray.push(simpleStorage2);
All operation in Solidity that change the state about your contract or read some variables you must to write into a function. In this case, because you're modifying a value about state variable, you must to put the statement above into function like this way:
pragma solidity ^0.6.0;
import "./SimpleStorage.sol";
contract StorageFactory {
SimpleStorage[] public simpleStorageArray;
SimpleStorage public simpleStorage2 = new SimpleStorage();
function createSimpleStorageContract() public {
SimpleStorage simpleStorage = new SimpleStorage();
simpleStorageArray.push(simpleStorage);
simpleStorageArray.push(simpleStorage2);
}
}

Related

Hi, I am just learning Solidity and i am getting an error "from solidity: ParserError: Expected identifier but got 'public'" at line 9

//SPDX- license-Identifier:MIT;
pragma solidity ^0.8.7;
uint256 favnumber;
struct People
{
string name;
uint256 numbers;
}
People[] public showdetails;
//mapping (uint256=>string) numbers;
function adddetails(uint256 _numbers) public
{
favnumber=_numbers;
/People newname=People({_name:_numbers});
numbers[_numbers]=[_name];/
}
This code was working fine on the other machine

Definition of base has to precede definition of derived contract (ERC721 implementation)

The top SO or ETH Stack Exchange answers don't seem to apply to my case (I could be wrong of course)
I'm getting the error describer in the title in this file:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "./ERC721Metadata.sol";
import "./ERC721.sol";
contract ERC721Connector is ERC721Metadata, ERC721 {
// ^^^^^^^ (Definition of base has to precede definition of derived contract)
constructor(string memory name, string memory symbol) ERC721Metadata(name, symbol) {}
}
Here's what ERC721Metadadata looks like:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract ERC721Metadata {
string private _name;
string private _symbol;
constructor(string memory named, string memory symbolified) {
_name = named;
_symbol = symbolified;
}
function name() external view returns (string memory) {
return _name;
}
function symbol() external view returns (string memory) {
return _symbol;
}
}
And here is what ERC721 looks like:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "./ERC721Metadata.sol";
import "./ERC721Connector.sol";
contract ERC721 {
event Transfer(
address indexed from,
address indexed to,
uint256 indexed tokenId
);
mapping(uint256 => address) private _tokenOwner;
mapping(address => uint256) private _OwnedTokensCount;
function _exists(uint256 tokenId) internal view returns (bool) {
address owner = _tokenOwner[tokenId];
return owner != address(0);
}
function _mint(address to, uint256 tokenId) internal {
require(to != address(0), "ERC721: minting to the zero address");
require(
!_exists(tokenId),
"ERC721: minting a token that already exists (been minted)"
);
_tokenOwner[tokenId] = to;
_OwnedTokensCount[to] += 1;
emit Transfer(address(0), to, tokenId);
}
}
Do you need to import ERC721Connector contract in your ERC721 contract? If not, you can remove
import "./ERC721Connector.sol"; // line 4, ERC721.sol
from your file and it should work fine. Your imports are causing the issue,
ERC721Connector tries to import ERC721, but ERC721 also needs ERC721Connector so the compiler says
Definition of base has to precede definition of derived contract
Because the base contract doesn't precede the defined contract.

Hardhat Compiling Error (_safeMint 'Undeclared identifier')

hello I'm writing a contract and got this error in my function.
function mintCardNFT(uint _cardIndex) external {
uint256 newItemId = _tokenIds.current();
_safeMint(msg.sender, newItemId);
nftHolderAttributes[newItemId] = CardAttributes({
cardIndex: _cardIndex,
name: defaultCards[_cardIndex].name,
imageURI: defaultCards[_cardIndex].imageURI,
alignments: defaultCards[_cardIndex].alignments,
power: defaultCards[_cardIndex].power,
maxPower: defaultCards[_cardIndex].maxPower,
resistance: defaultCards[_cardIndex].resistance,
income: defaultCards[_cardIndex].income
});
console.log("Minted NFT w/ tokenId %s and cardIndex %s", newItemId, _cardIndex);
nftHolders[msg.sender] = newItemId;
_tokenIds.increment();}
solidity version is 0.8.1 in hardhat.config and ^0.8.1 in contract.
everything looks normal to me. Merci!
You're trying to invoke a function _safeMint() but this function is not declared.
Most likely you forgot to derive your contract from the OpenZeppelin ERC721.
pragma solidity ^0.8;
// import the OpenZeppelin `ERC721` contract
import "#openzeppelin/contracts/token/ERC721/ERC721.sol";
// derive your contract from the imported `ERC721` contract
contract MyCollection is ERC721 {
// call the parent constructor
constructor() ERC721("MyCollection", "MyC") {}
function mintCardNFT() external {
// now the `_safeMint()` function is available
_safeMint(msg.sender, 1);
}
}

Not able to purchase the NFT I've created on Rarible (ropsten testnet)

I have created a nft on Rarible ( ropsten test net) but looks like people are not able to buy it....can anyone help me with why this seems to be happenning ?
rarible NFT : nft-link
solidity contract:
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "#openzeppelin/contracts/token/ERC721/ERC721.sol";
import "#openzeppelin/contracts/access/Ownable.sol";
import "#openzeppelin/contracts/utils/Counters.sol";
import "./rarible/impl/RoyaltiesV2Impl.sol";
import "./rarible/royalties/contracts/LibPart.sol";
import "./rarible/royalties/contracts/LibRoyaltiesV2.sol";
contract RoyaltyNFT is ERC721, Ownable, RoyaltiesV2Impl {
using Counters for Counters.Counter;
Counters.Counter private _tokenIdTracker;
constructor() ERC721("RoyaltyNFT", "ROYA") {}
function mint(address _to) public onlyOwner {
super._mint(_to, _tokenIdTracker.current());
_tokenIdTracker.increment();
}
function _baseURI() internal view virtual override returns (string memory) {
return "https://gateway.pinata.cloud/ipfs/QmbFMke1KXqnYyBBWxB74N4c5SBnJMVAiMNRcGu6x1AwQH";
}
function setRoyalties(uint _tokenId, address payable _royaltiesReceipientAddress, uint96 _percentageBasisPoints) public onlyOwner {
LibPart.Part[] memory _royalties = new LibPart.Part[](1);
_royalties[0].value = _percentageBasisPoints;
_royalties[0].account = _royaltiesReceipientAddress;
_saveRoyalties(_tokenId, _royalties);
}
function supportsInterface(bytes4 interfaceId) public view virtual override(ERC721) returns (bool) {
if(interfaceId == LibRoyaltiesV2._INTERFACE_ID_ROYALTIES) {
return true;
}
return super.supportsInterface(interfaceId);
}
}
I had followed a blog to create rarible NFT and i was able to do so but i was unable to sell it to other people as transfer could not be initiated by others.

Assert.equal is not available when running Truffle Tests

I have a simple contract and I am trying to write Solidity tests for that contract in Truffle. Below are the code samples -
File: dummycontract.sol
pragma solidity ^0.4.17;
contract DummyContract {
uint[] public elements;
function addElement(uint element) public {
elements.push(element);
}
function getNumberOfElements() public view returns(uint) {
uint numElements = uint(elements.length);
return numElements;
}
}
And the test file -
pragma solidity ^0.4.17;
import "truffle/Assert.sol";
import "truffle/DeployedAddresses.sol";
import "../contracts/dummycontract.sol";
contract TestZombieFactory {
function testInitialState() public {
DummyContract dummyContract = DummyContract(DeployedAddresses.DummyContract());
uint numberOfElements = dummyContract.getNumberOfElements();
Assert.equal(numberOfElements == 0);
}
}
On running truffle test, I get the following error -
TypeError: Member "equal" is not available in type(library Assert) outside of storage.
Assert.equal(numberOfElements == 0);
Can someone please explain this to me?
Your usage of Assert.equal() is not correct. It expects two values and does the comparison within the function. The function signature is
function equal(uint A, uint B, string message) constant returns (bool result)
Change your test contract to the following and it will work.
pragma solidity ^0.4.17;
import "truffle/Assert.sol";
import "truffle/DeployedAddresses.sol";
import "../contracts/dummycontract.sol";
contract TestZombieFactory {
function testInitialState() public {
DummyContract dummyContract = DummyContract(DeployedAddresses.DummyContract());
uint numberOfElements = dummyContract.getNumberOfElements();
Assert.equal(numberOfElements, 0, "Number of elements should be 0");
}
}
You should replace
Assert.equal(numberOfElements == 0);
with
Assert.equal(numberOfElements,0);