Skip to content

Commit

Permalink
Merge pull request #44 from zkLinkProtocol/feat-transfer-faild-v2
Browse files Browse the repository at this point in the history
1. filled puffer/magpie/rseth faild transfers;
2. remove layerbank and aqua pool contract;
3. points added aqua;
  • Loading branch information
robert-zklink authored May 6, 2024
2 parents 7b2ea8d + 79d8f17 commit 8a6f88f
Show file tree
Hide file tree
Showing 16 changed files with 496 additions and 124 deletions.
6 changes: 6 additions & 0 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,12 @@ import { BlockAddressPointOfLp } from "./entities/blockAddressPointOfLp.entity";
import { BlockAddressPointOfLpRepository } from "./repositories/blockAddressPointOfLp.repository";
import { Cache } from "./entities/cache.entity";
import { CacheRepository } from "./repositories/cache.repository";
import { BalanceOfLp } from "./entities/balanceOfLp.entity";
import { BalanceOfLpRepository } from "./repositories/balanceOfLp.repository";
import { NovaBalanceService } from "./nova/nova.balance.service";
import { CacheController } from "./cache/cache.controller";
import { CacheService } from "./cache/cache.service";
import { AquaService } from "./nova/aqua.service";

@Module({
imports: [
Expand All @@ -60,6 +63,7 @@ import { CacheService } from "./cache/cache.service";
PointsOfLp,
BlockAddressPointOfLp,
Cache,
BalanceOfLp,
]),
MetricsModule,
UnitOfWorkModule,
Expand Down Expand Up @@ -100,8 +104,10 @@ import { CacheService } from "./cache/cache.service";
ProjectRepository,
PointsOfLpRepository,
BlockAddressPointOfLpRepository,
BalanceOfLpRepository,
CacheRepository,
CacheService,
AquaService,
],
})
export class AppModule {}
4 changes: 2 additions & 2 deletions src/common/service/projectGraph.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import {
GraphPoint,
GraphQueryService,
GraphTotalPoint,
} from "src/common/service/graphQuery.service";
} from "./graphQuery.service";
import { BigNumber } from "bignumber.js";
import { WithdrawService } from "src/common/service/withdraw.service";
import { WithdrawService } from "./withdraw.service";
import transferFaildData from "../transferFaild.json";

export interface LocalPointsItem {
Expand Down
10 changes: 6 additions & 4 deletions src/common/service/withdraw.service.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import { Injectable, Logger, OnModuleInit } from "@nestjs/common";
import {
GraphQueryService,
GraphWithdrawPoint,
} from "src/common/service/graphQuery.service";
import { GraphQueryService, GraphWithdrawPoint } from "./graphQuery.service";

export interface WithdrawPoint {
project: string;
Expand Down Expand Up @@ -56,6 +53,11 @@ const timeToPeriod = [
end: 1714233600, // 2024-04-28 00:00:00
period: 7 * 24 * 3600,
},
{
start: 1714147200, // 2024-04-28 00:00:00
end: 2029766400, // 2034-04-28 00:00:00
period: 7 * 24 * 3600,
},
];

@Injectable()
Expand Down
80 changes: 54 additions & 26 deletions src/common/transferFaild.json.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,35 @@
// address, tokenAddress, balance, decimal
const transferFaildData = [
// puffer
[
"0x590f4590487Bd83D7A456b5DD2863810A076D1eC",
"0x1B49eCf1A8323Db4abf48b2F5EFaA33F7DdAB3FC",
"0.04",
"18",
],
[
"0xF74BAC410bcF3c55af7A9A0CAef8F9c83087a3A4",
"0xdA7Fa837112511F6E353091D7e388A4c45Ce7D6C",
"1.85",
"0x04c82fFd5dbA2ac0e00dD1F9Be955bd025Dec048",
"0x1B49eCf1A8323Db4abf48b2F5EFaA33F7DdAB3FC",
"0.9992",
"18",
],
[
"0xB84F419FD6DC9C30cCAA6ecbF5d194Af065A33ff",
"0x7b1fcd81F8b91C5eF3743c4d56bf7C1E52c93360",
"1.0499",
"0x63c43a9879AAac04127CB84105a5f2a3F9Bbba17",
"0x1B49eCf1A8323Db4abf48b2F5EFaA33F7DdAB3FC",
"0.489",
"18",
],
[
"0x04c82fFd5dbA2ac0e00dD1F9Be955bd025Dec048",
"0xE26E90e8F6f1170fda099B771e483cD30d5ba2Af",
"0x1B49eCf1A8323Db4abf48b2F5EFaA33F7DdAB3FC",
"0.9992",
"0.489",
"18",
],
// magpie
[
"0xB84F419FD6DC9C30cCAA6ecbF5d194Af065A33ff",
"0x7b1fcd81F8b91C5eF3743c4d56bf7C1E52c93360",
"1.0499",
"18",
],
[
Expand All @@ -31,15 +39,47 @@ const transferFaildData = [
"18",
],
[
"0x63c43a9879AAac04127CB84105a5f2a3F9Bbba17",
"0x1B49eCf1A8323Db4abf48b2F5EFaA33F7DdAB3FC",
"0.489",
"0x90fe3e64530B81D5613698cBAA5d99E8AebaDc69",
"0x7F62B7a0A9848D5e261960Ff4B4009206aD00bd5",
"19.9966",
"18",
],
[
"0xE26E90e8F6f1170fda099B771e483cD30d5ba2Af",
"0x1B49eCf1A8323Db4abf48b2F5EFaA33F7DdAB3FC",
"0.489",
"0xB84F419FD6DC9C30cCAA6ecbF5d194Af065A33ff",
"0xBB68f4548A1c26B6611cbB8087c25A616eDd8569",
"0.572",
"18",
],
// rseth
[
"0xbd2ca315098e13e91f9a91a4744052d62c8e9092",
"0x4A2da287deB06163fB4D77c52901683d69bD06f4",
"0.25",
"18",
],
[
"0x9f90230b05a8f3802923038eb99538000fc5a1cc",
"0x4A2da287deB06163fB4D77c52901683d69bD06f4",
"0.25055",
"18",
],
[
"0x2c443310faa7d98c95d3d44e7e43965ef74e184d",
"0x4A2da287deB06163fB4D77c52901683d69bD06f4",
"0.25",
"18",
],
[
"0x137eb1280e77ea4bec921644eb575e87356a474e",
"0x4A2da287deB06163fB4D77c52901683d69bD06f4",
"0.25065",
"18",
],
// renzo
[
"0xF74BAC410bcF3c55af7A9A0CAef8F9c83087a3A4",
"0xdA7Fa837112511F6E353091D7e388A4c45Ce7D6C",
"1.85",
"18",
],
[
Expand All @@ -66,12 +106,6 @@ const transferFaildData = [
"0.2375",
"18",
],
[
"0xbd2ca315098e13e91f9a91a4744052d62c8e9092",
"0x4A2da287deB06163fB4D77c52901683d69bD06f4",
"0.25",
"18",
],
[
"0x75411b249de43f61f138eedf775f2f5df1509ba1",
"0x3FDB1939daB8e2d4F7a04212F142469Cd52d6402",
Expand All @@ -96,12 +130,6 @@ const transferFaildData = [
"0.2463",
"18",
],
[
"0x9f90230b05a8f3802923038eb99538000fc5a1cc",
"0x4A2da287deB06163fB4D77c52901683d69bD06f4",
"0.25055",
"18",
],
[
"0x6b16602eb27141bd32b8b26402ea2e580637ae48",
"0x3DabBd8A31a411E85f628278d6601fCeD82f6844",
Expand Down
23 changes: 23 additions & 0 deletions src/entities/balanceOfLp.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { Entity, Column, PrimaryColumn, Index } from "typeorm";
import { BaseEntity } from "./base.entity";
import { bigIntNumberTransformer } from "../transformers/bigIntNumber.transformer";
import { hexTransformer } from "../transformers/hex.transformer";

@Entity({ name: "balancesOfLp" })
@Index(["blockNumber", "balance"])
export class BalanceOfLp extends BaseEntity {
@PrimaryColumn({ type: "bytea", transformer: hexTransformer })
public readonly address: string;

@PrimaryColumn({ type: "bytea", transformer: hexTransformer })
public readonly tokenAddress: string;

@PrimaryColumn({ type: "bytea", transformer: hexTransformer })
public readonly pairAddress: string;

@PrimaryColumn({ type: "bigint", transformer: bigIntNumberTransformer })
public readonly blockNumber: number;

@Column({ type: "varchar", length: 50 })
public readonly balance: string;
}
55 changes: 54 additions & 1 deletion src/magpie/magpie.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
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 {
LocalPointData,
ProjectGraphService,
Expand Down Expand Up @@ -40,6 +42,7 @@ export interface MagpieData {
export class MagpieService {
private readonly projectName: string = "magpie";
private readonly logger: Logger;
public tokenAddress: string[];

private magpieData: MagpieData = {
localTotalPoints: 0n,
Expand All @@ -49,6 +52,7 @@ export class MagpieService {
};

public constructor(
private readonly graphQueryService: GraphQueryService,
private readonly projectGraphService: ProjectGraphService,
private readonly magpieGraphQueryService: MagpieGraphQueryService,
) {
Expand All @@ -70,13 +74,62 @@ export class MagpieService {

// load points data
public async loadPointsData() {
// get tokens from graph
const tokens = this.graphQueryService.getAllTokenAddresses(
this.projectName,
);
if (tokens.length <= 0) {
this.logger.log(`Graph don't have ${this.projectName} tokens`);
return;
}
this.tokenAddress = tokens;

const realTotalPointsData = await this.getRealPointsData();
const localPointsData = await this.getLocalPointsData();
const localPoints = localPointsData.localPoints;
const localTotalPoints = localPointsData.localTotalPoints;

let data: MagpiePointItemWithBalance[] = [];
// start added transferFaildPoint
const transferFaildPoints = this.projectGraphService.getTransferFaildPoints(
this.tokenAddress,
);
const localPointsMap = new Map<string, LocalPointsItem>();
const totalPointsPerTokenMap = new Map<string, bigint>();
const now = (new Date().getTime() / 1000) | 0;
for (const item of localPoints) {
const key = `${item.address}_${item.token}`;
totalPointsPerTokenMap.set(item.token, item.totalPointsPerToken);
localPointsMap.set(key, item);
}
// loop transferFaildData, and added transferFaildPoint to localPoints
for (const item of transferFaildPoints) {
const key = `${item.address}_${item.tokenAddress}`;
const transferFaildTotalPoint =
this.projectGraphService.getTransferFaildTotalPoint(item.tokenAddress);
if (!localPointsMap.has(key)) {
const tmpTotalPointsPerToken =
totalPointsPerTokenMap.get(item.tokenAddress) ?? BigInt(0);
localPointsMap.set(key, {
address: item.address,
points: item.points,
withdrawPoints: BigInt(0),
withdrawTotalPointsPerToken: BigInt(0),
totalPointsPerToken: tmpTotalPointsPerToken + transferFaildTotalPoint,
balance: BigInt(0),
token: item.tokenAddress,
updatedAt: now,
});
} else {
const localPoint = localPointsMap.get(key);
localPoint.totalPointsPerToken =
localPoint.totalPointsPerToken + transferFaildTotalPoint;
localPoint.points = localPoint.points + item.points;
}
}
// end added transferFaildPoint

let data: MagpiePointItemWithBalance[] = [];
for (const [, item] of localPointsMap) {
const realEigenpiePoints =
(BigInt(item.points) * BigInt(realTotalPointsData.eigenpiePoints)) /
BigInt(localTotalPoints);
Expand Down
45 changes: 45 additions & 0 deletions src/nova/aqua.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { Injectable, Logger } from "@nestjs/common";
import { GraphQueryService } from "src/common/service/graphQuery.service";
import { NovaService, PointData } from "src/nova/nova.service";

@Injectable()
export class AquaService {
private readonly logger: Logger;
private readonly projectName: string = "aqua";

public constructor(
private graphQueryService: GraphQueryService,
private novaService: NovaService,
) {
this.logger = new Logger(NovaService.name);
}

public async getPoints(
tokenAddress: string,
addresses: string[],
): Promise<PointData> {
const finalPoints = [];
const finalTotalPoints = BigInt(0);
const projects = this.graphQueryService.getAllProjectIds(this.projectName);
const project = `${this.projectName}-${tokenAddress}`;
if (!projects.includes(project)) {
this.logger.error(`Notfound GraphQL data, project is : ${project} .`);
return { finalPoints, finalTotalPoints };
}

const [points, totalPoints] =
await this.graphQueryService.queryPointsRedistributedByAddress(
addresses,
project,
);

if (Array.isArray(points) && totalPoints) {
return this.novaService.getPointData(points, totalPoints);
} else {
// Exception in fetching GraphQL data.
throw new Error(
`Exception in fetching GraphQL data, project is : ${project}.`,
);
}
}
}
31 changes: 31 additions & 0 deletions src/nova/nova.balance.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { ProjectRepository } from "src/repositories/project.repository";
import { PointsOfLpRepository } from "src/repositories/pointsOfLp.repository";
import { PointsOfLp } from "src/entities/pointsOfLp.entity";
import { BlockAddressPointOfLpRepository } from "src/repositories/blockAddressPointOfLp.repository";
import { BalanceOfLpRepository } from "src/repositories/balanceOfLp.repository";

interface ProjectPoints {
name: string;
Expand All @@ -23,6 +24,7 @@ export class NovaBalanceService {
private readonly projectRepository: ProjectRepository,
private readonly pointsOfLpRepository: PointsOfLpRepository,
private readonly blockAddressPointOfLpRepository: BlockAddressPointOfLpRepository,
private readonly balanceOfLp: BalanceOfLpRepository,
) {
this.logger = new Logger(NovaBalanceService.name);
}
Expand Down Expand Up @@ -174,4 +176,33 @@ export class NovaBalanceService {
yesterdayEndStr,
);
}

/**
* Get the balance of the address in the pair by blockNumber
*/
public async getBalanceByBlockNumber(
addresses: string[],
tokenAddress: string,
pairAddress: string,
blockNumber: number = 0,
): Promise<bigint> {
// if blockNumber is 0, get the latest balance
let data;
if (blockNumber == 0) {
data = await this.balanceOfLp.getLastList(
addresses,
tokenAddress,
pairAddress,
);
} else {
data = await this.balanceOfLp.getListByBlockNumber(
addresses,
tokenAddress,
pairAddress,
blockNumber,
);
}

return data ? BigInt(data.balance) : BigInt(0);
}
}
Loading

0 comments on commit 8a6f88f

Please sign in to comment.