How to get one value from struct of array in solidity? - ethereum

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' .

Related

solidity array in struct

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?

How can I get contract address of a contract from another contract?

Here how can I find the address of A within Test() function?
contract A {
uint public target;
function setTarget(uint _target) public {
target = _target;
}
}
contract B {
A a = Test(0x123abc...); // address of deployed A
function editA() public {
a.setTarget(1);
}
}
You can get the address by casting the A type to type address.
contract B {
A a = Test(0x123abc...);
funciton getAddressA() public view returns (address) {
return address(a); // typecasting to `address`
}
}

Solidity CRUD trying to call All data return instead of one by one using ID

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
}

Not able to compare strings on Mist

I have created a contract where I am taking 2 Hashes from the user and trying to compare them both which in return would give a boolean value of true or false. It works good on remix but when I try to run the contract on Mist the compareString function just shows a message "NO". here is my code.
pragma solidity ^0.4.18;
contract Hash {
string fhash;
string comphash;
event Instructor(string _fhash);
event Instructors(string _comphash);
function setinstructor(string _fhash) public {
fhash = _fhash;
emit Instructor(_fhash);
}
function getinstructor() public constant returns(string){
return(fhash);
}
function setinstructors(string _comphash) public {
comphash = _comphash;
emit Instructors(_comphash);
}
function getinstructors() public constant returns(string){
return(comphash);
}
function compareStrings() public view returns (bool){
return sha256(fhash) == sha256(comphash)? true : false;
}
}
Image of Mist response

Creating a struct causes weird behaviors in remix ide

Problem:
Remix produces weird behaviors with a string param followed by an array param
Reproduce:
contract ItemMarket is ERC721 {
struct Item {
string name;
uint[3] others;
}
Item[] public items;
function createItem(string _name, uint[6] _others) public {
uint tokenId = items.push(Item({name: _name, traits:_traits})) - 1;
}
}
When you call createItem() in remix with the arguments "hello", [1,2,3] the first argument gets converted to \u0000. The same function call with the same arguments works fine when interacted with the contract through MEW
This now works in the latest version of Remix IDE:
pragma solidity 0.5.1;
import "https://github.com/0xcert/ethereum-erc721/src/contracts/tokens/nf-token.sol";
contract ItemMarket is ERC721 {
struct Item {
string name;
uint[3] traits;
}
Item[] public items;
function createItem(string memory name, uint[3] memory traits) public {
items.push(Item({name:name, traits:traits})) - 1;
}
}

Categories