Skip to content

Commit

Permalink
Change transfer ID creation
Browse files Browse the repository at this point in the history
  • Loading branch information
mj52951 committed Jul 29, 2024
1 parent 29d2302 commit 67cc0fc
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 153 deletions.
17 changes: 11 additions & 6 deletions src/evmIndexer/evmIndexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,20 @@ The Licensed Work is (c) 2024 Sygma
SPDX-License-Identifier: LGPL-3.0-only
*/
import type { Context } from "../evmProcessor";
import type { Transfer } from "../model";
import { Account, Deposit, Execution, Fee, TransferStatus } from "../model";
import {
Transfer,
Account,
Deposit,
Execution,
Fee,
TransferStatus,
} from "../model";

import type {
DecodedDepositLog,
DecodedFailedHandlerExecution,
DecodedProposalExecutionLog,
} from "./evmTypes";
import { getUpdatedTransfer } from "./utils";

export async function processDeposits(
ctx: Context,
Expand Down Expand Up @@ -45,7 +50,7 @@ export async function processDeposits(
handlerResponse: d.handlerResponse,
});

const transfer = await getUpdatedTransfer(ctx, {
const transfer = new Transfer({
id: d.id,
depositNonce: d.depositNonce,
amount: d.amount,
Expand Down Expand Up @@ -85,7 +90,7 @@ export async function processExecutions(
txHash: e.txHash,
});

const transfer = await getUpdatedTransfer(ctx, {
const transfer = new Transfer({
id: e.id,
depositNonce: e.depositNonce,
amount: null,
Expand Down Expand Up @@ -122,7 +127,7 @@ export async function processFailedExecutions(
txHash: e.txHash,
});

const transfer = await getUpdatedTransfer(ctx, {
const transfer = new Transfer({
id: e.id,
depositNonce: e.depositNonce,
amount: null,
Expand Down
46 changes: 19 additions & 27 deletions src/evmIndexer/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import { AbiCoder, Contract, ethers, formatUnits } from "ethers";
import * as FeeHandlerRouter from "../../abi/FeeHandlerRouter.json";
import * as bridge from "../../abi/bridge";
import type { Domain } from "../../config";
import type { Context, Log } from "../../evmProcessor";
import { Transfer } from "../../model";
import type { Log } from "../../evmProcessor";
import { generateTransferID } from "../../utils";
import { logger } from "../../utils/logger";
import type {
DecodedDepositLog,
Expand Down Expand Up @@ -55,7 +55,11 @@ export async function parseDeposit(
const transaction = assertNotNull(log.transaction, "Missing transaction");

return {
id: log.id,
id: generateTransferID(
event.depositNonce.toString(),
fromDomain.id.toString(),
event.destinationDomainID.toString(),
),
blockNumber: log.block.height,
depositNonce: event.depositNonce,
toDomainID: event.destinationDomainID,
Expand All @@ -66,7 +70,7 @@ export async function parseDeposit(
resourceType,
substrateRpcUrlConfig,
),
fromDomainID: Number(fromDomain.id),
fromDomainID: fromDomain.id,
resourceID: resource.resourceId,
txHash: transaction.hash,
timestamp: new Date(log.block.timestamp),
Expand Down Expand Up @@ -206,14 +210,18 @@ export function parseProposalExecution(
const transaction = assertNotNull(log.transaction, "Missing transaction");

return {
id: log.id,
id: generateTransferID(
event.depositNonce.toString(),
event.originDomainID.toString(),
toDomain.id.toString(),
),
blockNumber: log.block.height,
from: log.transaction!.from,
depositNonce: event.depositNonce,
txHash: transaction.hash,
timestamp: new Date(log.block.timestamp),
fromDomainID: event.originDomainID,
toDomainID: Number(toDomain.id),
toDomainID: toDomain.id,
};
}

Expand All @@ -225,7 +233,11 @@ export function parseFailedHandlerExecution(
const transaction = assertNotNull(log.transaction, "Missing transaction");

return {
id: log.id,
id: generateTransferID(
event.depositNonce.toString(),
event.originDomainID.toString(),
toDomain.id.toString(),
),
fromDomainID: event.originDomainID,
toDomainID: toDomain.id,
depositNonce: event.depositNonce,
Expand Down Expand Up @@ -301,23 +313,3 @@ function getContract(
return new Contract(contractAddress, FeeHandlerRouter.abi, provider);
}
}

export async function getUpdatedTransfer(
ctx: Context,
transferValues: Partial<Transfer>,
): Promise<Transfer> {
const transfer = await ctx.store.findOne(Transfer, {
where: {
depositNonce: transferValues.depositNonce!,
fromDomainID: transferValues.fromDomainID!,
toDomainID: transferValues.toDomainID!,
},
});

if (!transfer) {
return new Transfer(transferValues);
} else {
Object.assign(transfer, transferValues);
return transfer;
}
}
8 changes: 8 additions & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,11 @@ export async function initDatabase(): Promise<DataSource> {
await dataSource.initialize();
return dataSource;
}

export function generateTransferID(
depositNonce: string,
fromDomainID: string,
toDomainID: string,
): string {
return depositNonce + "-" + fromDomainID + "-" + toDomainID;
}
120 changes: 0 additions & 120 deletions tests/unit/getTransfer.spec.ts

This file was deleted.

0 comments on commit 67cc0fc

Please sign in to comment.