0

I have a little problem with my code. I'm trying to follow who are the current sender and the previous sender of a transaction respectively with the variable currentSender and lastSender, and then enregister them in a mapping nested in the struct Order. However trought the get function I created, I saw that all lastSender variable give me blank address, while currentSender give me the good address.

Here is code:

pragma solidity  ^0.5.16;

contract SupChain{
struct Status{
    address currentSender;
    address lastSender;
    string test;
    }

struct Order{
    uint256 orderID;
    uint256 NumberOfUpdate;
    string test2;
    mapping (uint256 => Status) statutes;
    }

uint256 public orderCount = 0;

mapping (uint256 => Order) public orders;


function createOrder(string memory test, string memory test2) public{
    orderCount++;
    Order storage newOrder = orders[orderCount];
    orders[orderCount] = Order(orderCount, 0, test2);
    newOrder.statutes[newOrder.NumberOfUpdate++] = Status(msg.sender, msg.sender, test);
    }

function storeOrder(uint256 id, string memory test) public{
    Order storage concernedOrder = orders[id];
    
    //Getting the previous sender before updating the NumberOfUpdate
    Status memory previousStatus = concernedOrder.statutes[concernedOrder.NumberOfUpdate];
    address previousSender = previousStatus.currentSender;
    
    concernedOrder.NumberOfUpdate = concernedOrder.NumberOfUpdate++;
    concernedOrder.statutes[concernedOrder.NumberOfUpdate++] = 
                            Status(msg.sender, previousSender, test);
    }

function getStatus(uint256 id, uint256 concernedStatusNumber) public view              
                   returns(address _currentSender, address _lastSender, 
                           string memory _test){
    Order storage concernedOrder = orders[id];
    Status memory concernedStatus = concernedOrder.statutes[concernedStatusNumber];
    return(concernedStatus.currentSender, concernedStatus.lastSender, 
           concernedStatus.test);

    }
}

So, I would like to request some help to resolve this problem please.

I thank in advance anybody who will take the time to help me.

What I tried:

Omitting the incrementation of new order from the createOrder function and obviusly leaving else the same as before:

function storeOrder(uint256 id, string memory test) public{
    Order storage concernedOrder = orders[id];

    //Getting the previous sender before updating the NumberOfUpdate
    Status memory previousStatus = concernedOrder.statutes[concernedOrder.NumberOfUpdate];
    address previousSender = previousStatus.currentSender;

    concernedOrder.NumberOfUpdate = concernedOrder.NumberOfUpdate++;
    concernedOrder.statutes[concernedOrder.NumberOfUpdate] = 
                            Status(msg.sender, previousSender, test);
    }

Adding a decrementation to the variable previousStatus of the storeOrder function and leaving else the same as before:

function storeOrder(uint256 id, string memory test) public{
    Order storage concernedOrder = orders[id];

    //Getting the previous sender before updating the NumberOfUpdate
    Status memory previousStatus = concernedOrder.statutes[concernedOrder.NumberOfUpdate--];
    address previousSender = previousStatus.currentSender;

    concernedOrder.NumberOfUpdate = concernedOrder.NumberOfUpdate++;
    concernedOrder.statutes[concernedOrder.NumberOfUpdate++] = 
                            Status(msg.sender, previousSender, test);
    }

Now I'm seing if the problem doesn't come from my getStatus function

0

The reason you are getting empty lastSender is because you are incrementing newOrder.NumberOfUpdate++ when you create order. It stores the status at index 0 and increments the variable. When you set the order, it retrieves the incremented value, which is 1 at that point and it does not have a status against this index because the status was stored at index 0.

You can either omit the incrementing in createOrder or you can decrement the NumberOfUpdate by 1 when retrieving old status in setOrder.

1
  • Just tried both method but they didn't work. I omitted the incrementing in newOrder.statutes[newOrder.NumberOfUpdate++] from the function createOrder without touching anything else but didn't work. The same when I decremented concernedOrder.statutes[concernedOrder.NumberOfUpdate] from the storeOrder function... Aug 26 '20 at 6:22
0

Your createOrder is confusing, try like that

function createOrder(string memory test, string memory test2) public{
    orderCount++;
    orders[orderCount] = Order(orderCount, 0, test2);
    orders[orderCount].NumberOfUpdate++;
    orders[orderCount].statutes[orders[orderCount].NumberOfUpdate] = Status(msg.sender, msg.sender, test);
}

I would recommend to not start your order count at 1, it's confusing to deal with array / mapping without 0 index as developer. Something like that looks better from my point of view.

function createOrder(string memory test, string memory test2) public{
    // orderCount  is 0 for first order
    orders[orderCount] = Order(orderCount, 0, test2); 

    // first update update is stored at 0
    orders[orderCount].statutes[orders[orderCount].NumberOfUpdate] = Status(msg.sender, msg.sender, test);

    orders[orderCount].NumberOfUpdate++;
    orderCount++;
}
1
0

I finally found the error... it was a really vicious one.

Basically when I was writting:

concernedOrder.statutes[concernedOrder.NumberOfUpdate++] = Status(msg.sender, previousSender, test);

The incrementation didn't increment the Status position inside of statutes but the number of updates of Order ! So to have the good position I need to use +1 in place of ++. Here is the good code:

pragma solidity  ^0.5.16;

contract SupChain{
struct Status{
    address currentSender;
    address lastSender;
    string test;
}

struct Order{
    uint256 orderID;
    uint256 NumberOfUpdate;
    string test2;
    mapping (uint256 => Status) statutes;
}

uint256 public orderCount = 0;

mapping (uint256 => Order) public orders;

function createOrder(string memory test, string memory test2) public{
    orders[orderCount] = Order(orderCount, 0, test2); 
    orders[orderCount].statutes[orders[orderCount].NumberOfUpdate] = Status(msg.sender, msg.sender, test);
    orderCount++;
}

function storeOrder(uint256 id, string memory test) public{
    orders[id].statutes[orders[id].NumberOfUpdate + 1] = Status(msg.sender, orders[id].statutes[orders[id].NumberOfUpdate].currentSender, test);
    orders[id].NumberOfUpdate++;
}

function getStatus(uint256 id, uint256 concernedStatusNumber) public view returns(address _currentSender, address _lastSender, string memory _test){
    Order storage concernedOrder = orders[id];
    Status memory concernedStatus = concernedOrder.statutes[concernedStatusNumber];
    return(concernedStatus.currentSender, concernedStatus.lastSender, concernedStatus.test);
}
}

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.