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
12 changes: 11 additions & 1 deletion positions/positions.controller.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Controller, Get } from '@nestjs/common';
import { Controller, Get, Query } from '@nestjs/common';
import { PositionsService } from './positions.service';
import {
ApiBestCloneable,
ApiMintingUpdateListing,
ApiMintingUpdateMapping,
ApiPositionDefault,
Expand All @@ -9,6 +10,7 @@ import {
ApiPositionsOwners,
ApiReferencePositions,
} from './positions.types';
import { Address } from 'viem';
import { ApiResponse, ApiTags } from '@nestjs/swagger';

@ApiTags('Positions Controller')
Expand Down Expand Up @@ -87,4 +89,12 @@ export class PositionsController {
getReferencePositions(): ApiReferencePositions {
return this.positionsService.getReferencePositions();
}

@Get('best-cloneable')
@ApiResponse({
description: 'Returns the best cloneable parent position for the given collateral (highest price, active, not challenged)',
})
getBestCloneable(@Query('collateral') collateral: string): ApiBestCloneable {
return this.positionsService.getBestCloneableParent((collateral ?? '').toLowerCase() as Address);
}
}
25 changes: 25 additions & 0 deletions positions/positions.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Address, erc20Abi, getAddress } from 'viem';
import { PONDER_CLIENT } from '../api.apollo.config';
import { CONFIG, VIEM_CONFIG } from '../api.config';
import {
ApiBestCloneable,
ApiMintingUpdateListing,
ApiMintingUpdateMapping,
ApiPositionDefault,
Expand Down Expand Up @@ -306,6 +307,30 @@ export class PositionsService {
return { num: collaterals.length, collaterals, map };
}

getBestCloneableParent(collateral: Address): ApiBestCloneable {
const now = Math.floor(Date.now() / 1000);
const collateralLower = collateral.toLowerCase() as Address;
const candidates = Object.values(this.fetchedPositions) as PositionQuery[];

const cloneable = candidates
.filter(
(p) =>
!p.closed &&
!p.denied &&
p.expiration > now &&
p.cooldown < now &&
BigInt(p.collateralBalance) >= BigInt(p.minimumCollateral) &&
p.collateral.toLowerCase() === collateralLower &&
!p.isChallenged,
)
.sort((a, b) => {
const diff = BigInt(b.price) - BigInt(a.price);
return diff > 0n ? 1 : diff < 0n ? -1 : 0;
});

return { position: cloneable[0] ?? null };
}

getMintingUpdatesList(): ApiMintingUpdateListing {
const m = Object.values(this.fetchedMintingUpdates).flat(1) as MintingUpdateQuery[];
return {
Expand Down
4 changes: 4 additions & 0 deletions positions/positions.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ export type ApiReferencePositions = {
map: ReferencePositionsMapping;
};

export type ApiBestCloneable = {
position: PositionQuery | null;
};

export type ApiPositionDefault = {
position: Address;
collateral: Address;
Expand Down
Loading