Skip to content

Commit

Permalink
Merge pull request #1 from dephy-io/feat/event
Browse files Browse the repository at this point in the history
feat: events and acl
  • Loading branch information
Kabie authored Aug 10, 2024
2 parents 0abc19f + 6b47a1e commit 6ae8497
Show file tree
Hide file tree
Showing 32 changed files with 1,994 additions and 108 deletions.
4 changes: 3 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
BASE_RPC_URL=https://base.rpc.subquery.network/public
BASE_SEPOLIA_RPC_URL=https://base-sepolia-rpc.publicnode.com
BASE_SEPOLIA_RPC_URL=https://base-sepolia.g.alchemy.com/v2/0ZS0OdXDqBpKt6wkusuFDyi0lLlTFRVf
BNB_RPC_URL=https://binance.llamarpc.com
BNB_TESTNET_RPC_URL=https://public.stackup.sh/api/v1/node/bsc-testnet

PRIVATE_KEY=
45 changes: 0 additions & 45 deletions .github/workflows/test.yml

This file was deleted.

6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,8 @@ docs/
# Dotenv file
.env

dependencies/
dependencies/

node_modules

bun.lockb
6 changes: 6 additions & 0 deletions addresses.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"BNBTestnet": {
"AccessTokenFactory": "0x35a8483444947B2166Aa85837F97FaEf122f5ebb",
"Marketplace": "0x647d77324E241709BaF63D7f96F0C19ecA06E2e0"
}
}
File renamed without changes.
File renamed without changes.
134 changes: 112 additions & 22 deletions src/Marketplace.sol → contracts/Marketplace.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ import {IMarketplace} from "./interfaces/IMarketplace.sol";
contract Marketplace is IMarketplace, Ownable {
using SafeERC20 for IERC20;

address private constant NATIVE_TOKEN = address(0);
address public constant NATIVE_TOKEN = address(0);

uint256 public constant MAX_POINTS = 10000;

AccessTokenFactory public immutable ACCESS_TOKEN_FACTORY;

address payable private _treasury;
address payable internal _treasury;

uint256 private _feePoints;
uint256 internal _feePoints;

/**
* @notice rent currency => is supported
Expand Down Expand Up @@ -53,22 +53,44 @@ contract Marketplace is IMarketplace, Ownable {
_feePoints = feePoints;
}

function getListingInfo(address accessToken, uint256 tokenId) external view returns (ListingInfo memory) {
/**
* @inheritdoc IMarketplace
*/
function getListingInfo(
address accessToken,
uint256 tokenId
) external view returns (ListingInfo memory) {
return _listings[accessToken][tokenId];
}

function getRentalInfo(address accessToken, uint256 tokenId, address tenant) external view returns (RentalInfo memory) {
/**
* @inheritdoc IMarketplace
*/
function getRentalInfo(
address accessToken,
uint256 tokenId,
address tenant
) external view returns (RentalInfo memory) {
return _rentals[accessToken][tokenId][tenant];
}

/**
* @inheritdoc IMarketplace
*/
function setFeePoints(uint256 feePoints) external onlyOwner {
_feePoints = feePoints;
}

/**
* @inheritdoc IMarketplace
*/
function setTreasury(address payable treasury) external onlyOwner {
_treasury = treasury;
}

/**
* @inheritdoc IMarketplace
*/
function addRentCurrencies(
address[] memory rentCurrencies
) external onlyOwner {
Expand All @@ -77,6 +99,9 @@ contract Marketplace is IMarketplace, Ownable {
}
}

/**
* @inheritdoc IMarketplace
*/
function removeRentCurrencies(
address[] memory rentCurrencies
) external onlyOwner {
Expand All @@ -85,11 +110,12 @@ contract Marketplace is IMarketplace, Ownable {
}
}

/**
* @inheritdoc IMarketplace
*/
function list(ListArgs memory args) public {
address accessToken = ACCESS_TOKEN_FACTORY.getAccessToken(
args.product
);
if(accessToken == address(0)) {
address accessToken = ACCESS_TOKEN_FACTORY.getAccessToken(args.product);
if (accessToken == address(0)) {
accessToken = ACCESS_TOKEN_FACTORY.createAccessToken(args.product);
}
require(
Expand All @@ -103,7 +129,8 @@ contract Marketplace is IMarketplace, Ownable {
"invalid maximum rental days"
);
require(
args.rentCurrency == NATIVE_TOKEN || supportedRentCurrencies[args.rentCurrency],
args.rentCurrency == NATIVE_TOKEN ||
supportedRentCurrencies[args.rentCurrency],
"unsupported rent currency"
);

Expand All @@ -122,28 +149,45 @@ contract Marketplace is IMarketplace, Ownable {
address(this),
args.tokenId
);

emit List(
msg.sender,
args.product,
accessToken,
args.tokenId,
args.minRentalDays,
args.maxRentalDays,
args.rentCurrency,
args.dailyRent,
args.rentRecipient
);
}

/**
* @inheritdoc IMarketplace
*/
function delist(DelistArgs memory args) public {
ListingInfo storage listing = _listings[args.accessToken][
args.tokenId
];
ListingInfo storage listing = _listings[args.accessToken][args.tokenId];
require(listing.owner == msg.sender, "not listing owner");
listing.status = ListingStatus.Delisted;

emit Delist(msg.sender, args.accessToken, args.tokenId);
}

/**
* @inheritdoc IMarketplace
*/
function relist(RelistArgs memory args) public {
ListingInfo storage listing = _listings[args.accessToken][
args.tokenId
];
ListingInfo storage listing = _listings[args.accessToken][args.tokenId];
require(listing.owner == msg.sender, "not listing owner");
require(args.minRentalDays > 0, "invalid minimum rental days");
require(
args.maxRentalDays >= args.minRentalDays,
"invalid maximum rental days"
);
require(
args.rentCurrency == NATIVE_TOKEN || supportedRentCurrencies[args.rentCurrency],
args.rentCurrency == NATIVE_TOKEN ||
supportedRentCurrencies[args.rentCurrency],
"unsupported rent currency"
);

Expand All @@ -153,8 +197,22 @@ contract Marketplace is IMarketplace, Ownable {
listing.dailyRent = args.dailyRent;
listing.rentRecipient = args.rentRecipient;
listing.status = ListingStatus.Listing;

emit Relist(
msg.sender,
args.accessToken,
args.tokenId,
args.minRentalDays,
args.maxRentalDays,
args.rentCurrency,
args.dailyRent,
args.rentRecipient
);
}

/**
* @inheritdoc IMarketplace
*/
function rent(RentArgs memory args) public payable {
require(
_rentals[args.accessToken][args.tokenId][args.tenant].status ==
Expand Down Expand Up @@ -190,12 +248,20 @@ contract Marketplace is IMarketplace, Ownable {
args.prepaidRent
);
// Mint access token to tenant
AccessToken(args.accessToken).mint(
AccessToken(args.accessToken).mint(args.tenant, args.tokenId);

emit Rent(
args.tenant,
args.tokenId
args.accessToken,
args.tokenId,
args.rentalDays,
args.prepaidRent
);
}

/**
* @inheritdoc IMarketplace
*/
function payRent(PayRentArgs memory args) public payable {
ListingInfo memory listing = _listings[args.accessToken][args.tokenId];
RentalInfo storage rental = _rentals[args.accessToken][args.tokenId][
Expand All @@ -209,8 +275,18 @@ contract Marketplace is IMarketplace, Ownable {

// Pay rent
_payRent(listing, rental, args.rent);

emit PayRent(
args.tenant,
args.accessToken,
args.tokenId,
args.rent
);
}

/**
* @inheritdoc IMarketplace
*/
function endLease(EndLeaseArgs memory args) public {
RentalInfo storage rental = _rentals[args.accessToken][args.tokenId][
args.tenant
Expand All @@ -227,12 +303,20 @@ contract Marketplace is IMarketplace, Ownable {
// Burn tenant's access token
AccessToken(args.accessToken).burn(args.tokenId);
rental.status = RentalStatus.EndedOrNotExist;

emit EndLease(
args.tenant,
args.accessToken,
args.tokenId,
msg.sender
);
}

/**
* @inheritdoc IMarketplace
*/
function withdraw(WithdrawArgs memory args) public {
ListingInfo storage listing = _listings[args.accessToken][
args.tokenId
];
ListingInfo storage listing = _listings[args.accessToken][args.tokenId];
require(listing.owner == msg.sender, "not listing owner");
require(
!AccessToken(args.accessToken).isExist(args.tokenId),
Expand All @@ -245,6 +329,12 @@ contract Marketplace is IMarketplace, Ownable {
listing.owner,
args.tokenId
);

emit Withdraw(
msg.sender,
args.accessToken,
args.tokenId
);
}

function _payRent(
Expand Down
40 changes: 40 additions & 0 deletions contracts/interfaces/IMarketplace.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

import {IMarketplaceStructs} from "./IMarketplaceStructs.sol";
import {IMarketplaceEvents} from "./IMarketplaceEvents.sol";

interface IMarketplace is IMarketplaceStructs, IMarketplaceEvents {
function getListingInfo(
address accessToken,
uint256 tokenId
) external view returns (ListingInfo memory);

function getRentalInfo(
address accessToken,
uint256 tokenId,
address tenant
) external view returns (RentalInfo memory);

function setFeePoints(uint256 feePoints) external;

function setTreasury(address payable treasury) external;

function addRentCurrencies(address[] memory rentCurrencies) external;

function removeRentCurrencies(address[] memory rentCurrencies) external;

function list(ListArgs memory args) external;

function delist(DelistArgs memory args) external;

function relist(RelistArgs memory args) external;

function rent(RentArgs memory args) external payable;

function payRent(PayRentArgs memory args) external payable;

function endLease(EndLeaseArgs memory args) external;

function withdraw(WithdrawArgs memory args) external;
}
Loading

0 comments on commit 6ae8497

Please sign in to comment.