forked from onflow/freshmint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scripts.ts
44 lines (35 loc) · 1.12 KB
/
scripts.ts
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
import { FreshmintConfig } from './config';
import { FCLTransaction } from './fcl';
export type ScriptResultTransformer<T> = (result: any) => T;
export type ScriptBody = {
script: string;
args: (arg: any, t: any) => any[]; // TODO: use real types
computeLimit: number;
};
export class Script<T> {
private create: (config: FreshmintConfig) => ScriptBody | Promise<ScriptBody>;
private onResult: ScriptResultTransformer<T>;
constructor(
create: (config: FreshmintConfig) => ScriptBody | Promise<ScriptBody>,
onResult: ScriptResultTransformer<T>,
) {
this.create = create;
this.onResult = onResult;
}
async toFCLScript(config: FreshmintConfig): Promise<FCLTransaction> {
const { script, args, computeLimit } = await this.create(config);
return {
cadence: script,
args,
limit: computeLimit,
};
}
async transformResult(result: any): Promise<T> {
return await this.onResult(result);
}
}
class ScriptError extends Error {}
export function convertScriptError(error: any): Error {
// TODO: parse error and convert to correct error class
return new ScriptError(error);
}