How to return array of address in solidity? - solidity

I am creating a smart contract in solidity ^0.5.1 in which I get the following error:
data location must be a memory for the return parameter in the function, but none was given.
In the below function I am getting error.
function getCitizen()public returns(address[]){
return citizenArray;
}
The smart contract that I have tried so far.
pragma solidity ^0.5.1;
contract Citizen{
struct Citizens{
uint age;
string fName;
string lName;
}
mapping(address => Citizens) citizenMap;
address [] citizenArray;
function setCitizen(address _address,uint _age,string memory _fName,string memory _lName) public{
//creating the object of the structure in solidity
Citizens storage citizen=citizenMap[_address];
citizen.age=_age;
citizen.fName=_fName;
citizen.lName=_lName;
citizenArray.push(_address) -1;
}
function getCitizen(address _address) public pure returns(uint,string memory ,string memory ){
return(citizenMap[_address].age,citizenMap[_address].fName,citizenMap[_address].lName);
}
function getCitizenAddress()public returns(address[]){
return citizenArray;
}
}
How can I return the array of addresses?

It make sense, as you are returning the storage array of address you cannot return it as it is, because it will try to return the actual address of citizenArray in the contract storage. You can send the array by making it in memory. Like this.
function getCitizenAddress()public view returns( address [] memory){
return citizenArray;
}
Once you put it as memory, you will get the warning for this which will state that as you are not changing any state in the function, you should mark it view, I already did that in the above code.
Lastly, when you resolved this error, you will get another error in this function:
function getCitizen(address _address) public pure returns(uint,string memory ,string memory ){
return(citizenMap[_address].age,citizenMap[_address].fName,citizenMap[_address].lName);
}
This error is because you mark this function as pure. There is a little but very important difference between pure and view.
view means you cannot change the state of the contract in that function.
pure means you cannot change the state in the function and not even can read the state or storage variables.
In the above function of getCitizen you are actually doing read operations in your return statement. You can fix this by just putting view instead of pure. Like So:
function getCitizen(address _address) public view returns(uint,string memory ,string memory ){
return(citizenMap[_address].age,citizenMap[_address].fName,citizenMap[_address].lName);
}
I hope it will resolve all your issues. Thanks

// SPDX-License-Identifier: MIT
// Version
pragma solidity >=0.8.0 < 0.9.0;
contract EstructuraDeDatos {
struct Customer {
string nameCustomer;
string idCustomer;
string emailCustomer;
}
mapping(address => Customer) public myClientes;
address[] public listClientes;
function registrationApp(string memory _name, string memory _id, string memory _email) public {
Customer memory customer = Customer(_name, _id, _email);
myClientes[msg.sender] = customer;
listClientes.push(msg.sender);
}
function retornarArrat() public view returns (address[] memory) {
return listClientes;
}
}

// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
contract Citizen {
struct Citizens {
uint age;
string fName;
string lName;
}
mapping(address => Citizens) citizenMap;
address [] citizenArray;
function setCitizen(address _address,uint _age,string memory _fName,string memory _lName) public {
// Citizens storage citizen=citizenMap[_address];
//creating the object of the structure in solidity
Citizens storage citizen;
citizen = citizenMap[_address];
citizen.age=_age;
citizen.fName=_fName;
citizen.lName=_lName;
citizenArray.push(_address);
}
function getCitizen(address _address) public view returns(uint,string memory ,string memory) {
return (citizenMap[_address].age,citizenMap[_address].fName,citizenMap[_address].lName);
}
// function getCitizenAddress()public returns(address[]) {
// return citizenArray;
// }
}

// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
contract Citizen{
struct Citizens{
uint age;
string fName;
string lName;
}
mapping(address => Citizens) citizenMap;
address [] citizenArray;
function setCitizen(address _address,uint _age,string memory _fName,string memory _lName) public{
// Citizens storage citizen=citizenMap[_address];
//creating the object of the structure in solidity
Citizens storage citizen;
citizen = citizenMap[_address];
citizen.age=_age;
citizen.fName=_fName;
citizen.lName=_lName;
citizenArray.push(_address);
}
function getCitizen(address _address) public view returns(uint,string memory ,string memory ){
return(citizenMap[_address].age,citizenMap[_address].fName,citizenMap[_address].lName);
}
function getCitizenAddress()public view returns(address[] memory){
return citizenArray;
}
}

Related

Does the order of memory arguments in the Solidity function matter?

i'm trying to make the simple storage using Solidity. Here I'm just trying to map name to some number.
The problem is the following: if I use store_first() it does not work (the retrieve() function returns 0 in any case). But if I change the order of the arguments and place the string memory _name before uint256 _favoriteNumber everything works (the retrieve() function returns correct number)
Does the problem relates to the order (if yes, why?) or I missed something else?
// SPDX-License-Identifier: MIT
pragma solidity >0.6.0;
contract SimpleStorage {
mapping(string => uint256) public nameToNumber;
// If i use this function, the retrieve function always returns 0
function store_first(uint256 _favoriteNumber, string memory _name) public {
nameToNumber[_name] = _favoriteNumber;
}
// If i use this function, the retrieve function returns correct number
function store_second(string memory _name, uint256 _favoriteNumber) public {
nameToNumber[_name] = _favoriteNumber;
}
function retrieve(string memory _name) public view returns(uint256){
return nameToNumber[_name];
}
}

how to override openzeppelin _countVote function in GovernorCountingSimple.sol file?

I am making a DAO using openzeppelin and I downloaded the governance contract that they provide from the openzeppelin contract wizard. once I had imported this file into remix IDE an error popped up saying
"Function has to override specified but does not override anything"
so I followed that error to the GovernorCountingSimple.sol file and saw that it was the _countVote function but I can't seem to override it. I think I am just missing something simple but I don't know how to override it when the last argument within the function is "bytes memory" with no declared name and I cant seem to just call the super statement on the function to pass the last argument.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
import "#openzeppelin/contracts/governance/Governor.sol";
import "#openzeppelin/contracts/governance/extensions/GovernorSettings.sol";
import "#openzeppelin/contracts/governance/extensions/GovernorCountingSimple.sol";
import "#openzeppelin/contracts/governance/extensions/GovernorVotes.sol";
import "#openzeppelin/contracts/governance/extensions/GovernorVotesQuorumFraction.sol";
import "#openzeppelin/contracts/governance/extensions/GovernorTimelockControl.sol";
contract GovernernerContract is Governor, GovernorSettings, GovernorCountingSimple,
GovernorVotes, GovernorVotesQuorumFraction, GovernorTimelockControl {
constructor(
IVotes _token,
TimelockController _timelock,
uint256 _votingDelay,
uint256 _votingPeriod,
uint256 _quorumPercentage
)
Governor("governernerContract")
GovernorSettings( _votingDelay /* 1 block */, _votingPeriod /* 45818= ~ 1 week */, 0)
GovernorVotes(_token)
GovernorVotesQuorumFraction(_quorumPercentage)
GovernorTimelockControl(_timelock)
{}
// The following functions are overrides required by Solidity.
//this is my attempt to override the function but i dont know how to deal with the bytes
//memory argument
function _countVote(
uint256 proposalId,
address account,
uint8 support,
uint256 weight,
bytes memory // params
) internal override {
// super._countVote(proposalId,account,support,weight,*);
}
function votingDelay()
public
view
override(IGovernor, GovernorSettings)
returns (uint256)
{
// return super.votingDelay();
}
function votingPeriod()
public
view
override(IGovernor, GovernorSettings)
returns (uint256)
{
return super.votingPeriod();
}
function quorum(uint256 blockNumber)
public
view
override(IGovernor, GovernorVotesQuorumFraction)
returns (uint256)
{
return super.quorum(blockNumber);
}
function state(uint256 proposalId)
public
view
override(Governor, GovernorTimelockControl)
returns (ProposalState)
{
return super.state(proposalId);
}
function propose(address[] memory targets, uint256[] memory values, bytes[] memory calldatas, string memory description)
public
override(Governor, IGovernor)
returns (uint256)
{
return super.propose(targets, values, calldatas, description);
}
function proposalThreshold()
public
view
override(Governor, GovernorSettings)
returns (uint256)
{
return super.proposalThreshold();
}
function _execute(uint256 proposalId, address[] memory targets, uint256[] memory values, bytes[] memory calldatas, bytes32 descriptionHash)
internal
override(Governor, GovernorTimelockControl)
{
super._execute(proposalId, targets, values, calldatas, descriptionHash);
}
function _cancel(address[] memory targets, uint256[] memory values, bytes[] memory calldatas, bytes32 descriptionHash)
internal
override(Governor, GovernorTimelockControl)
returns (uint256)
{
return super._cancel(targets, values, calldatas, descriptionHash);
}
function _executor()
internal
view
override(Governor, GovernorTimelockControl)
returns (address)
{
return super._executor();
}
function supportsInterface(bytes4 interfaceId)
public
view
override(Governor, GovernorTimelockControl)
returns (bool)
{
return super.supportsInterface(interfaceId);
}
}
You was closer to the solution...
Just change this line: super._countVote(proposalId,account,support,weight,*);
Instead of using this symbol * to omit last parameter, try with single quotes.
This should work:
super._countVote(proposalId,account,support,weight,'');

I keep on getting ParseError: Expected type name // when I want to return a struct I have just created

I am new in Solidity and I have been trying to create and get STRUCT without adding it to an Array. I alway see Structs with arrays and the method .push and I wanted to try it without it.
I have created a single Contract with one Struct and one function to create and get it.
If I create a single public function to create, but not return, the struct it doesn´t give me any error... Like the following:
struct Todo {
string name;
uint age;
}
function createTodo(string memory _name, uint _age) public pure{
Todo memory myTodo = Todo(_name, _age);
}
With the above code I would also like to know why it wouldn´t let me set the pointer "Todo" as storage like: Todo storage myTodo = Todo(_name, _age); It gives an TypeError: Todo memory is not implicity convertible to expect type struct storage pointer.
Next, I tried to modify the function to create and RETURN but then is when I start getting the ParseError.
The code is the following:
function getTodo(string memory _name, uint _age) public returns(struct myTodo) {
Todo memory myTodo = Todo(_name, _age);
return myTodo;
}
With the aobve code I also tried in "returns(struct), returns(struct memory)"....
I would really appreciate any type of help here.
Thank you very much
You received this error because you're wrong to set a returns keyword on getTodo() function. In your case, you must change your function in this way:
function getTodo(string memory _name, uint _age) external pure returns(Todo memory) {
Todo memory myTodo = Todo(_name, _age);
return myTodo;
}
If you want to handle storage structs, see this smart contract code:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract Test {
struct Todo {
string name;
uint age;
}
// Declare state variable
Todo[] todoArray;
// Push into array new struct
function createTodo(string memory _name, uint _age) public {
todoArray.push(Todo(_name, _age));
}
// Retrieve ToDo struct from specific index about struct array
function getTodo(uint _index) external view returns(Todo memory) {
return todoArray[_index];
}
}

TypeError: Data location can only be specified for array, struct or mapping types, but "calldata" was given

I created a function that stores data. But this function cannot receive data. I tried it without calldata, with memory but all this ways didn't work. Thanks for the help.
pragma solidity >=0.7.0 <0.9.0;
contract Test {
struct Messanges{
string message;
address sender;
}
Messanges[] public chatverlauf;
address a = 0x5B38Da6a701c568545dCfcB03FcB875f56beddC4;
function add(string calldata _message, address calldata _sender) public{
//chatverlauf.push(Messanges(_message, _sender));
Messanges memory newMessage;
newMessage.message = _message;
newMessage.sender = _sender;
chatverlauf.push(newMessage);
}
i lookt up in this but it didn't workt for me
https://ethereum.stackexchange.com/questions/83602/data-location-can-only-be-specified-for-array-struct-or-mapping-types-but-mem/83603
written with remix
the compiler version is 0.8.10
change
function add(string calldata _message, address calldata _sender) public
to
function add(string _message, address _sender) public
In Solidity 0.8.0 only array , struct and mapping type can specific data location.

Solidity Mapping use string to find struct?

I want to use the mapping find the struct but the mapping just can use the bytes and I need to use the string.
pragma solidity >=0.4.22 <0.6.0;
pragma experimental ABIEncoderV2;
contract land{
address public owner;
constructor() public{
owner = msg.sender;
}
struct Landpaper{
string number;
string landaddress;
string landnumber;
string landpurpose;
uint landgrades;
uint256 landarea;
string holdpoints;
}
mapping(bytes8 => Landpaper) public lp;
modifier Permission(){
require(msg.sender == owner);
_;
}
function set(string memory rfidnumber, Landpaper memory _landpaperRecord) public Permission{
lp[rfidnumber]=_landpaperRecord;
}
function get(string memory rfidnumber) view public returns(Landpaper memory){
return lp[rfidnumber];
}
}
because I have to read the rfid UID and I will translate the UID to string, so I need to use the string set my data. I use the bytes8 and I input string type, I need to change my string to bytes8, plz told me how to do.
You can write a function to convert string to bytes8
function stringToBytes8(string memory sourceStr) private pure returns(bytes8) {
bytes8 temp = 0x0;
assembly {
temp := mload(add(sourceStr, 32))
}
return temp;
}
And call it from set and get functions
function set(string memory rfidnumber, Landpaper memory _landpaperRecord) public Permission {
bytes8 rfidb8 = stringToByte8(rfidnumber);
lp[rfidb8]=_landpaperRecord;
}
function get(string memory rfidnumber) public view returns(Landpaper memory) {
bytes8 rfidb8 = stringToByte8(rfidnumber);
return lp[rfidb8];
}
Though I would suggest to avoid performing conversion process in contract as it would consume unnecessary gas. Instead perform the conversion on client side and pass bytes8 as parameter
function set(bytes8 rfidnumber, Landpaper memory _landpaperRecord) public Permission {
lp[rfidnumber]=_landpaperRecord;
}
function get(bytes8 rfidnumber) public view returns(Landpaper memory) {
return lp[rfidnumber];
}