Skip to content

Commit

Permalink
Merge pull request #178 from decentraland/feat/cancel-listing-if-owne…
Browse files Browse the repository at this point in the history
…r-changed

feat: cancel listing if the nft has been transfered
  • Loading branch information
juanmahidalgo committed Nov 21, 2022
2 parents 54817ed + 1523113 commit c7396e5
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 16 deletions.
6 changes: 6 additions & 0 deletions src/ports/rentals/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,12 @@ export async function createRentalsComponent(
SQL`UPDATE metadata SET search_text = ${indexerNFT.searchText}, updated_at = ${rentalData.updated_at} WHERE id = ${rentalData.metadata_id}`
)
)

// If the nft has been transfered, but not due to a rent starting
// Cancel the rental listing that now has a different owner
if (rentalData.status === RentalStatus.OPEN && indexerNFT.owner.address !== rentalData.lessor) {
database.query(SQL`UPDATE rentals SET status = ${RentalStatus.CANCELLED} WHERE id = ${rentalData.id}`)
}
}

// Identify the latest blockchain rental
Expand Down
65 changes: 49 additions & 16 deletions test/unit/rentals-component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -863,6 +863,7 @@ describe("when refreshing rental listings", () => {
signature: string
nonces: string[]
status: RentalStatus
lessor: string
}
let nftFromIndexer: NFT
let rentalFromIndexer: IndexerRental
Expand All @@ -880,6 +881,7 @@ describe("when refreshing rental listings", () => {
rentalsComponent = await createRentalsComponent({ database, marketplaceSubgraph, rentalsSubgraph, logs, config })
rentalFromDb = {
id: "an id",
lessor: "anAddress",
contract_address: "aContractAddress",
token_id: "aTokenId",
updated_at: new Date(Math.round(Date.now() / 1000) * 1000),
Expand Down Expand Up @@ -1010,26 +1012,57 @@ describe("when refreshing rental listings", () => {
})

describe("and it was updated after the one in the database", () => {
beforeEach(() => {
marketplaceSubgraphQueryMock.mockResolvedValueOnce({
nfts: [
{
...nftFromIndexer,
createdAt: (Math.round(rentalFromDb.updated_at.getTime() / 1000) + 10000).toString(),
updatedAt: (Math.round(rentalFromDb.updated_at.getTime() / 1000) + 10000).toString(),
},
],
describe("and the owner has not changed", () => {
beforeEach(() => {
marketplaceSubgraphQueryMock.mockResolvedValueOnce({
nfts: [
{
...nftFromIndexer,
createdAt: (Math.round(rentalFromDb.updated_at.getTime() / 1000) + 10000).toString(),
updatedAt: (Math.round(rentalFromDb.updated_at.getTime() / 1000) + 10000).toString(),
},
],
})
mockDefaultSubgraphNonces()
dbQueryMock.mockResolvedValueOnce(undefined).mockResolvedValueOnce({
rows: [result],
rowCount: 1,
})
})
mockDefaultSubgraphNonces()
dbQueryMock.mockResolvedValueOnce(undefined).mockResolvedValueOnce({
rows: [result],
rowCount: 1,
it("should update the metadata in the database and return the rental", async () => {
await expect(rentalsComponent.refreshRentalListing("an id")).resolves.toEqual(result)
expect(dbQueryMock.mock.calls[1][0].text).toEqual(expect.stringContaining("UPDATE metadata SET"))
})
})

it("should update the metadata in the database and return the rental", async () => {
await expect(rentalsComponent.refreshRentalListing("an id")).resolves.toEqual(result)
expect(dbQueryMock.mock.calls[1][0].text).toEqual(expect.stringContaining("UPDATE metadata SET"))
describe("and the owner has changed", () => {
beforeEach(() => {
marketplaceSubgraphQueryMock.mockResolvedValueOnce({
nfts: [
{
...nftFromIndexer,
owner: {
address: "aNewOwner",
},
createdAt: (Math.round(rentalFromDb.updated_at.getTime() / 1000) + 10000).toString(),
updatedAt: (Math.round(rentalFromDb.updated_at.getTime() / 1000) + 10000).toString(),
},
],
})
mockDefaultSubgraphNonces()
dbQueryMock
.mockResolvedValueOnce(undefined)
.mockResolvedValueOnce(undefined)
.mockResolvedValueOnce({
rows: [result],
rowCount: 1,
})
})
it("should cancel the listing if the owner has changed", async () => {
await expect(rentalsComponent.refreshRentalListing("an id")).resolves.toEqual(result)
expect(dbQueryMock.mock.calls[2][0].text).toEqual(expect.stringContaining(`UPDATE rentals SET status`))
expect(dbQueryMock.mock.calls[2][0].values).toEqual(expect.arrayContaining([RentalStatus.CANCELLED]))
})
})
})
})
Expand Down

0 comments on commit c7396e5

Please sign in to comment.