Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: copy indexer deposits data to scraper db #506

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
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
15 changes: 13 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
version: "3.7"

networks:
test-network:
external: true

Comment on lines +3 to +6
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This network can be renamed.
I still have to check that both the indexer and scraper work fine using this network.
Indexer draft PR configuring the network: across-protocol/indexer#142

services:
backend:
container_name: scraper-api-backend
Expand All @@ -18,6 +22,8 @@ services:
depends_on:
- postgres
- redis
networks:
- test-network
postgres:
container_name: scraper-api-postgres
image: postgres:12.2
Expand All @@ -27,21 +33,26 @@ services:
POSTGRES_PASSWORD: ${DB_PASSWORD}
POSTGRES_DB: ${DB_DATABASE_NAME}
PG_DATA: /var/lib/postgresql/data
command: ["postgres", "-c", "log_statement=all"]
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just to debug the auth issue I am having. Can be deleted after

ports:
- 5432:5432
- 5433:5432
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is needed to avoid port collisions between indexer and scraper

volumes:
- scraper-api-pgdata:/var/lib/postgresql/data
networks:
- test-network
redis:
container_name: scraper-api-redis
image: redis:6.2-alpine
restart: always
ports:
- 6379:6379
- 6378:6379
command: ["redis-server", "--appendonly", "yes"]
environment:
REDIS_PASSWORD: ${REDIS_PASSWORD}
volumes:
- scraper-api-redis-volume:/data
networks:
- test-network
volumes:
backend-node-modules:
scraper-api-pgdata:
Expand Down
35 changes: 35 additions & 0 deletions migrations/1735003840563-rewardedDeposit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { MigrationInterface, QueryRunner } from "typeorm";

export class RewardedDeposit1735003840563 implements MigrationInterface {
name = 'RewardedDeposit1735003840563';

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`
CREATE TABLE "rewarded_deposit" (
"id" SERIAL NOT NULL,
"relayHash" character varying NOT NULL,
"depositTxHash" character varying NOT NULL,
"depositId" integer NOT NULL,
"originChainId" integer NOT NULL,
"destinationChainId" integer NOT NULL,
"depositor" character varying NOT NULL,
"recipient" character varying NOT NULL,
"inputToken" character varying NOT NULL,
"inputAmount" character varying NOT NULL,
"outputToken" character varying NOT NULL,
"outputAmount" character varying NOT NULL,
"exclusiveRelayer" character varying NOT NULL,
"depositDate" TIMESTAMP NOT NULL,
"fillTxHash" character varying NOT NULL,
"relayer" character varying NOT NULL,
"totalBridgeFeeUsd" character varying NOT NULL,
"createdAt" TIMESTAMP NOT NULL DEFAULT now(),
CONSTRAINT "UK_rewardedDeposit_depositId_originChainId" UNIQUE ("depositId", "originChainId"),
CONSTRAINT "PK_891e96cd023b61f620566c0898a" PRIMARY KEY ("id"))`);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`DROP TABLE "rewarded_deposit"`);
}

}
38 changes: 38 additions & 0 deletions migrations/1735659642491-OpRewardV2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { MigrationInterface, QueryRunner } from "typeorm";

export class OpRewardV21735659642491 implements MigrationInterface {
name = 'OpRewardV21735659642491';

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`CREATE TABLE "op_reward_v2" (
"id" SERIAL NOT NULL,
"depositId" integer NOT NULL,
"originChainId" integer NOT NULL,
"depositDate" TIMESTAMP NOT NULL,
"recipient" character varying NOT NULL,
"rate" numeric NOT NULL,
"amount" character varying NOT NULL,
"amountUsd" character varying NOT NULL,
"rewardTokenId" integer NOT NULL,
"windowIndex" integer,
"isClaimed" boolean NOT NULL DEFAULT false,
"createdAt" TIMESTAMP NOT NULL DEFAULT now(),
"updatedAt" TIMESTAMP NOT NULL DEFAULT now(),
CONSTRAINT "UK_opRewardV2_recipient_depId_chainId" UNIQUE ("recipient", "depositId", "originChainId"),
CONSTRAINT "REL_f057b9c78adeb22f4f4fb69ea8" UNIQUE ("depositId", "originChainId"),
CONSTRAINT "PK_fc4c022f3de3d4170ae8200c76b" PRIMARY KEY ("id"))
`,
);
await queryRunner.query(`CREATE INDEX "IX_op_reward_v2_recipient" ON "op_reward_v2" ("recipient") `);
await queryRunner.query(`ALTER TABLE "op_reward_v2" ADD CONSTRAINT "FK_op_reward_deposit" FOREIGN KEY ("depositId", "originChainId") REFERENCES "rewarded_deposit"("depositId","originChainId") ON DELETE NO ACTION ON UPDATE NO ACTION`);
await queryRunner.query(`ALTER TABLE "op_reward_v2" ADD CONSTRAINT "FK_op_reward_token" FOREIGN KEY ("rewardTokenId") REFERENCES "token"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE "op_reward_v2" DROP CONSTRAINT "FK_op_reward_token"`);
await queryRunner.query(`ALTER TABLE "op_reward_v2" DROP CONSTRAINT "FK_op_reward_deposit"`);
await queryRunner.query(`DROP INDEX "public"."IX_op_reward_v2_recipient"`);
await queryRunner.query(`DROP TABLE "op_reward_v2"`);
}

}
4 changes: 4 additions & 0 deletions src/modules/database/database.providers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ import { ArbReward } from "../rewards/model/arb-reward.entity";
import { FindMissedFillEventJob } from "../scraper/model/FindMissedFillEventJob.entity";
import { HubPoolProcessedBlock } from "../scraper/model/HubPoolProcessedBlock.entity";
import { SetPoolRebalanceRouteEvent } from "../web3/model/SetPoolRebalanceRouteEvent.entity";
import { RewardedDeposit } from "../rewards/model/RewardedDeposit.entity";
import { OpRewardV2 } from "../rewards/model/OpRewardV2.entity";

// TODO: Add db entities here
const entities = [
Expand Down Expand Up @@ -66,6 +68,8 @@ const entities = [
FindMissedFillEventJob,
HubPoolProcessedBlock,
SetPoolRebalanceRouteEvent,
RewardedDeposit,
OpRewardV2,
];

@Injectable()
Expand Down
70 changes: 70 additions & 0 deletions src/modules/rewards/model/OpRewardV2.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { Token } from "../../web3/model/token.entity";
import {
Column,
CreateDateColumn,
Entity,
Index,
JoinColumn,
ManyToOne,
OneToOne,
PrimaryGeneratedColumn,
Unique,
UpdateDateColumn,
} from "typeorm";
import { RewardedDeposit } from "./RewardedDeposit.entity";

@Entity()
@Unique("UK_opRewardV2_recipient_depId_chainId", ["recipient", "depositId", "originChainId"])
@Index("IX_op_reward_v2_recipient", ["recipient"])
export class OpRewardV2 {
@PrimaryGeneratedColumn()
id: number;

@Column()
depositId: number;

@Column()
originChainId: number;

@OneToOne(() => RewardedDeposit)
@JoinColumn([
{ name: "depositId", referencedColumnName: "depositId", foreignKeyConstraintName: "FK_op_reward_deposit" },
{ name: "originChainId", referencedColumnName: "originChainId", foreignKeyConstraintName: "FK_op_reward_deposit" },
])
deposit: RewardedDeposit;
Comment on lines +29 to +34
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This relation used to be ManyToOne, but I think it's ok to set it to OneToOne.


@Column()
depositDate: Date;

@Column()
recipient: string;

@Column({ type: "decimal" })
rate: string;

@Column()
amount: string;

@Column()
amountUsd: string;

@Column()
rewardTokenId: number;

@ManyToOne(() => Token)
@JoinColumn([{ name: "rewardTokenId", referencedColumnName: "id", foreignKeyConstraintName: "FK_op_reward_token" }])
rewardToken: Token;

// The window index set in the MerkleDistributor contract
@Column({ nullable: true })
windowIndex: number;

@Column({ default: false })
isClaimed: boolean;

@CreateDateColumn()
createdAt: Date;

@UpdateDateColumn()
updatedAt: Date;
}
68 changes: 68 additions & 0 deletions src/modules/rewards/model/RewardedDeposit.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import {
Column,
CreateDateColumn,
Entity,
PrimaryGeneratedColumn,
Unique,
} from "typeorm";

@Entity()
@Unique("UK_rewardedDeposit_depositId_originChainId", [
"depositId",
"originChainId",
])
export class RewardedDeposit {
@PrimaryGeneratedColumn()
id: number;

@Column()
relayHash: string;

@Column()
depositTxHash: string;

@Column()
depositId: number;

@Column()
originChainId: number;

@Column()
destinationChainId: number;

@Column()
depositor: string;

@Column()
recipient: string;

@Column()
inputToken: string;

@Column()
inputAmount: string;

@Column()
outputToken: string;

@Column()
outputAmount: string;

@Column()
exclusiveRelayer: string;

@Column()
depositDate: Date;

@Column()
fillTxHash: string;

@Column()
relayer: string;

@Column()
totalBridgeFeeUsd: string;

@CreateDateColumn()
createdAt: Date;
}
11 changes: 9 additions & 2 deletions src/modules/rewards/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,26 @@ import { RewardsWindowJobFixture } from "./adapter/db/rewards-window-job-fixture
import { ArbRebateService } from "./services/arb-rebate-service";
import { ArbReward } from "./model/arb-reward.entity";
import { ArbRewardFixture } from "./adapter/db/arb-reward-fixture";
import { RewardedDeposit } from "./model/RewardedDeposit.entity";
import { OpRewardV2 } from "./model/OpRewardV2.entity";
import { OpRebateServiceV2 } from "./services/opRebateServiceV2";

@Module({})
export class RewardModule {
static forRoot(moduleOptions: ModuleOptions): DynamicModule {
const module: DynamicModule = {
module: RewardModule,
controllers: [RewardController],
providers: [RewardService, OpRebateService, ReferralService, ReferralRewardsService, ArbRebateService],
providers: [
ArbRebateService, OpRebateService, OpRebateServiceV2, ReferralService, ReferralRewardsService, RewardService,
],
imports: [
TypeOrmModule.forFeature([
Deposit,
OpReward,
OpRewardV2,
DepositsMv,
RewardedDeposit,
RewardsWindowJob,
ReferralRewardsWindowJobResult,
ArbReward,
Expand All @@ -44,7 +51,7 @@ export class RewardModule {
ReferralModule.forRoot(moduleOptions),
MarketPriceModule.forRoot(),
],
exports: [RewardService, OpRebateService, ReferralService, ArbRebateService],
exports: [ArbRebateService, OpRebateService, OpRebateServiceV2, ReferralService, RewardService],
};

if (moduleOptions.runModes.includes(RunMode.Test)) {
Expand Down
2 changes: 1 addition & 1 deletion src/modules/rewards/services/op-rebate-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { ReferralRewardsWindowJobResult } from "../model/RewardsWindowJobResult.

const OP_REBATE_RATE = 0.95;
const REWARDS_PERCENTAGE_LIMIT = 0.0025; // 25 bps
const ELIGIBLE_OP_REWARDS_CHAIN_IDS = [
export const ELIGIBLE_OP_REWARDS_CHAIN_IDS = [
ChainIds.base,
ChainIds.lisk,
ChainIds.mode,
Expand Down
Loading
Loading