Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 82 additions & 0 deletions website/docs/xdcchain/developers/subgraphs/performance.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
---
title: Performance
---

## Example Subgraphs

### XRC20 Token Tracker

A complete subgraph for indexing any XRC20 token with holder balances and transfer history.

**Schema:**

```graphql
type Token @entity {
id: ID!
symbol: String!
name: String!
decimals: Int!
totalSupply: BigInt!
transferCount: BigInt!
holderCount: BigInt!
}

type Account @entity {
id: ID!
balances: [TokenBalance!]! @derivedFrom(field: "account")
}

type TokenBalance @entity {
id: ID!
token: Token!
account: Account!
balance: BigInt!
}

type Transfer @entity {
id: ID!
token: Token!
from: Account!
to: Account!
value: BigInt!
timestamp: BigInt!
blockNumber: BigInt!
}
```

**Mapping:**

```typescript
import { Transfer } from "../generated/XRC20/XRC20";
import { Token, Account, TokenBalance, Transfer as TransferEntity } from "../generated/schema";

export function handleTransfer(event: Transfer): void {
// Implementation as shown in previous sections
}
```

### NFT Marketplace Indexer

Index NFT mints, transfers, sales, and marketplace listings.

**Key Entities:**

- `Collection`: NFT contract metadata
- `Token`: Individual NFT with metadata
- `Transfer`: Ownership changes
- `Sale`: Marketplace transactions with price
- `Listing`: Active marketplace listings

### DeFi Protocol Tracker

Index liquidity pools, swaps, deposits, and yield farming positions.

**Key Entities:**

- `Pool`: Liquidity pool with reserves
- `Swap`: Token exchange transactions
- `LiquidityPosition`: User LP token balances
- `YieldPosition`: Staking and farming positions

---

176 changes: 176 additions & 0 deletions website/docs/xdcchain/developers/subgraphs/querying.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
---
title: Querying
---

## Querying Subgraphs

### GraphQL Queries

Subgraphs expose a GraphQL endpoint for flexible data retrieval.

**Query Structure:**

```graphql
query GetTransfers($first: Int!, $skip: Int!) {
transfers(
first: $first
skip: $skip
orderBy: timestamp
orderDirection: desc
) {
id
from {
id
}
to {
id
}
value
timestamp
transactionHash
}
}
```

**Variables:**

```json
{
"first": 10,
"skip": 0
}
```

### Pagination

Use `first` and `skip` for offset-based pagination:

```graphql
query PaginatedTransfers($first: Int!, $skip: Int!) {
transfers(first: $first, skip: $skip) {
id
value
}
}
```

**Best Practice:** Limit `first` to 1000 maximum. For large datasets, use cursor-based pagination with `id` filtering:

```graphql
query CursorPagination($lastId: ID!) {
transfers(
first: 1000
where: { id_gt: $lastId }
orderBy: id
orderDirection: asc
) {
id
value
}
}
```

### Filtering

Apply `where` clauses for precise data selection:

```graphql
query FilteredTransfers {
transfers(
where: {
value_gt: "1000000000000000000"
timestamp_gt: "1700000000"
}
) {
id
from {
id
}
to {
id
}
value
}
}
```

**Available Operators:**

| Operator | Description |
|----------|-------------|
| `_eq` | Equal |
| `_gt` | Greater than |
| `_lt` | Less than |
| `_gte` | Greater than or equal |
| `_lte` | Less than or equal |
| `_in` | In array |
| `_not_in` | Not in array |
| `_contains` | Contains substring |
| `_starts_with` | Starts with |
| `_ends_with` | Ends with |

### Sorting

Order results with `orderBy` and `orderDirection`:

```graphql
query SortedTransfers {
transfers(
orderBy: timestamp
orderDirection: desc
first: 50
) {
id
timestamp
value
}
}
```

### Time-Travel Queries

Query historical state at a specific block:

```graphql
query HistoricalState {
token(id: "0x...", block: { number: 50000000 }) {
totalSupply
holderCount
}
}
```

### Real-Time Subscriptions

WebSocket subscriptions for live data updates:

```javascript
import { createClient } from 'graphql-ws';

const client = createClient({
url: 'wss://api.thegraph.com/subgraphs/name/username/xdc-token-indexer'
});

const subscription = client.subscribe(
{
query: `
subscription OnNewTransfer {
transfers(orderBy: timestamp, orderDirection: desc, first: 1) {
id
from { id }
to { id }
value
}
}
`
},
{
next: (data) => console.log('New transfer:', data),
error: (err) => console.error(err),
complete: () => console.log('Done')
}
);
```

---

Loading