Solidity: Retrieving values of Array of Structs in Mapping - ethereum

I have some solidity code where I am attempting to gather the ids which are a value stored on a Struct. I have a mapping where the key is an address, and the value is an array of Structs. Whenever I execute the getMediaByAddress function I get an invalid OpCode error. Any help would be greatly appreciated.
pragma solidity ^0.4.24;
contract MediaGallery {
address owner;
uint counter;
struct MediaAsset {
uint id;
string name;
address author;
uint createDate;
string[] tags;
string mediaHash;
}
mapping(address => MediaAsset[]) public mediaDatabase;
constructor () {
owner = msg.sender;
}
function addMedia(string _name, string _mediaHash) public returns (bool success) {
MediaAsset memory currentMedia;
currentMedia.id = counter;
currentMedia.name = _name;
currentMedia.author = msg.sender;
currentMedia.createDate = now;
currentMedia.mediaHash = _mediaHash;
mediaDatabase[msg.sender].push(currentMedia);
return true;
}
function addTag(uint _id, string _tag) public returns (bool success) {
mediaDatabase[msg.sender][_id].tags.push(_tag);
return true;
}
function getMediaByAddress(address _user) public view returns (uint[]) {
uint[] memory mediaAssetIds = new uint[](mediaDatabase[_user].length);
uint numberOfMediaAssets = 0;
for(uint i = 1; i <= mediaDatabase[_user].length; i++) {
mediaAssetIds[numberOfMediaAssets] = mediaDatabase[_user][i].id;
numberOfMediaAssets++;
}
return mediaAssetIds;
}
}

You're trying to read past the end of the array. Your loop variable i has an off-by-one error. Its greatest value is mediaDatabase[_user].length, which is 1 past the end of the array. Try this instead:
for (uint i = 0; i < mediaDatabase[_user].length; i++) {

Related

Solidity: How to type cast string memory to address and uint type?

I get the following errors when trying to type cast string memory to address and uint type.
TypeError: Explicit type conversion not allowed from "string memory" to "address".
TypeError: Explicit type conversion not allowed from "string memory" to "uint256".
Below is the solidity code.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract Test {
struct allowedTokenDetails {
address admin;
uint256 price;
uint256 balance;
address rewardToken;
uint256 timestampAdded;
uint256 timestampLastUpdated;
}
mapping(address => allowedTokenDetails) public allowedTokensData;
function setAllowedTokensData(address _token, string[][] memory _data) public {
for (uint256 dataIndex = 0; dataIndex < _data.length; dataIndex++) {
string memory dataKey = _data[dataIndex][0];
string memory dataValue = _data[dataIndex][1];
if (keccak256(abi.encodePacked(dataKey)) == keccak256(abi.encodePacked("admin"))) allowedTokensData[_token].admin = address(dataValue);
if (keccak256(abi.encodePacked(dataKey)) == keccak256(abi.encodePacked("price"))) allowedTokensData[_token].price = uint256(dataValue);
if (keccak256(abi.encodePacked(dataKey)) == keccak256(abi.encodePacked("balance"))) allowedTokensData[_token].balance = uint256(dataValue);
if (keccak256(abi.encodePacked(dataKey)) == keccak256(abi.encodePacked("rewardToken"))) allowedTokensData[_token].rewardToken = address(dataValue);
allowedTokensData[_token].timestampLastUpdated = block.timestamp;
}
}
}
Is there a solution to this?
Instead of validating the input with a set of if statements and typecasting each value based on the function logic, you can already pass the input data in expected types in a prepared struct:
struct inputData {
address admin;
uint256 price;
uint256 balance;
address rewardToken;
}
function setAllowedTokensData(address _token, inputData[] memory _data) public {
for (uint256 dataIndex = 0; dataIndex < _data.length; dataIndex++) {
allowedTokensData[_token] = allowedTokenDetails(
_data[dataIndex].admin,
_data[dataIndex].price,
_data[dataIndex].balance,
_data[dataIndex].rewardToken,
0, // `timestampAdded` not set in your snippet
block.timestamp
);
}
}

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
}

Check that object is null in solidity mapping

I have this solidity mapping
mapping (string => Ticket) public myMapping;
I want to check if myMapping[key] exists or not. How can I check?
The entire storage space is virtually initialized to 0 (there is no undefined).
So you have to compare the value to the 0 value for your type.
For example, mapping[key] == address(0x0) or mapping[key] = bytes4(0x0).
There is no direct method to check whether the mapping has particular key. But you can check if mapping property has value or not. The following example considered that the Ticket is the struct with some property.
pragma solidity >=0.4.21 <0.6.0;
contract Test {
struct Ticket {
uint seatNumber;
}
mapping (string => Ticket) myMapping;
function isExists(string memory key) public view returns (bool) {
if(myMapping[key].seatNumber != 0){
return true;
}
return false;
}
function add(string memory key, uint seatNumber) public returns (bool){
myMapping[key].seatNumber = seatNumber;
return true;
}
}
pragma solidity ^0.8.0;
contract BookLibNew{
address public owner;
constructor() public{
owner = msg.sender;
}
modifier onlyOwner(){
require(msg.sender == owner);
_;
}
struct bookDet{
uint bookId;
string bookTitle;
string bookAuthor;
}
mapping (uint8 => bookDet) public bookLib;
function addBookLib(uint8 _bookId, string memory _bookTitle, string memory _bookAuthor)
public onlyOwner {
require(bookLib(_bookId) == false, "Error: Book already exists");
bookLib[_bookId].bookTitle = _bookTitle;
bookLib[_bookId].bookAuthor = _bookAuthor;
}
function readBookDetails(uint8 _bookId) public view returns(string memory, string memory){
return(bookLib[_bookId].bookTitle, bookLib[_bookId].bookAuthor);
}
}

Solidity - Store random numbers to a dynamic array and return the array

I use remix IDE. When I call the function get number I got this 0: uint256[]:
How can I change it to return the dynamic array's numbers?
pragma solidity ^0.4.24;
contract dynamicarray {
uint public constant MaxNumber = 50;
uint[] numbers;
function randomnumber() public view returns (uint){
uint random = uint(sha3(block.timestamp)) % MaxNumber +1;
for(uint i = MaxNumber; i > numbers.length; i++){
numbers.push(random);
return random;
}
}
function getnumbers() public view returns(uint[]){
return numbers;
}
}
The function is a view, so it can't modify state. Calling randomnumber() will return a value, but it won't change the numbers array.
Drop the view modifier from randomnumber(), and it will add one item to the array. (The early return will prevent the loop from repeating.)
Try this
pragma solidity ^0.4.24;
contract dynamicarray {
uint public constant MaxNumber = 50;
uint[] numbers;
function randomnumber() public returns (uint){
uint random = uint(keccak256(block.timestamp)) % MaxNumber +1;
for(uint i = MaxNumber; i > numbers.length; i++){
numbers.push(random);
return random;
}
}
function getnumbers() public view returns(uint[]){
return numbers;
}
}

Geth + smart contract function = bad instruction

playing with ethereum smart contracts I encountered a following problem. The contract gets deployed on Ropsten and then I'm trying to call function 'addRecipe' with the following code on geth:
recipes.addRecipe(300, "zupa", "zupa z trupa", {from:web3.eth.accounts[0], gas: 20000000})
The function looks as following:
function addRecipe(uint256 _price, string _name, string _content) public {
recipes[recipeCount].price = _price;
recipes[recipeCount].name = _name;
recipes[recipeCount].content = _content;
recipes[recipeCount].recipeOwner = msg.sender;
recipeCount++;
}
I get the TX hash but looking up the transaction in Etherscan gives
Warning! Error encountered during contract execution [Bad instruction]
You can check it up here:
https://ropsten.etherscan.io/tx/0xe5999c2d122e4871e82f5986397dfd39107cee2056a9280132abeaa460c0f66d
Adding 'payable' modifier to the function or using the following command doesn't give any better results...
recipes.addRecipe.sendTransaction(300, "zupa", "zupa z trupa", {from:web3.eth.accounts[0], gas: 200000000})
The whole contract:
pragma solidity ^0.4.24;
contract Recipes {
address owner;
uint256 recipeCount = 0;
struct Recipe {
string name;
string content;
uint256 price;
address recipeOwner;
}
Recipe[] public recipes;
function () public {
owner = msg.sender;
}
function kill() public {
require (msg.sender == owner);
selfdestruct(owner);
}
function addRecipe(uint256 _price, string _name, string _content) public {
recipes[recipeCount].price = _price;
recipes[recipeCount].name = _name;
recipes[recipeCount].content = _content;
recipes[recipeCount].recipeOwner = msg.sender;
recipeCount++;
}
function showRecipes(uint256 _id) constant returns(string) {
return recipes[_id].content;
}
}
recipes is a dynamic storage array. You need to change the size of the array to add new elements to it. You can either do this by explicitly increasing the length of the array or by pushing a new element into the array.
function addRecipe(uint256 _price, string _name, string _content) public {
recipes.length++;
recipes[recipeCount].price = _price;
recipes[recipeCount].name = _name;
recipes[recipeCount].content = _content;
recipes[recipeCount].recipeOwner = msg.sender;
recipeCount++;
}
Or
function addRecipe(uint256 _price, string _name, string _content) public {
Recipe memory r;
r.price = _price;
r.name = _name;
r.content = _content;
r.recipeOwner = msg.sender;
recipes.push(r);
recipeCount++;
}