Skip to content

Commit

Permalink
fix: updating roleDeadline EndRental and adding test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
EDUARDO MELO DE SIQUEIRA authored and EDUARDO MELO DE SIQUEIRA committed Jun 25, 2024
1 parent 9cad6f3 commit fff0559
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 7 deletions.
7 changes: 4 additions & 3 deletions contracts/NftRentalMarketplace.sol
Original file line number Diff line number Diff line change
Expand Up @@ -234,9 +234,10 @@ contract NftRentalMarketplace is Initializable, OwnableUpgradeable, PausableUpgr
_offer.roles
);

for (uint256 i = 0; i < _offer.roles.length; i++) {
if( _offer.deadline > uint64(block.timestamp) ) {
roleDeadline[_offer.roles[i]][_offer.tokenAddress][_offer.tokenId] = uint64(block.timestamp);
uint64 _offerDeadline = nonceDeadline[_offer.lender][_offer.nonce];
if (_offerDeadline < uint64(block.timestamp)) {
for (uint256 i = 0; i < _offer.roles.length; i++) {
roleDeadline[_offer.roles[i]][_offer.tokenAddress][_offer.tokenId] = uint64(block.timestamp);
}
}

Expand Down
48 changes: 44 additions & 4 deletions test/NftRentalMarketplace.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -404,16 +404,13 @@ describe('NftRentalMarketplace', () => {
rentalOffer.nonce = `0x${randomBytes(32).toString('hex')}`
rentalOffer.deadline = Number(await time.latest()) + ONE_HOUR + TEN_MINUTES
const blockTimestamp = Number(await time.latest())
console.log('blockTimestamp:', blockTimestamp.toString())
const newExpirationDate = blockTimestamp + duration - 1
console.log('newExpirationDate', newExpirationDate.toString())
await marketplace.connect(lender).createRentalOffer(rentalOffer)
const updatedRoleDeadline = await marketplace.roleDeadline(
USER_ROLE,
await mockERC721.getAddress(),
tokenId,
)
console.log('Updated Role Deadline:', updatedRoleDeadline.toString())
await marketplace.connect(borrower).acceptRentalOffer(rentalOffer, duration)

expect(newExpirationDate).to.be.greaterThan(updatedRoleDeadline)
Expand Down Expand Up @@ -596,6 +593,50 @@ describe('NftRentalMarketplace', () => {
.to.emit(marketplace, 'RentalOfferCancelled')
.withArgs(rentalOffer.lender, rentalOffer.nonce)
})

it('Should end a rental offer with an active role and LET create a new offer ', async () => {
await time.increase(ONE_DAY)
rentalOffer.borrower = borrower.address
rentalOffer.nonce = `0x${randomBytes(32).toString('hex')}`
rentalOffer.deadline = Number(await time.latest()) + ONE_DAY
rentalOffer.feeAmountPerSecond = toWei('0')
await marketplace.connect(lender).createRentalOffer(rentalOffer)

const blockTimestamp = await time.latest()
const expirationDate = Number(blockTimestamp) + duration

await expect(marketplace.connect(borrower).acceptRentalOffer(rentalOffer, duration - 1))
.to.emit(marketplace, 'RentalStarted')
.withArgs(rentalOffer.lender, rentalOffer.nonce, borrower.address, expirationDate)
await rolesRegistry
.connect(borrower)
.setRoleApprovalForAll(await mockERC721.getAddress(), await marketplace.getAddress(), true)

await expect(marketplace.connect(lender).cancelRentalOffer(rentalOffer))
.to.emit(marketplace, 'RentalOfferCancelled')
.withArgs(rentalOffer.lender, rentalOffer.nonce)

await expect(marketplace.connect(borrower).endRental(rentalOffer))
.to.emit(marketplace, 'RentalEnded')
.withArgs(rentalOffer.lender, rentalOffer.nonce)
rentalOffer.nonce = `0x${randomBytes(32).toString('hex')}`

await expect(marketplace.connect(lender).createRentalOffer(rentalOffer))
.to.emit(marketplace, 'RentalOfferCreated')
.withArgs(
rentalOffer.nonce,
rentalOffer.tokenAddress,
rentalOffer.tokenId,
rentalOffer.lender,
rentalOffer.borrower,
rentalOffer.feeTokenAddress,
rentalOffer.feeAmountPerSecond,
rentalOffer.deadline,
rentalOffer.minDuration,
rentalOffer.roles,
rentalOffer.rolesData,
)
})
it('Should cancel a rental offer with an active role And DO NOT let create another rental offer', async () => {
await time.increase(ONE_DAY)
rentalOffer.borrower = borrower.address
Expand Down Expand Up @@ -660,7 +701,6 @@ describe('NftRentalMarketplace', () => {
)
})
})

describe('When Rental Offer is accepted', async () => {
beforeEach(async () => {
await marketplace.connect(borrower).acceptRentalOffer(rentalOffer, duration)
Expand Down

0 comments on commit fff0559

Please sign in to comment.