I'm having this problem:
StorageFactory.sol:13:27: ParserError: Expected ';' but got '('
simpleStorage store(_simpleStorageNumber);
^
and this is my code:
// SPDX-License-Identifier: MIT
pragma solidity ^0.6.0;
import "./SimpleStorage.sol";
contract StorageFactory{
SimpleStorage[] public simpleStorageArray;
function createSimpleStorageContract() public {
SimpleStorage simpleStorage = new SimpleStorage();
simpleStorageArray.push(simpleStorage);
}
function sfStore(uint256 _simpleStorrageIndex, uint256 _simpleStorageNumber) public{
SimpleStorage simpleStorage = SimpleStorage(address(simpleStorageArray[_simpleStorageIndex]));
simpleStorage store(_simpleStorageNumber);
}
I dont know what exactly the SimpleStorage.sol contract you are importing is contain but if i understand it right then this might help -
//SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.5.0 <0.9.0;
import "./SimpleStorage.sol";
contract StorageFactory{
SimpleStorage[] public simpleStorageArray;
function createSimpleStorageContract() public
{
SimpleStorage simpleStorageContract = new SimpleStorage();
simpleStorageArray.push(simpleStorageContract);
}
function sfStore(uint256 _simpleStorrageIndex, uint256 _simpleStorageNumber) public
{
SimpleStorage simpleStorageContract = SimpleStorage(address(simpleStorageArray[_simpleStorageIndex]));
simpleStorageContract.store(_simpleStorageNumber);
}
}
If this is not helping, please show us the code for the simpleStorage contract.
Anyway the solution you trying to create will be very costy.
Here is a cotract that get your goal without using another contract and is much cheaper to run -
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
contract Answer
{
mapping(address => uint) public StorageMap; //collection of address and numbers
function InsertToStorage(address name, uint favoritenumber) public //insert a address and favorite number
{
StorageMap[name] = favoritenumber;
}
function GetFavoriteNumber(address name) public view returns(uint) //insert an address and return the favorite number of that address
{
return StorageMap[name];
}
}
Good Luck!
Related
Here, I'm trying to get tempMap data from MainContract in Temp contract but I'm not able to get value form tempMap, as well as I'm using external visibility in MainContract but still didn’t get any response.
// SPDX-License-Identifier: MIT
pragma solidity >=0.4.22 <0.9.0;
contract MainContract {
mapping(uint => string) public tempMap;
function addData(uint number,string memory name ) public {
tempMap[number] = name;
}
function get(uint number) external view returns(string memory){
return tempMap[number];
}
}
contract Temp{
MainContract main = new MainContract();
function getData(uint number) public view returns(string memory){
return main.get(number);
}
}
You have to pass the address of the deployed contract, so it knows where to make the call.
Your temp contract will look something like this.
contract Temp{
MainContract main;
constructor(MainContract _main) {
main = _main;
}
function getData(uint number) public view returns(string memory){
return main.get(number);
}
}
could anyone help me with this??
used online remix in order to create a calucator using following criteria
Add Log of transactions using the event/emit
Each time one of the methods is called you will add
the address (msg.sender)
the action (“add”, “subtract”…)
the number
ii. Create a modifier “isOwner” like we did in class and add it to all the
methods in the calculator.
iii. Add a check using require or revert for the divide method to make sure
you are not dividing by zero (0).
// SPDX-License-Identifier: MIT
pragma solidity 0.8.7;
contract Calculator{
mapping(address => uint256) public transactions;
address public owner;
address[] public transList; //// Showing to modify this line of code
constructor() {
owner = msg.sender;
}
modifier isOwner() {
require(msg.sender == owner, "Sender is not Owner");
_;
}
function whoIsOwner() public isOwner view returns (address) {
return owner;
}
uint256 public total =0;
uint256 public current_total =0;
function add(uint256 _amount) isOwner public{
total = total + _amount;
transList.push(msg.sender);
transList.push("add"); /// Error in this line
transList.push(_amount);
}
I created a lottery application in solidity. All things are working fine but I want to add this feature that when the winner is selected it should return me the address of winner as well.
What can i do for this? Any help will be appreciated, thanks. Here is my solidity code.
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
contract Lottery_Application {
address public manager;
address payable[] public participants;
constructor() {
manager=msg.sender;
}
receive() external payable {
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 random() public view returns(uint) {
return uint(keccak256(abi.encodePacked(block.difficulty,block.timestamp,participants.length)));
}
function selectwinner() public returns(address) {
require(msg.sender==manager);
require(participants.length>=3);
uint r=random();
address payable winner;
uint index=r% participants.length;
winner=participants[index];
winner.transfer(getbalance());
winner=address(msg.sender);
return winner;
}
}
State changing functions are unable to return anything. You could do this two ways.
Create and use events.
event WinnerSelected(address winner)
At the end of your winnerSelect function,
emit WinnerSelected(winner).
Create a global variable to store recent winner.
address public recentWinner
At the end of your winnerSelect function,
recentWinner = winner.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
interface IBank {
function addTax(uint256 moneyToAdd) external payable;
}
contract Counter {
uint public count;
address bankAddress;
function setbankAddress(address _bankAddress) public payable {
bankAddress = _bankAddress;
}
function addMon(uint moneyToAdd) public payable {
require(moneyToAdd <= msg.value, "Ether value sent is not
correct");
}
function pay(uint _Amount) public payable {
IBank(bankAddress).addTax{value:_Amount}(_Amount);
}
}
when I deploy it I first add the contract address that I want to interact with, it's working with all functions that do not require value
I'm using Truffle and upgradable Openzeppelin contracts. I have two contracts.
Token.sol:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.2;
import "#openzeppelin/contracts-upgradeable/token/ERC20/ERC20Upgradeable.sol";
import "#openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";
contract MyToken is Initializable, ERC20Upgradeable {
/// #custom:oz-upgrades-unsafe-allow constructor
constructor() initializer {}
function initialize() initializer public {
__ERC20_init("MyToken", "MTK");
_mint(msg.sender, 10000000 * 10 ** decimals());
}
}
AnotherContract.sol:
pragma solidity ^0.8.2;
import "./IAnotherContract.sol";
contract AnotherContract is IAnotherContract {
function doSomethingIfBalanceIsEnough()
external
returns (string memory)
{
// ...
}
}
How do I check how many MTK tokens a user has? I need to check it in doSomethingIfBalanceIsEnough function.
The token contract implements a balanceOf() function that returns token balance of an address.
You can make an external call from AnotherContract to MyToken address, invoking its balanceOf() function, passing it the user's address.
pragma solidity ^0.8.2;
import "./IAnotherContract.sol";
interface IERC20 {
function balanceOf(address) external view returns (uint256);
}
contract AnotherContract is IAnotherContract {
function doSomethingIfBalanceIsEnough()
external
returns (string memory)
{
uint256 userBalance = IERC20(myTokenAddress).balanceOf(msg.sender);
if (userBalance > 0) {
// ...
}
}
}