How to get an overview of created structs in a mapping for a specific address in solidity? - struct

After I created some structs which belong to specific addresses I want to get an overview of structs with associated parameters regarding an address.
So what could I do for solving this problem?
If I am running my code inside remix, I get back only my first stored struct for an address. But I want to get back all stored structs for one address. I know that we can not iterate through a mapping, but maybe it is possible to make some index-counter for the array of structs to solve it? - So is it also possible to store the index of array in a variable?
pragma solidity ^0.4.17;
contract Prescribe {
struct Prescription {
address patients_address;
string medicament;
string dosage_form;
uint amount;
uint date;
//uint index_counter;
}
mapping (address => Prescription[]) public ownerOfPrescription;
address [] public patients;
function createPrescription(address patients_address, string
medicament, string dosage_form, uint amount, uint date) public
restricted {
ownerOfPrescription[patients_address].push(Prescription({
patients_address: patients_address,
medicament: medicament,
dosage_form: dosage_form,
amount: amount,
date: date
}));
patients.push(patients_address);
}
function getOverview(address patient) public view restricted
returns(string, string, uint, uint) {
for(uint i = 0; i < ownerOfPrescription[patient].length; i++) {
if(ownerOfPrescription[patient][i].patients_address == patient) {
return(ownerOfPrescription[patient][i].medicament,
ownerOfPrescription[patient][i].dosage_form,
ownerOfPrescription[patient][i].amount,
ownerOfPrescription[patient][i].date);
}
}
}
So I want to have the return-values of all separate structs of one address as in the function getOverview on the screen, but it gives me back only the first struct of an address

Well, it's returning only the first one, because after the statement
if(ownerOfPrescription[patient][i].patients_address == patient)
return true, your code is executing the return statement which will make the control to exit from the function, and no further statement will be executed.

Ok after a research I made the conclusion that it is still not possible to get an array of structs as a return value. One has only the possibility to access individual elements of the array right? - If there are any updates on this topic, I would be very grateful for a hint.

There is a way to return array of struct but it will cost a little bit more gas fees.
Below is an example
contract Test {
struct FlexiblePlan {
string token;
address _address;
}
struct Plans {
FlexiblePlan[] flexiblePlans;
}
Plans plans;
function createPlan(string memory _token, address _address) external {
plans.flexiblePlans.push(FlexiblePlan(_token, _address));
}
function getAllPlans() external view returns(Plans memory){
return plans;
}
}

Related

Solidity struct mapping not stored in contract

I read many articles on how to use mappings, mappings in struct and came out with something that should be correct to me, based on a few threads.
I know that since solidity 0.7.0 things have changed with nested mappings in struct and did the following :
contract Test {
constructor() {
}
struct Bid {
uint auction_id;
address addr;
uint amount;
}
struct Auction {
uint id;
string dtype;
uint start_date;
uint end_date;
string label;
uint price;
uint amount;
bool closed;
mapping(uint => Bid) bids;
uint bidCount;
}
uint public auctionCount = 0;
mapping(uint => Auction) public auctions;
function createAuction( string memory plabel, string memory ptype, uint nbhours, uint pprice) external {
Auction storage nd = auctions[auctionCount];
nd.id = auctionCount;
nd.dtype = ptype;
nd.start_date = block.timestamp;
nd.end_date = block.timestamp+nbhours*60*60;
nd.label = plabel;
nd.price = pprice;
nd.amount = 0;
nd.closed = false;
nd.bidCount = 0;
auctionCount++;
}
}
Everything compiles fine, the createAuction transaction is succesful.
When checking on the contract in Ganache, auctionCount is incremented but I have no items added in the drawsmapping.
I also debugged the transaction with truffle and it goes through the function, assigning values through the execution of createAuction, but the changes are not persistent.
I even tried removing one string attribute since I read that when there are 3 it could have been a problem (ok, I have only 2 max ;)).
I must have missed something, but I'm out of options right now.
Thanks in advance for your help !
If you are talking about auctions mapping, ensure you use the correct index when accessing mapping items. In your case, the first Auction item you add to the mapping will have a 0 index. I tried your contract in Remix, and everything worked well.

Passing a std::unique_ptr to CListBox::GetSelItems

I have seen a great answer here which has helped me to a great extent (Proper way to create unique_ptr that holds an allocated array) but I still have an issue.
Code:
void CSelectedBroHighlight::BuildSelectedArray()
{
CString strText;
// empty current array
m_aryStrSelectedBro.RemoveAll();
// get selected count
const auto iSize = m_lbBrothers.GetSelCount();
if(iSize > 0)
{
//auto pIndex = std::make_unique<int[]>(iSize);
auto pIndex = new int[iSize];
m_lbBrothers.GetSelItems(iSize, pIndex);
for(auto i = 0; i < iSize; i++)
{
m_lbBrothers.GetText(pIndex[i], strText);
m_aryStrSelectedBro.Add(strText);
}
delete[] pIndex;
}
}
If I turn pIndex into a smart pointer:
auto pIndex = std::make_unique<int[]>(iSize);
So that I don't need the delete[] pIndex; call. Then I can't pass pIndex to GetSelItems. I can pass pIndex.release() here but then we have a problem for deleting again.
I have looked at this discussion (Issue passing std::unique_ptr's) but we don't want to pass ownership.
If I simplify this and declar my variable: auto pIndex = std::make_unique<int[]>(iSize).release(); then I can pass it, but now have the issue of calling delete[] pIndex;.
Whats correct?
If you need access to the pointer to an object managed by a std::unique_ptr without transferring ownership, you can call its get() method. This is useful for interop with a C interface such as here (GetSelItems() is really just wrapping a call to SendMessage with the LB_GETSELITEMS message).
That'd work, though in this case I'd probably use a std::vector<int> instead. It provides the same properties as a std::unique_ptr with respect to automatic cleanup, but also has other features that come in handy (specifically range adapters). It also feels more natural to use a container here, but that's a matter of personal preference.
The following implements the proposed changes:
void CSelectedBroHighlight::BuildSelectedArray() {
// empty current array
m_aryStrSelectedBro.RemoveAll();
// get selected count
auto const sel_item_count{ m_lbBrothers.GetSelCount() };
if(sel_item_count > 0) {
// get selected indices
std::vector<int> sel_items(sel_item_count);
m_lbBrothers.GetSelItems(sel_items.size(), sel_items.data());
// iterate over all selected item indices
for(auto const index : sel_items) {
CString strText;
m_lbBrothers.GetText(index, strText);
m_aryStrSelectedBro.Add(strText);
}
}
}
This provides the same automatic cleanup as an implementation based on std::unique_ptr, but also enables use of a range-based for loop further down.

How to save structs in array in Solidity?

I wrote a very simple smart contract which does the following:
Declares a struct named Player which has 2 fields: name and goals
Defines a function which takes as inputs names and goals to create a new instance of the structure
Stores the new player into an array of players
Here is the code:
pragma solidity ^0.8.0;
contract MyContract {
// Declare struct
struct Player {
string name;
uint goals;
}
// Declare array
Player[] player;
function addPlayer(string calldata _name, uint _goals) external pure{
Player memory player = Player({name:_name, goals:_goals}); // This declaration shadows an existing declaration.
players.push(player); // Member "push" not found or not visible after argument-dependent lookup in struct MyContract.Player memory.
}
}
My goal is to use the addPlayer() function to add players to the players array
However the VisualStudio Editor returns an error since it gets confused by the declaration of the Player variable which appears to be twice.
Could you please provide a smart and elegant way to achieve my goal please?
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract MyContract {
// Declare struct
struct Player {
string name;
uint goals;
}
// Declare array
Player[] players;
function addPlayer(string calldata _name, uint _goals) external {
Player memory player = Player(_name,_goals); // This declaration shadows an existing declaration.
players.push(player); // Member "push" not found or not visible after argument-dependent lookup in struct MyContract.Player memory.
}
}
Thanks to Chíp Thảo for leaving the answer with not complete explain :)
Here is continue of explain:
In line 12 you declared an array, Player[] player, with name player and also in line 16 you declared a variable again with name player! better change line 12 into players.

Is the mapping in solidity of one key on multiple structs of same type possible?

I am trying to map one address on multiple structs of same type, which belongs to the same address. How can I do this, if I want to choose any of the "stored" structs for one address on request afterwards?
I created a struct called Prescription, and a mapping with the patients address. So what I really want is to map the patients address to several Prescription-structs.
struct Prescription {
address patients_address;
string medicament;
string dosage_form;
uint amount;
uint date;
}
mapping (address => Prescription) ownerOfPrescription;
address [] public patients;
function createPrescription(address patients_address, string medicament, string dosage_form, uint amount, uint date) public restricted {
var newPrescription = ownerOfPrescription[patients_address];
newPrescription.medicament = medicament;
newPrescription.dosage_form = dosage_form;
newPrescription.amount = amount;
newPrescription.date = date;
patients.push(patients_address) -1;
}
function getPre(address _address)view public returns (string, string, uint, uint){
return(
ownerOfPrescription[_address].medicament,
ownerOfPrescription[_address].dosage_form,
ownerOfPrescription[_address].amount,
ownerOfPrescription[_address].date);
}
Now I would have a function, where I can call all written Prescriptions for one patient. Actually I am able to call only the last written prescription for one address.
Sure, the value type of a mapping can be an array:
// map to an array
mapping (address => Prescription[]) ownerOfPrescription;
function createPrescription(...) ... {
// add to the end of the array
ownerOfPrescription[patients_address].push(Prescription({
medicament: medicament,
...
});
patients.push(patients_address);
}

Is there an additional runtime cost for using named parameters?

Consider the following struct:
public struct vip
{
string email;
string name;
int category;
public vip(string email, int category, string name = "")
{
this.email = email;
this.name = name;
this.category = category;
}
}
Is there a performance difference between the following two calls?
var e = new vip(email: "foo", name: "bar", category: 32);
var e = new vip("foo", 32, "bar");
Is there a difference if there are no optional parameters defined?
I believe none. It's only a language/compiler feature, call it syntactic sugar if you like. The generated CLR code should be the same.
There's a compile-time cost, but not a runtime one...and the compile time is very, very minute.
Like extension methods or auto-implemented properties, this is just magic the compiler does, but in reality generates the same IL we're all familiar with and have been using for years.
Think about it this way, if you're using all the parameters, the compiler would call the method using all of them, if not, it would generate something like this behind the scenes:
var e = new vip(email: "foo", category: 32); //calling
//generated, this is what it's actually saving you from writing
public vip(string email, int category) : this(email, category, "bar") { }
No it is a compile-time feature only. If you inspect the generated IL you'll see no sign of the named parameters. Likewise, optional parameters is also a compile-time feature.
One thing to keep in mind regarding named parameters is that the names are now part of the signature for calling a method (if used obviously) at compile time. I.e. if names change the calling code must be changed as well if you recompile. A deployed assembly, on the other hand, will not be affected until recompiled, as the names are not present in the IL.
There shouldn't be any. Basically, named parameters and optional parameters are syntactic sugar; the compiler writes the actual values or the default values directly into the call site.
EDIT: Note that because they are a compiler feature, this means that changes to the parameters only get updated if you recompile the "clients". So if you change the default value of an optional parameter, for example, you will need to recompile all "clients", or else they will use the old default value.
Actually, there is cost at x64 CLR
Look at here http://www.dotnetperls.com/named-parameters
I am able to reproduce the result: named call takes 4.43 ns, and normal call takes 3.48 ns
(program runs in x64)
However, in x86, both take around 0.32 ns
The code is attached below, compile and run it yourself to see the difference.
Note that in VS2012 the default targat is AnyCPU x86 prefered, you have to switch to x64 to see the difference.
using System;
using System.Diagnostics;
class Program
{
const int _max = 100000000;
static void Main()
{
Method1();
Method2();
var s1 = Stopwatch.StartNew();
for (int i = 0; i < _max; i++)
{
Method1();
}
s1.Stop();
var s2 = Stopwatch.StartNew();
for (int i = 0; i < _max; i++)
{
Method2();
}
s2.Stop();
Console.WriteLine(((double)(s1.Elapsed.TotalMilliseconds * 1000 * 1000) /
_max).ToString("0.00 ns"));
Console.WriteLine(((double)(s2.Elapsed.TotalMilliseconds * 1000 * 1000) /
_max).ToString("0.00 ns"));
Console.Read();
}
static void Method1()
{
Method3(flag: true, size: 1, name: "Perl");
}
static void Method2()
{
Method3(1, "Perl", true);
}
static void Method3(int size, string name, bool flag)
{
if (!flag && size != -1 && name != null)
{
throw new Exception();
}
}
}

Resources