Skip to content

Commit

Permalink
Merge pull request #2 from dhiraj0911/test
Browse files Browse the repository at this point in the history
chainlink upkeep
  • Loading branch information
dhiraj0911 authored Jun 22, 2024
2 parents f966191 + 3962655 commit 22b47de
Show file tree
Hide file tree
Showing 6 changed files with 1,445 additions and 80 deletions.
4 changes: 2 additions & 2 deletions components/ImportedCard.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ const ImportedCard = ({ nft }) => {
</div>

<a
href={`https://sepolia.etherscan.io/nft/${nft.contract}/${nft.tokenId}`}
href={`https://amoy.polygonscan.com/token/${nft.contract}/${nft.tokenId}`}
aria-describedby="tier-starter"
className="items-center justify-center px-2 py-2.5 text-center text-black duration-50 bg-white border-2 border-white rounded inline-flex hover:bg-transparent hover:border-white hover:text-white focus:outline-none focus-visible:outline-white text-xs focus-visible:ring-white"
target="_blank"
Expand All @@ -87,7 +87,7 @@ const ImportedCard = ({ nft }) => {
</div>
) : (
<a
href={`https://sepolia.etherscan.io/nft/${nft.contract}/${nft.tokenId}`}
href={`https://amoy.polygonscan.com/token/${nft.contract}/${nft.tokenId}`}
aria-describedby="tier-starter"
className="items-center justify-center w-1/2 px-2 py-2.5 text-center text-black duration-50 bg-white border-2 border-white rounded inline-flex hover:bg-transparent hover:border-white hover:text-white focus:outline-none focus-visible:outline-white text-xs focus-visible:ring-white"
target="_blank"
Expand Down
11 changes: 8 additions & 3 deletions context/RentableNFTMarketplace.json

Large diffs are not rendered by default.

82 changes: 48 additions & 34 deletions contracts/RentableNFTMarketplace.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "@openzeppelin/contracts/token/ERC721/extensions/IERC721Metadata.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/utils/Counters.sol";
import "@chainlink/contracts/src/v0.8/interfaces/KeeperCompatibleInterface.sol";
import "@chainlink/contracts/src/v0.8/automation/AutomationCompatible.sol";

interface IWETH {
function transferFrom(address sender, address recipient, uint amount) external returns (bool);
}

contract RentableNFTMarketplace is
ERC721,
KeeperCompatibleInterface
AutomationCompatible
{
using Counters for Counters.Counter;
Counters.Counter public _tokenIds;
Expand Down Expand Up @@ -337,47 +337,61 @@ contract RentableNFTMarketplace is
}

// Chainlink Automation: Check if any rented NFT's rental period has expired
function checkUpkeep(bytes calldata checkData)
external
override
view
returns (bool upkeepNeeded, bytes memory performData)
{
if(keccak256(checkData) == keccak256(hex'01')) {
for (uint i = 0; i < rentedTokenIds.length; i++) {
uint256 tokenId = rentedTokenIds[i];
RentedItem storage item = idToRentedItem[tokenId];
if (block.timestamp >= item.expires) {
return (true, abi.encode(tokenId, true)); // true indicates it's a native token
}
function checkUpkeep(
bytes calldata /* checkData */
) external view override returns (bool upkeepNeeded, bytes memory performData) {
uint256[] memory expiredTokenIds = new uint256[](rentedTokenIds.length);
bytes32[] memory expiredImportedTokenIds = new bytes32[](importedTokenIds.length);
uint256 expiredCount = 0;
uint256 importedExpiredCount = 0;

for (uint256 i = 0; i < rentedTokenIds.length; i++) {
uint256 tokenId = rentedTokenIds[i];
RentedItem memory rentedItem = idToRentedItem[tokenId];
if (block.timestamp >= rentedItem.expires) {
expiredTokenIds[expiredCount] = tokenId;
expiredCount++;
}
}
if(keccak256(checkData) == keccak256(hex'02')) {
for (uint i = 0; i < importedTokenIds.length; i++) {
bytes32 tokenId = importedTokenIds[i];
ImportedItem storage item = idToImportedItem[tokenId];
if (block.timestamp >= item.expiry) {
return (true, abi.encode(tokenId, false)); // false indicates it's an imported token
}

for (uint256 i = 0; i < importedTokenIds.length; i++) {
bytes32 rentalId = importedTokenIds[i];
ImportedItem memory importedItem = idToImportedItem[rentalId];
if (block.timestamp >= importedItem.expiry) {
expiredImportedTokenIds[importedExpiredCount] = rentalId;
importedExpiredCount++;
}
}
return (false, "");

if (expiredCount > 0 || importedExpiredCount > 0) {
upkeepNeeded = true;
performData = abi.encode(expiredTokenIds, expiredCount, expiredImportedTokenIds, importedExpiredCount);
} else {
upkeepNeeded = false;
performData = bytes("");
}
}

function performUpkeep(bytes calldata performData) external override {
(bytes32 encodedId, bool isNative) = abi.decode(performData, (bytes32, bool));

if(isNative) {
uint256 tokenId = uint256(encodedId);
RentedItem storage item = idToRentedItem[tokenId];
if (block.timestamp >= item.expires) {
(
uint256[] memory expiredTokenIds,
uint256 expiredCount,
bytes32[] memory expiredImportedTokenIds,
uint256 importedExpiredCount
) = abi.decode(performData, (uint256[], uint256, bytes32[], uint256));

for (uint256 i = 0; i < expiredCount; i++) {
uint256 tokenId = expiredTokenIds[i];
if (tokenId != 0) {
_returnToken(tokenId);
}
} else {
(address collection, uint256 tokenId) = decodeImportedRentedKey(encodedId);
ImportedItem storage item = idToImportedItem[encodedId];
if (block.timestamp >= item.expiry) {
_returnImportedToken(tokenId, collection);
}

for (uint256 i = 0; i < importedExpiredCount; i++) {
bytes32 rentalId = expiredImportedTokenIds[i];
if (rentalId != bytes32(0)) {
ImportedItem memory importedItem = idToImportedItem[rentalId];
_returnImportedToken(importedItem.tokenId, importedItem.collection);
}
}
}
Expand Down
7 changes: 1 addition & 6 deletions hardhat.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,13 @@ module.exports = {
amoy: {
url: API_URL,
accounts: [PRIVATE_KEY],
chainId: 80001,
chainId: 80002,
},
sepolia: {
url: API_URL,
accounts: [PRIVATE_KEY],
chainId: 11155111,
},
arb_sepolia: {
url: "https://arb-sepolia.g.alchemy.com/v2/jojD2pcZBUa_hixqqp4jnHsLWf4zQfiF",
accounts: [PRIVATE_KEY],
chainId: 421614,
},
},
settings: { optimizer: { enabled: true, runs: 200 } },
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"web3modal": "1.9.5"
},
"devDependencies": {
"@chainlink/contracts": "^0.2.2",
"@chainlink/contracts": "^1.1.1",
"@nomiclabs/hardhat-ethers": "^2.2.1",
"@nomiclabs/hardhat-waffle": "^2.0.3",
"@openzeppelin/contracts": "4.5.0",
Expand Down
Loading

0 comments on commit 22b47de

Please sign in to comment.