1 | import Blockchain from '@neo-one/node-blockchain';
|
2 | import Ast, { DiagnosticCategory, SourceFile } from 'ts-simple-ast';
|
3 | import { InvocationResult } from '@neo-one/client-core';
|
4 | import { Monitor } from '@neo-one/monitor';
|
5 |
|
6 | import levelup from 'levelup';
|
7 | import levelUpStorage from '@neo-one/node-storage-levelup';
|
8 | import memdown from 'memdown';
|
9 | import { test as testNet } from '@neo-one/node-neo-settings';
|
10 | import vm from '@neo-one/node-vm';
|
11 |
|
12 | import { compile } from './compile';
|
13 |
|
14 | export const executeScript = async (
|
15 | monitor: Monitor,
|
16 | ast: Ast,
|
17 | sourceFile: SourceFile,
|
18 | prelude: Buffer = Buffer.alloc(0, 0),
|
19 | ): Promise<InvocationResult> => {
|
20 | const blockchain = await Blockchain.create({
|
21 | log: () => {
|
22 |
|
23 | },
|
24 | settings: testNet,
|
25 | storage: levelUpStorage({
|
26 | context: { messageMagic: testNet.messageMagic },
|
27 | db: levelup(
|
28 |
|
29 | memdown(),
|
30 | ),
|
31 | }),
|
32 | vm,
|
33 | monitor,
|
34 | });
|
35 | const { code: compiledCode, context } = compile({ ast, sourceFile });
|
36 | const error = context.diagnostics.filter(
|
37 | (diagnostic) => diagnostic.category === DiagnosticCategory.Error,
|
38 | )[0];
|
39 | if (error != null) {
|
40 | throw new Error(
|
41 | `Compilation error: ${error.messageText} at ${error.source}`,
|
42 | );
|
43 | }
|
44 |
|
45 | const result = await blockchain.invokeScript(
|
46 | Buffer.concat([prelude, compiledCode]),
|
47 | monitor,
|
48 | );
|
49 | return result;
|
50 | };
|