UNPKG

10.5 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 txHelper = require("./utils/TxHelper");
13const ProviderFacade_1 = require("./providers/ProviderFacade");
14const Constant_1 = require("./Constant");
15const await_to_js_1 = require("await-to-js");
16/// TODO Reverse dependency, should be optimized
17const Transactions_1 = require("./Transactions");
18class TemplateWarehouse extends ProviderFacade_1.ProviderFacade {
19 constructor(args) {
20 super(args);
21 this.args = args;
22 this.initialized = false;
23 }
24 init() {
25 return __awaiter(this, void 0, void 0, function* () {
26 if (!this.chainRpcProvider) {
27 throw new Error('Can not init due to missing chain rpc provider');
28 }
29 let [err, res] = yield await_to_js_1.default(this.chainRpcProvider.getGenesisContract(Constant_1.SYSTEM_CONTRACT_ADDRESS.TemplateWarehouse));
30 if (err) {
31 throw err;
32 }
33 this.warehouseAddress = res.addressHex;
34 this.warehouseAbiInfo = JSON.parse(res.abiInfo);
35 this.warehouseByteCode = res.code;
36 for (let i = 0, len = this.warehouseAbiInfo.length; i < len; i++) {
37 let a = this.warehouseAbiInfo[i];
38 if (a.name == 'create') {
39 this.createAbi = a;
40 this.createAbiHash = txHelper.encodeFunctionId(a);
41 }
42 }
43 this.initialized = true;
44 this.transactions = new Transactions_1.Transactions(this.chainRpcProvider);
45 });
46 }
47 checkInit() {
48 return __awaiter(this, void 0, void 0, function* () {
49 if (!this.initialized) {
50 let [err, result] = yield await_to_js_1.default(this.init());
51 if (err) {
52 throw err;
53 }
54 }
55 });
56 }
57 createTemplate(params) {
58 return __awaiter(this, void 0, void 0, function* () {
59 yield this.checkInit();
60 const data = params.data || txHelper.generateCreateTemplateData(params.category, params.name, params.bytecode, params.abi, params.source);
61 if (params.privateKey) {
62 let from = txHelper.getAddressByPrivateKey(params.privateKey);
63 let [err2, tx] = yield await_to_js_1.default(this.transactions.generateRawTransaction({
64 to: this.warehouseAddress,
65 from: from,
66 amount: 0,
67 asset: Constant_1.DefaultAsset,
68 fee: params.fee,
69 gasLimit: params.gasLimit,
70 contractType: "template",
71 data: data
72 }));
73 if (err2) {
74 throw err2;
75 }
76 let rawTx = tx.sign([params.privateKey]).toHex();
77 let [err3, res] = yield await_to_js_1.default(this.chainRpcProvider.sendRawTransaction(rawTx));
78 if (err3) {
79 throw err3;
80 }
81 return res;
82 }
83 else {
84 if (!this.asiLinkProvider) {
85 throw new Error('Can not create template due to missing AsiLink provider');
86 }
87 if (!data) {
88 throw new Error('Can not create template due to missing binary data');
89 }
90 let callParams = {
91 to: this.warehouseAddress,
92 data: data,
93 type: 'template'
94 };
95 let [err, res] = yield await_to_js_1.default(this.asiLinkProvider.callContract(callParams));
96 if (err) {
97 throw err;
98 }
99 return res;
100 }
101 });
102 }
103 deploy(params) {
104 return __awaiter(this, void 0, void 0, function* () {
105 yield this.checkInit();
106 if (!params.templateId && !params.templateName) {
107 throw new Error('template id or template name is not specified');
108 }
109 // get template
110 let res, err;
111 if (params.templateId) {
112 [err, res] = yield await_to_js_1.default(this.chainRpcProvider.getContractTemplateInfoByKey({
113 key: params.templateId
114 }));
115 }
116 else {
117 [err, res] = yield await_to_js_1.default(this.chainRpcProvider.getContractTemplateInfoByName({
118 templateName: params.templateName,
119 category: params.category || Constant_1.DefaultCategory
120 }));
121 }
122 if (err) {
123 throw err;
124 }
125 let { abi, template_name, category, } = res;
126 let abiJson = JSON.parse(abi);
127 let constructorABI = {};
128 abiJson.forEach(i => {
129 if (i.type == 'constructor') {
130 constructorABI = i;
131 }
132 });
133 if (!constructorABI) {
134 throw new Error('no constructor abi found');
135 }
136 if (constructorABI.inputs && (constructorABI.inputs.length !== params.arguments.length)) {
137 throw new Error('input parameters length does not match the constructor arguments length!');
138 }
139 let data = txHelper.generateDeployContractData(category, template_name, constructorABI, params.arguments);
140 //deploy contract
141 if (params.privateKey) {
142 let from = txHelper.getAddressByPrivateKey(params.privateKey);
143 let [err2, tx] = yield await_to_js_1.default(this.transactions.generateRawTransaction({
144 to: from,
145 from: from,
146 amount: params.amount,
147 asset: params.asset,
148 fee: params.fee,
149 gasLimit: params.gasLimit,
150 contractType: "create",
151 data: data
152 }));
153 if (err2) {
154 throw err2;
155 }
156 let rawTx = tx.sign([params.privateKey]).toHex();
157 let [err3, res] = yield await_to_js_1.default(this.chainRpcProvider.sendRawTransaction(rawTx));
158 if (err3) {
159 throw err3;
160 }
161 return res;
162 }
163 else {
164 if (!this.asiLinkProvider) {
165 throw new Error('can not deploy contract instance due to missing AsiLink provider');
166 }
167 if (!data) {
168 throw new Error('can not deploy contract instance due to missing binary data');
169 }
170 let callParams = {
171 amount: params.amount,
172 data: data,
173 type: 'create'
174 };
175 let [err, res] = yield await_to_js_1.default(this.asiLinkProvider.callContract(callParams));
176 if (err) {
177 throw err;
178 }
179 return res;
180 }
181 });
182 }
183 // TODO: whether private key should be passed in?
184 create(params) {
185 return __awaiter(this, void 0, void 0, function* () {
186 yield this.checkInit();
187 if (params.category == undefined) {
188 throw new Error('Template category is undefined');
189 }
190 if (!params.name) {
191 throw new Error('Template name is not specified');
192 }
193 if (!params.bytecode) {
194 throw new Error('Template bytecode is not specified');
195 }
196 if (!params.abi) {
197 throw new Error('Template abi is not specified');
198 }
199 if (!params.source) {
200 throw new Error('Template source is not specified');
201 }
202 const data = txHelper.generateCreateTemplateData(params.category, params.name, params.bytecode, params.abi, params.source);
203 // send transaction directly
204 if (params.privateKey) {
205 let address = txHelper.getAddressByPrivateKey(params.privateKey);
206 let [err1, tx] = yield await_to_js_1.default(this.transactions.generateRawTransaction({
207 from: address,
208 to: this.warehouseAddress,
209 amount: 0,
210 asset: Constant_1.DefaultAsset,
211 fee: params.fee,
212 data: data,
213 contractType: "template",
214 gasLimit: params.gasLimit
215 }));
216 if (err1) {
217 throw err1;
218 }
219 let rawTx = tx.sign([params.privateKey]).toHex();
220 let [err2, res] = yield await_to_js_1.default(this.chainRpcProvider.sendRawTransaction(rawTx));
221 if (err2) {
222 throw err2;
223 }
224 return res;
225 }
226 else {
227 if (!this.asiLinkProvider) {
228 throw new Error('can not create template due to missing AsiLink provider');
229 }
230 let callParams = {
231 to: this.warehouseAddress,
232 data: data,
233 type: 'template'
234 };
235 let [err, res] = yield await_to_js_1.default(this.asiLinkProvider.callContract(callParams));
236 if (err) {
237 throw err;
238 }
239 return res;
240 }
241 });
242 }
243}
244exports.TemplateWarehouse = TemplateWarehouse;
245//# sourceMappingURL=TemplateWarehouse.js.map
\No newline at end of file