1 | "use strict";
|
2 | var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
3 | function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
4 | return new (P || (P = Promise))(function (resolve, reject) {
|
5 | function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
6 | function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
7 | function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
8 | step((generator = generator.apply(thisArg, _arguments || [])).next());
|
9 | });
|
10 | };
|
11 | Object.defineProperty(exports, "__esModule", { value: true });
|
12 | const await_to_js_1 = require("await-to-js");
|
13 | const Setting_1 = require("./Setting");
|
14 | const TemplateWarehouse_1 = require("./TemplateWarehouse");
|
15 | const Constant_1 = require("./Constant");
|
16 |
|
17 |
|
18 |
|
19 | class Templates {
|
20 | |
21 |
|
22 |
|
23 |
|
24 | constructor(config = {}) {
|
25 | this.setting = Setting_1.Setting.getInstance();
|
26 | this._privateKey = this.setting.privateKey;
|
27 | this.chainRpcProvider = this.setting.chainRpcProvider;
|
28 | this.asiLinkProvider = this.setting.asiLinkProvider;
|
29 | if (config.chainRpcProvider) {
|
30 | this.chainRpcProvider = config.chainRpcProvider;
|
31 | }
|
32 | if (config.asiLinkProvider) {
|
33 | this.asiLinkProvider = config.asiLinkProvider;
|
34 | }
|
35 | this.templateWarehouse = new TemplateWarehouse_1.TemplateWarehouse({
|
36 | chainRpcProvider: this.chainRpcProvider,
|
37 | asiLinkProvider: this.asiLinkProvider
|
38 | });
|
39 | this.compiler = this.setting.compiler;
|
40 | }
|
41 | |
42 |
|
43 |
|
44 | get chainRpcProvider() {
|
45 | return this._chainRpcProvider;
|
46 | }
|
47 | |
48 |
|
49 |
|
50 | set chainRpcProvider(rpc) {
|
51 | this._chainRpcProvider = rpc;
|
52 | }
|
53 | |
54 |
|
55 |
|
56 | get asiLinkProvider() {
|
57 | return this._asiLinkProvider;
|
58 | }
|
59 | |
60 |
|
61 |
|
62 | set asiLinkProvider(asilink) {
|
63 | this._asiLinkProvider = asilink;
|
64 | }
|
65 | |
66 |
|
67 |
|
68 | get compiler() {
|
69 | return this._compiler;
|
70 | }
|
71 | |
72 |
|
73 |
|
74 | set compiler(compiler) {
|
75 | this._compiler = compiler;
|
76 | }
|
77 | |
78 |
|
79 |
|
80 | get privateKey() {
|
81 | return this._privateKey;
|
82 | }
|
83 | |
84 |
|
85 |
|
86 | set privateKey(pk) {
|
87 | this._privateKey = pk;
|
88 | }
|
89 | |
90 |
|
91 |
|
92 |
|
93 |
|
94 | submitTemplate(params) {
|
95 | return __awaiter(this, void 0, void 0, function* () {
|
96 | if (!params.source && !params.path) {
|
97 | throw new Error('source file object or path is not specified!');
|
98 | }
|
99 | if (!params.templateName) {
|
100 | throw new Error('template name is not specified!');
|
101 | }
|
102 | if (!this.privateKey) {
|
103 | throw new Error('private key is not specified in setting!');
|
104 | }
|
105 | let compileResult, err;
|
106 | if (params.path) {
|
107 | [err, compileResult] = yield await_to_js_1.to(this.compiler.compileSol(params.source || params.path));
|
108 | if (err) {
|
109 | throw err;
|
110 | }
|
111 | }
|
112 | if (params.source) {
|
113 | let sources = {};
|
114 | sources[params.templateName] = {
|
115 | content: params.source
|
116 | };
|
117 | if (!this.compiler) {
|
118 | throw new Error('compiler is not specified!');
|
119 | }
|
120 | this.compiler.setLibs(params.libs || {});
|
121 | [err, compileResult] = yield await_to_js_1.to(this.compiler.compile(sources));
|
122 | if (err) {
|
123 | throw err;
|
124 | }
|
125 | }
|
126 | let contracts = compileResult.contracts;
|
127 | let source = compileResult.source;
|
128 | let contract = contracts[params.contractName] || contracts[0];
|
129 | let byteCode = contract.evm.bytecode.object;
|
130 | let abi = contract.abi;
|
131 | let createParams = {
|
132 | category: Constant_1.DefaultCategory,
|
133 | name: params.templateName,
|
134 | bytecode: byteCode,
|
135 | abi: JSON.stringify(abi),
|
136 | source: source,
|
137 | privateKey: this.privateKey,
|
138 | fee: params.fee || this.setting.fee,
|
139 | gasLimit: params.gasLimit
|
140 | };
|
141 | let [err1, res] = yield await_to_js_1.to(this.templateWarehouse.createTemplate(createParams));
|
142 | if (err1) {
|
143 | throw err1;
|
144 | }
|
145 | return res;
|
146 | });
|
147 | }
|
148 | |
149 |
|
150 |
|
151 |
|
152 |
|
153 | deployContract(params) {
|
154 | return __awaiter(this, void 0, void 0, function* () {
|
155 | if (!this.privateKey) {
|
156 | throw new Error('private key is not specified in setting!');
|
157 | }
|
158 | let deployParams = {
|
159 | templateId: params.templateId,
|
160 | arguments: params.constructorArguments || [],
|
161 | privateKey: this.privateKey,
|
162 | amount: params.amount || 0,
|
163 | asset: params.asset || Constant_1.DefaultAsset,
|
164 | fee: params.fee || this.setting.fee,
|
165 | gasLimit: params.gasLimit
|
166 | };
|
167 | let [err, res] = yield await_to_js_1.to(this.templateWarehouse.deploy(deployParams));
|
168 | if (err) {
|
169 | return err;
|
170 | }
|
171 | return res;
|
172 | });
|
173 | }
|
174 | }
|
175 | exports.Templates = Templates;
|
176 |
|
\ | No newline at end of file |