running into decleration error in solidity course - solidity

just started a solidity coding course and ran into this issue with this code:
//SPDX-License-Identifier: MIT
pragma solidity >=0.6.0;
import "./simplestorage.sol";
``
contract StorageFactory {
function createsimplestoragecontract() public {
simplestorage _simplestorage = new simplestorage();
}
}
I'm running into this error:
DeclarationError: Identifier not found or not unique.
--> contracts/storageFactory.sol:9:9:
|
9 | simplestorage _simplestorage = new simplestorage();
| ^^^^^^^^^^^^^

The following code worked fine for me.
//SPDX-License-Identifier: MIT
pragma solidity >=0.6.0;
import "./1_Storage.sol";
contract StorageFactory {
function createsimplestoragecontract() public {
Storage _simplestorage = new Storage();
}
}
Did you check if the name of the contract in simplestorage.sol is "simplestorage"? Usually is Pascal case (not mandatory, but good practice), something like SimpleStorage. Be aware about this. Your error probably is about lowercase instead of uppercase.

Related

Solidity, Wrapping Eth from a contract

I'm trying to wrap Eth from an smart contract as I want to swap weth later in uniswap but I don't know how to import the WETH code from goerli scan WETH = 0xB4FBF271143F4FBf7B91A5ded31805e42b2208d6;.
Following internet examples I accomplished wrapping Eth but all influencers just import a ERC20 library to create a new one. I don't understand why they are using this as they are not interacting with the right weth contract.
Here is the code I used but is just creating a new token. Can anyone give me some advice?
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity ^0.8.0;
pragma abicoder v2;
import '#uniswap/v3-periphery/contracts/interfaces/ISwapRouter.sol';
import '#uniswap/v3-periphery/contracts/libraries/TransferHelper.sol';
import '#openzeppelin/contracts/token/ERC20/ERC20.sol';
contract SwapExamples is ERC20 {
// For the scope of these swap examples,
// we will detail the design considerations when using `exactInput`, `exactInputSingle`, `exactOutput`, and `exactOutputSingle`.
// It should be noted that for the sake of these examples we pass in the swap router as a constructor argument instead of inheriting it.
// More advanced example contracts will detail how to inherit the swap router safely.
// This example swaps DAI/WETH9 for single path swaps and DAI/USDC/WETH9 for multi path swaps.
ISwapRouter public immutable swapRouter;
address payable [] private s_Wallets;
uint256 public walletA = address(this).balance;
// Router = 0xE592427A0AEce92De3Edee1F18E0157C05861564
address public constant WETH = 0xB4FBF271143F4FBf7B91A5ded31805e42b2208d6; //0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2;
// For this example, we will set the pool fee to 0.3%.
uint24 public constant poolFee = 3000;
uint256 public UsdOut;
constructor(ISwapRouter _swapRouter) ERC20("Wrapped Ether", "WETH") {//ERC20("Wrapped Ether", "WETH")
swapRouter = _swapRouter;
}
function Deposit() public payable {
s_Wallets.push(payable(msg.sender));
}
function Mint() external payable {
_mint(address(this), address(this).balance);
}
}
You need not create a new ERC20 token.
Calling:
WETH.deposit.value(msg.value)();
helps you wrap your ETH and you don't need to import WETH code from anywhere.
After wrapping, you can then move on to swapping WETH for any other token on Uniswap like you said.

Created custom ERC20 contract, balanceOf msg.sender is zero

I've created a ERC20 contract in Remix:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "#openzeppelin/contracts/token/ERC20/ERC20.sol";
contract MyToken is ERC20 {
constructor(string memory name, string memory symbol) ERC20(name, symbol) {
_mint(msg.sender, 1002000);
}
}
Then I deployed it:
But the balance of msg.sender is zero:
Does anyone know what's wrong?
I just tried your code and it works perfectly like intended.
It is important, that you compile the correct contract using Remix, see my attached picture. You need to choose the contract "MyToken". I guess you could have deployed the contract "ERC20 - #openzeppelin/contracts/token/ERC20/ERC20.sol" which would lead to the behaviour you mentioned.
compile "MyToken" in Remix

Execution is reverted in Solidity and Remix

I've wrote a simple code for fetching ETH price by using Chainlink interfaces as below:
// SPDX-License-Identifier: MIT
pragma solidity ^0.6.0;
import "#chainlink/contracts/src/v0.6/interfaces/AggregatorV3Interface.sol";
contract ABI {
AggregatorV3Interface internal priceFeed;
constructor() public {
priceFeed = AggregatorV3Interface(0xF4030086522a5bEEa4988F8cA5B36dbC97BeE88c);
}
function latestPrice() public view returns (int256) {
(, int256 answer,,,) = priceFeed.latestRoundData();
return answer;
}
}
The problem is when is being compiled by Remix, it has no problem but after execution it throws the error below:
call to ContractName.FunctionName errored: execution reverted
Do you think what the problem is?
Because your question didn't specify on which network you're running the script, I'm assuming that you're using the Remix VM emulator.
The specified Chainlink contract is available on Ethereum mainnet only. Any other network (including the emulator) does not have this contract deployed on this address.
To use the data feed contract in Remix, you can create a local fork of the mainnet, and then connect to the local network in the IDE.
Change ENVIRONMENT to Injected Web3 in Remix IDE and connect to metamask. For example, if you are using Kovan network use address as mentioned in the docs.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;
import "#chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol";
contract PriceConsumerV3 {
AggregatorV3Interface internal priceFeed;
/**
* Network: Kovan
* Aggregator: ETH/USD
* Address: 0x9326BFA02ADD2366b30bacB125260Af641031331
*/
constructor() {
priceFeed = AggregatorV3Interface(0x9326BFA02ADD2366b30bacB125260Af641031331);
}
/**
* Returns the latest price
*/
function getLatestPrice() public view returns (int) {
(
/*uint80 roundID*/,
int price,
/*uint startedAt*/,
/*uint timeStamp*/,
/*uint80 answeredInRound*/
) = priceFeed.latestRoundData();
return price;
}
}

Error: Compiler version ^0.8.0 does not satisfy the r semver requirement

I am new to solidity and I am running code on Remix.
It doesn't matter what version of compiler I specify, I keep on getting the same error.
Can someone help me out? What does "Compiler version ^0.8.0 does not satisfy the r semver requirement" exactly mean?
Here is my code:
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^ 0.8.0;
contract Storage {
struct People {
uint256 favoriteNumber;
string name;
}
mapping(string => uint256) public nameToFavoriteNumber;
People[] public people;
function addPerson(uint _personFavoriteNumber, string memory _personName ) public {
people.push(People({favoriteNumber: _personFavoriteNumber, name: _personName}));
nameToFavoriteNumber[_personName] = _personFavoriteNumber;
}
}
I had the same issue a couple of times. In Remix, I added a ".0" to the compiler version like so:
pragma solidity ^0.8.4.0;
I ran into this in Virtual Studio code also but I just ignored it and everything worked fine. I hope this helps!
It works in Remix as well, but I have worked on contracts that work without adding that ".0" at last, now even they show this error.
pragma solidity ^0.8.8.0;

Cannot Compile StorageFactory.sol because "not found contracts/SimpleStorage.sol

I am doing the Solidity, Blockchain, and Smart Contract Course – Beginner to Expert Python Tutorial from freeCodeCamp and Patrick AlphaC
https://github.com/PatrickAlphaC/storage_factory
Below is my code image from my IDE too here
// SPDX-License_Identifier: MIT
pragma solidity ^0.8.0;
import "./SimpleStorage.sol";
contract StorageFactory {
SimpleStorage[] public simpleStorageArray;
function createSimpleStorageContact() public {
SimpleStorage simpleStorage = new SimpleStorage();
simpleStorageArray.push(simpleStorage);
}
}
and I am getting the error:
not found contracts/SimpleStorage.sol
even though all the files are spelt correctly and in the same folder.
Please help as I am trying to follow along with the tutorial and don't want to get left behind.
In your picture your filename is wrong. You write SimpleSorage but it must be SimpleStorage. (Note the missing t in Storage.)