Skip to content
Merged
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
11 changes: 11 additions & 0 deletions challenges/challenges.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,17 @@ export class ChallengesService {
};
}

/** Lowercased addresses of positions under an active challenge — used by other services to exclude them. */
getActiveChallengedPositions(): Set<Address> {
const active = new Set<Address>();
for (const challenge of Object.values(this.fetchedChallengesMapping)) {
if (challenge.status === ChallengesQueryStatus.Active) {
active.add(challenge.position.toLowerCase() as Address);
}
}
return active;
}

// challenges prices
getChallengesPrices(): ApiChallengesPrices {
const pr = this.fetchedPrices;
Expand Down
18 changes: 17 additions & 1 deletion positions/positions.controller.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { Controller, Get } from '@nestjs/common';
import { Controller, Get, Query } from '@nestjs/common';
import { PositionsService } from './positions.service';
import {
ApiBestCloneable,
ApiMintingUpdateListing,
ApiMintingUpdateMapping,
ApiPositionsListing,
ApiPositionsMapping,
ApiPositionsOwners,
} from './positions.types';
import { Address } from 'viem';
import { ApiResponse, ApiTags } from '@nestjs/swagger';

@ApiTags('Positions Controller')
Expand Down Expand Up @@ -69,4 +71,18 @@ export class PositionsController {
geMintingtMapping(): ApiMintingUpdateMapping {
return this.positionsService.getMintingUpdatesMapping();
}

@Get('best-cloneable')
@ApiResponse({
description: 'Returns the best cloneable parent position for the given collateral. Optionally filter by minting hub.',
})
getBestCloneable(
@Query('collateral') collateral: string,
@Query('mintingHubAddress') mintingHubAddress?: string,
): ApiBestCloneable {
return this.positionsService.getBestCloneableParent(
(collateral ?? '').toLowerCase() as Address,
mintingHubAddress ? (mintingHubAddress.toLowerCase() as Address) : undefined,
);
}
}
54 changes: 53 additions & 1 deletion positions/positions.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import { FIVEDAYS_MS } from 'utils/const-helper';
import { Address, erc20Abi, getAddress } from 'viem';
import { ADDR, isV3Hub, VIEM_CONFIG } from '../api.config';
import { PONDER_CLIENT } from '../api.apollo.config';
import { ChallengesService } from '../challenges/challenges.service';
import {
ApiBestCloneable,
ApiMintingUpdateListing,
ApiMintingUpdateMapping,
ApiPositionsListing,
Expand All @@ -25,7 +27,43 @@ export class PositionsService {
private fetchedPositions: PositionsQueryObjectArray = {};
private fetchedMintingUpdates: MintingUpdateQueryObjectArray = {};

constructor() {}
constructor(private readonly challengesService: ChallengesService) {}

private normalizeOptionalAddress(address?: Address): Address | undefined {
return address ? (address.toLowerCase() as Address) : undefined;
}

private compareParentPositions(a: PositionQuery, b: PositionQuery): number {
if (a.version !== b.version) return b.version - a.version;

if (a.expiration !== b.expiration) return b.expiration - a.expiration;

const priceDiff = BigInt(b.price) - BigInt(a.price);
if (priceDiff !== 0n) return priceDiff > 0n ? 1 : -1;

const availableA = BigInt(a.availableForClones);
const availableB = BigInt(b.availableForClones);
if (availableA !== availableB) return availableA < availableB ? 1 : -1;

return a.position.localeCompare(b.position);
}

private isCloneableParentCandidate(
position: PositionQuery,
now: number,
collateral: Address,
challengedPositions: Set<Address>,
mintingHubAddress?: Address,
): boolean {
if (mintingHubAddress && position.mintingHubAddress.toLowerCase() !== mintingHubAddress) return false;
if (position.closed || position.denied) return false;
if (challengedPositions.has(position.position.toLowerCase() as Address)) return false;
if (position.expiration <= now || position.cooldown >= now) return false;
if (position.collateral.toLowerCase() !== collateral.toLowerCase()) return false;
if (BigInt(position.collateralBalance) < BigInt(position.minimumCollateral)) return false;
if (BigInt(position.availableForClones) <= 0n) return false;
return true;
}

getPositionsList(): ApiPositionsListing {
const pos = Object.values(this.fetchedPositions) as PositionQuery[];
Expand Down Expand Up @@ -75,6 +113,20 @@ export class PositionsService {
};
}

getBestCloneableParent(collateral: Address, mintingHubAddress?: Address): ApiBestCloneable {
const now = Math.floor(Date.now() / 1000);
const collateralLower = collateral.toLowerCase() as Address;
const candidates = Object.values(this.fetchedPositions) as PositionQuery[];
const hubFilter = this.normalizeOptionalAddress(mintingHubAddress);
const challengedPositions = this.challengesService.getActiveChallengedPositions();

const cloneable = candidates
.filter((position) => this.isCloneableParentCandidate(position, now, collateralLower, challengedPositions, hubFilter))
.sort((a, b) => this.compareParentPositions(a, b));

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

async updatePositonV2s() {
this.logger.debug('Updating Positions V2');
const { data } = await PONDER_CLIENT.query({
Expand Down
4 changes: 4 additions & 0 deletions positions/positions.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,7 @@ export type ApiMintingUpdateMapping = {
positions: Address[];
map: MintingUpdateQueryObjectArray;
};

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