0

How can i use multiple arrays please?

e.g: I have this contract:

pragma solidity >=0.4.21 <0.6.0;

contract SessionManagement { 

    struct ChargeProfileSegment{
        uint startDateTimeSeg;
        uint power;
    }

    struct Session{
        address userWalletAddr;
        uint stationId;
        uint startDateTime; 
        uint endDateTime; 
        ChargeProfileSegment[] chargeProfileSegments;
    }

    mapping (address => Session[]) sessionList;
    Session[] public sessions;
    ChargeProfileSegment[] public chargeProfileSegments;

}

Now i want to create a function to save a list[] of Sessions, and in each Session i want to save a list[] of ChargeProfileSegment.

Your healp will please me.

1

My first impression leads me to question the use of a Session structure with begin and end times. That concept seems related to log in and log out processes that are appropriate in a server-centric setting. In Ethereum, you can proceed on the basis that all users are logged in at all times. They will not appear unless they are authenticated. It casts doubt on what would be meant by session start and session end.

mapping and arrays have certain limitations to be aware of as you create a structure that will meet all requirements. This intro might help: https://medium.com/robhitchens/solidity-crud-part-1-824ffa69509a

This is a little more refined and up-to-date: https://medium.com/robhitchens/solidity-crud-epilogue-e563e794fde

In your code, this line ...

address userWalletAddr;

... seems pointless, because this line...

mapping (address => Session[]) sessionList;

... requires that the caller knows the address to look for. What is the value of storing the address if the address must be known to recall the data?

Your goal isn't clear from your code. My response will, therefore, be more general.

Here is a nested CRUD (Create, Retrieve, Update, Delete) arrangement. Each Outer struct contains an inner set of structs.

pragma solidity 0.5.1;

import "./HitchensUnorderedKeyList.sol";

contract NestedCRUD {

    using HitchensUnorderedKeyListLib for HitchensUnorderedKeyListLib.Set;

    struct Outer {
        bool arg1;
        uint arg2;
        // carry on 
        HitchensUnorderedKeyListLib.Set nestedCRUD; // inner key list 
    }

    mapping(bytes32 => Outer) public outerStructs; // id => struct 
    HitchensUnorderedKeyListLib.Set outerKeys; // outer key list

    struct Inner {
        address arg1;
        uint arg2;
        // carry on
        bytes32 outerKey; // optional pointer to outer struct if you need to read it that way
    }

    mapping(bytes32 => Inner) public innerStructs;
    HitchensUnorderedKeyListLib.Set innerKeys;

    // Great ... create new ones

    function newOuter(bytes32 id, bool arg1, uint arg2) public {
        outerKeys.insert(id);
        outerStructs storage o = outerStructs[id];
        o.arg1 = arg1;
        o.arg2 = arg2;
    }

    function newInner(bytes32 id, bytes32 outerId, address arg1, uint arg2) public {
        require(outerKeys.exists(outerId), "Outer not found.");
        innerKeys.insert(id);
        Inner storage i = innerStructs[id];
        i.arg1 = arg1;
        i.arg2 = arg2;
        i.outerKey = outerKey;
    }

    // update existing

    function updateOuter(bytes32 id, bool arg1, uint arg2) public {
        require(outer.exists(id), "Outer not found.");
        Outer storage o = outerStructs[id];
        o.arg1 = arg1;
        o.arg2 = args;
    }

    function updateInner(bytes32 id, address arg1, uint arg2) public {
        require(inner.exists(id), "Inner not found");
        Inner storage i = innerStructs[id];
        i.arg1 = arg1;
        1.arg2 = arg2;
        // intentionally disallow changing to a different outer
    }

    // carry on with getters, other functions as needed.
}

Get the library from here: https://github.com/rob-Hitchens/UnorderedKeySet

Hope it helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.