Skip to content

Commit 90248c1

Browse files
Add options parameter to the transaction interpreter (#225)
1 parent c7c0819 commit 90248c1

File tree

4 files changed

+41
-8
lines changed

4 files changed

+41
-8
lines changed

.changeset/many-houses-walk.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@3loop/transaction-interpreter': patch
3+
---
4+
5+
Add options parameter to the transaction interpreter

packages/transaction-interpreter/src/EvalInterpreter.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,28 @@ const make = Effect.succeed({
2323
id: `${decodedTx.chainID}:${decodedTx.toAddress}`,
2424
}
2525
},
26-
interpretTx: (decodedTx: DecodedTransaction, interpreter: Interpreter) =>
26+
interpretTx: (
27+
decodedTx: DecodedTransaction,
28+
interpreter: Interpreter,
29+
options?: {
30+
interpretAsUserAddress?: string
31+
},
32+
) =>
2733
Effect.sync(() => {
2834
// TODO: add ability to surpress warning on acknowledge
2935
Effect.logWarning('Using eval in production can result in security vulnerabilities. Use at your own risk.')
3036

31-
const input = stringify(decodedTx)
32-
const code = interpreter.schema
33-
const result = localEval(code, input)
37+
let input
38+
if (options?.interpretAsUserAddress) {
39+
input = stringify({
40+
...decodedTx,
41+
fromAddress: options.interpretAsUserAddress,
42+
})
43+
} else {
44+
input = stringify(decodedTx)
45+
}
46+
47+
const result = localEval(interpreter.schema, input)
3448
return result
3549
}).pipe(Effect.withSpan('TransactionInterpreter.interpretTx')),
3650
})

packages/transaction-interpreter/src/QuickjsInterpreter.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,22 @@ const make = Effect.gen(function* () {
2222
id: `${decodedTx.chainID}:${decodedTx.toAddress}`,
2323
}
2424
},
25-
interpretTx: (decodedTx: DecodedTransaction, interpreter: Interpreter) =>
25+
interpretTx: (
26+
decodedTx: DecodedTransaction,
27+
interpreter: Interpreter,
28+
options?: { interpretAsUserAddress?: string },
29+
) =>
2630
Effect.gen(function* () {
27-
const input = stringify(decodedTx)
28-
const code = interpreter.schema
29-
const result = yield* vm.eval(code + '\n' + 'transformEvent(' + input + ')')
31+
let input
32+
if (options?.interpretAsUserAddress) {
33+
input = stringify({
34+
...decodedTx,
35+
fromAddress: options.interpretAsUserAddress,
36+
})
37+
} else {
38+
input = stringify(decodedTx)
39+
}
40+
const result = yield* vm.eval(interpreter.schema + '\n' + 'transformEvent(' + input + ')')
3041
return result
3142
}).pipe(Effect.withSpan('TransactionInterpreter.interpretTx')),
3243
}

packages/transaction-interpreter/src/interpreter.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ export interface TransactionInterpreter {
88
readonly interpretTx: (
99
decodedTx: DecodedTransaction,
1010
interpreter: Interpreter,
11+
options?: {
12+
interpretAsUserAddress?: string
13+
},
1114
) => Effect.Effect<InterpretedTransaction, InterpreterError, never>
1215
}
1316

0 commit comments

Comments
 (0)