Skip to content

Commit

Permalink
Merge pull request #36 from overlay-market/fix/read-only-caching
Browse files Browse the repository at this point in the history
Fix/read only caching
  • Loading branch information
Aliona-D authored Nov 22, 2024
2 parents e14cb06 + a6369b0 commit 327f63a
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 77 deletions.
2 changes: 1 addition & 1 deletion packages/overlay-sdk/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "overlay-sdk",
"version": "1.0.0",
"version": "1.0.1",
"description": "",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ export type LiquidatedPositionData = {

export class OverlaySDKLiquidatedPositions extends OverlaySDKModule {
private sdk: OverlaySDK;
private liquidatedPositionsCache: Record<string, { data: any; lastUpdated: number }> = {};

constructor(props: OverlaySDKCommonProps, sdk: OverlaySDK) {
super(props);
Expand All @@ -48,16 +47,6 @@ export class OverlaySDKLiquidatedPositions extends OverlaySDKModule {
}
const chainId = this.core.chainId;

// check if we have the data in cache and if it's not too old
const cacheKey = `${walletClient}-${chainId}`;
if (!noCaching && this.liquidatedPositionsCache[cacheKey]) {
const cachedData = this.liquidatedPositionsCache[cacheKey];
const isCacheValid = Date.now() - cachedData.lastUpdated < 300 * 1000; // 5 minutes
if (isCacheValid) {
return paginate(this.filterLiquidatedPositionsByMarketId(cachedData.data, marketId), page, pageSize);
}
}

const rawliquidatedPositions = await getLiquidatedPositions({
chainId: chainId,
account: walletClient.toLowerCase()
Expand All @@ -66,7 +55,8 @@ export class OverlaySDKLiquidatedPositions extends OverlaySDKModule {
const marketDetails = await getMarketsDetailsByChainId(chainId as CHAINS);

// slice the raw data using page and pageSize
const liquidatedPositions = paginate(rawliquidatedPositions, page, pageSize).data;
const positionsFiltered = await this.filterLiquidatedPositionsByMarketId(rawliquidatedPositions, marketId);
const liquidatedPositions = paginate(positionsFiltered, page, pageSize).data;

for (const liquidated of liquidatedPositions) {
const marketName =
Expand Down Expand Up @@ -116,25 +106,21 @@ export class OverlaySDKLiquidatedPositions extends OverlaySDKModule {
});
}

// cache the data
if (!noCaching) {
this.liquidatedPositionsCache[cacheKey] = {
data: transformedLiquidated,
lastUpdated: Date.now(),
};
return {
data: transformedLiquidated,
total: positionsFiltered.length,
}

return paginate(this.filterLiquidatedPositionsByMarketId(transformedLiquidated, marketId), page, pageSize);
};

// private method to filter liquidated positions by marketId
private filterLiquidatedPositionsByMarketId = (
liquidatedPositions: LiquidatedPositionData[],
private filterLiquidatedPositionsByMarketId = async (
liquidatedPositions: any[],
marketId?: string
): LiquidatedPositionData[] => {
) => {
if (!marketId) return liquidatedPositions;
const {marketAddress} = await this.sdk.markets.getMarketDetails(marketId);
return liquidatedPositions.filter(
(liquidated) => liquidated.marketName === marketId
(liquidated) => liquidated.id.split("-")[0] === marketAddress
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,7 @@ export class OverlaySDKOpenPositions extends OverlaySDKModule {
}
const chainId = this.core.chainId;

// check if we have the data in cache and if it's not too old
const cacheKey = `${walletClient}-${chainId}`;
if (!noCaching && this.openPositionsCache[cacheKey]) {
const cachedData = this.openPositionsCache[cacheKey];
const isCacheValid = Date.now() - cachedData.lastUpdated < 300 * 1000; // 5 minutes
if (isCacheValid) {
return paginate(this.filterOpenPositionsByMarketId(cachedData.data, marketId), page, pageSize);
}
}

const [rawOpenData, marketDetails] = await Promise.all([
getOpenPositions({
Expand All @@ -104,41 +96,71 @@ export class OverlaySDKOpenPositions extends OverlaySDKModule {
const transformedOpens: OpenPositionData[] = [];
invariant(marketDetails, "Failed to get market details");
// slice the raw data using page and pageSize
const openPositions = paginate(rawOpenData, page, pageSize).data;
const positionsFiltered = await this.filterOpenPositionsByMarketId(rawOpenData, marketId);
const openPositions = paginate(positionsFiltered, page, pageSize).data;

const positionsData: {
[key: string]: PositionData | undefined
let positionsData: {
[key: string]: PositionData | null | undefined
} = {};
// get positions data in batch of 15 positions
for (let i = 0; i < openPositions.length; i += 15) {
const positions = openPositions.slice(i, i + 15).map((position) => ({
marketId: position.market.id as Address,
positionId: BigInt(position.id.split("-")[1])
}));
Object.assign(positionsData, await this.getPositionsData(chainId, walletClient, positions));

if (!noCaching && this.openPositionsCache[cacheKey]) {
// console.log('using cached data');
const cachedData = this.openPositionsCache[cacheKey];
// 3 minutes cache
const isCacheValid = Date.now() - cachedData.lastUpdated < 3 * 60 * 1000;
if (isCacheValid) {
positionsData = cachedData.data;
}
} else {
// console.log('fetching data');
// get positions data in batch of 15 positions
for (let i = 0; i < openPositions.length; i += 15) {
const positions = openPositions.slice(i, i + 15).map((position) => ({
marketId: position.market.id as Address,
positionId: BigInt(position.id.split("-")[1])
}));
Object.assign(positionsData, await this.getPositionsData(chainId, walletClient, positions));
}
}

for (const open of openPositions) {
const positionId = BigInt(open.id.split("-")[1]);
const marketId = open.market.id as Address;
const positionData = positionsData[`${marketId}-${positionId}`];
let positionData = positionsData[`${marketId}-${positionId}`];

if (positionData === undefined) {
// console.log('position not cached, fetching', marketId, positionId);
const posData = await this.getPositionsData(chainId, walletClient, [{
marketId: marketId,
positionId: positionId
}]);
// console.log('fetched', posData);
positionsData[`${marketId}-${positionId}`] = posData[`${marketId}-${positionId}`];
positionData = posData[`${marketId}-${positionId}`];
}

if (positionData) {
const formattedOpen = await this.formatOpenPosition(open, marketDetails, positionData);
if (formattedOpen) {
transformedOpens.push(formattedOpen);
}
} else {
console.log('position not found', marketId, positionId, positionData);
}
}

// cache the data
if (!noCaching) {
this.openPositionsCache[cacheKey] = {
data: transformedOpens,
lastUpdated: Date.now(),
};
data: positionsData,
lastUpdated: Date.now()
}
}

return paginate(this.filterOpenPositionsByMarketId(transformedOpens, marketId), page, pageSize);
return {
data: transformedOpens,
total: positionsFiltered.length
}
};

private async formatOpenPosition(
Expand Down Expand Up @@ -275,7 +297,7 @@ export class OverlaySDKOpenPositions extends OverlaySDKModule {
walletClient: Address,
positions: { marketId: Address; positionId: bigint }[]
): Promise<{
[key: string]: PositionData | undefined
[key: string]: PositionData | null
}> {
const OverlayV1StateABIFunctions = OverlayV1StateABI.filter((abi) => abi.type === "function")
const OverlayV1StateABIPositionFunctions = OverlayV1StateABIFunctions.filter(
Expand Down Expand Up @@ -350,14 +372,14 @@ export class OverlaySDKOpenPositions extends OverlaySDKModule {
});

const data: {
[key: string]: PositionData | undefined
[key: string]: PositionData | null
} = {};

for (let i = 0; i < positions.length; i++) {
const { marketId, positionId } = positions[i];

if (results[i * 7].status === 'success' && results[i * 7].result as bigint === BigInt(0)) {
data[`${marketId}-${positionId}`] = undefined;
data[`${marketId}-${positionId}`] = null;
console.log('position not found', marketId, positionId);
continue;
}
Expand Down Expand Up @@ -519,13 +541,14 @@ export class OverlaySDKOpenPositions extends OverlaySDKModule {
}

// private method to filter open positions by marketId
private filterOpenPositionsByMarketId = (
openPositions: OpenPositionData[],
private filterOpenPositionsByMarketId = async (
openPositions: any[],
marketId?: string
): OpenPositionData[] => {
) => {
if (!marketId) return openPositions;
const {marketAddress} = await this.sdk.markets.getMarketDetails(marketId);
return openPositions.filter(
(open) => open.marketName === marketId
(open) => open.id.split("-")[0] === marketAddress
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ export type UnwindPositionData = {

export class OverlaySDKUnwindPositions extends OverlaySDKModule {
private sdk: OverlaySDK;
private unwindPositionsCache: Record<string, { data: any; lastUpdated: number }> = {};

constructor(props: OverlaySDKCommonProps, sdk: OverlaySDK) {
super(props);
Expand All @@ -50,16 +49,6 @@ export class OverlaySDKUnwindPositions extends OverlaySDKModule {
}
const chainId = this.core.chainId;

// check if we have the data in cache and if it's not too old
const cacheKey = `${walletClient}-${chainId}`;
if (!noCaching && this.unwindPositionsCache[cacheKey]) {
const cachedData = this.unwindPositionsCache[cacheKey];
const isCacheValid = Date.now() - cachedData.lastUpdated < 300 * 1000; // 5 minutes
if (isCacheValid) {
return paginate(this.filterUnwindPositionsByMarketId(cachedData.data, marketId), page, pageSize);
}
}

const rawUnwindData = await getUnwindPositions({
chainId: chainId,
account: walletClient.toLowerCase()
Expand All @@ -68,7 +57,8 @@ export class OverlaySDKUnwindPositions extends OverlaySDKModule {
const marketDetails = await getMarketsDetailsByChainId(chainId as CHAINS);

// slice the raw data using page and pageSize
const unwindPositions = paginate(rawUnwindData, page, pageSize).data;
const positionsFiltered = await this.filterUnwindPositionsByMarketId(rawUnwindData, marketId);
const unwindPositions = paginate(positionsFiltered, page, pageSize).data;

for (const unwind of unwindPositions) {
const marketName =
Expand Down Expand Up @@ -123,23 +113,22 @@ export class OverlaySDKUnwindPositions extends OverlaySDKModule {
positionId: Number(unwind.position.positionId),
});
}

// cache the data
if (!noCaching) {
this.unwindPositionsCache[cacheKey] = { data: transformedUnwinds, lastUpdated: Date.now() };
}

return paginate(this.filterUnwindPositionsByMarketId(transformedUnwinds, marketId), page, pageSize);
return {
data: transformedUnwinds,
total: positionsFiltered.length,
};
};

// private method to filter unwind positions by marketId
private filterUnwindPositionsByMarketId = (
unwindPositions: UnwindPositionData[],
private filterUnwindPositionsByMarketId = async (
unwindPositions: any[],
marketId?: string
): UnwindPositionData[] => {
) => {
if (!marketId) return unwindPositions;
const {marketAddress} = await this.sdk.markets.getMarketDetails(marketId);
return unwindPositions.filter(
(unwind) => unwind.marketName === marketId
(unwind) => unwind.id.split("-")[0] === marketAddress
);
}
}

0 comments on commit 327f63a

Please sign in to comment.