Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ON-872: delist rental offer #51

Merged
merged 5 commits into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
36 changes: 36 additions & 0 deletions contracts/OriumSftMarketplace.sol
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,26 @@ contract OriumSftMarketplace is Initializable, OwnableUpgradeable, PausableUpgra
emit RentalOfferCancelled(_offer.lender, _offer.nonce);
}

/**
* @notice Delist a rental offer.
* @param _offer The rental offer struct. It should be the same as the one used to create the offer.
*/
function delistRentalOffer(RentalOffer calldata _offer) external whenNotPaused {
_delistRentalOffer(_offer);
}

/**
* @notice Delist a rental offer and withdraw it.
* @param _offer The rental offer struct. It should be the same as the one used to create the offer.
*/
function delistRentalOfferAndWithdraw(RentalOffer calldata _offer) external whenNotPaused {
_delistRentalOffer(_offer);
IERC7589 _rolesRegistry = IERC7589(
IOriumMarketplaceRoyalties(oriumMarketplaceRoyalties).sftRolesRegistryOf(_offer.tokenAddress)
);
_rolesRegistry.releaseTokens(_offer.commitmentId);
}

/**
* @notice Ends the rental prematurely.
* @dev Can only be called by the borrower.
Expand Down Expand Up @@ -426,6 +446,22 @@ contract OriumSftMarketplace is Initializable, OwnableUpgradeable, PausableUpgra
);
}

/**
* @dev Cancels a rental offer.
* @param _offer The rental offer struct.
*/
function _delistRentalOffer(RentalOffer calldata _offer) internal {
bytes32 _offerHash = LibOriumSftMarketplace.hashRentalOffer(_offer);
require(isCreated[_offerHash], 'OriumSftMarketplace: Offer not created');
require(msg.sender == _offer.lender, 'OriumSftMarketplace: Only lender can cancel a rental offer');
require(
nonceDeadline[_offer.lender][_offer.nonce] > block.timestamp,
'OriumSftMarketplace: Nonce expired or not used yet'
);
nonceDeadline[msg.sender][_offer.nonce] = uint64(block.timestamp);
emit RentalOfferCancelled(_offer.lender, _offer.nonce);
}

/** ============================ Core Functions ================================== **/

/** ######### Setters ########### **/
Expand Down
87 changes: 87 additions & 0 deletions test/OriumSftMarketplace.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,93 @@ describe('OriumSftMarketplace', () => {
})
})

describe('Delist Rental Offer and Withdraw', async () => {
it('Should delist a rental offer and releaseTokens from rolesRegistry', async () => {
await expect(marketplace.connect(lender).delistRentalOfferAndWithdraw(rentalOffer))
.to.emit(marketplace, 'RentalOfferCancelled')
.withArgs(rentalOffer.lender, rentalOffer.nonce)
.to.emit(rolesRegistry, 'TokensReleased')
.withArgs(rentalOffer.commitmentId)
})
it('Should NOT delist a rental offer if tokens was released before directly from registry', async () => {
await rolesRegistry.connect(lender).releaseTokens(rentalOffer.commitmentId)
await expect(marketplace.connect(lender).delistRentalOfferAndWithdraw(rentalOffer)).to.be.revertedWith(
'SftRolesRegistry: account not approved',
)
})
it('Should NOT delist a rental offer if contract is paused', async () => {
await marketplace.connect(operator).pause()
await expect(marketplace.connect(borrower).delistRentalOfferAndWithdraw(rentalOffer)).to.be.revertedWith(
'Pausable: paused',
)
})
it('Should NOT delist a rental offer if nonce not used yet by caller', async () => {
await expect(
marketplace.connect(notOperator).delistRentalOfferAndWithdraw(rentalOffer),
).to.be.revertedWith('OriumSftMarketplace: Only lender can cancel a rental offer')
})
it("Should NOT delist a rental offer after deadline's expiration", async () => {
// move forward in time to expire the offer
const blockTimestamp = (await ethers.provider.getBlock('latest'))?.timestamp
const timeToMove = rentalOffer.deadline - Number(blockTimestamp) + 1
await ethers.provider.send('evm_increaseTime', [timeToMove])

await expect(marketplace.connect(lender).delistRentalOfferAndWithdraw(rentalOffer)).to.be.revertedWith(
'OriumSftMarketplace: Nonce expired or not used yet',
)
})
it("Should NOT delist a rental offer if it's not created", async () => {
await expect(
marketplace
.connect(lender)
.delistRentalOfferAndWithdraw({ ...rentalOffer, nonce: `0x${randomBytes(32).toString('hex')}` }),
).to.be.revertedWith('OriumSftMarketplace: Offer not created')
})
})

describe('Delist Rental Offer', async () => {
it('Should delist a rental offer and releaseTokens from rolesRegistry', async () => {
await expect(marketplace.connect(lender).delistRentalOffer(rentalOffer))
.to.emit(marketplace, 'RentalOfferCancelled')
.withArgs(rentalOffer.lender, rentalOffer.nonce)
.to.not.emit(rolesRegistry, 'TokensReleased')
})
it('Should delist a rental offer if tokens was released before directly from registry', async () => {
await rolesRegistry.connect(lender).releaseTokens(rentalOffer.commitmentId)
await expect(marketplace.connect(lender).delistRentalOffer(rentalOffer))
.to.emit(marketplace, 'RentalOfferCancelled')
.withArgs(rentalOffer.lender, rentalOffer.nonce)
})
it('Should NOT delist a rental offer if contract is paused', async () => {
await marketplace.connect(operator).pause()
await expect(marketplace.connect(borrower).delistRentalOffer(rentalOffer)).to.be.revertedWith(
'Pausable: paused',
)
})
it('Should NOT delist a rental offer if nonce not used yet by caller', async () => {
await expect(marketplace.connect(notOperator).delistRentalOffer(rentalOffer)).to.be.revertedWith(
'OriumSftMarketplace: Only lender can cancel a rental offer',
)
})
it("Should NOT delist a rental offer after deadline's expiration", async () => {
// move forward in time to expire the offer
const blockTimestamp = (await ethers.provider.getBlock('latest'))?.timestamp
const timeToMove = rentalOffer.deadline - Number(blockTimestamp) + 1
await ethers.provider.send('evm_increaseTime', [timeToMove])

await expect(marketplace.connect(lender).delistRentalOffer(rentalOffer)).to.be.revertedWith(
'OriumSftMarketplace: Nonce expired or not used yet',
)
})
it("Should NOT delist a rental offer if it's not created", async () => {
await expect(
marketplace
.connect(lender)
.delistRentalOffer({ ...rentalOffer, nonce: `0x${randomBytes(32).toString('hex')}` }),
).to.be.revertedWith('OriumSftMarketplace: Offer not created')
})
})

describe('Batch Release Tokens', async () => {
it('Should release tokens from rolesRegistry', async () => {
await time.increase(ONE_DAY)
Expand Down
Loading