Skip to content

Commit

Permalink
Shijiang/l1l2relation l1l2api (blockscout#88)
Browse files Browse the repository at this point in the history
* l1 and l2 message deocde to db

* change data logic and l1l2 api
  • Loading branch information
guoshijiang committed Jan 12, 2023
1 parent 9890b5a commit b1d6731
Show file tree
Hide file tree
Showing 17 changed files with 538 additions and 36 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
defmodule Explorer.Repo.Migrations.AddL1l2StatusToTransactions do
use Ecto.Migration

def change do
alter table(:transactions) do
add(:l1l2_l1_token, :bytea, null: true)
add(:l1l2_l2_token, :bytea, null: true)
add(:l1l2_status, :integer, null: false, default: 0) # 0: wait transaction; 1: ready for Relay transaction; 2: Relayed transaction
add(:l1l2_type, :integer, null: false, default: 0) # 0: normal transaction; 1: deposit transaction; 2: withdraw
add(:l1l2_from, :bytea, null: true)
add(:l1l2_to, :bytea, null: true)
add(:l1l2_value, :numeric, precision: 100, null: false, default: 0)
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
defmodule Explorer.Repo.Migrations.AddL1MessageToL1SentMessageEvents do
use Ecto.Migration

def change do
alter table(:l1_sent_message_events) do
add(:l1_token, :bytea, null: true)
add(:l2_token, :bytea, null: true)
add(:from, :bytea, null: true)
add(:to, :bytea, null: true)
add(:type, :integer, null: false, default: 0) # 0: reward; 1: deposit
add(:value, :numeric, precision: 100, null: false, default: 0)
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
defmodule Explorer.Repo.Migrations.AddL2MessageToL2SentMessageEvents do
use Ecto.Migration

def change do
alter table(:l2_sent_message_events) do
add(:l1_token, :bytea, null: true)
add(:l2_token, :bytea, null: true)
add(:from, :bytea, null: true)
add(:to, :bytea, null: true)
add(:value, :numeric, precision: 100, null: false, default: 0)
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
defmodule Explorer.Repo.Migrations.AddL2MessageToL2ToL1 do
use Ecto.Migration

def change do
alter table(:l2_to_l1) do
add(:name, :string, null: true)
add(:symbol, :string, null: true)
add(:l1_token, :bytea, null: true)
add(:l2_token, :bytea, null: true)
add(:from, :bytea, null: true)
add(:to, :bytea, null: true)
add(:value, :numeric, precision: 100, null: false, default: 0)
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
defmodule Explorer.Repo.Migrations.AddL1MessageToL1ToL2 do
use Ecto.Migration

def change do
alter table(:l1_to_l2) do
add(:name, :string, null: true)
add(:symbol, :string, null: true)
add(:l1_token, :bytea, null: true)
add(:l2_token, :bytea, null: true)
add(:from, :bytea, null: true)
add(:to, :bytea, null: true)
add(:type, :integer, null: false, default: 0) # 0: reward; 1: deposit
add(:value, :numeric, precision: 100, null: false, default: 0)
end
end
end
3 changes: 2 additions & 1 deletion data-sync-service/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
"class-transformer": "^0.5.1",
"class-validator": "^0.13.2",
"dotenv": "^16.0.0",
"ethers": "^5.7.0",
"ethereumjs-util": "^7.1.5",
"ethers": "^5.7.2",
"mysql2": "^2.3.3",
"nest-status-monitor": "^0.1.4",
"pg": "^8.7.3",
Expand Down
49 changes: 48 additions & 1 deletion data-sync-service/src/l1Ingestion/l1Ingestion.controller.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { L1IngestionService } from './l1Ingestion.service';
import { Controller, Get } from '@nestjs/common';
import { Controller, Get, Post, Body } from '@nestjs/common';
import { isValidAddress, publicToAddress } from 'ethereumjs-util';

@Controller('/')
export class L1IngestionController {
Expand All @@ -13,4 +14,50 @@ export class L1IngestionController {
getL2ToL1Relation() {
return this.l1IngestionService.getL2ToL1Relation();
}
@Post('l1l2_transaction')
getL1L2Transaction(@Body() param) {
const address = param['address'];
if (!isValidAddress(address)) {
return { ok: false, code: 4000, result: 'invalid address' };
}
const page = param['page'];
const page_size = param['page_size'];
if (Number(page) <= 0 || Number(page_size) <= 0) {
return {
ok: false,
code: 4000,
result: 'page and page_size must more than 0',
};
}
if (Number(page_size) > 1000) {
return {
ok: false,
code: 4000,
result: 'page_size must less than 1000',
};
}
const type = param['type'];
if (Number(type) < 0 || Number(type) > 2) {
return {
ok: false,
code: 4000,
result: 'invalid transaction type',
};
}
const order_by = param['order_by'];
if (String(order_by) != 'DESC' && String(order_by) != 'ASC') {
return {
ok: false,
code: 4000,
result: 'invalid order type',
};
}
return this.l1IngestionService.getL1L2Transaction(
address,
page,
page_size,
type,
order_by,
);
}
}
2 changes: 2 additions & 0 deletions data-sync-service/src/l1Ingestion/l1Ingestion.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
L2ToL1,
L1ToL2,
Transactions,
Tokens,
} from 'src/typeorm';

@Module({
Expand All @@ -24,6 +25,7 @@ import {
L2ToL1,
L1ToL2,
Transactions,
Tokens,
]),
],
controllers: [L1IngestionController],
Expand Down
Loading

0 comments on commit b1d6731

Please sign in to comment.