Querying reNFT's on-chain data

Collateral Free (aka Traditional Rentals)

Ethereum

Get Renters

To know who the active renters of your collection are at any point in time, you should make a GraphQL API request. Here is GraphiQL playground for you. It's useful for testing out queries:

Here is what it looks like if you follow the above link:

To actually make calls from your client, you simply make GraphQL POST requests to the following URL: https://api.studio.thegraph.com/query/3020/sylvester/1.0.3

You can play around with our GraphQL API schema in the GraphiQL UI. You will notice we have information such as lendings as well. However, you are interested in rentings, so that you can pinpoint who the current renters of your NFTs are. To do so, use the following query:

query CollectionRenters($nftAddress: Bytes!) {
  rentings(
    where: {
    	lending_: {nftAddress: $nftAddress},
        expired: false,
        cursor_gt: 0
    },
    first: 1000,
    skip: 0,
    orderBy: cursor,
    orderDirection: desc
  ) {
    id
    cursor
    renterAddress
    rentedAt
    rentDuration
    rentAmount
    lending {
      nftAddress
      tokenID
    }
  }
}

Let's walk through the above. You need to supply nftAddress parameter in this query. This is your collection's address on Ethereum (this section concerns Ethereum renters querying). We have an expired flag that gets set to true when the rentings has overrun its duration and the lender has collected their payment on our dapp. Note that on Ethereum, rentals can over-run their duration. In our own API that is releasing soon, we will only return you rentals that are active at a given point in time. With the above approach, however, there is an extra step you need to do to ensure rental is in fact active. We will talk about it shortly. cursor_gt: 0 is a way to paginate, that you will only ever need if you have more than 1000 active rentals at any given point in time. The restriction on this API is that it can only return a maximum of 1000 items. That is why we have: first: 1000. If you have more active rentals, then you will need to paginate like this (until you retrieve all rentals):

query CollectionRenters($nftAddress: Bytes!) {
  rentings(
    where: {
    	lending_: {nftAddress: $nftAddress},
        expired: false,
        cursor_gt: 2003
    },
    first: 1000,
    skip: 1000,
    orderBy: cursor,
    orderDirection: desc
  ) {
    id
    cursor
    renterAddress
    rentedAt
    rentDuration
    rentAmount
    lending {
      nftAddress
      tokenID
    }
  }
}

The most important part here is that we have changed the value of cursor_gt: 2003. Taken the resultset of the original query, we noticed, hypothetically that the max cursor value was 2003. This now retrieves the next 1000 rentals (or however many are left). You then repeat this process as required.

Note that id and cursor are equivalent. This is your renting id, you will not need to use it, but cursor is Int so that we can easily paginate. If we were to order the id which is a string, we would get an incorrect ordering of items.

We are now at a stage where we can talk about what information you need to use in your application / game to enable rentals. You simply recognise the result-set you are getting from this query as renters. You get their wallet addresses by looking at the renterAddress field above.

Finally, a note on expired. As I have mentioned previously, you need to do a small extra step here. After a rental over-runs its duration (that is to say, you take rentedAt UTC timestamp and add rentDuration * 24 * 60 * 60 (duration in seconds) and compare to current UTC timestamp), its expired is still false. This is because our contract requires triggering of a particular function for lender (or anyone for that matter) to invoke for them to collect the rental payments. This action emits an on-chain event that then sets expired to true. So, the extra step that you need to take here is to simply after having received all the rentals from the above queries, go through each one and ensure that:

rentedAt + rentDuration * 24 * 60 * 60 > current_timestamp_in_utc

whatever rental items you have left after applying the above filter will ensure that you have the set of currently active rentals. Note that you can also take a route of writing a bot that invokes the mentioned function for your users. This would then ensure that all the rentals you are getting from the queries above are definitively active. If you are interested in how to do this, see this repository (we are using this bot on polygon to stop rentals for everyone).

Polygon

TODO

Last updated