I'm new to solidity and the whole blockchain concept and I'm trying to do a simple e-commerce website where a user can select some products, put them in his basket and place an order.
The problem is I don't know how to make this work. I'm trying to initialize the order contract with a mock order that contains an array of products, but I'm getting TypeErrors such as this:
"TypeError: Member "createProduct" not found or not visible after argument-dependent lookup in type(contract Products)."
Here is my Products.sol
pragma solidity ^0.5.0;
contract Products {
uint256 public productsCount = 0;
struct Product {
uint256 id;
string name;
string category;
int256 price;
}
mapping(uint256 => Product) public products;
event ProductCreated(
uint256 id,
string name,
string category,
int256 price
);
event ProductEdited(uint256 id, string name, string category, int256 price);
event ProductDeleted(
uint256 id,
string name,
string category,
int256 price
);
constructor() public {
createProduct("Test", "Test", 53);
}
function createProduct(
string memory _name,
string memory _category,
int256 _price
) public {
productsCount++;
products[productsCount] = Product(
productsCount,
_name,
_category,
_price
);
emit ProductCreated(productsCount, _name, _category, _price);
}
function editProduct(
uint256 _id,
string memory _name,
string memory _category,
int256 _price
) public {
Product memory _product = products[_id];
_product.name = _name;
_product.category = _category;
_product.price = _price;
products[_id] = _product;
emit ProductEdited(_id, _name, _category, _price);
}
function deleteProduct(uint256 _id) public {
Product memory _product = products[_id];
delete products[_id];
productsCount--;
emit ProductDeleted(
_id,
_product.name,
_product.category,
_product.price
);
}
}
And here is my Orders.sol
pragma solidity ^0.5.0;
import "./Products.sol";
contract Orders {
uint256 public ordersCount = 0;
struct Order {
uint256 id;
int256 totalPrice;
string date;
Products[] products;
}
mapping(uint256 => Order) public orders;
constructor() public {
createOrder(
150,
"01.01.2021",
[Products.createProduct("name", "category", 1)]
);
}
function createOrder(
int256 _totalPrice,
string memory _date,
Products[] memory _products
) public {
ordersCount++;
orders[ordersCount] = Order(ordersCount, _totalPrice, _date, _products);
}
}
Probably it's a simple question but I've never used solidity before and all the tutorials on youtube are too simple and don't explain this kind of nested datas.
I'd really appreciate some help here :D
Your createProduct
method returns nothing. It is for this reason that it does not work. You can change your method like
function createProduct(
string memory _name,
string memory _category,
int256 _price
) public returns (Product) {
productsCount++;
products[productsCount] = Product(
productsCount,
_name,
_category,
_price
);
emit ProductCreated(productsCount, _name, _category, _price);
return products[productsCount];
}
Or you can create another method that will call createProduct
to create a product and then return it.