UNPKG

6.45 kBJavaScriptView Raw
1"use strict";
2var __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};
11Object.defineProperty(exports, "__esModule", { value: true });
12const await_to_js_1 = require("await-to-js");
13const Setting_1 = require("./Setting");
14const TemplateWarehouse_1 = require("./TemplateWarehouse");
15const Constant_1 = require("./Constant");
16/**
17 * High level Templates object used to submit new templates and deploy contract instances on Asimov blockchain.
18 */
19class Templates {
20 /**
21 * Constructor of Contracts
22 * @param config configurations including ChainRPC provider and AsiLink provider
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 * getter of ChainRPC provider
43 */
44 get chainRpcProvider() {
45 return this._chainRpcProvider;
46 }
47 /**
48 * setter of ChainRPC provider
49 */
50 set chainRpcProvider(rpc) {
51 this._chainRpcProvider = rpc;
52 }
53 /**
54 * getter of AsiLink provider
55 */
56 get asiLinkProvider() {
57 return this._asiLinkProvider;
58 }
59 /**
60 * setter of AsiLink provider. AsiLink provider is set when developing Web DApps.
61 */
62 set asiLinkProvider(asilink) {
63 this._asiLinkProvider = asilink;
64 }
65 /**
66 * getter of internal Complier object
67 */
68 get compiler() {
69 return this._compiler;
70 }
71 /**
72 * setter of internal Complier object
73 */
74 set compiler(compiler) {
75 this._compiler = compiler;
76 }
77 /**
78 * getter of private key.
79 */
80 get privateKey() {
81 return this._privateKey;
82 }
83 /**
84 * setter of private key. Private key is set when developing automation scripts.
85 */
86 set privateKey(pk) {
87 this._privateKey = pk;
88 }
89 /**
90 * Submit a template on Asimov blockchain.
91 * @param params Parameters used to submit a template.
92 * @return Transaction id/template id.
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 * Deploy a contract instance from a template on Asimov blockchain.
150 * @param params Parameters used to deploy a contract instance.
151 * @return Transaction id.
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}
175exports.Templates = Templates;
176//# sourceMappingURL=Templates.js.map
\No newline at end of file