This repository was archived by the owner on Mar 18, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstacks-account-do.ts
More file actions
106 lines (89 loc) · 3.48 KB
/
stacks-account-do.ts
File metadata and controls
106 lines (89 loc) · 3.48 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
import { DurableObject } from 'cloudflare:workers';
import { Env } from '../../worker-configuration';
import { AppConfig } from '../config';
import { StacksAccountDataService } from '../services/stacks-account-data-service';
import { handleRequest } from '../utils/request-handler-util';
import { ApiError } from '../utils/api-error-util';
import { ErrorCode } from '../utils/error-catalog-util';
import { validateStacksAddress } from '@stacks/transactions';
export class StacksAccountDO extends DurableObject<Env> {
private accountDataService: StacksAccountDataService;
constructor(ctx: DurableObjectState, env: Env) {
super(ctx, env);
this.env = env;
const config = AppConfig.getInstance(env).getConfig();
const hiroConfig = config.HIRO_API_RATE_LIMIT;
// Use Hiro API rate limits since we're hitting their endpoints
this.accountDataService = new StacksAccountDataService(
env,
hiroConfig.MAX_REQUESTS_PER_INTERVAL,
hiroConfig.INTERVAL_MS,
config.MAX_RETRIES,
config.RETRY_DELAY
);
}
async fetch(request: Request): Promise<Response> {
const url = new URL(request.url);
const path = url.pathname;
const address = path.split('/')[2];
// e.g., /stacks-account/{address}/nonce -> /nonce
const endpoint = url.pathname.replace(`/stacks-account/${address}`, '') || '/';
const method = request.method;
return handleRequest(
async () => {
if (!validateStacksAddress(address)) {
throw new ApiError(ErrorCode.INVALID_CONTRACT_ADDRESS, { address: address });
}
// Route to different functions based on the endpoint
if (endpoint.startsWith('/nonce')) {
return this.handleNonceRequest(request, endpoint, address);
}
// Default response for the root of the DO
if (endpoint === '/') {
return { message: `StacksAccountDO for ${address}. Supported endpoints: /nonce` };
}
throw new ApiError(ErrorCode.NOT_FOUND, { resource: endpoint });
},
this.env,
{ path: url.pathname, method }
);
}
private async handleNonceRequest(request: Request, endpoint: string, address: string): Promise<{ nonce: number }> {
const url = new URL(request.url);
const method = request.method;
if (endpoint === '/nonce' && method === 'GET') {
const bustCache = url.searchParams.get('bustCache') === 'true';
return this.getNonce(address, bustCache);
}
if (endpoint === '/nonce/sync' && method === 'POST') {
return this.syncNonce(address);
}
if (endpoint === '/nonce/update' && method === 'POST') {
const { nonce } = (await request.json()) as { nonce: number };
if (typeof nonce !== 'number') {
throw new ApiError(ErrorCode.INVALID_ARGUMENTS, { reason: 'Nonce must be a number' });
}
return this.updateNonce(nonce);
}
throw new ApiError(ErrorCode.INVALID_REQUEST, { reason: `Method ${method} not supported for ${endpoint}` });
}
private async getNonce(address: string, bustCache: boolean): Promise<{ nonce: number }> {
if (!bustCache) {
const storedNonce = await this.ctx.storage.get<number>('nonce');
if (storedNonce !== undefined) {
return { nonce: storedNonce };
}
}
// If cache is busted or nonce is not in storage, sync it
return this.syncNonce(address);
}
private async syncNonce(address: string): Promise<{ nonce: number }> {
const nonce = await this.accountDataService.fetchNonce(address);
await this.ctx.storage.put('nonce', nonce);
return { nonce };
}
private async updateNonce(nonce: number): Promise<{ nonce: number }> {
await this.ctx.storage.put('nonce', nonce);
return { nonce };
}
}