-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathfilterAmmCreate.ts
More file actions
201 lines (170 loc) · 6.27 KB
/
filterAmmCreate.ts
File metadata and controls
201 lines (170 loc) · 6.27 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
import WebSocket from 'ws';
const testAccount = 'rwpNZgUHJfXP8pjoCps53YM8fW3X1JFU1c';
const DEFAULT_XRPL_SERVER = 'wss://xrplcluster.com';
interface PendingRequest {
resolve: (value: any) => void;
reject: (error: Error) => void;
}
interface AMMTransactionResult {
totalTransactions: number;
ammCreateTransactions: any[];
allTransactions: any[];
accountSequence?: number;
ledgerRange: {
min: number;
max: number;
};
}
interface AMMAnalysis {
hash: string;
creator: string;
ammAccount: string;
asset1: string;
asset2: string;
asset2Issuer: string;
tradingFee: number;
amount1: any;
amount2: any;
date: string;
ledgerIndex: number;
pairKey: string;
}
export default class XRPLAMMChecker {
private ws: WebSocket | null = null;
private requestId: number = 1;
private pendingRequests: Map<number, PendingRequest> = new Map();
connect(serverUrl: string = DEFAULT_XRPL_SERVER): Promise<void> {
return new Promise((resolve, reject) => {
this.ws = new WebSocket(serverUrl);
this.ws.on('open', () => {
resolve();
});
this.ws.on('message', (data: WebSocket.Data) => {
this.handleMessage(JSON.parse(data.toString()));
});
this.ws.on('error', (error: Error) => {
console.error('WebSocket error:', error);
reject(error);
});
this.ws.on('close', () => {});
});
}
private handleMessage(message: any): void {
if (message.id && this.pendingRequests.has(message.id)) {
const { resolve, reject } = this.pendingRequests.get(message.id)!;
this.pendingRequests.delete(message.id);
if (message.status === 'success') {
resolve(message.result);
} else {
reject(new Error(message.error_message || 'Request failed'));
}
}
}
request(command: any): Promise<any> {
return new Promise((resolve, reject) => {
if (!this.ws || this.ws.readyState !== WebSocket.OPEN) {
reject(new Error('WebSocket not connected'));
return;
}
const id = this.requestId++;
const request = { id, ...command };
this.pendingRequests.set(id, { resolve, reject });
this.ws.send(JSON.stringify(request));
setTimeout(() => {
if (this.pendingRequests.has(id)) {
this.pendingRequests.delete(id);
reject(new Error('Request timeout'));
}
}, 30000);
});
}
async getAccountAMMTransactions(accountAddress: string, limit: number = 1000): Promise<AMMTransactionResult> {
try {
const response = await this.request({
command: 'account_tx',
account: accountAddress,
ledger_index_min: -1,
ledger_index_max: -1,
limit: limit,
forward: true
});
// Filter for AMMCreate transactions
const ammCreateTxs = response.transactions ? response.transactions.filter((tx: any) =>
tx.tx?.TransactionType === 'AMMCreate'
) : [];
return {
totalTransactions: response.transactions ? response.transactions.length : 0,
ammCreateTransactions: ammCreateTxs,
allTransactions: response.transactions || [],
accountSequence: response.account_sequence_available,
ledgerRange: {
min: response.ledger_index_min,
max: response.ledger_index_max
}
};
} catch (error) {
console.error('Error fetching account transactions:', error instanceof Error ? error.message : 'Unknown error');
throw error;
}
}
analyzeAMMCreate(tx: any): AMMAnalysis {
const txData = tx.tx;
const meta = tx.meta;
const asset1 = txData.Asset?.currency || 'XRP';
const asset2 = txData.Asset2?.currency || 'Unknown';
const asset2Issuer = txData.Asset2?.issuer || 'N/A';
let ammAccount = 'Unknown';
if (meta?.CreatedNode?.NewFields?.AMMAccount) {
ammAccount = meta.CreatedNode.NewFields.AMMAccount;
}
return {
hash: txData.hash,
creator: txData.Account,
ammAccount: ammAccount,
asset1: asset1,
asset2: asset2,
asset2Issuer: asset2Issuer,
tradingFee: txData.TradingFee || 0,
amount1: txData.Amount,
amount2: txData.Amount2,
date: new Date((txData.date + 946684800) * 1000).toISOString(),
ledgerIndex: txData.ledger_index,
pairKey: `${asset1}:${asset2}:${asset2Issuer}`
};
}
async checkIfNewTokenLaunch(accountAddress: string): Promise<{ isNewCreator: boolean; ammHistory: any[] }> {
try {
const result = await this.getAccountAMMTransactions(accountAddress);
const ammTransactions = result.ammCreateTransactions;
if (ammTransactions.length === 0) {
return { isNewCreator: true, ammHistory: [] };
} else if (ammTransactions.length === 1) {
return { isNewCreator: true, ammHistory: [ammTransactions[0]] };
} else {
return { isNewCreator: false, ammHistory: ammTransactions };
}
} catch (error) {
console.error('Error checking token launch status:', error instanceof Error ? error.message : 'Unknown error');
throw error;
}
}
close(): void {
if (this.ws) {
this.ws.close();
}
}
}
async function testAMMChecker(): Promise<void> {
const checker = new XRPLAMMChecker();
try {
await checker.connect();
await checker.getAccountAMMTransactions(testAccount);
} catch (error) {
console.error('Test failed:', error instanceof Error ? error.message : 'Unknown error');
} finally {
checker.close();
}
}
if (require.main === module) {
testAMMChecker();
}