Resolved issue mapping tx hash

This commit is contained in:
julian merlo 2024-03-25 18:41:08 -03:00
parent 83198e8565
commit 617a886b25
4 changed files with 78 additions and 67 deletions

View File

@ -5,26 +5,28 @@ import { WormchainLog } from "../../entities/wormchain";
export class HandleWormchainLogs {
constructor(
private readonly cfg: HandleWormchainLogsOptions,
private readonly mapper: (tx: WormchainLog) => TransactionFoundEvent,
private readonly mapper: (tx: WormchainLog) => TransactionFoundEvent[],
private readonly target: (parsed: TransactionFoundEvent[]) => Promise<void>,
private readonly statsRepo: StatRepository
) {}
public async handle(logs: WormchainLog[]): Promise<TransactionFoundEvent[]> {
const mappedItems = logs.map((log) => {
const logMap = this.mapper(log);
return logMap;
});
const filterLogs: TransactionFoundEvent[] = [];
const filterItems = mappedItems.filter((item) => {
if (item) {
this.report();
return item;
logs.map((log) => {
const logMapped = this.mapper(log);
if (logMapped && logMapped.length > 0) {
logMapped.forEach((log) => {
this.report();
filterLogs.push(log);
});
}
return logMapped;
});
await this.target(filterItems);
return filterItems;
await this.target(filterLogs);
return filterLogs;
}
private report() {

View File

@ -9,44 +9,52 @@ let logger: winston.Logger = winston.child({ module: "wormchainLogMessagePublish
export const wormchainLogMessagePublishedMapper = (
log: WormchainLog
): LogFoundEvent<LogMessagePublished> | undefined => {
const { coreContract, sequence, payload, emitter, nonce, hash } = transactionAttibutes(log);
): LogFoundEvent<LogMessagePublished[]> | [] => {
const transactionAttributesMapped = transactionAttributes(log);
if (coreContract && sequence && payload && emitter && payload && nonce && hash) {
if (transactionAttributesMapped.length === 0) {
return [];
}
const logMessages: any = [];
transactionAttributesMapped.forEach((tx) => {
logger.info(
`[wormchain] Source event info: [tx: ${hash}][emitterChain: ${CHAIN_ID_WORMCHAIN}][sender: ${emitter}][sequence: ${sequence}]`
`[wormchain] Source event info: [tx: ${tx.hash}][emitterChain: ${CHAIN_ID_WORMCHAIN}][sender: ${tx.emitter}][sequence: ${tx.sequence}]`
);
return {
logMessages.push({
name: "log-message-published",
address: CORE_ADDRESS,
chainId: 3104,
txHash: hash,
txHash: tx.hash,
blockHeight: log.blockHeight,
blockTime: log.timestamp,
attributes: {
sender: emitter,
sequence: sequence,
payload: payload,
nonce: nonce,
sender: tx.emitter,
sequence: tx.sequence,
payload: tx.payload,
nonce: tx.nonce,
consistencyLevel: 0,
},
};
}
});
});
return logMessages;
};
function transactionAttibutes(log: WormchainLog): TransactionAttributes {
let coreContract;
let sequence;
let payload;
let emitter;
let nonce;
let hash;
function transactionAttributes(log: WormchainLog): TransactionAttributes[] {
const transactionAttributes: TransactionAttributes[] = [];
log.transactions?.forEach((tx) => {
hash = tx.hash;
let coreContract = false;
let sequence: number | undefined;
let payload: string | undefined;
let emitter: string | undefined;
let nonce: number | undefined;
let hash: string | undefined;
tx.attributes.forEach((attr) => {
for (const attr of tx.attributes) {
const key = Buffer.from(attr.key, "base64").toString().toLowerCase();
const value = Buffer.from(attr.value, "base64").toString().toLowerCase();
@ -65,22 +73,20 @@ function transactionAttibutes(log: WormchainLog): TransactionAttributes {
break;
case "_contract_address":
case "contract_address":
if (value.toLocaleLowerCase() === CORE_ADDRESS.toLowerCase()) {
if (value.toLowerCase() === CORE_ADDRESS.toLowerCase()) {
coreContract = true;
}
break;
}
});
}
if (coreContract && sequence && payload && emitter && nonce) {
hash = tx.hash;
transactionAttributes.push({ coreContract, sequence, payload, emitter, nonce, hash });
}
});
return {
coreContract,
sequence,
payload,
emitter,
nonce,
hash,
};
return transactionAttributes;
}
type TransactionAttributes = {

View File

@ -40,22 +40,24 @@ describe("HandleWormchainLogs", () => {
});
const mapper = (tx: WormchainLog) => {
return {
name: "log-message-published",
address: "wormhole1ufs3tlq4umljk0qfe8k5ya0x6hpavn897u2cnf9k0en9jr7qarqqaqfk2j",
chainId: 3104,
txHash: "0x7f61bf387fdb700d32d2b40ccecfb70ae46a2f82775242d04202bb7a538667c6",
blockHeight: 153549311n,
blockTime: 1709645685704036,
attributes: {
sender: "wormhole1ufs3tlq4umljk0qfe8k5ya0x6hpavn897u2cnf9k0en9jr7qarqqaqfk2j",
sequence: 203,
payload: "",
nonce: 75952,
consistencyLevel: 0,
protocol: "Token Bridge",
return [
{
name: "log-message-published",
address: "wormhole1ufs3tlq4umljk0qfe8k5ya0x6hpavn897u2cnf9k0en9jr7qarqqaqfk2j",
chainId: 3104,
txHash: "0x7f61bf387fdb700d32d2b40ccecfb70ae46a2f82775242d04202bb7a538667c6",
blockHeight: 153549311n,
blockTime: 1709645685704036,
attributes: {
sender: "wormhole1ufs3tlq4umljk0qfe8k5ya0x6hpavn897u2cnf9k0en9jr7qarqqaqfk2j",
sequence: 203,
payload: "",
nonce: 75952,
consistencyLevel: 0,
protocol: "Token Bridge",
},
},
};
];
};
const targetRepo = {

View File

@ -1,31 +1,32 @@
import { wormchainLogMessagePublishedMapper } from "../../../../src/infrastructure/mappers/wormchain/wormchainLogMessagePublishedMapper";
import { describe, it, expect } from "@jest/globals";
import { WormchainLog } from "../../../../src/domain/entities/wormchain";
import { LogFoundEvent, LogMessagePublished } from "../../../../src/domain/entities";
describe("wormchainLogMessagePublishedMapper", () => {
it("should be able to map log to aptosLogMessagePublishedMapper", async () => {
// When
const result = wormchainLogMessagePublishedMapper(log);
const result = wormchainLogMessagePublishedMapper(log) as any;
if (result) {
// Then
expect(result.name).toBe("log-message-published");
expect(result.chainId).toBe(3104);
expect(result.txHash).toBe(
expect(result[0].name).toBe("log-message-published");
expect(result[0].chainId).toBe(3104);
expect(result[0].txHash).toBe(
"0xa08b0ac6ee67e21d3dd89f48f60cc907fc867288f4439bcf72731b0884d8aff2"
);
expect(result.address).toBe(
expect(result[0].address).toBe(
"wormhole1ufs3tlq4umljk0qfe8k5ya0x6hpavn897u2cnf9k0en9jr7qarqqaqfk2j"
);
expect(result.attributes.consistencyLevel).toBe(0);
expect(result.attributes.nonce).toBe(7671);
expect(result.attributes.payload).toBe(
expect(result[0].attributes.consistencyLevel).toBe(0);
expect(result[0].attributes.nonce).toBe(7671);
expect(result[0].attributes.payload).toBe(
"0100000000000000000000000000000000000000000000000000000000555643a3f5edec8471c75624ebc4079a634326d96a689e6157d79abe8f5a6f94472853bc00018622b98735cb870ae0cb22bd4ea58cfb512bd4002247ccd0b250eb6d0c5032fc00010000000000000000000000000000000000000000000000000000000000000000"
);
expect(result.attributes.sender).toBe(
expect(result[0].attributes.sender).toBe(
"aeb534c45c3049d380b9d9b966f9895f53abd4301bfaff407fa09dea8ae7a924"
);
expect(result.attributes.sequence).toBe(28603);
expect(result[0].attributes.sequence).toBe(28603);
}
});
});