Hello. I have a "how to" question in Solidity. Should be simple I'm sure but I can't seem to get this right. I have a user that will add articles to sell. So when the function to get articles is invoiked by a buyer, they can get a list of articles for sale by a specific wallet address. My difficulty is having the right setup for arrays that will be returned to the calling end which will be Javascript. Below is the code snippet:
Dec 2, 2022, 6:54 AM
// state variables
address seller;
address buyer;
string name;
string description;
uint256 price;
// events
event LogSellArticle(
address indexed _seller,
string _name,
uint256 _price
);
event LogBuyArticle(
address indexed _seller,
address indexed _buyer,
string _name,
uint256 _price
);
// sell an article
function sellArticle(string memory _name, string memory _description, uint256 _price) public {
seller = msg.sender;
name = _name;
description = _description;
price = _price;
emit LogSellArticle(seller, name, price);
}
// get an article
function getArticle() public view returns (
address _seller,
address _buyer,
string memory _name,
string memory _description,
uint256 _price
) {
return(seller, buyer, name, description, price);
}
address seller;
address buyer;
string name;
string description;
uint256 price;
// events
event LogSellArticle(
address indexed _seller,
string _name,
uint256 _price
);
event LogBuyArticle(
address indexed _seller,
address indexed _buyer,
string _name,
uint256 _price
);
// sell an article
function sellArticle(string memory _name, string memory _description, uint256 _price) public {
seller = msg.sender;
name = _name;
description = _description;
price = _price;
emit LogSellArticle(seller, name, price);
}
// get an article
function getArticle() public view returns (
address _seller,
address _buyer,
string memory _name,
string memory _description,
uint256 _price
) {
return(seller, buyer, name, description, price);
}
Any help is greatly appreciated. Thanks!!!
Dec 2, 2022, 6:55 AM
learn C first?
Dec 2, 2022, 6:55 AM
i don't see the array
Dec 2, 2022, 6:56 AM
The struct would be something like struct article{address; name; desc; price;}
I think mapping would be mapping(address => articles[]) articlesForSale;
Dec 2, 2022, 6:59 AM
i didn't understand your issue
so you have a mapping that gives an array of structures
Dec 2, 2022, 7:00 AM
it's the syntax that I'm trying to figure out to return the array of articles from the getArticles function.
Dec 2, 2022, 7:01 AM
ok than you first have to dexide what info about article you want
ok wait
if you use the mapping as you said
Dec 2, 2022, 7:04 AM
so the struct are the info of an article being sold
Dec 2, 2022, 7:04 AM
you get an error?
Dec 2, 2022, 7:04 AM
no errors at least on the sellArticle.
Dec 2, 2022, 7:05 AM
i mean if you do as you said
what do you get
what do you get
Dec 2, 2022, 7:05 AM
I'll share more of my code, at least something to show.
brb
Dec 2, 2022, 7:06 AM
Create a struct first
Then create a array of that struct
Then loop through all articles, and push the struct into the array
Last, return array.
Then create a array of that struct
Then loop through all articles, and push the struct into the array
Last, return array.
Dec 2, 2022, 7:06 AM
thanks for the help still
Dec 2, 2022, 7:06 AM
pragma solidity ^0.5.0;
contract ReturnOrders {
// Define a struct that represents an order
struct Order {
uint orderId;
address customerAddress;
uint quantity;
uint price;
}
// Define a mapping from order ID to order struct
mapping(uint => Order) public orders;
constructor() public {
// Add some orders to the mapping
orders[0] = Order(0, 0x123456..., 10, 100);
orders[1] = Order(1, 0x789012..., 5, 50);
orders[2] = Order(2, 0x345678..., 15, 75);
}
// Define a public function that returns all orders
function getOrders() public view returns (Order[] memory) {
// Create a new array to store the orders
Order[] memory result = new Order[](orders.length);
// Loop through the orders mapping and add each order to the result array
uint i = 0;
for (uint orderId in orders) {
result[i] = orders[orderId];
i++;
}
// Return the result array
return result;
}
}
contract ReturnOrders {
// Define a struct that represents an order
struct Order {
uint orderId;
address customerAddress;
uint quantity;
uint price;
}
// Define a mapping from order ID to order struct
mapping(uint => Order) public orders;
constructor() public {
// Add some orders to the mapping
orders[0] = Order(0, 0x123456..., 10, 100);
orders[1] = Order(1, 0x789012..., 5, 50);
orders[2] = Order(2, 0x345678..., 15, 75);
}
// Define a public function that returns all orders
function getOrders() public view returns (Order[] memory) {
// Create a new array to store the orders
Order[] memory result = new Order[](orders.length);
// Loop through the orders mapping and add each order to the result array
uint i = 0;
for (uint orderId in orders) {
result[i] = orders[orderId];
i++;
}
// Return the result array
return result;
}
}
You will get your answers if you understand this
pragma solidity ^0.5.0;
contract ReturnArticles {
// Define a struct that represents an article
struct Article {
uint articleId;
string title;
string author;
string content;
}
// Define a mapping from article ID to article struct
mapping(uint => Article) public articles;
constructor() public {
// Add some articles to the mapping
articles[0] = Article(0, "Article 1", "John Doe", "Lorem ipsum dolor sit amet...");
articles[1] = Article(1, "Article 2", "Jane Doe", "Consectetur adipiscing elit...");
articles[2] = Article(2, "Article 3", "Bob Smith", "Sed do eiusmod tempor incididunt...");
}
// Define a public function that returns all articles
function getArticles() public view returns (Article[] memory) {
// Create a new array to store the articles
Article[] memory result = new Article[](articles.length);
// Loop through the articles mapping and add each article to the result array
uint i = 0;
for (uint articleId in articles) {
result[i] = articles[articleId];
i++;
}
// Return the result array
return result;
}
}
contract ReturnArticles {
// Define a struct that represents an article
struct Article {
uint articleId;
string title;
string author;
string content;
}
// Define a mapping from article ID to article struct
mapping(uint => Article) public articles;
constructor() public {
// Add some articles to the mapping
articles[0] = Article(0, "Article 1", "John Doe", "Lorem ipsum dolor sit amet...");
articles[1] = Article(1, "Article 2", "Jane Doe", "Consectetur adipiscing elit...");
articles[2] = Article(2, "Article 3", "Bob Smith", "Sed do eiusmod tempor incididunt...");
}
// Define a public function that returns all articles
function getArticles() public view returns (Article[] memory) {
// Create a new array to store the articles
Article[] memory result = new Article[](articles.length);
// Loop through the articles mapping and add each article to the result array
uint i = 0;
for (uint articleId in articles) {
result[i] = articles[articleId];
i++;
}
// Return the result array
return result;
}
}
Or this
Dec 2, 2022, 7:08 AM
you are using uint for the key type as I'm using address. that makes sense instead.
Dec 2, 2022, 7:08 AM
Are you trying to get a single article from getArticle or all articles ?
Dec 2, 2022, 7:10 AM
no returning an array of articles that for sale.
So anyone can add something to sell. Then a buyer can view all these items for sale
Dec 2, 2022, 7:10 AM
You need to do something more than the code you already did.
Dec 2, 2022, 7:11 AM
btw this will break if orders.length > 200 or 1000
Dec 2, 2022, 7:13 AM
pragma solidity ^0.5.0;
contract ArticlesMarketplace {
// Struct that represents an article
struct Article {
// The address of the seller of an article
address seller;
// The name of the article
string articleName;
// The price of the article in wei
uint price;
// The state of the article (true = available, false = sold)
bool isAvailable;
}
// Mapping from article ID to article struct
mapping(uint => Article) public articles;
// The total number of articles
uint public totalArticles;
// Event that is emitted when an article is listed
event ArticleListed(uint articleId, address seller, string articleName, uint price);
// Event that is emitted when an article is bought
event ArticleBought(uint articleId, address seller, string articleName);
// Function that allows someone to list an article for sale
function listArticle(string memory _articleName, uint _price) public {
// Increment the total number of articles
totalArticles++;
// Create a new article struct and populate its fields
Article memory newArticle = Article({
seller: msg.sender,
articleName: _articleName,
price: _price,
isAvailable: true
});
// Add the new article to the mapping
articles[totalArticles] = newArticle;
// Emit the ArticleListed event
emit ArticleListed(totalArticles, newArticle.seller, newArticle.articleName, newArticle.price);
}
// Function that allows someone to buy an article
function buyArticle(uint _articleId) public payable {
// Get the article struct
Article storage article = articles[_articleId];
// Check if the article is available for purchase
require(article.isAvailable == true, "Article is not available for purchase");
// Check if the sender has sent enough ether
require(msg.value >= article.price, "Insufficient funds");
// Transfer the ether to the seller
article.seller.transfer(article.price);
// Set the article to sold
article.isAvailable = false;
// Emit the ArticleBought event
emit ArticleBought(_articleId, article.seller, article.articleName);
}
}
contract ArticlesMarketplace {
// Struct that represents an article
struct Article {
// The address of the seller of an article
address seller;
// The name of the article
string articleName;
// The price of the article in wei
uint price;
// The state of the article (true = available, false = sold)
bool isAvailable;
}
// Mapping from article ID to article struct
mapping(uint => Article) public articles;
// The total number of articles
uint public totalArticles;
// Event that is emitted when an article is listed
event ArticleListed(uint articleId, address seller, string articleName, uint price);
// Event that is emitted when an article is bought
event ArticleBought(uint articleId, address seller, string articleName);
// Function that allows someone to list an article for sale
function listArticle(string memory _articleName, uint _price) public {
// Increment the total number of articles
totalArticles++;
// Create a new article struct and populate its fields
Article memory newArticle = Article({
seller: msg.sender,
articleName: _articleName,
price: _price,
isAvailable: true
});
// Add the new article to the mapping
articles[totalArticles] = newArticle;
// Emit the ArticleListed event
emit ArticleListed(totalArticles, newArticle.seller, newArticle.articleName, newArticle.price);
}
// Function that allows someone to buy an article
function buyArticle(uint _articleId) public payable {
// Get the article struct
Article storage article = articles[_articleId];
// Check if the article is available for purchase
require(article.isAvailable == true, "Article is not available for purchase");
// Check if the sender has sent enough ether
require(msg.value >= article.price, "Insufficient funds");
// Transfer the ether to the seller
article.seller.transfer(article.price);
// Set the article to sold
article.isAvailable = false;
// Emit the ArticleBought event
emit ArticleBought(_articleId, article.seller, article.articleName);
}
}
In this contract, anyone can list an article for sale by calling the listArticle() function and providing the name of the article and its price. This will create a new article struct and add it to the articles mapping, using the total number of articles as the article ID. It will also emit the ArticleListed event, indicating that the article is now available for purchase. Anyone can then buy the article by calling the buyArticle() function and providing the ID of the article they want to buy, as well as sending the correct amount of ether to the contract. When the article is bought, the ArticleBought event is emitted and the contract sets the isAvailable variable in the article struct to false to indicate that the article has been sold.
Dec 2, 2022, 7:17 AM
okay I think I have something.
/ get an article
function getArticle() public view returns (Article[] memory articles) {
Article[] result = new Article[];
for(uint counter=0; counter result[counter] = articlesForSale[counter];
counter++;
}
return result;
}
function getArticle() public view returns (Article[] memory articles) {
Article[] result = new Article[];
for(uint counter=0; counter
counter++;
}
return result;
}
but I get error for line "Article[] result..." it's not implicitly converted to struct which is the return type
Dec 2, 2022, 7:59 AM
nope
you have an empty array
Dec 2, 2022, 8:01 AM
is someone trying to call me from in here?
Dec 2, 2022, 8:02 AM
Telegram call?
Probably a scammer
Probably a scammer
Dec 2, 2022, 8:02 AM
more than likely "SUPPORT TEAM"
they are a dime a dozen
Best place to start is read up on Truffle Suite.
Then google for Solidity tutorials
Dec 2, 2022, 8:16 AM
Okay thank you
Dec 2, 2022, 8:18 AM
👍
Dec 2, 2022, 8:45 AM