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 Constant_1 = require("./Constant");
|
14 | const Setting_1 = require("./Setting");
|
15 | const Contract_1 = require("./contract/Contract");
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 | class Contracts {
|
24 | |
25 |
|
26 |
|
27 |
|
28 | constructor(config = {}) {
|
29 | this.setting = Setting_1.Setting.getInstance();
|
30 | this._privateKey = this.setting.privateKey;
|
31 | this.chainRpcProvider = this.setting.chainRpcProvider;
|
32 | this.asiLinkProvider = this.setting.asiLinkProvider;
|
33 | if (config.chainRpcProvider) {
|
34 | this.chainRpcProvider = config.chainRpcProvider;
|
35 | }
|
36 | if (config.asiLinkProvider) {
|
37 | this.asiLinkProvider = config.asiLinkProvider;
|
38 | }
|
39 | }
|
40 | |
41 |
|
42 |
|
43 | get chainRpcProvider() {
|
44 | return this._chainRpcProvider;
|
45 | }
|
46 | |
47 |
|
48 |
|
49 | set chainRpcProvider(rpc) {
|
50 | this._chainRpcProvider = rpc;
|
51 | }
|
52 | |
53 |
|
54 |
|
55 | get asiLinkProvider() {
|
56 | return this._asiLinkProvider;
|
57 | }
|
58 | |
59 |
|
60 |
|
61 | set asiLinkProvider(asilink) {
|
62 | this._asiLinkProvider = asilink;
|
63 | }
|
64 | |
65 |
|
66 |
|
67 | get privateKey() {
|
68 | return this._privateKey;
|
69 | }
|
70 | |
71 |
|
72 |
|
73 | set privateKey(pk) {
|
74 | this._privateKey = pk;
|
75 | }
|
76 | |
77 |
|
78 |
|
79 |
|
80 | getContract(address) {
|
81 | return __awaiter(this, void 0, void 0, function* () {
|
82 | let [err1, templateInfo] = yield await_to_js_1.to(this.chainRpcProvider.getContractTemplate(address));
|
83 | if (err1) {
|
84 | throw err1;
|
85 | }
|
86 | let { template_type, template_name } = templateInfo;
|
87 | let [err2, template] = yield await_to_js_1.to(this.chainRpcProvider.getContractTemplateInfoByName({
|
88 | category: template_type,
|
89 | templateName: template_name
|
90 | }));
|
91 | if (err2) {
|
92 | throw err2;
|
93 | }
|
94 | let abi = JSON.parse(template.abi);
|
95 | try {
|
96 | abi = JSON.parse(template.abi);
|
97 | }
|
98 | catch (e) {
|
99 | throw e;
|
100 | }
|
101 | let contractConfig = {
|
102 | contractAddress: address,
|
103 | abi: abi,
|
104 | asiLinkProvider: this.asiLinkProvider,
|
105 | chainRpcProvider: this.chainRpcProvider
|
106 | };
|
107 | let contract = new Contract_1.Contract(contractConfig);
|
108 | return contract;
|
109 | });
|
110 | }
|
111 | |
112 |
|
113 |
|
114 |
|
115 | execute(params) {
|
116 | return __awaiter(this, void 0, void 0, function* () {
|
117 | let { address, method, args = [], assetValue = 0, assetType = Constant_1.DefaultAsset, feeValue, feeType, caller, gasLimit } = params;
|
118 | if (!address) {
|
119 | throw new Error('Contract address is not provided');
|
120 | }
|
121 | if (!method) {
|
122 | throw new Error('Contract method is not provided');
|
123 | }
|
124 | let fee = this.setting.fee;
|
125 | if (feeValue && feeType) {
|
126 | fee = {
|
127 | amount: feeValue,
|
128 | asset: feeType
|
129 | };
|
130 | }
|
131 | let [err, contract] = yield await_to_js_1.to(this.getContract(address));
|
132 | if (err) {
|
133 | throw err;
|
134 | }
|
135 | let callParams = {
|
136 | methodName: method,
|
137 | args: args,
|
138 | amount: assetValue,
|
139 | asset: assetType,
|
140 | privateKey: this.privateKey,
|
141 | caller: caller,
|
142 | fee: fee,
|
143 | contractType: "call",
|
144 | gasLimit: gasLimit
|
145 | };
|
146 | let [err1, res] = yield await_to_js_1.to(contract.call(callParams));
|
147 | if (err1) {
|
148 | throw err1;
|
149 | }
|
150 | return res;
|
151 | });
|
152 | }
|
153 | |
154 |
|
155 |
|
156 |
|
157 | vote(params) {
|
158 | return __awaiter(this, void 0, void 0, function* () {
|
159 | let { address, method, args = [], voteValue = 0, assetType = Constant_1.DefaultAsset, feeValue, feeType, caller, gasLimit } = params;
|
160 | if (!address) {
|
161 | throw new Error('Contract address is not provided');
|
162 | }
|
163 | if (!method) {
|
164 | throw new Error('Contract method is not provided');
|
165 | }
|
166 | let fee = this.setting.fee;
|
167 | if (feeValue && feeType) {
|
168 | fee = {
|
169 | amount: feeValue,
|
170 | asset: feeType
|
171 | };
|
172 | }
|
173 | let [err, contract] = yield await_to_js_1.to(this.getContract(address));
|
174 | if (err) {
|
175 | throw err;
|
176 | }
|
177 | let callParams = {
|
178 | methodName: method,
|
179 | args: args,
|
180 | amount: 0,
|
181 | voteValue: voteValue,
|
182 | voteId: args[0],
|
183 | asset: assetType,
|
184 | privateKey: this.privateKey,
|
185 | fee: fee,
|
186 | caller: caller,
|
187 | contractType: "vote",
|
188 | gasLimit: gasLimit
|
189 | };
|
190 | let [err1, res] = yield await_to_js_1.to(contract.call(callParams));
|
191 | if (err1) {
|
192 | throw err1;
|
193 | }
|
194 | return res;
|
195 | });
|
196 | }
|
197 | |
198 |
|
199 |
|
200 |
|
201 |
|
202 | read(params) {
|
203 | return __awaiter(this, void 0, void 0, function* () {
|
204 | let { address, method, args = [], caller } = params;
|
205 | if (!address) {
|
206 | throw new Error('Contract address is not provided');
|
207 | }
|
208 | if (!method) {
|
209 | throw new Error('Contract method is not provided');
|
210 | }
|
211 | let [err, contract] = yield await_to_js_1.to(this.getContract(address));
|
212 | if (err) {
|
213 | throw err;
|
214 | }
|
215 | if (!contract.isReadOnlyMethod(method, args)) {
|
216 | throw new Error(method + " is not a view or pure method");
|
217 | }
|
218 | let callParams = {
|
219 | methodName: method,
|
220 | args: args,
|
221 | privateKey: this.privateKey,
|
222 | caller: caller
|
223 | };
|
224 | let [err1, res] = yield await_to_js_1.to(contract.call(callParams));
|
225 | if (err1) {
|
226 | throw err1;
|
227 | }
|
228 | return res;
|
229 | });
|
230 | }
|
231 | }
|
232 | exports.Contracts = Contracts;
|
233 |
|
\ | No newline at end of file |