Skip to content

Commit

Permalink
fix: make to field optional for transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
petarTxFusion committed Dec 17, 2024
1 parent e0fe9ea commit e0033d4
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ export const normalizeAddressTransformer: ValueTransformer = {
if (!hex) {
return null;
}
if (hexTransformer.from(hex) === "0x") {
return "";
}

return getAddress(hexTransformer.from(hex));
},
};
7 changes: 4 additions & 3 deletions packages/api/src/transaction/dtos/transaction.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ export class TransactionDto {
@ApiProperty({
type: String,
description: "The address this transaction is to",
example: "0xc7e0220d02d549c4846A6EC31D89C3B670Ebe35C",
example: ["0xc7e0220d02d549c4846A6EC31D89C3B670Ebe35C", null],
nullable: true,
})
public readonly to: string;
public readonly to?: string;

@ApiProperty({
type: String,
Expand Down Expand Up @@ -216,7 +217,7 @@ export class TransactionDto {
@ApiProperty({
type: String,
description: "Address of the first deployed EVM contract",
example: "0xc7e0220d02d549c4846A6EC31D89C3B670Ebe35C",
example: ["0xc7e0220d02d549c4846A6EC31D89C3B670Ebe35C", null],
nullable: true,
})
public readonly contractAddress?: string;
Expand Down
4 changes: 2 additions & 2 deletions packages/api/src/transaction/entities/transaction.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ export class Transaction extends BaseEntity {
@Column({ generated: true, type: "bigint" })
public number: number;

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

@Index()
@Column({ type: "bytea", transformer: normalizeAddressTransformer })
Expand Down
4 changes: 1 addition & 3 deletions packages/data-fetcher/src/transaction/transaction.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,13 @@ export class TransactionService {
);

const isEvmLike = transactionInfo.to === null;
const toAddress = isEvmLike ? "0x" : transactionInfo.to;
const contractAddress = isEvmLike
? getCreateAddress({ from: transactionInfo.from, nonce: transactionInfo.nonce })
: "0x";
: null;

const updatedTransactionInfo = {
...transactionInfo,
isEvmLike,
to: toAddress,
contractAddress,
} as unknown as TransactionInfo;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { MigrationInterface, QueryRunner } from "typeorm";

export class MakeTransactionToFieldOptional1734453474315 implements MigrationInterface {
name = "MakeTransactionToFieldOptional1734453474315";

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE "transactions" ALTER COLUMN "to" DROP NOT NULL`);
await queryRunner.query(`DROP INDEX "public"."IDX_f8b841e005e13e12008d9778ed"`);
await queryRunner.query(`ALTER TABLE "addressTransactions" ALTER COLUMN "address" DROP NOT NULL`);
await queryRunner.query(`ALTER TABLE "transactionReceipts" ALTER COLUMN "to" DROP NOT NULL`);
await queryRunner.query(
`CREATE INDEX "IDX_f8b841e005e13e12008d9778ed" ON "addressTransactions" ("address", "blockNumber", "receivedAt", "transactionIndex") `
);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`DROP INDEX "public"."IDX_f8b841e005e13e12008d9778ed"`);
await queryRunner.query(`ALTER TABLE "transactionReceipts" ALTER COLUMN "to" SET NOT NULL`);
await queryRunner.query(`ALTER TABLE "addressTransactions" ALTER COLUMN "address" SET NOT NULL`);
await queryRunner.query(
`CREATE INDEX "IDX_f8b841e005e13e12008d9778ed" ON "addressTransactions" ("address", "blockNumber", "receivedAt", "transactionIndex") `
);
await queryRunner.query(`ALTER TABLE "transactions" ALTER COLUMN "to" SET NOT NULL`);
}
}

0 comments on commit e0033d4

Please sign in to comment.