Documentation
Setup Token Swap via Onchain Task App

How to Setup Verify Token Swap using On-chain Task App

Step 1: Deploy your subgraph on The Graph

The first step is to deploy your subgraph on The Graph.

The Graph is a decentralized protocol for indexing and querying data from blockchains. It makes it easy to query data from the blockchain using GraphQL.

You can deploy your subgraph on The Graph Studio uisng this documentation: Deploying a Subgraph to Studio (opens in a new tab)

Ensure that the deployment is successful. You will be provided with a GraphQL endpoint URL upon successful deployment.

Since the subgraph is swapped token data, the data might be structured like this,

{
  "data": {
    "swaps": [
      {
        "amountUSD": "1000",
        "id": "0x8fa728714f3a608af7903ad8b91b25fb0561f9cc34a0ebe425e2a9895242f6eb",
        "sender": "0x6B0D52475c1D57EFBB6814DC43f6CbF175ACa18A"
      },
      {
        "amountUSD": "400",
        "id": "0xd9a35db13ca803428ac20d00d4ab6b06e3abc6de5ab3c4331b941ed274840db6",
        "sender": "0x85222290DD7278Aa3Ddd389Cc1E1d165CC4BAfe5"
      }
    ]
  }
}

Step 2: Create a new On-chain Task App and configure the task

Now that we have deployed the subgraph, we can create a new On-chain Task App (opens in a new tab) and configure the task to verify the token swap.

Graph Endpoint

Use the GraphQL endpoint URL provided by The Graph after deploying the subgraph.

Graph Query

Use the following query to fetch the latest swap transaction for the user.

query info($address: Bytes!) {
  swaps(first: 1, where: { sender_in: [$address] }) {
    id
  }
}

Graph Expression

Use the following expression to verify if the user has performed at least one swap transaction.

function (data) {
    if (data.swaps && data.swaps.length > 0) {
      return 1;
    }
    return 0;
}
 

Step 3: Save and submit your campaign

Once you have set up the On-chain Task App with the Graph query and expression, save and submit your campaign. The user's swap transaction will be fetched from the subgraph, and the response will be validated based on the expression provided.