-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(backend): implement arbitrum mainnet camelot amm v3 resolver (#3)
Adds CamelotV3 AMM resolver. Updates controller to support multiple resolvers. --------- Signed-off-by: Luca Georges Francois <[email protected]>
- Loading branch information
1 parent
eb8b728
commit 473da24
Showing
10 changed files
with
3,013 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,31 @@ | ||
import { IsNotEmpty } from 'class-validator'; | ||
import { Type } from 'class-transformer'; | ||
import { | ||
IsArray, | ||
IsEnum, | ||
IsEthereumAddress, | ||
IsNotEmpty, | ||
IsNumber, | ||
} from 'class-validator'; | ||
import { ProtocolName } from '../resolver-registry/implementations/constants'; | ||
|
||
/** | ||
* Parameters for the `/positions` endpoint, used to specify: | ||
* | ||
* - The Ethereum Address of the owner of the positions. | ||
* - The list of networks to fetch positions from. | ||
* - The list of protocols to fetch positions from. | ||
*/ | ||
export class ListPositionsDTO { | ||
// TODO: Validate that the owner is a valid Ethereum Address. | ||
@IsNotEmpty() | ||
@IsEthereumAddress() | ||
owner: string; | ||
|
||
// TODO: Add a field to specify the list of chains. | ||
// TODO: Add a field to specify the list of protocols. | ||
@Type(() => Number) | ||
@IsArray() | ||
@IsNumber({}, { each: true }) | ||
chainIDs: number[]; | ||
|
||
@IsArray() | ||
@IsEnum(ProtocolName, { each: true }) | ||
protocols: ProtocolName[]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,54 @@ | ||
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'; | ||
import { | ||
Resolver, | ||
ResolverPositionExtra, | ||
} from './resolver-registry/resolver.entity'; | ||
import { ListPositionsRO, PositionsPerProtocol } from './ro/list-positions.ro'; | ||
|
||
@Injectable() | ||
export class PositionsService { | ||
async findAll( | ||
query: ListPositionsDTO, | ||
resolverRegistryService: ResolverRegistryService, | ||
): Promise<Position<ResolverPositionExtra>[]> { | ||
): Promise<ListPositionsRO> { | ||
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), | ||
// Quick and dirty filtering of resolvers based on the query. | ||
const store = new Map<number, Resolver[]>( | ||
[...resolverRegistryService.getStore()] | ||
.filter( | ||
([chainID, resolvers]) => | ||
query.chainIDs.includes(chainID) && | ||
resolvers.some((resolver) => | ||
query.protocols.includes(resolver.getProtocolName()), | ||
), | ||
); | ||
}), | ||
) | ||
) | ||
.flat() | ||
.flat(); | ||
) | ||
.map(([chain, resolvers]) => [ | ||
chain, | ||
resolvers.filter((resolver) => | ||
query.protocols.includes(resolver.getProtocolName()), | ||
), | ||
]), | ||
); | ||
|
||
return positions; | ||
const res: ListPositionsRO = {}; | ||
|
||
// Note that we don't use Promise.all here because for some reason viem does not work properly in that case. | ||
for (const [chainID, resolvers] of store) { | ||
const positions: PositionsPerProtocol = {}; | ||
|
||
for (const resolver of resolvers) { | ||
const resolverPositions = | ||
await resolver.findAllPositions<ResolverPositionExtra>(owner); | ||
positions[resolver.getProtocolName()] = resolverPositions; | ||
} | ||
|
||
res[chainID] = positions; | ||
} | ||
|
||
return res; | ||
} | ||
} |
Oops, something went wrong.