Expired Rentals

How to know when a rental on reNFT has expired?

When we query for rentals on a Subgraph, there are various attributes which define the time-variant rental status of a Renting. These attributes are: rentedAt, rentDuration and expired; and they are often easily misinterpreted.

In this short section, we'll demystify the purpose of these fields.

Expired Rentals

The Rental subgraph data modal possesses an expired property. This is used to define when a rental successfully terminated within the agreement of the rental period.

By taking a look through the Subgraph Mapping (which indexes high-level representations of all on-chain transactions which took place), we can see that when a rental has been claimed, the Rental object is marked as expired.

Conversely, we can see the expired property initialized to false when a Rental is first initiated.

Elapsed Rentals

If the current date is greater than the time the asset was rented plus the duration of the rent, the rental period has been exceeded by the renter.

This rental state can be determined programmatically by consulting the appropriate subgraph for the smart contract the rental took place on. Using GraphQL to query a subgraph, we can determine the rentedAt and rentDuration of a specific Renting as follows:

{
  renting(id: "2") {
    id
    rentedAt
    rentDuration
    expired
  }
}

Upon this request, the subgraph will provide the caller with a Renting data model with the specified expiration fields included, for instance:

{
  "data": {
    "renting": {
       "id": "1",
      "rentedAt": "1671617899",
      "rentDuration": "1",
      "expired": false
    }
  }
}

Note that there are no on-chain events for an expiration, since this would require an associated transaction.

In this regard, if we need to determine if a rental has expired off-chain, we must compute the time at which a rental expires and compare this against the current time.

Below, we outline an example implementation in TypeScript:

const rentedAtInSeconds = Number(renting.rentedAt);
const rentDurationInDays = Number(renting.rentDuration);

const rentedAtMilliseconds = new Date(rentedAtInSeconds * 1000).getTime();
const rentDurationMilliseconds = rentDurationInDays * 24 * 60 * 60 * 1000;

const now = Date.now(); // Current time in milliseconds.

const rentingHasElapsed =
  now > rentedAtMilliseconds + rentDurationMilliseconds;
  
if (rentingHasElapsed)
  doSomethingWithElapsedRenting(renting); // Perform some app-specific handling.

Last updated