feat: 接入天河链存证接口
This commit is contained in:
@@ -9,6 +9,12 @@ interface OnchainJobData {
|
||||
proofId: number;
|
||||
}
|
||||
|
||||
function requestMeta(raw: unknown): Record<string, unknown> | undefined {
|
||||
if (!raw || typeof raw !== 'object') return undefined;
|
||||
const meta = (raw as Record<string, unknown>).requestMeta;
|
||||
return meta && typeof meta === 'object' ? (meta as Record<string, unknown>) : undefined;
|
||||
}
|
||||
|
||||
@Processor(QUEUE_NAMES.ONCHAIN)
|
||||
export class OnchainProcessor extends WorkerHost {
|
||||
private readonly logger = new Logger(OnchainProcessor.name);
|
||||
@@ -25,27 +31,38 @@ export class OnchainProcessor extends WorkerHost {
|
||||
const proof = await this.prisma.blockchainProof.findUnique({ where: { id: proofId } });
|
||||
if (!proof) return;
|
||||
try {
|
||||
const result = await this.blockchain.attest({ dataHash: proof.dataHash });
|
||||
const result = await this.blockchain.attest({
|
||||
dataHash: proof.dataHash,
|
||||
meta: requestMeta(proof.rawResponse),
|
||||
});
|
||||
await this.prisma.blockchainProof.update({
|
||||
where: { id: proofId },
|
||||
data: {
|
||||
status: ProofStatus.ONCHAIN,
|
||||
txHash: result.txHash,
|
||||
blockHash: result.blockHash,
|
||||
blockHeight: BigInt(result.blockHeight),
|
||||
blockHash: result.blockHash || null,
|
||||
blockHeight: result.blockHeight !== null && result.blockHeight !== undefined ? BigInt(result.blockHeight) : null,
|
||||
chainName: result.chainName,
|
||||
explorerUrl: result.explorerUrl,
|
||||
explorerUrl: result.explorerUrl || null,
|
||||
onchainAt: result.onchainAt,
|
||||
rawResponse: result.raw as object,
|
||||
},
|
||||
});
|
||||
this.logger.log(`存证 ${proofId} 上链成功`);
|
||||
} catch (e) {
|
||||
const message = (e as Error).message;
|
||||
await this.prisma.blockchainProof.update({
|
||||
where: { id: proofId },
|
||||
data: { status: ProofStatus.FAILED },
|
||||
data: {
|
||||
status: ProofStatus.FAILED,
|
||||
rawResponse: {
|
||||
...(proof.rawResponse && typeof proof.rawResponse === 'object' ? (proof.rawResponse as object) : {}),
|
||||
error: message,
|
||||
failedAt: new Date().toISOString(),
|
||||
},
|
||||
},
|
||||
});
|
||||
this.logger.error(`存证 ${proofId} 上链失败: ${(e as Error).message}`);
|
||||
this.logger.error(`存证 ${proofId} 上链失败: ${message}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user