Skip to content
Open
Show file tree
Hide file tree
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
104 changes: 104 additions & 0 deletions code/celo101-code-chapter_4/4-1-edit-functions/marketplace.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
// SPDX-License-Identifier: MIT

pragma solidity >=0.7.0 <0.9.0;

interface IERC20Token {
function transfer(address, uint256) external returns (bool);
function approve(address, uint256) external returns (bool);
function transferFrom(address, address, uint256) external returns (bool);
function totalSupply() external view returns (uint256);
function balanceOf(address) external view returns (uint256);
function allowance(address, address) external view returns (uint256);

event Transfer(address indexed from, address indexed to, uint256 value);
event Approval(address indexed owner, address indexed spender, uint256 value);
}

contract Marketplace {

uint internal productsLength = 0;
address internal cUsdTokenAddress = 0x874069Fa1Eb16D44d622F2e0Ca25eeA172369bC1;

struct Product {
address payable owner;
string name;
string image;
string description;
string location;
uint price;
uint sold;
}

mapping (uint => Product) internal products;

function writeProduct(
string memory _name,
string memory _image,
string memory _description,
string memory _location,
uint _price
) public {
uint _sold = 0;
products[productsLength] = Product(
payable(msg.sender),
_name,
_image,
_description,
_location,
_price,
_sold
);
productsLength++;
}

function readProduct(uint _index) public view returns (
address payable,
string memory,
string memory,
string memory,
string memory,
uint,
uint
) {
return (
products[_index].owner,
products[_index].name,
products[_index].image,
products[_index].description,
products[_index].location,
products[_index].price,
products[_index].sold
);
}

function buyProduct(uint _index) public payable {
require(
IERC20Token(cUsdTokenAddress).transferFrom(
msg.sender,
products[_index].owner,
products[_index].price
),
"Transfer failed."
);
products[_index].sold++;
}

function editProduct(uint _index,
string memory _name,
string memory _image,
string memory _description,
string memory _location,
uint _price
) public {
require(products[_index].owner == msg.sender, "You can not edit this product.");
products[_index].name = _name;
products[_index].image = _image;
products[_index].description = _description;
products[_index].location = _location;
products[_index].price = _price;
}

function getProductsLength() public view returns (uint) {
return (productsLength);
}
}
109 changes: 109 additions & 0 deletions code/celo101-code-chapter_4/4-2-delete-functions/marketplace.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
// SPDX-License-Identifier: MIT

pragma solidity >=0.7.0 <0.9.0;

interface IERC20Token {
function transfer(address, uint256) external returns (bool);
function approve(address, uint256) external returns (bool);
function transferFrom(address, address, uint256) external returns (bool);
function totalSupply() external view returns (uint256);
function balanceOf(address) external view returns (uint256);
function allowance(address, address) external view returns (uint256);

event Transfer(address indexed from, address indexed to, uint256 value);
event Approval(address indexed owner, address indexed spender, uint256 value);
}

contract Marketplace {

uint internal productsLength = 0;
address internal cUsdTokenAddress = 0x874069Fa1Eb16D44d622F2e0Ca25eeA172369bC1;

struct Product {
address payable owner;
string name;
string image;
string description;
string location;
uint price;
uint sold;
}

mapping (uint => Product) internal products;

function writeProduct(
string memory _name,
string memory _image,
string memory _description,
string memory _location,
uint _price
) public {
uint _sold = 0;
products[productsLength] = Product(
payable(msg.sender),
_name,
_image,
_description,
_location,
_price,
_sold
);
productsLength++;
}

function readProduct(uint _index) public view returns (
address payable,
string memory,
string memory,
string memory,
string memory,
uint,
uint
) {
return (
products[_index].owner,
products[_index].name,
products[_index].image,
products[_index].description,
products[_index].location,
products[_index].price,
products[_index].sold
);
}

function buyProduct(uint _index) public payable {
require(
IERC20Token(cUsdTokenAddress).transferFrom(
msg.sender,
products[_index].owner,
products[_index].price
),
"Transfer failed."
);
products[_index].sold++;
}

function editProduct(uint _index,
string memory _name,
string memory _image,
string memory _description,
string memory _location,
uint _price
) public {
require(products[_index].owner == msg.sender, "You can not edit this product.");
products[_index].name = _name;
products[_index].image = _image;
products[_index].description = _description;
products[_index].location = _location;
products[_index].price = _price;
}

function deleteProduct(uint _index) public {
require(products[_index].owner == msg.sender, "You can not delete this product.");
delete products[_index];
}

function getProductsLength() public view returns (uint) {
return (productsLength);
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading