In this recipe we’ll show you how to use the Codex api to fetch a list of swaps for a token, complete with filtering, sorting, and realtime updates. This data powers the transactions tables view on defined.fi.

Queries

To get a list of swaps for a token, you can use the getTokenEvents query.

For example, for the WBNB token on the BNB chain (networkId 56), you can use the following query:

To get more (paginate), you can use the cursor argument to get the next page of results.

Modify the query to include the cursor like this:

Now you know how to use a paginated cursor to fetch more results.

This pattern is used in many places in the Codex API, see getTokenEventsForMaker, holders for an example.

Realtime Updates

To get realtime updates, you can use the onTokenEventsCreated subscription.

Now you’ve got all you need to build a fully functional swap list view.

See more event queries in the api reference, like getTokenEventsForMaker, getEventLabels for an example.

And more subscriptions like