Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactoring contract #10

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 16 additions & 13 deletions celo/celo-frontend-101/code/marketplace.sol
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,8 @@ contract Marketplace {
// Keeps track of the number of products in the marketplace
uint256 internal productsLength = 0;
// Address of the cUSDToken
address internal cUsdTokenAddress =
address internal immutable cUsdTokenAddress =
0x874069Fa1Eb16D44d622F2e0Ca25eeA172369bC1;

// Structure for a product
struct Product {
// Address of the product owner
Expand Down Expand Up @@ -109,15 +108,17 @@ contract Marketplace {
uint256
)
{
// Declaring and initializing the variable product with the current product
Product storage product = products[_index];
// Returns the details of the product
return (
products[_index].owner,
products[_index].name,
products[_index].image,
products[_index].description,
products[_index].location,
products[_index].price,
products[_index].sold
product.owner,
product.name,
product.image,
product.description,
product.location,
product.price,
product.sold
);
}

Expand All @@ -126,25 +127,27 @@ contract Marketplace {
// Index of the product
uint256 _index
) public payable {
// Declaring and initializing the variable product with the current product
Product storage product = products[_index];
// Transfers the tokens from the buyer to the seller
require(
IERC20Token(cUsdTokenAddress).transferFrom(
// Sender's address is the buyer
msg.sender,
// Receiver's address is the seller
products[_index].owner,
product.owner,
// Amount of tokens to transfer is the price of the product
products[_index].price
product.price
),
// If transfer fails, throw an error message
"Transfer failed."
);
// Increases the number of times the product has been sold
products[_index].sold++;
product.sold++;
}

// Returns the number of products in the marketplace
function getProductsLength() public view returns (uint256) {
return (productsLength);
}
}
}