Skip to content

Commit

Permalink
format
Browse files Browse the repository at this point in the history
  • Loading branch information
CapCap committed Dec 6, 2024
1 parent c7d9d0b commit baff241
Show file tree
Hide file tree
Showing 11 changed files with 55 additions and 43 deletions.
5 changes: 2 additions & 3 deletions src/typescript/frontend/src/app/dexscreener/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# DEX Screener Adapter Specs

<b>Taken from https://dexscreener.notion.site/DEX-Screener-Adapter-Specs-cc1223cdf6e74a7799599106b65dcd0e </b>

<b>Taken from https://dexscreener.notion.site/DEX-Screener-Adapter-Specs-cc1223cdf6e74a7799599106b65dcd0e </b>

v1.1 / Dec 2023

Expand All @@ -19,4 +18,4 @@ The DEX Screener Adapter is a set of HTTP endpoints that allows DEX Screener to
- An in-depth explanation of the schemas expected for each endpoint is described on the `Schemas` section below
- Numbers for amounts and price can be both `number` and `string`. Strings are more suitable when dealing with extremely small or extremely large numbers that can't be accurately serialized into JSON numbers.

- Indexing will halt if schemas are invalid or contain unexpected values (i.e.: `swapEvent.priceNative=0` or `pair.name=""`)
- Indexing will halt if schemas are invalid or contain unexpected values (i.e.: `swapEvent.priceNative=0` or `pair.name=""`)
2 changes: 1 addition & 1 deletion src/typescript/frontend/src/app/dexscreener/asset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,4 @@ export async function GET(request: NextRequest): Promise<NextResponse<AssetRespo
}
const asset = await getAsset(assetId);
return NextResponse.json({ asset });
}
}
24 changes: 14 additions & 10 deletions src/typescript/frontend/src/app/dexscreener/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,17 @@

import { type NextRequest, NextResponse } from "next/server";
import { type Block } from "./latest-block";
import { fetchLiquidityEventsByBlock, fetchSwapEventsByBlock } from "@sdk/indexer-v2/queries/app/dexscreener";
import {
fetchLiquidityEventsByBlock,
fetchSwapEventsByBlock,
} from "@sdk/indexer-v2/queries/app/dexscreener";
import { type toLiquidityEventModel, type toSwapEventModel } from "@sdk/indexer-v2/types";
import Big from "big.js";
import { calculateRealReserves } from "@sdk/markets";
import { toCoinDecimalString } from "../../lib/utils/decimals";
import { DECIMALS } from "@sdk/const";
import { symbolEmojisToPairId } from "./util";


/**
* - `txnId` is a transaction identifier such as a transaction hash
* - `txnIndex` refers to the order of a transaction within a block, the higher the index the later in the block the
Expand Down Expand Up @@ -175,9 +177,9 @@ export interface EventsResponse {
events: Event[];
}


export function toDexscreenerSwapEvent(event: ReturnType<typeof toSwapEventModel>): SwapEvent & BlockInfo {

export function toDexscreenerSwapEvent(
event: ReturnType<typeof toSwapEventModel>
): SwapEvent & BlockInfo {
let assetInOut;

if (event.swap.isSell) {
Expand All @@ -204,12 +206,12 @@ export function toDexscreenerSwapEvent(event: ReturnType<typeof toSwapEventModel
asset1: toCoinDecimalString(quote, DECIMALS),
};

const priceNative = (new Big(event.swap.avgExecutionPriceQ64.toString())).div(2 ** 64).toFixed(64);
const priceNative = new Big(event.swap.avgExecutionPriceQ64.toString()).div(2 ** 64).toFixed(64);

return {
block: {
blockNumber: Number(event.blockAndEvent.blockNumber),
blockTimestamp: event.transaction.timestamp.getTime() / 1000
blockTimestamp: event.transaction.timestamp.getTime() / 1000,
},
eventType: "swap",
txnId: event.transaction.version.toString(),
Expand All @@ -229,7 +231,9 @@ export function toDexscreenerSwapEvent(event: ReturnType<typeof toSwapEventModel
};
}

export function toDexscreenerJoinExitEvent(event: ReturnType<typeof toLiquidityEventModel>): JoinExitEvent & BlockInfo {
export function toDexscreenerJoinExitEvent(
event: ReturnType<typeof toLiquidityEventModel>
): JoinExitEvent & BlockInfo {
const { base, quote } = calculateRealReserves(event.state);
const reserves = {
asset0: toCoinDecimalString(base, DECIMALS),
Expand All @@ -239,7 +243,7 @@ export function toDexscreenerJoinExitEvent(event: ReturnType<typeof toLiquidityE
return {
block: {
blockNumber: Number(event.blockAndEvent.blockNumber),
blockTimestamp: event.transaction.timestamp.getTime() / 1000
blockTimestamp: event.transaction.timestamp.getTime() / 1000,
},
eventType: event.liquidity.liquidityProvided ? "join" : "exit",

Expand Down Expand Up @@ -301,4 +305,4 @@ export async function GET(request: NextRequest): Promise<NextResponse<EventsResp
const events = await getEventsByVersion(parseInt(fromBlock, 10), parseInt(toBlock, 10));

return NextResponse.json({ events });
}
}
4 changes: 2 additions & 2 deletions src/typescript/frontend/src/app/dexscreener/latest-block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,6 @@ export async function GET(_request: NextRequest): Promise<NextResponse<LatestBlo
blockNumber,
// Convert to seconds
blockTimestamp: status.lastTransactionTimestamp.getTime() / 1000,
}
},
});
}
}
13 changes: 6 additions & 7 deletions src/typescript/frontend/src/app/dexscreener/pair.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,11 @@
**/

import { type NextRequest, NextResponse } from "next/server";
import {
fetchMarketRegistrationEventBySymbolEmojis
} from "@sdk/indexer-v2/queries/app/dexscreener";
import { fetchMarketRegistrationEventBySymbolEmojis } from "@sdk/indexer-v2/queries/app/dexscreener";
import { getAptosClient } from "@sdk/utils/aptos-client";
import { INTEGRATOR_FEE_RATE_BPS } from "@sdk/const";
import { pairIdToSymbolEmojis } from "./util";


/**
* - All `Pair` props are immutable - Indexer will not query a given pair more than once
* - In most cases, pair ids will correspond to contract addresses. Ids are case-sensitive.
Expand Down Expand Up @@ -78,13 +75,15 @@ export interface PairResponse {
* @param pairId is the pair ID. Generally it's `event.market.symbolEmojis.join("") + "-APT"`
*/
export async function getPair(pairId: string): Promise<Pair> {
const sybolEmojis = pairIdToSymbolEmojis(pairId)
const sybolEmojis = pairIdToSymbolEmojis(pairId);

const marketRegistrations = await fetchMarketRegistrationEventBySymbolEmojis({ sybolEmojis });
const marketRegistration = marketRegistrations[0];

const aptos = getAptosClient();
const block = await aptos.getBlockByVersion({ ledgerVersion: marketRegistration.transaction.version });
const block = await aptos.getBlockByVersion({
ledgerVersion: marketRegistration.transaction.version,
});

return {
id: pairId,
Expand All @@ -107,4 +106,4 @@ export async function GET(request: NextRequest): Promise<NextResponse<PairRespon
}
const pair = await getPair(pairId);
return NextResponse.json({ pair });
}
}
2 changes: 0 additions & 2 deletions src/typescript/frontend/src/app/dexscreener/util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,5 @@ describe("dexscreener utilities", () => {
const deserializedSymbolEmojis = pairIdToSymbolEmojis(pairId);
expect(deserializedSymbolEmojis).toEqual(symbolEmojis);
}

});

});
2 changes: 1 addition & 1 deletion src/typescript/frontend/src/app/dexscreener/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export function symbolEmojisToString(symbolEmojis: Array<SymbolEmoji>): string {

export function symbolEmojiStringToArray(symbolEmojiString: string): SymbolEmoji[] {
const marketEmojiData = toMarketEmojiData(symbolEmojiString);
return marketEmojiData.emojis;
return marketEmojiData.emojis.map((emojiData) => emojiData.emoji);
}

export function pairIdToSymbolEmojis(pairId: string): SymbolEmoji[] {
Expand Down
16 changes: 11 additions & 5 deletions src/typescript/sdk/src/indexer-v2/queries/app/dexscreener.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { queryHelper } from "../utils";
import { toLiquidityEventModel, toMarketRegistrationEventModel, toSwapEventModel } from "../../types";
import {
toLiquidityEventModel,
toMarketRegistrationEventModel,
toSwapEventModel,
} from "../../types";
import { LIMIT } from "../../../queries";
import type { MarketStateQueryArgs } from "../../types/common";
import { postgrest, toQueryArray } from "../client";
import { TableName } from "../../types/json-types";
import { type SymbolEmoji } from "../../../emoji_data";


const selectMarketRegistrationEventBySymbolEmojis = ({
searchEmojis,
page = 1,
Expand All @@ -23,7 +26,7 @@ const selectSwapEventsByBlock = ({
toBlock,
page = 1,
pageSize = LIMIT,
}: { fromBlock: number, toBlock: number } & MarketStateQueryArgs) =>
}: { fromBlock: number; toBlock: number } & MarketStateQueryArgs) =>
postgrest
.from(TableName.SwapEvents)
.select("*")
Expand All @@ -36,7 +39,7 @@ const selectLiquidityEventsByBlock = ({
toBlock,
page = 1,
pageSize = LIMIT,
}: { fromBlock: number, toBlock: number } & MarketStateQueryArgs) =>
}: { fromBlock: number; toBlock: number } & MarketStateQueryArgs) =>
postgrest
.from(TableName.LiquidityEvents)
.select("*")
Expand All @@ -50,4 +53,7 @@ export const fetchMarketRegistrationEventBySymbolEmojis = queryHelper(
);

export const fetchSwapEventsByBlock = queryHelper(selectSwapEventsByBlock, toSwapEventModel);
export const fetchLiquidityEventsByBlock = queryHelper(selectLiquidityEventsByBlock, toLiquidityEventModel);
export const fetchLiquidityEventsByBlock = queryHelper(
selectLiquidityEventsByBlock,
toLiquidityEventModel
);
8 changes: 3 additions & 5 deletions src/typescript/sdk/src/indexer-v2/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ import {
postgresTimestampToDate,
TableName,
type ProcessedFields,
DatabaseRpc, type BlockAndEventIndexMetadata,
DatabaseRpc,
type BlockAndEventIndexMetadata,
} from "./json-types";
import { type MarketEmojiData, type SymbolEmoji, toMarketEmojiData } from "../../emoji_data";
import { toPeriod, toTrigger, type Period, type Trigger } from "../../const";
Expand Down Expand Up @@ -356,10 +357,7 @@ export const withMarketAndStateMetadataAndEmitTime = curryToNamedType(
toMarketMetadataModel,
"market"
);
export const withBlockAndEventIndex = curryToNamedType(
toBlockAndEventIndex,
"blockAndEvent"
);
export const withBlockAndEventIndex = curryToNamedType(toBlockAndEventIndex, "blockAndEvent");
export const withLastSwap = curryToNamedType(toLastSwapFromDatabase, "lastSwap");
export const withGlobalStateEventData = curryToNamedType(toGlobalStateEventData, "globalState");
export const withPeriodicStateMetadata = curryToNamedType(
Expand Down
18 changes: 13 additions & 5 deletions src/typescript/sdk/src/indexer-v2/types/json-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,9 @@ type TransactionMetadata = {
};

export type BlockAndEventIndexMetadata = {
block_number: Uint64String,
event_index: number,
}
block_number: Uint64String;
event_index: number;
};

type MarketAndStateMetadata = {
market_id: Uint64String;
Expand Down Expand Up @@ -319,10 +319,18 @@ export type DatabaseJsonType = {
TransactionMetadata & MarketAndStateMetadata & MarketRegistrationEventData
>;
[TableName.SwapEvents]: Flatten<
TransactionMetadata & MarketAndStateMetadata & SwapEventData & StateEventData & BlockAndEventIndexMetadata
TransactionMetadata &
MarketAndStateMetadata &
SwapEventData &
StateEventData &
BlockAndEventIndexMetadata
>;
[TableName.LiquidityEvents]: Flatten<
TransactionMetadata & MarketAndStateMetadata & LiquidityEventData & StateEventData & BlockAndEventIndexMetadata
TransactionMetadata &
MarketAndStateMetadata &
LiquidityEventData &
StateEventData &
BlockAndEventIndexMetadata
>;
[TableName.ChatEvents]: Flatten<
TransactionMetadata & MarketAndStateMetadata & ChatEventData & StateEventData
Expand Down
4 changes: 2 additions & 2 deletions src/typescript/sdk/src/mini-processor/event-groups/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export const addModelsForBumpEvent = (args: {
const liquidity = toLiquidityEventData(event);
const liquidityEventModel: DatabaseModels["liquidity_events"] = {
transaction,
blockAndEvent: {blockNumber: transaction.version, eventIndex: eventIndex!},
blockAndEvent: { blockNumber: transaction.version, eventIndex: eventIndex! },
market,
state,
lastSwap,
Expand Down Expand Up @@ -129,7 +129,7 @@ export const addModelsForBumpEvent = (args: {
} else if (isSwapEvent(event)) {
rows.swapEvents.push({
transaction,
blockAndEvent: {blockNumber: transaction.version, eventIndex: eventIndex!},
blockAndEvent: { blockNumber: transaction.version, eventIndex: eventIndex! },
market,
state,
swap: toSwapEventData(event),
Expand Down

0 comments on commit baff241

Please sign in to comment.