0

pragma solidity ^0.6.0;

contract SupplyChain {

event Added(uint256 index);

struct State{
    string description;
    address person;
}

struct Product{
    address creator;
    string productName;
    uint256 productId;
    string date;
    uint256 totalStates;
    mapping (uint256 => State) positions;
}

mapping(uint => Product) allProducts;
uint256 items=0;

function concat(string memory _a, string memory _b) public returns (string memory){
    bytes memory bytes_a = bytes(_a);
    bytes memory bytes_b = bytes(_b);
    string memory length_ab = new string(bytes_a.length + bytes_b.length);
    bytes memory bytes_c = bytes(length_ab);
    uint k = 0;
    for (uint i = 0; i < bytes_a.length; i++) bytes_c[k++] = bytes_a[i];
    for (uint i = 0; i < bytes_b.length; i++) bytes_c[k++] = bytes_b[i];
    return string(bytes_c);
}

function newItem(string memory _text, string memory _date) public returns (bool) {
    Product memory newItem = Product({creator: msg.sender, totalStates: 0,productName: _text, productId: items, date: _date});
    allProducts[items]=newItem;
    items = items+1;
    emit Added(items-1);
    return true;
}

function addState(uint _productId, string memory info) public returns (string memory) {
    require(_productId<=items);
    
    State memory newState = State({person: msg.sender, description: info});
    
    allProducts[_productId].positions[ allProducts[_productId].totalStates ]=newState;
    
    allProducts[_productId].totalStates = allProducts[_productId].totalStates +1;
    return info;
}

function searchProduct(uint _productId) public returns (string memory) {

    require(_productId<=items);
    string memory output="Product Name: ";
    output=concat(output, allProducts[_productId].productName);
    output=concat(output, "<br>Manufacture Date: ");
    output=concat(output, allProducts[_productId].date);
    
    for (uint256 j=0; j<allProducts[_productId].totalStates; j++){
        output=concat(output, allProducts[_productId].positions[j].description);
    }
    return output;
    
}

}

Errors I am receiving is:-

1.contracts/test.sol:36:9: Warning: This declaration shadows an existing declaration. Product memory newItem = Product({creator: msg.sender, totalStates: 0,productName: _text, productId: items, date: _date}); ^--------------------^ contracts/test.sol:35:5: The shadowed declaration is here: function newItem(string memory _text, string memory _date) public returns (bool) { ^ (Relevant source part starts here and spans across multiple lines).

  1. contracts/test.sol:24:5: Warning: Function state mutability can be restricted to pure function concat(string memory _a, string memory _b) public returns (string memory){ ^ (Relevant source part starts here and spans across multiple lines).
1
  • Probably best to use a new version of Solidity (we're on 0.8.19) and not 0.6
    – Ryan Sea
    Feb 27 at 18:03

1 Answer 1

1

Your first error comes from that you have named a variable with the same name as your function:

function newItem(...) ... {}
    Product memory newItem = ...

Change either the name of the function or the name of the variable in it.

The second error means that you can put pure modifier to the funcion, meaning that this funcion does any calculations without the need to read or write data to the blockchain.

You can do it like so:

function concat(string memory _a, string memory _b) public pure returns (string memory) {}

Good luck.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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