Find offers
Querying the Discovery Contract to Find Transactions
In this article, we'll walk you through querying the discovery contract to find transactions related to offers. We'll cover both TimeframeDiscovery and PersonalDiscovery contracts, explaining how they differ and how to use them to fetch relevant data.
Step 1: Set Up and Validate Contract Deployment
We begin by opening the MultiSwapRouter contract and checking if it is deployed. If the router isn’t deployed, the script will stop with an error message.
Step 2: Query the Discovery Contracts
2.1 Timeframe Discovery Contract
The TimeframeDiscovery contract is used to retrieve offers related to a specific timeframe, which updates every three days (this might change in the future). Offers in this contract are typically initiated offers, meaning the user is offering something to others.
To query the TimeframeDiscovery contract:
Open the TimeframeDiscovery contract using the router’s address and the current timeframe.
Validate if the contract is deployed.
2.2 Personal Discovery Contract
The PersonalDiscovery contract is linked to specific wallet addresses and is used to retrieve offers initiated or responded to by that address. This contract provides a more personal view of transactions, including both initiated and responded offers.
To query PersonalDiscovery:
Ask for the seller's address.
Open the PersonalDiscovery contract using the router’s address and the seller’s address.
Validate if the contract is deployed for the seller.
Step 3: Retrieve and Parse Offers
Now that the discovery contract is set up, we query it to get a list of offers. You can specify the number of the most recent offers you want to fetch.
For TimeframeDiscovery, offers will be for the current timeframe. For PersonalDiscovery, the offers are specific to a wallet’s activity.
First parameter is id of last transaction we want to query over and second is the limit of transactions.
Parsing many transactions at one might slow down request so it's better to limit it
Step 4: Iterate Over Offers
The script will loop through the retrieved offers, fetch details about each one, and log relevant data like:
State of the offer
Expiration time
Items offered and requested
Step 5: Understanding Offer States
The state of each offer provides valuable information about its current condition. The possible states are:
StateDeployment (0): The offer is being deployed.
StateAwaitingFunds (1): Waiting for funds to be received.
StateReadyToSwap (2): The offer is ready to be swapped.
StateSwapLock (3): The swap is locked.
StateSwapped (4): The offer has been swapped successfully.
StateFailed (-1): The offer has failed.
We convert the state into a human-readable string using the getStateString function.
Querying Older Timeframes
To query older timeframes, you can use the getCurrentTimeframe() method to get current timeframe.
Current timestamp is being divided by that number, so to find previous timeframe, just sub 1 from timeframe.
You can also find timeframe by dividing unix timestamp in seconds by timeFramePeriod: