Skip to content

Commit

Permalink
feat: change contracts by inheriting ApplicationBase
Browse files Browse the repository at this point in the history
  • Loading branch information
CanvasL committed Aug 30, 2024
1 parent 4b43968 commit d0a5777
Show file tree
Hide file tree
Showing 24 changed files with 494 additions and 486 deletions.
4 changes: 1 addition & 3 deletions addresses.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
{
"BaseSepolia": {
"Application": "0xf628B6024af73D0f29c251e0fB306e5f8bA6FcFA",
"Marketplace": "0xC6B5c98FD8A8C9d8aa2B0f79a66EC55b0D2dad69",
"AccessIdentities": "0x4Cd640e4177a5d86B06BDB147E7efECFf3E478b3"
"Marketplace": "0x397C2649409F4dA69e8191e75A5Fe7Bb26cde597"
}
}
2 changes: 1 addition & 1 deletion backend/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ contracts:
BASE_SEPOLIA:
RPC: "wss://base-sepolia.g.alchemy.com/v2/_j5REyXi-XySkwLHp790Wvs9kBbxhUba"
Marketplace:
address: "0xC6B5c98FD8A8C9d8aa2B0f79a66EC55b0D2dad69"
address: "0x397C2649409F4dA69e8191e75A5Fe7Bb26cde597"
start_at: 14271894
query_history: true
query_interval: 200000
Expand Down
31 changes: 0 additions & 31 deletions contracts/AccessIdentities.sol

This file was deleted.

135 changes: 135 additions & 0 deletions contracts/IMarketplace.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.24;

interface IMarketplace {
// Statuses of a listing. WithdrawnOrNotExist, which is 0, is effectively the same as never listed before.
enum ListingStatus {
WithdrawnOrNotExist,
Listing,
Delisted
}

// Statuses of a rental. EndedOrNotExist, which is 0, is effectively the same as never exist before.
enum RentalStatus {
EndedOrNotExist,
Renting
}

struct ListingInfo {
address owner;
uint256 minRentalDays;
uint256 maxRentalDays;
address rentCurrency;
uint256 dailyRent;
address payable rentRecipient;
ListingStatus status;
}

struct RentalInfo {
uint256 accessId;
uint256 startTime;
uint256 endTime;
uint256 rentalDays;
address rentCurrency;
uint256 dailyRent;
uint256 totalPaidRent;
RentalStatus status;
}

event List(
address indexed owner,
address indexed device,
uint256 minRentalDays,
uint256 maxRentalDays,
address rentCurrency,
uint256 dailyRent,
address rentRecipient
);

event Delist(address indexed owner, address indexed device);

event Relist(
address indexed owner,
address indexed device,
uint256 minRentalDays,
uint256 maxRentalDays,
address rentCurrency,
uint256 dailyRent,
address rentRecipient
);

event Rent(
address indexed device,
uint256 indexed accessId,
address indexed tenant,
uint256 startTime,
uint256 endTime,
uint256 rentalDays,
uint256 prepaidRent
);

event PayRent(
address indexed device,
uint256 rent
);

event EndLease(
address indexed device,
address operator
);

event Withdraw(address indexed owner, address indexed device);

function getListingInfo(
address device
) external view returns (ListingInfo memory);

function getRentalInfo(
address device
) 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(
address device,
uint256 minRentalDays,
uint256 maxRentalDays,
address rentCurrency,
uint256 dailyRent,
address rentRecipient
) external;

function delist(address device) external;

function relist(
address device,
uint256 minRentalDays,
uint256 maxRentalDays,
address rentCurrency,
uint256 dailyRent,
address rentRecipient
) external;

function rent(
address device,
address tenant,
uint256 rentalDays,
uint256 prepaidRent,
string memory accessURI
) external payable;

function payRent(
address device,
uint256 rent_
) external payable;

function endLease(address device) external;

function withdraw(address device) external;
}
56 changes: 23 additions & 33 deletions contracts/Marketplace.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@ import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol";
import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import {IERC721} from "@openzeppelin/contracts/token/ERC721/IERC721.sol";
import {SafeERC20} from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import {IApplication} from "./interfaces/IApplication.sol";
import {IMarketplace} from "./interfaces/IMarketplace.sol";
import {ApplicationBase} from "./access/ApplicationBase.sol";
import {IMarketplace} from "./IMarketplace.sol";

contract Marketplace is IMarketplace, Ownable {
contract Marketplace is IMarketplace, ApplicationBase, Ownable {
using SafeERC20 for IERC20;

IApplication public immutable APPLICATION;

address public constant NATIVE_TOKEN = address(0);

uint256 public constant MAX_POINTS = 10000;
Expand All @@ -38,12 +36,13 @@ contract Marketplace is IMarketplace, Ownable {

constructor(
address initialOwner,
address application,
address productFactory,
string memory name,
string memory symbol,
address[] memory rentCurrencies,
address payable treasury,
uint256 feePoints
) Ownable(initialOwner) {
APPLICATION = IApplication(application);
) Ownable(initialOwner) ApplicationBase(productFactory, name, symbol) {
for (uint256 i = 0; i < rentCurrencies.length; i++) {
supportedRentCurrencies[rentCurrencies[i]] = true;
}
Expand Down Expand Up @@ -138,8 +137,6 @@ contract Marketplace is IMarketplace, Ownable {
status: ListingStatus.Listing
});

_transferDevice(device, msg.sender, address(this));

emit List(
msg.sender,
device,
Expand Down Expand Up @@ -208,7 +205,8 @@ contract Marketplace is IMarketplace, Ownable {
address device,
address tenant,
uint256 rentalDays,
uint256 prepaidRent
uint256 prepaidRent,
string memory accessURI
) public payable {
require(
_rentals[device].status == RentalStatus.EndedOrNotExist,
Expand All @@ -227,8 +225,8 @@ contract Marketplace is IMarketplace, Ownable {
"insufficient prepaid rent"
);

// Mint app device to tenant
uint256 accessId = APPLICATION.mint(tenant, device);
// grant access to tenant
uint256 accessId = _grantAccess(tenant, device, accessURI);

uint256 startTime = block.timestamp;
uint256 endTime = block.timestamp + rentalDays * 1 days;
Expand All @@ -246,16 +244,21 @@ contract Marketplace is IMarketplace, Ownable {
// Pay rent
_payRent(listing, _rentals[device], prepaidRent);

emit Rent(device, accessId, tenant, startTime, endTime, rentalDays, prepaidRent);
emit Rent(
device,
accessId,
tenant,
startTime,
endTime,
rentalDays,
prepaidRent
);
}

/**
* @inheritdoc IMarketplace
*/
function payRent(
address device,
uint256 rent_
) public payable {
function payRent(address device, uint256 rent_) public payable {
ListingInfo memory listing = _listings[device];
RentalInfo storage rental = _rentals[device];
require(
Expand Down Expand Up @@ -284,8 +287,8 @@ contract Marketplace is IMarketplace, Ownable {
"cannot end lease"
);

// Burn tenant's app device
APPLICATION.burn(device, rental.accessId);
// revoke tenant's access
_revokeAccess(device, rental.accessId);
rental.status = RentalStatus.EndedOrNotExist;

emit EndLease(device, msg.sender);
Expand All @@ -304,8 +307,6 @@ contract Marketplace is IMarketplace, Ownable {
);
listing.status = ListingStatus.WithdrawnOrNotExist;

_transferDevice(device, address(this), listing.owner);

emit Withdraw(msg.sender, device);
}

Expand Down Expand Up @@ -348,15 +349,4 @@ contract Marketplace is IMarketplace, Ownable {

rental.totalPaidRent += rent_;
}

function _transferDevice(
address device,
address from,
address to
) internal {
(address product, uint256 tokenId) = APPLICATION.getDeviceBinding(
device
);
IERC721(product).transferFrom(from, to, tokenId);
}
}
Loading

0 comments on commit d0a5777

Please sign in to comment.