How to store in an orders contract an array of products contract - e-commerce

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.

I think that the error is that you are treating the contract as they were classes.
To access a contract from another contract you have to instantiate it with it address.
You could store the products id in order contract and interact with products contract when you need to get products information.
If you want to create products with order contract you should do something like this
function createProduct(address addr) public {
Products p = Products(addr);
p.createProduct("name", "category", 1);
}

Related

Struct within another struct Solidity

I just started learning solidity and I am working on a bidding contract that allows bidders to bid on a campaign.
I have a struct for campaigns. Bidders have details (address, name), I want to store bidders with their information inside of the campaign. There can be more than one bidder for a campaign
This is my Campaign and Bidder struct
struct Campaign {
uint256 campaignID;
uint256 budget;
uint256 bidCount;
}
struct Bidder {
bool bided;
uint256 bid;
string name;
address bidderAddress;
}
mapping(address => Bidder) public bidders;
Campaign[] public campaigns;
I wrote down a bid function here that takes the index of campaign and bid then populate bidCount.
function bid(uint256 _bidIndex, uint256 _twitterID) public {
require(!bidders[msg.sender].bided);
bidders[msg.sender].bid = _bidIndex;
campaigns[_bidIndex].bidCount += 1;
totalBids += 1;
}
So the Campaign can look something like this (if this is possible)
0: campaignID 1
1: budget 2ETH
2: bidCount 3
3: Bidder {0: name Bidder1, 1: address 0xahaaahha}
{0: name Bidder2, 2: address 0x2334jddd}
Any help will be greatly appreciated. Thanks
According to me, in this case you can use nested mapping into Campaign struct for 'connect' different bids to a single campaign. I created a smart contract ad hoc for your case, you can see it in the following lines:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;
contract Auction {
address owner;
constructor() {
owner = msg.sender;
}
struct Campaign {
uint256 campaignID;
uint256 budget;
uint256 bidCount;
mapping(uint => Bidder[]) bidders;
}
struct Bidder {
bool bided;
uint256 bid;
string name;
address bidderAddress;
}
Campaign[] public campaigns;
uint totalCampaign = 0;
modifier onlyOwner {
require(msg.sender == owner, "Error! You're not the smart contract owner!");
_;
}
// Create campaigns
function createCampaing(uint _budgetCampaign) public onlyOwner {
Campaign storage _firstCampaigns = campaigns.push();
_firstCampaigns.campaignID = totalCampaign;
_firstCampaigns.budget = _budgetCampaign;
totalCampaign++;
}
// Bid
function bid(uint _indexCampaign, string memory _nameBidder) public {
Campaign storage _bidCampaign = campaigns[_indexCampaign];
_bidCampaign.bidCount += 1;
uint _bidIndex = _bidCampaign.bidCount;
_bidCampaign.bidders[_indexCampaign].push(Bidder(true, _bidIndex, _nameBidder, msg.sender));
}
// Getter Bids
function getBids(uint _indexCampaign) onlyOwner external view returns(Bidder[] memory) {
return campaigns[_indexCampaign].bidders[_indexCampaign];
}
}
With getBids() function, you retrieve all bids for a specific campaign that it identify with its index. I put a onlyOwner modifier that it allows to specific function that they'll call only from the smart contract owner (rather who deployed for first time smart contract into blockchain).

payments ( pre set value) in solidity

i am developing a Movie renting smartcontract. Where the owner can add new movies, clients can search movies and pay for the movies they select.Adding and searching is working to my liking.
problem: i want to develop the pay function as such- where it takes one argument( the title of the movie) and clients has to pay the exact amount set by the owner, he can not pay less then the price.
for example: owner add a movie: title-titanic,price-10 eth. when client use the pay function he put the title titanic and pay 10 eth. if he tries to pay less or more the transaction will not be successful.
here is the code
pragma solidity 0.8.7;
// SPDX-License-Identifier: MIT
contract Movie{
address public owner;
struct Move{
uint year;
uint price;
}
mapping (string => Move ) public movieInfo;
mapping(uint => Move) amount;
constructor() payable{
owner= msg.sender;
}
function addMovie(string memory _title, uint _year, uint _price) public {
require( msg.sender==owner);
movieInfo[_title]= Move(_year, _price);
}
function pay(string memory _title) public payable{
}
function totalFunds() public view returns(uint){
return address(this).balance;
}
}
Don't think that this kind of stuff is good to store into the blockchain ATM, due to the highly cost of storing.
In case you would like to have it:
mapping(string -> address[]) paidUsers;
function pay(string memory _title) public payable {
require(msg.value == movieInfo[_title].price, "Invalid price for the film");
paidUsers[_title].push(msg.sender);
}

Error when i click my "people" button. I can sucessfully "addPerson" but the information is not saved

call to SimpleStorage.people errored: Error encoding arguments: Error: invalid BigNumber string (argument="value" value="" code=INVALID_ARGUMENT version=bignumber/5.5.0)
Thank you anyone for their time.
pragma solidity ^0.6.0;
contract SimpleStorage {
// 0 is fav number.
uint256 favoriteNumber;
bool favoriteBool;
struct People {
uint256 favoriteNumber;
string name;
}
People[] public people;
function store(uint256 _favoriteNumber) public {
favoriteNumber = _favoriteNumber;
}
function retrieve() public view returns(uint256) {
return favoriteNumber;
}
function addPerson(string memory _name, uint256 _favoriteNumber) public{
people.push(People(_favoriteNumber, _name));
}
}
People is an array, so you need to specify the index of the array first.
So if you want to access the first person, you have to specify 0 (index begins with 0) in the input box next to the person button.

How can i push element on array defined on struct in Solidity?

I'm starting to approach with Solidity (current version 0.8.0) smart contracts, but I can't understand why the compiler print me an error during the push.
The problem is the following: I'm trying to push an element on array in struct but compile failed with error: Copying of type struct memory[] memory to storage not yet supported.
Here my example:
contract TestNFT is ERC721, Ownable {
struct Child {
string name;
}
struct Father {
string name;
Child[] childs;
}
mapping(uint256 => Child) private _childs;
uint256 nextChild = 0;
mapping(uint256 => Father) private _fathers;
uint256 nextFather = 0;
constructor(string memory name, string memory symbol)
ERC721(name, symbol)
{}
function mint(
string memory name
) public onlyOwner {
_safeMint(msg.sender, nextCharacter);
_childs[nextChild] = Child(name);
nextChild++;
}
function mintFather(string memory name) public onlyOwner {
_safeMint(msg.sender, nextClan);
_fathers[nextFather] = Father(name, new Child[](0));
nextFather++;
}
function insertChildToFather(uint256 fatherId, uint256 childId)
public
onlyOwner
{
Child memory child = _childs[childId];
_fathers[fatherId].childs.push(child);
}
}
How can I fix the insertChildToFather function?

Solidity: return array in a public method

I am trying to create a public funcion that returns an array,
this is the error
Return argument type mapping(uint256 => struct ItemList.Item storage
ref) is not implicitly convertible to expected type (type of first
return variable) uint256[] memory.
pragma solidity ^0.5.0;
contract ItemList {
uint public itemCount = 0;
mapping(uint256 => Item) public items;
event ItemCreated (
uint id,
string proofdocument
);
struct Item {
uint id;
string proofdocument;
}
constructor() public {
}
function createItem(string memory _proofdocument) public {
itemCount++;
items[itemCount] = Item(itemCount, _proofdocument);
emit ItemCreated(itemCount, _proofdocument);
}
function getItems() public pure returns(uint256[] memory ) {
return items; <----------ERROR
}
}
Thanks Andrea
You can get every item in the loop via web3.js library
const array = []
for (let i = 0; i < itemCount; itemCount += 1) {
array.push(contract.getItem(i)) // where getItem do items[I] in solidity
}
Or you can use pragma experimental version:
pragma solidity ^0.5.0;
pragma experimental ABIEncoderV2;
contract ItemList {
uint public itemCount = 0;
struct Item {
uint id;
string proofdocument;
}
Item[] items;
constructor() public {}
function createItem(string memory _proofdocument) public {
itemCount++;
items.push(Item(itemCount, _proofdocument));
}
function getItems() external view returns(Item[] memory) {
return items;
}
}