0

I have here a solidity program that I am trying to make, it should store the transaction to the blockchain, this transaction contains, id, date, buyer, seller, ORnumber, a multiple products with their corresponding prices, and a total of those prices. I tried creating two structs and the other struct will contain the Products with their prices. This is my code:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract TransactionContract {
    struct Product {
        string name;
        uint256 price;
    }

struct Transaction {
    uint256 id;
    uint256 date;
    address buyer;
    address seller;
    uint256 ORnumber;
    Product[] products;
    uint256 total;
}

Transaction[] public transactions;

function addTransaction(
    uint256 _id,
    uint256 _date,
    address _buyer,
    address _seller,
    uint256 _ORnumber,
    Product[] memory _products
) public {
    uint256 _total = 0;
    for (uint256 i = 0; i < _products.length; i++) {
        _total += _products[i].price;
    }

    Transaction memory newTransaction = Transaction({
        id: _id,
        date: _date,
        buyer: _buyer,
        seller: _seller,
        ORnumber: _ORnumber,
        products: _products,
        total: _total
    });

    transactions.push(newTransaction);
}

function getTransaction(uint256 _index)
    public
    view
    returns (
        uint256 id,
        uint256 date,
        address buyer,
        address seller,
        uint256 ORnumber,
        Product[] memory products,
        uint256 total
    )
{
    require(_index < transactions.length, "Transaction index out of bounds.");
    Transaction storage transaction = transactions[_index];

    return (
        transaction.id,
        transaction.date,
        transaction.buyer,
        transaction.seller,
        transaction.ORnumber,
        transaction.products,
        transaction.total
    );
}

function getAllTransactions()
    public
    view
    returns (Transaction[] memory)
{
    return transactions;
}
}

But an error is keeping me from compiling it.

UnimplementedFeatureError: Copying of type struct TransactionContract.Product memory[] memory to storage not yet supported.

Please help

1 Answer 1

0

The error is becaused you're trying to directly assign a dynamic array of structs (_products) from memory to storage.

products: _products,

Solidity doesn't support this operation.

I've modified the addTransaction function to fix it.

The main changes are:

  • Directly assign values to the struct in storage, avoiding the copying error.
  • Loop through the _products array and push each product into the storage array individually.

These changes should resolve the error and make your function work as expected.

function addTransaction(
    uint256 _id,
    uint256 _date,
    address _buyer,
    address _seller,
    uint256 _ORnumber,
    Product[] memory _products
) public   {
    
    uint256 _total = 0;
    for (uint256 i = 0; i < _products.length; i++) {
        _total += _products[i].price;
    }

    uint256 transactionIndex = transactions.length;

    // Assign values to the Transaction struct in storage
    Transaction storage newTransaction = transactions[transactionIndex];
    newTransaction.id = _id;
    newTransaction.date = _date;
    newTransaction.buyer = _buyer;
    newTransaction.seller = _seller;
    newTransaction.ORnumber = _ORnumber;
    newTransaction.total = _total;

    for (uint256 i = 0; i < _products.length; i++) {
        newTransaction.products.push(_products[i]);
    }
}

Your Answer

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

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