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

feat: add token offchain data #72

Merged
merged 17 commits into from
Nov 1, 2023
Merged
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
2 changes: 2 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 9 additions & 3 deletions packages/api/src/api/api.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,9 @@ export class ApiController {

@ApiTags("Account API")
@Get("api?module=account&action=txlistinternal")
@ApiOperation({ summary: "Retrieve internal transactions" })
@ApiOperation({
summary: "Retrieve internal transactions for a given blocks range (only transfers are supported for now)",
})
@ApiQuery({
name: "startblock",
type: "integer",
Expand Down Expand Up @@ -265,7 +267,9 @@ export class ApiController {

@ApiTags("Account API")
@Get("api?module=account&action=txlistinternal&address=")
@ApiOperation({ summary: "Retrieve internal transactions for a given address" })
@ApiOperation({
summary: "Retrieve internal transactions for a given address (only transfers are supported for now)",
})
@ApiQuery({
name: "address",
description: "The address to filter internal transactions by",
Expand Down Expand Up @@ -302,7 +306,9 @@ export class ApiController {

@ApiTags("Account API")
@Get("api?module=account&action=txlistinternal&txhash=")
@ApiOperation({ summary: "Retrieve internal transactions for a given transaction hash" })
@ApiOperation({
summary: "Retrieve internal transactions for a given transaction hash (only transfers are supported for now)",
})
@ApiQuery({
name: "txhash",
description: "The transaction hash to filter internal transaction by",
Expand Down
14 changes: 14 additions & 0 deletions packages/api/src/api/mappers/transferMapper.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,5 +163,19 @@ describe("transferMapper", () => {
});
});
});

describe("when transfer amount is NULL", () => {
it("sets value as undefined", () => {
expect(
mapTransferListItem(
{
...transfer,
amount: null,
} as unknown as Transfer,
100
).value
).toBe(undefined);
});
});
});
});
2 changes: 1 addition & 1 deletion packages/api/src/api/mappers/transferMapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const mapTransferListItem = (transfer: Transfer, lastBlockNumber: number)
transactionIndex: transfer.transaction?.transactionIndex.toString(),
from: transfer.from,
to: transfer.to,
value: transfer.amount,
value: transfer.amount || undefined,
tokenID: transfer.fields?.tokenId,
tokenName: transfer.token?.name,
tokenSymbol: transfer.token?.symbol,
Expand Down
19 changes: 18 additions & 1 deletion packages/api/src/token/token.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,21 @@ export const ETH_TOKEN: Token = {
symbol: "ETH",
name: "Ether",
decimals: 18,
iconURL: null,
liquidity: null,
usdPrice: null,
} as Token;

@Entity({ name: "tokens" })
@Index(["blockNumber", "logIndex"])
@Index(["liquidity", "blockNumber", "logIndex"])
export class Token extends BaseEntity {
@PrimaryColumn({ type: "bytea", transformer: normalizeAddressTransformer })
public readonly l2Address: string;

@Column({ generated: true, type: "bigint", select: false })
public readonly number: number;

@Index()
@Column({ type: "bytea", nullable: true, transformer: normalizeAddressTransformer })
public readonly l1Address?: string;

Expand All @@ -42,4 +46,17 @@ export class Token extends BaseEntity {

@Column({ type: "int", select: false })
public readonly logIndex: number;

@Column({ type: "double precision", nullable: true })
public readonly usdPrice?: number;

@Column({ type: "double precision", nullable: true })
public readonly liquidity?: number;

@Column({ nullable: true })
public readonly iconURL?: string;

@Index()
@Column({ type: "timestamp", nullable: true, select: false })
public readonly offChainDataUpdatedAt?: Date;
}
10 changes: 7 additions & 3 deletions packages/api/src/token/token.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ describe("TokenService", () => {
l2Address: "0x000000000000000000000000000000000000800A",
name: "Ether",
symbol: "ETH",
iconURL: null,
liquidity: null,
usdPrice: null,
});
});

Expand Down Expand Up @@ -139,11 +142,12 @@ describe("TokenService", () => {
expect(repositoryMock.createQueryBuilder).toHaveBeenCalledWith("token");
});

it("returns tokens ordered by blockNumber and logIndex DESC", async () => {
it("returns tokens ordered by liquidity, blockNumber and logIndex DESC", async () => {
await service.findAll(pagingOptions);
expect(queryBuilderMock.orderBy).toBeCalledTimes(1);
expect(queryBuilderMock.orderBy).toHaveBeenCalledWith("token.blockNumber", "DESC");
expect(queryBuilderMock.addOrderBy).toBeCalledTimes(1);
expect(queryBuilderMock.orderBy).toHaveBeenCalledWith("token.liquidity", "DESC");
expect(queryBuilderMock.addOrderBy).toBeCalledTimes(2);
expect(queryBuilderMock.addOrderBy).toHaveBeenCalledWith("token.blockNumber", "DESC");
expect(queryBuilderMock.addOrderBy).toHaveBeenCalledWith("token.logIndex", "DESC");
});

Expand Down
3 changes: 2 additions & 1 deletion packages/api/src/token/token.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ export class TokenService {

public async findAll(paginationOptions: IPaginationOptions): Promise<Pagination<Token>> {
const queryBuilder = this.tokenRepository.createQueryBuilder("token");
queryBuilder.orderBy("token.blockNumber", "DESC");
queryBuilder.orderBy("token.liquidity", "DESC");
queryBuilder.addOrderBy("token.blockNumber", "DESC");
queryBuilder.addOrderBy("token.logIndex", "DESC");
return await paginate<Token>(queryBuilder, paginationOptions);
}
Expand Down
Loading
Loading