contract TEST {
struct testStruct {
address[] testArr;
}
mapping(address => testStruct) public testMap;
function addToStruct() public {
test storage a = testMap[msg.sender];
a.testArr.push(msg.sender);
}
function getStruct() public view returns(address[] memory){
return testMap[msg.sender].testArr;
}
}
When i call await testMap(my address) after calling await addToStruct(), it seems that the array defined in the structure is not returned. However, if you define a get function like getStruct() and look up information within the contract, it seems to bring information well, so is there anyone who can explain this phenomenon?
Related
I am trying to create a function that would return me the struct I select from multiple structs.
Here is my code:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract Structing {
struct DataOne {
uint indexOne;
address toOne;
}
struct DataTwo {
uint indexTwo;
address toTwo;
}
function selectingStruct(string name_struct) public view returns(struct) {
return _name_struct public final_selection;
}
}
This is the error I get:
Parser error, expecting type name.
How to create a function that returns me my selection between more structs in Solidity?
Your snippet only contains struct definitions, but not storage properties storing any data.
You'll need to define the exact datatype that you want to return. There's no magic typing that would allow returning a "1 of N" type in Solidity.
pragma solidity ^0.8;
contract Structing {
// type definition
struct DataOne {
uint indexOne;
address toOne;
}
// type definition
struct DataTwo {
uint indexTwo;
address toTwo;
}
// typed properties
DataOne dataOne;
DataTwo dataTwo;
// TODO implement setters of `dataOne` and `dataTwo` properties
function getDataOne() public view returns (DataOne memory) { // returns type
return dataOne; // property name
}
function getDataTwo() public view returns (DataTwo memory) { // returns type
return dataTwo; // property name
}
}
I have created a solidity that record data CRUD like where I can create read update delete but I want to create a readAll function wondering how can I write in solidity since I write like below it does not work. For calling with id it return the correct but not readAll. Looking forward for your help <3
example
function readAllTask() public view returns (uint, uint256, uint256) {
return (tasks.id, tasks.stackAddress, tasks.nftId); <============= return everything
}
pragma solidity ^0.8.6;
contract RecordData {
struct Task {
uint id;
uint256 stackAddress;
uint256 nftId;
}
Task[] tasks;
uint nextId; // default value 0, add public to see the value
function createTask(uint256 _stackAddress, uint256 _nftId) public {
tasks.push(Task(nextId, _stackAddress, _nftId));
nextId++;
}
function findIndex(uint _id) internal view returns (uint) {
for (uint i = 0; i < tasks.length; i++) {
if (tasks[i].id == _id) {
return i;
}
}
revert("Task not found");
}
function updateTask(uint _id, uint256 _stackAddress, uint256 _nftId) public {
uint index = findIndex(_id);
tasks[index].stackAddress = _stackAddress;
tasks[index].nftId = _nftId;
}
function readTask(uint _id) public view returns (uint, uint256, uint256) {
uint index = findIndex(_id);
return (tasks[index].id, tasks[index].stackAddress, tasks[index].nftId);
}
function deleteTask(uint _id) public {
uint index = findIndex(_id);
delete tasks[index];
}
}
You can return an array of structs:
function readAllTask() public view returns (Task[] memory) {
return tasks;
}
I think you have reason to do it, but just a remind that saving the data into blockchain is not a good deal, due to the high cost.
If you would like to return all the task, then simply make the tasks public. Solidity automatically assign a get function for it.
If you would like to get some specific content of task struct, then consider something like this:
function readAllTask() public view returns (uint, uint256, uint256 [] memory) {
// something
}
I'm trying to create a variable "project" to store data from a mapping but I get "Decalration error, undefined identifier" on project = projects[addr]
function getProjectInfo(address addr) public view returns (string memory name, string memory url, uint funds){
var project = projects[addr];
}```
Use explicit variable type definition:
pragma solidity ^0.5.8;
contract Test
{
struct Project
{
bytes32 name ;
}
mapping (address => Project) projects ;
constructor () public {
}
function getProjectInfo(address addr) public view returns (string memory name, string memory url, uint funds)
{
Project memory project = projects[addr];
// ...
}
}
I want to get the value that is inside of struct in Solidity,
but I have no idea how to get it.
pragma solidity >=0.4.21 <0.6.0;
contract PlaceList {
struct hoge {
uint id;
address user;
}
hoge[] public hoges;
constructor() public {
admin = msg.sender;
}
function set(uint id) public {
hoges.push(hoge(id, msg.sender));
}
function getId() public view returns(uint) {
return (hoges[0].id);
}
}
When I call getId, console command say this,
ƒ () {
if (abiItemModel.isOfType('constructor')) {
return target.executeMethod(abiItemModel, arguments, 'contract-deployment');
}
return targe…
Could you give me any advise how to get id by using solidity function, please?
hoges[0][0] will work as it points to the first element, 'id' .
This is my contract code. Here I'm trying to store the coordinates of a particular trip. While storing the information contract executing fine. But When I retrieve the data, It should give the array of coordinates. But it is throwing an error.
reason: 'insufficient data for uint256 type'
contract TripHistory {
struct Trip {
string lat;
string lon;
}
mapping(string => Trip[]) trips;
function getTrip(string _trip_id) public view returns (Trip[]) {
return trips[_trip_id];
}
function storeTrip(string _trip_id, string _lat, string _lon) public {
trips[_trip_id].push(Trip(_lat, _lon));
}
}
What I'm missing here. Is there any other way to achieve what I'm trying here?
P.S: I'm new to solidity.
First of returning structs is not supported in Solidity directly. Instead you need to return every individual element in the struct as below.
Function xyz(uint256 _value) returns(uint256 User.x, uint256 User.y)
public {}
But then there’s an experimental feature that will help you with returning struct. All that you need to do is add the following after your first pragma line
pragma experimental ABIEncoderV2;
then continue with your code. That should work with no changes to your code.
An example of abiencoderv2 returning struct can be found at this link
It is not possible in solidity to return struct array.
As jlo said in this link, after version 0.8.0, it is possible to return a struct. jlo describes how to set and return an element of array of struct. Here I describe how to set, reset, and return a struct type variable.
I tested it and my test environment is:
private Ethereum network
Geth version 1.10.9-stable (for private network)
Slocjs Compiler version 0.8.7
web3js version 1.5.1
Note, you have to first define the struct type outside of any function inside the contract.
A supper intuitive example code is as follows:
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
contract testContract {
struct funcResultType {
uint[] var1;
string[] var2;
string message;
}
funcResultType private funcResult;
function testSetFunc(string memory inputVar) public payable {
funcResult.var1.push(123);
funcResult.var2.push(inputVar);
funcResult.message = "Done!";
}
function testResetFunc() public payable {
delete funcResult; // reset variales
}
function testGetFunc() public view returns (funcResultType memory){
return funcResult;
}
}
The result of the test with Web3js in the console is as follow:
As you can see the whole variables are accessible. I upload its web3js code in Github in this link.