[Blockchain Watcher] (FIX - APTOS) Improve aptos request (#1251)

* Improve aptos request

* Resolve comment in PR

* Improve httpClient and create constants

* Add start params

* Resolve comment in PR

---------

Co-authored-by: julian merlo <julianmerlo@julians-MacBook-Pro.local>
This commit is contained in:
Julian 2024-04-08 12:41:46 -03:00 committed by GitHub
parent 04da0b3e4d
commit 0e487dcdfa
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 59 additions and 18 deletions

View File

@ -10,6 +10,11 @@ import {
AptosEvent,
} from "../../../domain/entities/aptos";
let TRANSACTION_BY_VERSION_ENDPOINT = "/transactions/by_version";
let BLOCK_BY_VERSION_ENDPOINT = "/blocks/by_version";
let TRANSACTION_ENDPOINT = "/transactions";
let ACCOUNT_ENDPOINT = "/accounts";
type ProviderPoolMap = ProviderPool<InstrumentedHttpProvider>;
export class AptosJsonRPCBlockRepository implements AptosRepository {
@ -26,15 +31,13 @@ export class AptosJsonRPCBlockRepository implements AptosRepository {
filter: TransactionFilter
): Promise<AptosEvent[]> {
try {
let endpoint = `${ACCOUNT_ENDPOINT}/${filter.address}/events/${filter.event}/${filter.fieldName}`;
let results: AptosEvent[] = [];
const from = range?.from ? Number(range?.from) : undefined;
const limit = range?.limit ? Number(range?.limit) : undefined;
const endpoint = `/accounts/${filter.address}/events/${filter.event}/${filter.fieldName}?start=${from}&limit=${limit}`;
results = await this.pool.get().get<typeof results>({ endpoint });
results = await this.pool.get().get<typeof results>(endpoint, {
limit: range?.limit,
start: range?.from,
});
return results;
} catch (e) {
this.handleError(
@ -52,14 +55,14 @@ export class AptosJsonRPCBlockRepository implements AptosRepository {
try {
const transactions = await Promise.all(
records.map(async (event) => {
const txEndpoint = `/transactions/by_version/${Number(event.version)}`;
const blockEndpoint = `/blocks/by_version/${Number(event.version)}`;
const txEndpoint = `${TRANSACTION_BY_VERSION_ENDPOINT}/${Number(event.version)}`;
const blockEndpoint = `${BLOCK_BY_VERSION_ENDPOINT}/${Number(event.version)}`;
let txResult: AptosTransactionByVersion = {};
let blockResult: AptosBlockByVersion = {};
txResult = await this.pool.get().get<typeof txResult>({ endpoint: txEndpoint });
blockResult = await this.pool.get().get<typeof blockResult>({ endpoint: blockEndpoint });
txResult = await this.pool.get().get<typeof txResult>(txEndpoint);
blockResult = await this.pool.get().get<typeof blockResult>(blockEndpoint);
return {
blockHeight: BigInt(blockResult.block_height!),
@ -83,9 +86,10 @@ export class AptosJsonRPCBlockRepository implements AptosRepository {
try {
let results: AptosTransaction[] = [];
const endpoint = `/transactions?start=${range.from}&limit=${range.limit}`;
results = await this.pool.get().get<typeof results>({ endpoint });
results = await this.pool.get().get<typeof results>(TRANSACTION_ENDPOINT, {
limit: range?.limit,
start: range?.from,
});
return results;
} catch (e) {
this.handleError(`Range params: ${JSON.stringify(range)}, error: ${e}`, "getTransactions");

View File

@ -40,14 +40,18 @@ export class InstrumentedHttpProvider {
return this.execute("POST", body, undefined, opts);
}
public async get<T>(params: any, opts?: HttpClientOptions): Promise<T> {
return this.execute("GET", undefined, params, opts);
public async get<T>(endpoint: string, params?: any, opts?: HttpClientOptions): Promise<T> {
const queryParamBuilder = new QueryParamBuilder().addParams(params).build();
const endpointBuild = `${endpoint}${queryParamBuilder}`;
return this.execute("GET", undefined, endpointBuild, opts);
}
private async execute<T>(
method: string,
body?: any,
params?: any,
endpoint?: string,
opts?: HttpClientOptions
): Promise<T> {
let response;
@ -64,7 +68,7 @@ export class InstrumentedHttpProvider {
requestOpts.body = JSON.stringify(body);
}
const url = method === "POST" ? this.url : `${this.url}${params.endpoint}`;
const url = method === "POST" ? this.url : `${this.url}${endpoint}`;
response = await this.health.fetch(url, requestOpts);
} catch (e: AxiosError | any) {
@ -107,3 +111,36 @@ type RequestOpts = {
};
body?: string;
};
class QueryParamBuilder {
private queryParams: Map<string, string>;
constructor() {
this.queryParams = new Map();
}
addParams(params: any): QueryParamBuilder {
for (const key in params) {
if (params[key]) {
this.queryParams.set(key, params[key]);
}
}
return this;
}
removeParam(key: string): QueryParamBuilder {
this.queryParams.delete(key);
return this;
}
build(): string {
if (this.queryParams.size === 0) {
return "";
}
const queryString = Array.from(this.queryParams.entries())
.map(([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(value)}`)
.join("&");
return `?${queryString}`;
}
}