Repository navigation
Expand file tree
/
Copy pathTypescript
More file actions
212 lines (177 loc) · 6.29 KB
/
Copy pathTypescript
File metadata and controls
212 lines (177 loc) · 6.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import { Kafka, KafkaJS, logLevel } from 'kafkajs';
import { Registry, AvroSerializer } from '@kafkajs/confluent-schema-registry';
import { PrometheusMetrics } from '../utils/metrics';
import { APZTransaction, APZBlock, APZTokenTransfer } from '../../../common/types';
export class APZTransactionProcessor {
private kafka: Kafka;
private registry: Registry;
private metrics: PrometheusMetrics;
private producer: KafkaJS.Producer;
private consumer: KafkaJS.Consumer;
constructor(
private readonly config: {
kafkaBrokers: string[];
schemaRegistryUrl: string;
sourceTopic: string;
outputTopic: string;
groupId: string;
}
) {
this.kafka = new Kafka({
clientId: 'apz-transaction-processor',
brokers: config.kafkaBrokers,
logLevel: logLevel.INFO,
});
this.registry = new Registry({
host: config.schemaRegistryUrl
});
this.metrics = new PrometheusMetrics();
this.producer = this.kafka.producer();
this.consumer = this.kafka.consumer({ groupId: config.groupId });
}
async start(): Promise<void> {
await this.connect();
await this.setupConsumer();
console.log('🚀 APZ Transaction Processor started successfully');
}
private async connect(): Promise<void> {
await this.producer.connect();
await this.consumer.connect();
await this.consumer.subscribe({
topic: this.config.sourceTopic,
fromBeginning: false
});
}
private async setupConsumer(): Promise<void> {
await this.consumer.run({
eachMessage: async ({ topic, partition, message }) => {
try {
const startTime = Date.now();
// دیکد کردن پیام Avro
const decodedMessage = await this.registry.decode(message.value);
const processedData = await this.processTransaction(decodedMessage);
// تولید پیام پردازش شده
await this.produceProcessedMessage(processedData);
// ثبت متریکها
this.metrics.recordTransactionProcessed(
'success',
Date.now() - startTime
);
} catch (error) {
console.error('Error processing transaction:', error);
this.metrics.recordTransactionProcessed('error', 0);
}
},
});
}
private async processTransaction(rawTransaction: any): Promise<APZTransaction> {
// پردازش تراکنش APZ
const processedTx: APZTransaction = {
hash: rawTransaction.hash,
blockNumber: rawTransaction.blockNumber,
blockHash: rawTransaction.blockHash,
from: rawTransaction.from,
to: rawTransaction.to,
value: this.convertHexToWei(rawTransaction.value),
gas: BigInt(rawTransaction.gas),
gasPrice: BigInt(rawTransaction.gasPrice),
input: rawTransaction.input,
nonce: rawTransaction.nonce,
transactionIndex: rawTransaction.transactionIndex,
timestamp: Date.now(),
status: 'pending',
chainId: this.config.chainId || 'apz-mainnet',
// فیلدهای مخصوص APZ Chain
apzSpecific: {
tokenTransfers: await this.extractTokenTransfers(rawTransaction),
contractInteractions: this.parseContractInteractions(rawTransaction),
fee: this.calculateTransactionFee(rawTransaction)
}
};
// اعتبارسنجی تراکنش
this.validateTransaction(processedTx);
return processedTx;
}
private async extractTokenTransfers(tx: any): Promise<APZTokenTransfer[]> {
const transfers: APZTokenTransfer[] = [];
// پردازش انتقال توکنهای APZ
if (tx.input && tx.input !== '0x') {
try {
// تشخیص استانداردهای توکن (ERC-20, ERC-721, etc.)
const tokenStandard = this.detectTokenStandard(tx.input);
if (tokenStandard === 'ERC20') {
const transfer = this.parseERC20Transfer(tx);
if (transfer) transfers.push(transfer);
}
// اضافه کردن سایر استانداردها
if (tokenStandard === 'ERC721') {
const transfer = this.parseERC721Transfer(tx);
if (transfer) transfers.push(transfer);
}
} catch (error) {
console.warn('Failed to extract token transfers:', error);
}
}
return transfers;
}
private parseERC20Transfer(tx: any): APZTokenTransfer | null {
// پیادهسازی پارس کردن تراکنشهای ERC-20
try {
// اینجا باید ABI مربوط به توکنهای APZ پیادهسازی شود
return {
type: 'ERC20',
from: tx.from,
to: this.decodeAddress(tx.input, 4), // استخراج آدرس مقصد از input
value: this.decodeUint256(tx.input, 36), // استخراج مقدار از input
tokenAddress: tx.to,
transactionHash: tx.hash,
logIndex: 0
};
} catch (error) {
return null;
}
}
private calculateTransactionFee(tx: any): bigint {
const gasUsed = BigInt(tx.gasUsed || tx.gas);
const gasPrice = BigInt(tx.gasPrice);
return gasUsed * gasPrice;
}
private validateTransaction(tx: APZTransaction): void {
const errors: string[] = [];
if (!tx.hash || tx.hash.length !== 66) {
errors.push('Invalid transaction hash');
}
if (!tx.from || !this.isValidAddress(tx.from)) {
errors.push('Invalid from address');
}
if (tx.to && !this.isValidAddress(tx.to)) {
errors.push('Invalid to address');
}
if (errors.length > 0) {
throw new Error(`Transaction validation failed: ${errors.join(', ')}`);
}
}
private isValidAddress(address: string): boolean {
return /^0x[a-fA-F0-9]{40}$/.test(address);
}
private convertHexToWei(hexValue: string): bigint {
return BigInt(hexValue);
}
private async produceProcessedMessage(processedTx: APZTransaction): Promise<void> {
const encodedMessage = await this.registry.encode(processedTx);
await this.producer.send({
topic: this.config.outputTopic,
messages: [
{
key: processedTx.hash,
value: encodedMessage
}
]
});
}
async shutdown(): Promise<void> {
await this.consumer.disconnect();
await this.producer.disconnect();
console.log('APZ Transaction Processor shutdown complete');
}
}