-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathQuickjsInterpreter.ts
More file actions
46 lines (41 loc) · 1.5 KB
/
QuickjsInterpreter.ts
File metadata and controls
46 lines (41 loc) · 1.5 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
import { stringify } from './helpers/stringify.js'
import type { DecodedTransaction } from '@3loop/transaction-decoder'
import { Effect, Layer } from 'effect'
import { Interpreter } from './types.js'
import { getInterpreter } from './interpreters.js'
import { QuickjsVM } from './quickjs.js'
import { TransactionInterpreter } from './interpreter.js'
const make = Effect.gen(function* () {
const vm = yield* QuickjsVM
return {
// NOTE: We could export this separately to allow bundling the interpreters separately
findInterpreter: (decodedTx: DecodedTransaction) => {
if (!decodedTx.toAddress) return undefined
const code = getInterpreter(decodedTx)
if (!code) return undefined
return {
schema: code,
id: `${decodedTx.chainID}:${decodedTx.toAddress}`,
}
},
interpretTx: (
decodedTx: DecodedTransaction,
interpreter: Interpreter,
options?: { interpretAsUserAddress?: string },
) =>
Effect.gen(function* () {
let input
if (options?.interpretAsUserAddress) {
input = stringify({
...decodedTx,
fromAddress: options.interpretAsUserAddress,
})
} else {
input = stringify(decodedTx)
}
const result = yield* vm.eval(interpreter.schema + '\n' + 'transformEvent(' + input + ')')
return result
}).pipe(Effect.withSpan('TransactionInterpreter.interpretTx')),
}
})
export const QuickjsInterpreterLive = Layer.scoped(TransactionInterpreter, make)