-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy paths3.ts
More file actions
260 lines (228 loc) · 5.69 KB
/
s3.ts
File metadata and controls
260 lines (228 loc) · 5.69 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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
import {
GetObjectCommand,
PutObjectCommand,
S3Client,
type S3ClientConfig,
} from "@aws-sdk/client-s3";
function createS3Client(): S3Client {
const configType = process.env.TIPS_UI_S3_CONFIG_TYPE || "aws";
const region = process.env.TIPS_UI_AWS_REGION || "us-east-1";
if (configType === "manual") {
console.log("Using Manual S3 configuration");
const config: S3ClientConfig = {
region,
forcePathStyle: true,
};
if (process.env.TIPS_UI_S3_ENDPOINT) {
config.endpoint = process.env.TIPS_UI_S3_ENDPOINT;
}
if (
process.env.TIPS_UI_S3_ACCESS_KEY_ID &&
process.env.TIPS_UI_S3_SECRET_ACCESS_KEY
) {
config.credentials = {
accessKeyId: process.env.TIPS_UI_S3_ACCESS_KEY_ID,
secretAccessKey: process.env.TIPS_UI_S3_SECRET_ACCESS_KEY,
};
}
return new S3Client(config);
}
console.log("Using AWS S3 configuration");
return new S3Client({
region,
});
}
const s3Client = createS3Client();
const BUCKET_NAME = process.env.TIPS_UI_S3_BUCKET_NAME || "tips";
export interface TransactionMetadata {
bundle_ids: string[];
sender: string;
nonce: string;
}
async function getObjectContent(key: string): Promise<string | null> {
try {
const command = new GetObjectCommand({
Bucket: BUCKET_NAME,
Key: key,
});
const response = await s3Client.send(command);
const body = await response.Body?.transformToString();
return body || null;
} catch (_error) {
return null;
}
}
export async function getTransactionMetadataByHash(
hash: string,
): Promise<TransactionMetadata | null> {
const key = `transactions/by_hash/${hash}`;
const content = await getObjectContent(key);
if (!content) {
return null;
}
try {
return JSON.parse(content) as TransactionMetadata;
} catch (error) {
console.error(
`Failed to parse transaction metadata for hash ${hash}:`,
error,
);
return null;
}
}
export interface BundleTransaction {
signer: string;
type: string;
chainId: string;
nonce: string;
gas: string;
maxFeePerGas: string;
maxPriorityFeePerGas: string;
to: string | null;
value: string;
accessList: unknown[];
input: string;
r: string;
s: string;
yParity: string;
v: string;
hash: string;
}
export interface MeterBundleResult {
coinbaseDiff: string;
ethSentToCoinbase: string;
fromAddress: string;
gasFees: string;
gasPrice: string;
gasUsed: number;
toAddress: string;
txHash: string;
value: string;
executionTimeUs: number;
}
export interface MeterBundleResponse {
bundleGasPrice: string;
bundleHash: string;
coinbaseDiff: string;
ethSentToCoinbase: string;
gasFees: string;
results: MeterBundleResult[];
stateBlockNumber: number;
totalGasUsed: number;
totalExecutionTimeUs: number;
stateRootTimeUs: number;
stateRootAccountNodeCount: number;
stateRootStorageNodeCount: number;
}
export interface BundleData {
uuid: string;
txs: BundleTransaction[];
block_number: string;
max_timestamp: number;
reverting_tx_hashes: string[];
meter_bundle_response: MeterBundleResponse;
}
export interface BundleEventData {
key: string;
timestamp: number;
bundle?: BundleData;
block_number?: number;
block_hash?: string;
builder?: string;
flashblock_index?: number;
reason?: string;
}
export interface BundleEvent {
event: string;
data: BundleEventData;
}
export interface BundleHistory {
history: BundleEvent[];
}
export async function getBundleHistory(
bundleId: string,
): Promise<BundleHistory | null> {
const key = `bundles/${bundleId}`;
const content = await getObjectContent(key);
if (!content) {
return null;
}
try {
return JSON.parse(content) as BundleHistory;
} catch (error) {
console.error(
`Failed to parse bundle history for bundle ${bundleId}:`,
error,
);
return null;
}
}
export interface BlockTransaction {
hash: string;
from: string;
to: string | null;
gasLimit: bigint;
gasUsed: bigint | null;
executionTimeUs: number | null;
stateRootTimeUs: number | null;
bundleId: string | null;
index: number;
}
export interface BlockData {
hash: string;
number: bigint;
timestamp: bigint;
transactions: BlockTransaction[];
gasUsed: bigint;
gasLimit: bigint;
cachedAt: number;
}
export async function getBlockFromCache(
blockHash: string,
): Promise<BlockData | null> {
const key = `blocks/${blockHash}`;
const content = await getObjectContent(key);
if (!content) {
return null;
}
try {
const parsed = JSON.parse(content);
return {
...parsed,
number: BigInt(parsed.number),
timestamp: BigInt(parsed.timestamp),
gasUsed: BigInt(parsed.gasUsed),
gasLimit: BigInt(parsed.gasLimit),
transactions: parsed.transactions.map(
(tx: {
gasLimit?: string;
gasUsed?: string | null;
[key: string]: unknown;
}) => ({
...tx,
gasLimit: BigInt(tx.gasLimit ?? tx.gasUsed ?? "0"),
gasUsed: tx.gasUsed != null ? BigInt(tx.gasUsed) : null,
}),
),
} as BlockData;
} catch (error) {
console.error(`Failed to parse block data for hash ${blockHash}:`, error);
return null;
}
}
export async function cacheBlockData(blockData: BlockData): Promise<void> {
const key = `blocks/${blockData.hash}`;
try {
const command = new PutObjectCommand({
Bucket: BUCKET_NAME,
Key: key,
Body: JSON.stringify(blockData, (_, value) =>
typeof value === "bigint" ? value.toString() : value,
),
ContentType: "application/json",
});
await s3Client.send(command);
} catch (error) {
console.error(`Failed to cache block data for ${blockData.hash}:`, error);
}
}