Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/many-houses-walk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@3loop/transaction-interpreter': patch
---

Add options parameter to the transaction interpreter
22 changes: 18 additions & 4 deletions packages/transaction-interpreter/src/EvalInterpreter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,28 @@ const make = Effect.succeed({
id: `${decodedTx.chainID}:${decodedTx.toAddress}`,
}
},
interpretTx: (decodedTx: DecodedTransaction, interpreter: Interpreter) =>
interpretTx: (
decodedTx: DecodedTransaction,
interpreter: Interpreter,
options?: {
interpretAsUserAddress?: string
},
) =>
Effect.sync(() => {
// TODO: add ability to surpress warning on acknowledge
Effect.logWarning('Using eval in production can result in security vulnerabilities. Use at your own risk.')

const input = stringify(decodedTx)
const code = interpreter.schema
const result = localEval(code, input)
let input
if (options?.interpretAsUserAddress) {
input = stringify({
...decodedTx,
fromAddress: options.interpretAsUserAddress,
})
} else {
input = stringify(decodedTx)
}

const result = localEval(interpreter.schema, input)
return result
}).pipe(Effect.withSpan('TransactionInterpreter.interpretTx')),
})
Expand Down
19 changes: 15 additions & 4 deletions packages/transaction-interpreter/src/QuickjsInterpreter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,22 @@ const make = Effect.gen(function* () {
id: `${decodedTx.chainID}:${decodedTx.toAddress}`,
}
},
interpretTx: (decodedTx: DecodedTransaction, interpreter: Interpreter) =>
interpretTx: (
decodedTx: DecodedTransaction,
interpreter: Interpreter,
options?: { interpretAsUserAddress?: string },
) =>
Effect.gen(function* () {
const input = stringify(decodedTx)
const code = interpreter.schema
const result = yield* vm.eval(code + '\n' + 'transformEvent(' + input + ')')
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')),
}
Expand Down
3 changes: 3 additions & 0 deletions packages/transaction-interpreter/src/interpreter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ export interface TransactionInterpreter {
readonly interpretTx: (
decodedTx: DecodedTransaction,
interpreter: Interpreter,
options?: {
interpretAsUserAddress?: string
},
) => Effect.Effect<InterpretedTransaction, InterpreterError, never>
}

Expand Down
Loading