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: nft support #384

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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 packages/api/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { metricProviders } from "./metrics";
import { DbMetricsService } from "./dbMetrics.service";
import { disableExternalAPI } from "./config/featureFlags";
import config from "./config";
import { NftItemModule } from "./nft/nftItem.module";

@Module({
imports: [
Expand All @@ -49,6 +50,7 @@ import config from "./config";
LogModule,
StatsModule,
HealthModule,
NftItemModule,
],
providers: [Logger, ...metricProviders, DbMetricsService],
})
Expand Down
31 changes: 31 additions & 0 deletions packages/api/src/nft/nftItem.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { hexTransformer } from "../common/transformers/hex.transformer";
import { BaseEntity, Column, Entity, Index, PrimaryColumn } from "typeorm";

@Entity({ name: "nftitems" })
@Index(["tokenId", "tokenAddress"])
export class NftItem extends BaseEntity {
@PrimaryColumn()
public readonly tokenId: string;

@PrimaryColumn({ type: "bytea", transformer: hexTransformer })
public readonly tokenAddress: string;

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

@Index()
@Column({ type: "bytea", transformer: hexTransformer })
public readonly owner: string;

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

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

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

@Column({ nullable: true })
public readonly metadataUrl?: string;
}
7 changes: 7 additions & 0 deletions packages/api/src/nft/nftItem.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Module } from "@nestjs/common";
import { TypeOrmModule } from "@nestjs/typeorm";
import { NftItem } from "./nftItem.entity";
@Module({
imports: [TypeOrmModule.forFeature([NftItem])],
})
export class NftItemModule {}
10 changes: 10 additions & 0 deletions packages/api/src/token/token.dto.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ApiProperty } from "@nestjs/swagger";
import { TokenType } from "./token.entity";

export class TokenDto {
@ApiProperty({ type: String, description: "L2 token address", example: "0xd754Ff5e8a6f257E162F72578A4bB0493c0681d8" })
Expand Down Expand Up @@ -58,4 +59,13 @@ export class TokenDto {
required: false,
})
public readonly iconURL?: string;

@ApiProperty({
type: String,
description: "Token type",
example: "ERC20",
enum: ["BASETOKEN", "ERC20", "ERC721"],
default: "ERC20",
})
public readonly type?: TokenType;
}
3 changes: 3 additions & 0 deletions packages/api/src/token/token.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,7 @@ export class Token extends BaseEntity {
@Index()
@Column({ type: "timestamp", nullable: true, select: false })
public readonly offChainDataUpdatedAt?: Date;

@Column({ type: "enum", enum: TokenType, default: TokenType.ERC20 })
public readonly type: TokenType;
}
6 changes: 5 additions & 1 deletion packages/api/src/transfer/transfer.entity.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { Entity, Column, Index, ManyToOne, JoinColumn, PrimaryColumn, AfterLoad } from "typeorm";
import { Entity, Column, Index, ManyToOne, JoinColumn, PrimaryColumn, AfterLoad, OneToOne } from "typeorm";
import { BaseEntity } from "../common/entities/base.entity";
import { Token, TokenType } from "../token/token.entity";
import { normalizeAddressTransformer } from "../common/transformers/normalizeAddress.transformer";
import { bigIntNumberTransformer } from "../common/transformers/bigIntNumber.transformer";
import { hexTransformer } from "../common/transformers/hex.transformer";
import { Transaction } from "../transaction/entities/transaction.entity";
import { baseToken, ethToken } from "../config";
import { NftItem } from "../nft/nftItem.entity";

export enum TransferType {
Deposit = "deposit",
Expand Down Expand Up @@ -80,6 +81,9 @@ export class Transfer extends BaseEntity {
@Column({ type: "boolean", default: false })
public readonly isInternal: boolean;

@OneToOne(() => NftItem)
public nftItem?: NftItem;

toJSON() {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { number, ...restFields } = this;
Expand Down
13 changes: 13 additions & 0 deletions packages/api/src/transfer/transfer.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { Transfer, TransferType } from "./transfer.entity";
import { TokenType } from "../token/token.entity";
import { AddressTransfer } from "./addressTransfer.entity";
import { normalizeAddressTransformer } from "../common/transformers/normalizeAddress.transformer";
import { NftItem } from "../nft/nftItem.entity";

export interface FilterTransfersOptions {
tokenAddress?: string;
Expand Down Expand Up @@ -56,6 +57,12 @@ export class TransferService {
const queryBuilder = this.addressTransferRepository.createQueryBuilder("addressTransfer");
queryBuilder.select("addressTransfer.number");
queryBuilder.leftJoinAndSelect("addressTransfer.transfer", "transfer");
queryBuilder.leftJoinAndMapOne(
"transfer.nftItem",
NftItem,
"nftItem",
"nftItem.tokenAddress = transfer.tokenAddress AND nftItem.tokenId = (transfer.fields->>'tokenId')::text"
);
queryBuilder.leftJoinAndSelect("transfer.token", "token");
queryBuilder.where(filterOptions);
queryBuilder.orderBy("addressTransfer.timestamp", "DESC");
Expand All @@ -69,6 +76,12 @@ export class TransferService {
const queryBuilder = this.transferRepository.createQueryBuilder("transfer");
queryBuilder.where(filterOptions);
queryBuilder.leftJoinAndSelect("transfer.token", "token");
queryBuilder.leftJoinAndMapOne(
"transfer.nftItem",
NftItem,
"nftItem",
"nftItem.tokenAddress = transfer.tokenAddress AND nftItem.tokenId = (transfer.fields->>'tokenId')::text"
);
queryBuilder.orderBy("transfer.timestamp", "DESC");
queryBuilder.addOrderBy("transfer.logIndex", "ASC");
return await paginate<Transfer>(queryBuilder, paginationOptions);
Expand Down
1 change: 1 addition & 0 deletions packages/api/test/account-api.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ describe("Account API (e2e)", () => {
decimals: 18,
blockNumber: 1,
logIndex: 1,
type: TokenType.ERC20,
});

const tokens = [
Expand Down
Loading
Loading