UNPKG

1.41 kBPlain TextView Raw
1import Blockchain from '@neo-one/node-blockchain';
2import Ast, { DiagnosticCategory, SourceFile } from 'ts-simple-ast';
3import { InvocationResult } from '@neo-one/client-core';
4import { Monitor } from '@neo-one/monitor';
5
6import levelup from 'levelup';
7import levelUpStorage from '@neo-one/node-storage-levelup';
8import memdown from 'memdown';
9import { test as testNet } from '@neo-one/node-neo-settings';
10import vm from '@neo-one/node-vm';
11
12import { compile } from './compile';
13
14export 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 // tslint:disable-next-line
23 },
24 settings: testNet,
25 storage: levelUpStorage({
26 context: { messageMagic: testNet.messageMagic },
27 db: levelup(
28 // @ts-ignore
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};