Skip to content

Commit

Permalink
sync 로직
Browse files Browse the repository at this point in the history
  • Loading branch information
jewerlykim committed Jun 2, 2023
1 parent 9ce97c5 commit df4d7c8
Show file tree
Hide file tree
Showing 5 changed files with 593 additions and 12 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"cache-manager": "^5.2.1",
"class-transformer": "^0.5.1",
"class-validator": "^0.14.0",
"ethers": "5.7.2",
"pg": "8.10.0",
"reflect-metadata": "^0.1.13",
"rxjs": "^7.8.1",
Expand Down
3 changes: 3 additions & 0 deletions src/common/database/entities/transaction.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,7 @@ export class Transaction {

@Column()
hash: string;

@Column()
blockNumber: number;
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { MigrationInterface, QueryRunner } from "typeorm";

export class InitializeTable1685712599802 implements MigrationInterface {
name = 'InitializeTable1685712599802'
export class InitializeTable1685719722511 implements MigrationInterface {
name = 'InitializeTable1685719722511'

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`CREATE TABLE "wallet" ("id" SERIAL NOT NULL, "walletAddress" character varying NOT NULL, "transactionNum" integer NOT NULL, CONSTRAINT "PK_bec464dd8d54c39c54fd32e2334" PRIMARY KEY ("id"))`);
await queryRunner.query(`CREATE TABLE "transaction" ("id" SERIAL NOT NULL, "walletId" integer NOT NULL, "protocolId" integer NOT NULL, "timestamp" integer NOT NULL, "eventName" character varying NOT NULL, "totalValue" integer NOT NULL, "coinValue" integer NOT NULL, "tokenValue" integer NOT NULL, "hash" character varying NOT NULL, CONSTRAINT "PK_89eadb93a89810556e1cbcd6ab9" PRIMARY KEY ("id"))`);
await queryRunner.query(`CREATE TABLE "transaction" ("id" SERIAL NOT NULL, "walletId" integer NOT NULL, "protocolId" integer NOT NULL, "timestamp" integer NOT NULL, "eventName" character varying NOT NULL, "totalValue" integer NOT NULL, "coinValue" integer NOT NULL, "tokenValue" integer NOT NULL, "hash" character varying NOT NULL, "blockNumber" integer NOT NULL, CONSTRAINT "PK_89eadb93a89810556e1cbcd6ab9" PRIMARY KEY ("id"))`);
await queryRunner.query(`CREATE TABLE "protocol" ("id" SERIAL NOT NULL, "protocolAddress" character varying NOT NULL, "protocolName" character varying NOT NULL, "protocolType" character varying NOT NULL, CONSTRAINT "PK_bae34901abddccbddda15ea000c" PRIMARY KEY ("id"))`);
await queryRunner.query(`ALTER TABLE "transaction" ADD CONSTRAINT "FK_900eb6b5efaecf57343e4c0e79d" FOREIGN KEY ("walletId") REFERENCES "wallet"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`);
await queryRunner.query(`ALTER TABLE "transaction" ADD CONSTRAINT "FK_20aecaef95140a727ea90ec0f9f" FOREIGN KEY ("protocolId") REFERENCES "protocol"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`);
Expand Down
137 changes: 128 additions & 9 deletions src/external-api/etherscan/etherscan-api.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,13 @@ import { InjectRepository } from "@nestjs/typeorm";
import { Repository } from "typeorm";
import { Wallet } from "@common/database/entities/wallet.entity";
import { Protocol } from "@common/database/entities/protocol.entity";
import { ethers } from "ethers";

@Injectable()
export class EtherscanApiService {
baseUrl = this.configService.get<string>("EtherscanApi.url");
apiKey = this.configService.get<string>("EtherscanApi.apiKey");

constructor(
private httpService: HttpService,
private configService: ConfigService,
Expand All @@ -21,12 +25,18 @@ export class EtherscanApiService {
private walletRepository: Repository<Wallet>,
@InjectRepository(Protocol)
private protocolRepository: Repository<Protocol>,
) {}
) {
const check = async () => {
// const isSynced = await this.syncTransactions("0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D");
// console.log("isSynced", isSynced);
};
check();
}

async getTxList() {
const result: TransactionDto[] = [];
const baseUrl = this.configService.get<string>("EtherscanApi.url");
const apiKey = this.configService.get<string>("EtherscanApi.apiKey");
// const baseUrl = this.configService.get<string>("EtherscanApi.url");
// const apiKey = this.configService.get<string>("EtherscanApi.apiKey");

const request: EtherScanTxListRequest = {
address: "0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D",
Expand All @@ -35,10 +45,10 @@ export class EtherscanApiService {
page: "1",
offset: "10",
sort: "desc",
apikey: apiKey,
apikey: this.apiKey,
};

const url = `${baseUrl}&address=${request.address}&startblock=${request.startblock}&endblock=${request.endblock}&page=${request.page}&offset=${request.offset}&sort=${request.sort}&apikey=${request.apikey}`;
const url = `${this.baseUrl}&address=${request.address}&startblock=${request.startblock}&endblock=${request.endblock}&page=${request.page}&offset=${request.offset}&sort=${request.sort}&apikey=${request.apikey}`;

const { data } = await firstValueFrom(this.httpService.get<EtherScanResponse>(url).pipe());

Expand All @@ -55,8 +65,8 @@ export class EtherscanApiService {
return result;
}

async createTransaction(transactionDto: TransactionDto): Promise<Boolean> {
const { from: walletAddress, to: protocolAddress, timeStamp, value, hash } = transactionDto;
async createTransaction(transactionDto: TransactionDto): Promise<boolean> {
const { from: walletAddress, to: protocolAddress, timeStamp, value, hash, blockNumber } = transactionDto;
try {
console.log("createTransaction", JSON.stringify(transactionDto));
let wallet = await this.walletRepository.findOne({ where: { walletAddress } });
Expand Down Expand Up @@ -96,11 +106,10 @@ export class EtherscanApiService {
totalValue: 0,
coinValue: 0,
tokenValue: 0,
blockNumber: Number(blockNumber),
});
}

// wallet.transactionNum++;
// await this.walletRepository.save(wallet);
return true;
} catch (error) {
console.error(error);
Expand All @@ -119,4 +128,114 @@ export class EtherscanApiService {
return false;
}
}

async checkIsSyncedByBlockNumber(protocolAddress: string): Promise<boolean> {
try {
const protocol = await this.protocolRepository.findOne({ where: { protocolAddress } });

if (protocol == null) {
return false;
}

const transactions = await this.transactionRepository.find({
where: { protocolId: protocol.id },
order: { blockNumber: "DESC" },
});
if (transactions == null || transactions.length === 0) {
return false;
}

const savedLastBlockNumber = transactions[0].blockNumber;
const lastBlockNumber = await this.getLastBlockNumberFromEtherscan(
protocolAddress,
savedLastBlockNumber.toString(),
);

if (lastBlockNumber == 0) {
return false;
}

if (lastBlockNumber != savedLastBlockNumber) {
return false;
}

return true;
} catch (error) {
console.error(error);
return false;
}
}

async getLastBlockNumberFromEtherscan(protocolAddress: string, savedBlockNumber: string): Promise<number> {
const request: EtherScanTxListRequest = {
address: protocolAddress,
startblock: savedBlockNumber,
endblock: "99999999",
page: "1",
offset: "1",
sort: "desc",
apikey: this.apiKey,
};

const url = `${this.baseUrl}&address=${request.address}&startblock=${request.startblock}&endblock=${request.endblock}&page=${request.page}&offset=${request.offset}&sort=${request.sort}&apikey=${request.apikey}`;

const { data } = await firstValueFrom(this.httpService.get<EtherScanResponse>(url).pipe());

if (data.status === "1") {
return Number(data.result[0].blockNumber);
}
return 0;
}

async syncTransactions(protocolAddress: string): Promise<boolean> {
try {
const lowerCasedProtocolAddress = protocolAddress.toLowerCase();
const isSynced = await this.checkIsSyncedByBlockNumber(lowerCasedProtocolAddress);
if (isSynced) {
return true;
}

const protocol = await this.protocolRepository.findOne({ where: { protocolAddress: lowerCasedProtocolAddress } });
const protocolId = protocol.id;
const transactions = await this.transactionRepository.find({
where: { protocolId },
order: { blockNumber: "DESC" },
});
const savedLastBlockNumber = transactions[0].blockNumber + 1;

let page = 1;
while (true) {
const request: EtherScanTxListRequest = {
address: lowerCasedProtocolAddress,
startblock: savedLastBlockNumber.toString(),
endblock: "99999999",
page: page.toString(),
offset: "500",
sort: "desc",
apikey: this.apiKey,
};

const url = `${this.baseUrl}&address=${request.address}&startblock=${request.startblock}&endblock=${request.endblock}&page=${request.page}&offset=${request.offset}&sort=${request.sort}&apikey=${request.apikey}`;

const { data } = await firstValueFrom(this.httpService.get<EtherScanResponse>(url).pipe());

if (data.status === "1") {
for (const tx of data.result) {
await this.createTransaction(tx);
}
}

if (data.result.length < 500) {
break;
}

page++;
}

return true;
} catch (error) {
console.error(error);
return false;
}
}
}
Loading

0 comments on commit df4d7c8

Please sign in to comment.