-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathinterpreter-server.ts
More file actions
56 lines (51 loc) · 1.47 KB
/
interpreter-server.ts
File metadata and controls
56 lines (51 loc) · 1.47 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
import type { DecodedTransaction } from '@3loop/transaction-decoder'
import {
Interpreter,
QuickjsInterpreterLive,
QuickjsConfig,
TransactionInterpreter,
} from '@3loop/transaction-interpreter'
import { Effect, Layer } from 'effect'
import variant from '@jitl/quickjs-singlefile-cjs-release-sync'
// Server-side interpreter configuration using Node.js QuickJS variant
// This runs in Node.js context where CORS restrictions don't apply
const config = Layer.succeed(QuickjsConfig, {
variant: variant,
runtimeConfig: {
timeout: 5000,
useFetch: true,
},
})
const layer = Layer.provide(QuickjsInterpreterLive, config)
export interface Interpretation {
tx: DecodedTransaction
interpretation: any
error?: string
}
export const applyInterpreterServer = async (
decodedTx: DecodedTransaction,
interpreter: Interpreter,
interpretAsUserAddress?: string,
): Promise<Interpretation> => {
const runnable = Effect.gen(function* () {
const interpreterService = yield* TransactionInterpreter
const interpretation = yield* interpreterService.interpretTransaction(decodedTx, interpreter, {
interpretAsUserAddress,
})
return interpretation
}).pipe(Effect.provide(layer))
return Effect.runPromise(runnable)
.then((interpretation) => {
return {
tx: decodedTx,
interpretation,
}
})
.catch((e) => {
return {
tx: decodedTx,
interpretation: null,
error: (e as Error).message,
}
})
}