Skip to content

Commit

Permalink
remove clone deep.
Browse files Browse the repository at this point in the history
  • Loading branch information
robert-zklink committed May 9, 2024
1 parent f6111aa commit 288d7c1
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 53 deletions.
2 changes: 1 addition & 1 deletion src/magpie/magpie.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export class MagpieController {
try {
pointData = await this.magpieService.getPointsData(address);
} catch (err) {
this.logger.error("Get magpie all points failed", err);
this.logger.error("Get magpie all points failed", err.stack);
return SERVICE_EXCEPTION;
}
return this.getReturnData(
Expand Down
43 changes: 31 additions & 12 deletions src/magpie/magpie.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Injectable, Logger } from "@nestjs/common";
import { cloneDeep } from "lodash";
import { GraphQueryService } from "../common/service/graphQuery.service";
import { LocalPointsItem } from "../common/service/projectGraph.service";
import waitFor from "src/utils/waitFor";
Expand Down Expand Up @@ -37,6 +36,10 @@ export interface MagpieData {
localTotalPoints: bigint;
realTotalEigenpiePoints: bigint;
realTotalEigenLayerPoints: bigint;
itemMaps?: Map<
string,
MagpiePointItemWithBalance[] | MagpiePointItemWithoutBalance[]
>;
items: MagpiePointItemWithBalance[] | MagpiePointItemWithoutBalance[];
}

Expand All @@ -50,6 +53,7 @@ export class MagpieService extends Worker {
localTotalPoints: 0n,
realTotalEigenpiePoints: 0n,
realTotalEigenLayerPoints: 0n,
itemMaps: new Map(),
items: [],
};

Expand Down Expand Up @@ -154,11 +158,23 @@ export class MagpieService extends Worker {
};
data.push(pointsItem);
}

const itemMaps = new Map();
for (let i = 0; i < data.length; i++) {
const item = data[i];
if (!itemMaps.has(item.address)) {
itemMaps.set(item.address, [item]);
} else {
const tmpItems = itemMaps.get(item.address);
itemMaps.set(item.address, [...tmpItems, item]);
}
}
if (data.length > 0) {
this.magpieData = {
localTotalPoints: localTotalPoints,
realTotalEigenpiePoints: realTotalPointsData.eigenpiePoints,
realTotalEigenLayerPoints: realTotalPointsData.eigenLayerPoints,
itemMaps: itemMaps,
items: data,
};
} else {
Expand All @@ -168,14 +184,14 @@ export class MagpieService extends Worker {

// return points data
public getPointsData(address?: string): MagpieData {
let result: MagpieData = cloneDeep(this.magpieData);
if (address) {
const _address = address.toLocaleLowerCase();
result.items = this.magpieData.items.filter(
(item) => item.address === _address,
);
}
return result;
return {
localTotalPoints: this.magpieData.localTotalPoints,
realTotalEigenpiePoints: this.magpieData.realTotalEigenpiePoints,
realTotalEigenLayerPoints: this.magpieData.realTotalEigenLayerPoints,
items: address
? this.magpieData.itemMaps.get(address) ?? []
: this.magpieData.items,
};
}

// return local points and totalPoints
Expand All @@ -190,7 +206,6 @@ export class MagpieService extends Worker {

// return real points group by address
public getPointsDataGroupByAddress(): MagpieData {
let result: MagpieData = cloneDeep(this.magpieData);
let data: Map<string, MagpiePointItemWithoutBalance> = new Map();
const now = (new Date().getTime() / 1000) | 0;
for (let i = 0; i < this.magpieData.items.length; i++) {
Expand All @@ -208,7 +223,11 @@ export class MagpieService extends Worker {
tmpItem.realEigenpiePoints += item.realEigenpiePoints;
}
}
result.items = Array.from(data.values());
return result;
return {
localTotalPoints: this.magpieData.localTotalPoints,
realTotalEigenpiePoints: this.magpieData.realTotalEigenpiePoints,
realTotalEigenLayerPoints: this.magpieData.realTotalEigenLayerPoints,
items: Array.from(data.values()),
};
}
}
53 changes: 26 additions & 27 deletions src/renzo/renzo.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
import { GraphQueryService } from "src/common/service/graphQuery.service";
import { RenzoApiService, RenzoPoints } from "src/renzo/renzoapi.service";
import { ExplorerService } from "src/common/service/explorer.service";
import { cloneDeep } from "lodash";
import BigNumber from "bignumber.js";
import waitFor from "src/utils/waitFor";
import { Worker } from "src/common/worker";
Expand All @@ -30,6 +29,7 @@ export interface RenzoData {
localTotalPoints: bigint;
realTotalRenzoPoints: number;
realTotalEigenLayerPoints: number;
itemMaps?: Map<string, RenzoPointItem[]>;
items: RenzoPointItem[];
}

Expand All @@ -43,6 +43,7 @@ export class RenzoService extends Worker {
localTotalPoints: BigInt(0),
realTotalRenzoPoints: 0,
realTotalEigenLayerPoints: 0,
itemMaps: new Map(),
items: [],
};
private readonly l1Erc20BridgeEthereum: string;
Expand Down Expand Up @@ -84,7 +85,7 @@ export class RenzoService extends Worker {
}

// load points data
public async loadPointsData() {
private async loadPointsData() {
// get tokens from graph
const tokens = this.graphQueryService.getAllTokenAddresses(
this.projectName,
Expand All @@ -96,19 +97,10 @@ export class RenzoService extends Worker {
this.tokenAddress = tokens;

const realTokenTotalPoints = await this.getRealPointsData();
this.logger.log(
"renzoLog: realTokenTotalPoints:",
realTokenTotalPoints.size,
);
const pointsData = await this.getLocalPointsData();
this.logger.log("renzoLog: pointsData");
const localPoints = pointsData.localPoints;
const localTotalPoints = pointsData.localTotalPoints;
const tokensMapBridgeTokens = await this.getTokensMapBriageTokens();
this.logger.log(
"renzoLog: tokensMapBridgeTokens:",
tokensMapBridgeTokens.size,
);
if (
tokensMapBridgeTokens.size < 1 ||
localPoints.length < 1 ||
Expand All @@ -127,10 +119,6 @@ export class RenzoService extends Worker {
const transferFaildPoints = this.projectGraphService.getTransferFaildPoints(
this.tokenAddress,
);
this.logger.log(
"renzoLog: transferFaildPoints:",
transferFaildPoints.length,
);
const localPointsMap = new Map<string, LocalPointsItem>();
const totalPointsPerTokenMap = new Map<string, bigint>();
const now = (new Date().getTime() / 1000) | 0;
Expand All @@ -139,7 +127,7 @@ export class RenzoService extends Worker {
totalPointsPerTokenMap.set(item.token, item.totalPointsPerToken);
localPointsMap.set(key, item);
}
this.logger.log("renzoLog: localPointsMap:", localPointsMap.size);

// loop transferFaildData, and added transferFaildPoint to localPoints
for (const item of transferFaildPoints) {
const key = `${item.address}_${item.tokenAddress}`;
Expand All @@ -166,7 +154,6 @@ export class RenzoService extends Worker {
}
}
// end added transferFaildPoint
this.logger.log("renzoLog: end added transferFaildPoint");
for (const [, point] of localPointsMap) {
const tokenAddress = point.token.toLocaleLowerCase();
if (!tokenAddress) {
Expand Down Expand Up @@ -210,12 +197,23 @@ export class RenzoService extends Worker {
};
data.push(pointsItem);
}
this.logger.log("renzoLog: data", data.length);

const itemMaps = new Map();
for (let i = 0; i < data.length; i++) {
const item = data[i];
if (!itemMaps.has(item.address)) {
itemMaps.set(item.address, [item]);
} else {
const tmpItems = itemMaps.get(item.address);
itemMaps.set(item.address, [...tmpItems, item]);
}
}
if (data.length > 0) {
this.renzoData = {
localTotalPoints: localTotalPoints,
realTotalRenzoPoints: realTotalRenzoPoints,
realTotalEigenLayerPoints: realTotalEigenLayerPoints,
itemMaps: itemMaps,
items: data,
};
} else {
Expand All @@ -225,23 +223,24 @@ export class RenzoService extends Worker {

// return points data
public getPointsData(address?: string): RenzoData {
const result: RenzoData = cloneDeep(this.renzoData);
if (address) {
const _address = address.toLocaleLowerCase();
result.items = this.renzoData.items.filter(
(item) => item.address === _address,
);
}
const result: RenzoData = {
localTotalPoints: this.renzoData.localTotalPoints,
realTotalRenzoPoints: this.renzoData.realTotalRenzoPoints,
realTotalEigenLayerPoints: this.renzoData.realTotalEigenLayerPoints,
items: address
? this.renzoData.itemMaps.get(address) ?? []
: this.renzoData.items,
};
return result;
}

// return local points and totalPoints
public async getLocalPointsData(): Promise<LocalPointData> {
private async getLocalPointsData(): Promise<LocalPointData> {
return await this.projectGraphService.getPoints(this.projectName);
}

// return real totalPoints
public async getRealPointsData(): Promise<Map<string, RenzoPoints>> {
private async getRealPointsData(): Promise<Map<string, RenzoPoints>> {
return await this.renzoApiService.fetchTokensRenzoPoints();
}

Expand Down
2 changes: 1 addition & 1 deletion src/renzo/renzoapi.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export class RenzoApiService {
for (const bridgeAddress of this.l1Erc20Bridges) {
const renzoPoints = await this._fetchRenzoPoints(bridgeAddress);
allRenzoPoints.set(bridgeAddress.toLocaleLowerCase(), renzoPoints);
//await new Promise((resolve) => setTimeout(resolve, 1000)); // wait 1s
await new Promise((resolve) => setTimeout(resolve, 1000)); // wait 1s
}
return allRenzoPoints;
}
Expand Down
43 changes: 31 additions & 12 deletions src/rseth/rseth.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Injectable, Logger } from "@nestjs/common";
import { cloneDeep } from "lodash";
import {
LocalPointData,
ProjectGraphService,
Expand Down Expand Up @@ -37,6 +36,10 @@ export interface RsethData {
localTotalPoints: bigint;
realTotalElPoints: number;
realTotalKelpMiles: number;
itemMaps?: Map<
string,
RsethPointItemWithBalance[] | RsethPointItemWithoutBalance[]
>;
items: RsethPointItemWithBalance[] | RsethPointItemWithoutBalance[];
}

Expand All @@ -50,6 +53,7 @@ export class RsethService extends Worker {
localTotalPoints: BigInt(0),
realTotalElPoints: 0,
realTotalKelpMiles: 0,
itemMaps: new Map(),
items: [],
};
private readonly l1Erc20BridgeEthereum: string;
Expand Down Expand Up @@ -188,11 +192,23 @@ export class RsethService extends Worker {
};
data.push(pointsItem);
}

const itemMaps = new Map();
for (let i = 0; i < data.length; i++) {
const item = data[i];
if (!itemMaps.has(item.address)) {
itemMaps.set(item.address, [item]);
} else {
const tmpItems = itemMaps.get(item.address);
itemMaps.set(item.address, [...tmpItems, item]);
}
}
if (data.length > 0) {
this.rsethData = {
localTotalPoints: localTotalPoints,
realTotalElPoints: realTotalElPoints,
realTotalKelpMiles: realTotalKelpMiles,
itemMaps: itemMaps,
items: data,
};
} else {
Expand All @@ -202,14 +218,14 @@ export class RsethService extends Worker {

// return points data
public getPointsData(address?: string): RsethData {
const result: RsethData = cloneDeep(this.rsethData);
if (address) {
const _address = address.toLocaleLowerCase();
result.items = this.rsethData.items.filter(
(item) => item.address === _address,
);
}
return result;
return {
localTotalPoints: this.rsethData.localTotalPoints,
realTotalElPoints: this.rsethData.realTotalElPoints,
realTotalKelpMiles: this.rsethData.realTotalKelpMiles,
items: address
? this.rsethData.itemMaps.get(address) ?? []
: this.rsethData.items,
};
}

// return local points and totalPoints
Expand All @@ -224,7 +240,6 @@ export class RsethService extends Worker {

// return real points group by address
public getPointsDataGroupByAddress(): RsethData {
const result: RsethData = cloneDeep(this.rsethData);
const data: Map<string, RsethPointItemWithoutBalance> = new Map();
const now = (new Date().getTime() / 1000) | 0;
for (let i = 0; i < this.rsethData.items.length; i++) {
Expand All @@ -242,8 +257,12 @@ export class RsethService extends Worker {
tmpItem.realElPoints += item.realElPoints;
}
}
result.items = Array.from(data.values());
return result;
return {
localTotalPoints: this.rsethData.localTotalPoints,
realTotalElPoints: this.rsethData.realTotalElPoints,
realTotalKelpMiles: this.rsethData.realTotalKelpMiles,
items: Array.from(data.values()),
};
}

// token match bridge token
Expand Down

0 comments on commit 288d7c1

Please sign in to comment.