Skip to content

Commit

Permalink
improve v3 pools support
Browse files Browse the repository at this point in the history
  • Loading branch information
mendesfabio committed Oct 16, 2024
1 parent c0b014c commit 802f483
Show file tree
Hide file tree
Showing 15 changed files with 235 additions and 986 deletions.
185 changes: 110 additions & 75 deletions docs/new-pool-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,81 +4,116 @@ To integrate new pool types into the Balancer subgraph, follow these steps below

## Setup

Navigate to the `subgraphs/pools` directory before starting the integration process.

## Adding the Factory

Run the following command to add your pool factory:

```
pnpm add-factory [factory-address] [network]
```

This command fetch the factory ABI, retrieves the deployment block, and adds it to the subgraph manifest.

## Adding a Pool Instance

Execute this command to fetch the pool ABI:

```
pnpm add-pool [pool-address] [network]
```

Ensure you have a pool deployed from your factory before running this command.

## Generating Types

Run the codegen command to ensure that types from your ABIs are generated:

```
pnpm codegen
```
Navigate to the `subgraphs/v3-pools` directory before starting the integration process.

## Updating the GraphQL Schema

Edit the `schema.graphql` file to add any specific parameters based on your pool type to the `Pool` entity.

```graphql
type Pool @entity {
# ... Existing fields

" Your custom parameter "
customParam: BigInt
}
```

Add your custom fields to this schema as needed for your specific pool type.

## Modifying the Pool Creation Handler

Navigate to `src/mappings/factories.ts` and update the handler function for the new pool type:


```typescript
import { YourPool } from "../types/YourPoolFactory/YourPool";

export function handleYourPoolCreated(event: PoolCreated): void {
let poolAddress = event.params.pool;
let pool = new Pool(poolAddress);
pool.address = poolAddress;

let factory = getFactory(event.address, PoolType.YourPoolType, 1);
pool.factory = factory.id;


// First, bind the pool address to its contract
let yourPool = YourPool.bind(poolAddress);

// Then fetch the pool-specific parameters
let customParamResult = yourPool.try_getCustomParameter();
if (!customParamResult.reverted) {
pool.customParam = customParamResult.value;
}

// Add more parameter fetching logic as needed

pool.save();
}
```

In this function, replace the example parameter fetching logic with calls to your pool's specific methods to retrieve and store the relevant parameters.
1. Edit the `schema.graphql` file:

a. Add your pool type to the `PoolType` enum:

```graphql
enum PoolType {
Weighted
Stable
Custom # Add your new pool type here
}
```

b. Create an entity for your pool-type specific parameters:

```graphql
type CustomParams @entity {
id: Bytes!
customParam: BigInt!
# Add other custom parameters as needed
}
```

c. Extend the `Pool` entity to expose your custom params:

```graphql
type Pool @entity {
id: Bytes!
address: Bytes!
factory: Factory!
stableParams: StableParams
weightedParams: WeightedParams
customParams: CustomParams # Add the new entity here
}
```

2. Save the `schema.graphql` file. Run the codegen command to generate types from your ABIs:

```bash
pnpm codegen
```

## Creating the Subgraph Manifest

1. Update the `subgraph.sepolia.yaml` file to include your new pool factory:

```yaml
dataSources:
# Existing data sources...
- kind: ethereum/contract
name: CustomPoolFactory
network: sepolia
source:
address: "0x..." # Your custom pool factory address
abi: BasePoolFactory
mapping:
kind: ethereum/events
apiVersion: 0.0.7
language: wasm/assemblyscript
file: ./src/mappings/custom.ts
entities:
- Pool
- CustomParams
abis:
- name: BasePoolFactory
file: ./abis/BasePoolFactory.json
eventHandlers:
- event: PoolCreated(indexed address,bytes32)
handler: handleCustomPoolCreated
```

## Implementing Pool Factory Mapping

1. Update `src/mappings/common.ts` to include your new pool type:

```typescript
export class PoolType {
static Weighted: string = "Weighted";
static Stable: string = "Stable";
static Custom: string = "Custom"; // Add your new pool type here
}
```

2. Create a new file in the `src/mappings` directory for your custom pool factory (e.g., `custom.ts`).

3. Implement the factory mapping. See Stable and Weighted implementations as reference.

```typescript
import { Address } from "@graphprotocol/graph-ts";

import { PoolCreated } from "../types/CustomPoolFactory/BasePoolFactory";
import { handlePoolCreated } from "./factories";
import { PoolType } from "./constants";

export function handleCustomPoolCreated(event: CustomPoolCreated): void {
handlePoolCreated(
event.params.pool,
event.address, // Factory
PoolType.Custom,
1, // version of your pool type factory
handleCustomPoolParams,
"customParams" // should be the name of the field you added to the Pool entity
);
}

function handleCustomPoolParams(poolAddress: Address): Bytes {
// Implement custom logic to fetch and save custom parameters
// Return the ID of the saved CustomParams entity
}
```
15 changes: 15 additions & 0 deletions subgraphs/v3-pools/abis/BasePoolFactory.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"internalType": "address",
"name": "pool",
"type": "address"
}
],
"name": "PoolCreated",
"type": "event"
}
]
Loading

0 comments on commit 802f483

Please sign in to comment.