Skip to content

Commit

Permalink
feat(backend): implement eth mainnet uniswap v3 resolver (#2)
Browse files Browse the repository at this point in the history
Adds new positions controller.
Adds Uniswap V3 resolver.
Adds resolver registry for future resolver implementations.

---------

Signed-off-by: Luca Georges Francois <[email protected]>
  • Loading branch information
0xpanoramix authored May 8, 2024
1 parent c398596 commit eb8b728
Show file tree
Hide file tree
Showing 18 changed files with 2,958 additions and 2 deletions.
3 changes: 3 additions & 0 deletions backend/CONRTIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Contributing to lipso backend

_Coming soon!_
5 changes: 5 additions & 0 deletions backend/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ This will start the backend server in development mode. The server will automati

### Production

## Contribute

We welcome contributions to the project with ❤️.
Before you start contributing, please read our [Contributing Guidelines](CONTRIBUTING.md).

## Authors

Made with ❤️ by the 📡 at [Quartz](https://quartz.technology).
6 changes: 5 additions & 1 deletion backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,13 @@
"dependencies": {
"@nestjs/common": "^10.0.0",
"@nestjs/core": "^10.0.0",
"@nestjs/mapped-types": "*",
"@nestjs/platform-express": "^10.0.0",
"class-transformer": "^0.5.1",
"class-validator": "^0.14.1",
"reflect-metadata": "^0.2.0",
"rxjs": "^7.8.1"
"rxjs": "^7.8.1",
"viem": "^2.9.32"
},
"devDependencies": {
"@nestjs/cli": "^10.0.0",
Expand Down
3 changes: 2 additions & 1 deletion backend/src/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { PositionsModule } from './positions/positions.module';

@Module({
imports: [],
imports: [PositionsModule],
controllers: [AppController],
providers: [AppService],
})
Expand Down
2 changes: 2 additions & 0 deletions backend/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { ValidationPipe } from '@nestjs/common';

async function bootstrap() {
const app = await NestFactory.create(AppModule);
app.useGlobalPipes(new ValidationPipe());
await app.listen(3000);
}
bootstrap();
15 changes: 15 additions & 0 deletions backend/src/positions/dto/list-positions.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { IsNotEmpty } from 'class-validator';

/**
* Parameters for the `/positions` endpoint, used to specify:
*
* - The Ethereum Address of the owner of the positions.
*/
export class ListPositionsDTO {
// TODO: Validate that the owner is a valid Ethereum Address.
@IsNotEmpty()
owner: string;

// TODO: Add a field to specify the list of chains.
// TODO: Add a field to specify the list of protocols.
}
11 changes: 11 additions & 0 deletions backend/src/positions/entities/position.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* A liquidity position on a DEX.
*
* As the goal of this project is to provide insights to the user about their liquidity positions, this entity helps to understand if their position is in range or not.
* Depending on the DEX, the extra field can be used to store additional information about the position.
* For example, in UniswapV3, the extra field stores the position ID can be used to create a link to the position on the official UniswapV3 interface.
*/
export class Position<T> {
inRange: boolean;
extra: T;
}
21 changes: 21 additions & 0 deletions backend/src/positions/positions.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Test, TestingModule } from '@nestjs/testing';
import { PositionsController } from './positions.controller';
import { PositionsService } from './positions.service';
import { ResolverRegistryService } from './resolver-registry/resolver-registry.service';

describe('PositionsController', () => {
let controller: PositionsController;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [PositionsController],
providers: [PositionsService, ResolverRegistryService],
}).compile();

controller = module.get<PositionsController>(PositionsController);
});

it('should be defined', () => {
expect(controller).toBeDefined();
});
});
40 changes: 40 additions & 0 deletions backend/src/positions/positions.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { Controller, Get, Query } from '@nestjs/common';
import { PositionsService } from './positions.service';
import { ListPositionsDTO } from './dto/list-positions.dto';
import { Position } from './entities/position.entity';
import { ResolverRegistryService } from './resolver-registry/resolver-registry.service';
import { mainnet } from 'viem/chains';
import { createPublicClient, getAddress, http } from 'viem';
import { ResolverPositionExtra } from './resolver-registry/resolver.entity';

@Controller('positions')
export class PositionsController {
constructor(
private readonly positionsService: PositionsService,
private resolverRegistryService: ResolverRegistryService,
) {
// TODO: Get HTTP endpoint from config.
const ethMainnetClient = createPublicClient({
chain: mainnet,
transport: http(undefined, {
batch: {
batchSize: 10,
},
}),
});

// TODO: Get UniswapV3 mainnet deployment addresses from config.
this.resolverRegistryService.registerUniswapV3Resolver(
ethMainnetClient,
getAddress('0x1F98431c8aD98523631AE4a59f267346ea31F984'),
getAddress('0xC36442b4a4522E871399CD717aBDD847Ab11FE88'),
);
}

@Get()
async findAll(
@Query() query: ListPositionsDTO,
): Promise<Position<ResolverPositionExtra>[]> {
return this.positionsService.findAll(query, this.resolverRegistryService);
}
}
10 changes: 10 additions & 0 deletions backend/src/positions/positions.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Module } from '@nestjs/common';
import { PositionsService } from './positions.service';
import { PositionsController } from './positions.controller';
import { ResolverRegistryService } from './resolver-registry/resolver-registry.service';

@Module({
controllers: [PositionsController],
providers: [PositionsService, ResolverRegistryService],
})
export class PositionsModule {}
18 changes: 18 additions & 0 deletions backend/src/positions/positions.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Test, TestingModule } from '@nestjs/testing';
import { PositionsService } from './positions.service';

describe('PositionsService', () => {
let service: PositionsService;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [PositionsService],
}).compile();

service = module.get<PositionsService>(PositionsService);
});

it('should be defined', () => {
expect(service).toBeDefined();
});
});
33 changes: 33 additions & 0 deletions backend/src/positions/positions.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { Injectable } from '@nestjs/common';
import { ListPositionsDTO } from './dto/list-positions.dto';
import { Position } from './entities/position.entity';
import { ResolverRegistryService } from './resolver-registry/resolver-registry.service';
import { getAddress } from 'viem';
import { ResolverPositionExtra } from './resolver-registry/resolver.entity';

@Injectable()
export class PositionsService {
async findAll(
query: ListPositionsDTO,
resolverRegistryService: ResolverRegistryService,
): Promise<Position<ResolverPositionExtra>[]> {
const owner = getAddress(query.owner);
const store = resolverRegistryService.getStore();

const positions = (
await Promise.all(
Array.from(store.values()).map((resolver) => {
return Promise.all(
resolver.map((r) =>
r.findAllPositions<ResolverPositionExtra>(owner),
),
);
}),
)
)
.flat()
.flat();

return positions;
}
}
Loading

0 comments on commit eb8b728

Please sign in to comment.