1 | "use strict";
|
2 | var __extends = (this && this.__extends) || (function () {
|
3 | var extendStatics = Object.setPrototypeOf ||
|
4 | ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
5 | function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
|
6 | return function (d, b) {
|
7 | extendStatics(d, b);
|
8 | function __() { this.constructor = d; }
|
9 | d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
10 | };
|
11 | })();
|
12 | var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
13 | return new (P || (P = Promise))(function (resolve, reject) {
|
14 | function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
15 | function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
16 | function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
|
17 | step((generator = generator.apply(thisArg, _arguments || [])).next());
|
18 | });
|
19 | };
|
20 | var __generator = (this && this.__generator) || function (thisArg, body) {
|
21 | var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
22 | return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
23 | function verb(n) { return function (v) { return step([n, v]); }; }
|
24 | function step(op) {
|
25 | if (f) throw new TypeError("Generator is already executing.");
|
26 | while (_) try {
|
27 | if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
28 | if (y = 0, t) op = [op[0] & 2, t.value];
|
29 | switch (op[0]) {
|
30 | case 0: case 1: t = op; break;
|
31 | case 4: _.label++; return { value: op[1], done: false };
|
32 | case 5: _.label++; y = op[1]; op = [0]; continue;
|
33 | case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
34 | default:
|
35 | if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
36 | if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
37 | if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
38 | if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
39 | if (t[2]) _.ops.pop();
|
40 | _.trys.pop(); continue;
|
41 | }
|
42 | op = body.call(thisArg, _);
|
43 | } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
44 | if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
45 | }
|
46 | };
|
47 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
48 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
49 | };
|
50 | Object.defineProperty(exports, "__esModule", { value: true });
|
51 | var google_protobuf_1 = require("google-protobuf");
|
52 | var events_1 = __importDefault(require("events"));
|
53 | var client_1 = require("./client");
|
54 | var loom_pb_1 = require("./proto/loom_pb");
|
55 | var crypto_utils_1 = require("./crypto-utils");
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 | var Contract = (function (_super) {
|
74 | __extends(Contract, _super);
|
75 | |
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 | function Contract(params) {
|
84 | var _this = _super.call(this) || this;
|
85 | _this._client = params.client;
|
86 | _this.name = params.contractName;
|
87 | _this.address = params.contractAddr;
|
88 | _this.caller = params.callerAddr;
|
89 | var emitContractEvent = _this._emitContractEvent.bind(_this);
|
90 | _this.on('newListener', function (event) {
|
91 | if (event === Contract.EVENT && _this.listenerCount(event) === 0) {
|
92 | _this._client.on(client_1.ClientEvent.Contract, emitContractEvent);
|
93 | }
|
94 | });
|
95 | _this.on('removeListener', function (event) {
|
96 | if (event === Contract.EVENT && _this.listenerCount(event) === 0) {
|
97 | _this._client.removeListener(client_1.ClientEvent.Contract, emitContractEvent);
|
98 | }
|
99 | });
|
100 | return _this;
|
101 | }
|
102 | |
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 | Contract.prototype.callAsync = function (method, args, output) {
|
110 | return __awaiter(this, void 0, void 0, function () {
|
111 | var methodTx, request, callTx, msgTx, tx, result, resp, msgClass;
|
112 | return __generator(this, function (_a) {
|
113 | switch (_a.label) {
|
114 | case 0:
|
115 | methodTx = new loom_pb_1.ContractMethodCall();
|
116 | methodTx.setMethod(method);
|
117 | methodTx.setArgs(args.serializeBinary());
|
118 | request = new loom_pb_1.Request();
|
119 | request.setContentType(loom_pb_1.EncodingType.PROTOBUF3);
|
120 | request.setAccept(loom_pb_1.EncodingType.PROTOBUF3);
|
121 | request.setBody(methodTx.serializeBinary());
|
122 | callTx = new loom_pb_1.CallTx();
|
123 | callTx.setVmType(loom_pb_1.VMType.PLUGIN);
|
124 | callTx.setInput(request.serializeBinary());
|
125 | msgTx = new loom_pb_1.MessageTx();
|
126 | msgTx.setFrom(this.caller.MarshalPB());
|
127 | msgTx.setTo(this.address.MarshalPB());
|
128 | msgTx.setData(callTx.serializeBinary());
|
129 | tx = new loom_pb_1.Transaction();
|
130 | tx.setId(2);
|
131 | tx.setData(msgTx.serializeBinary());
|
132 | return [4 , this._client.commitTxAsync(tx)];
|
133 | case 1:
|
134 | result = _a.sent();
|
135 | if (result && output) {
|
136 | resp = loom_pb_1.Response.deserializeBinary(crypto_utils_1.bufferToProtobufBytes(result));
|
137 | msgClass = output.constructor;
|
138 | google_protobuf_1.Message.copyInto(msgClass.deserializeBinary(resp.getBody_asU8()), output);
|
139 | }
|
140 | return [2 , output];
|
141 | }
|
142 | });
|
143 | });
|
144 | };
|
145 | |
146 |
|
147 |
|
148 |
|
149 |
|
150 |
|
151 |
|
152 | Contract.prototype.staticCallAsync = function (method, args, output) {
|
153 | return __awaiter(this, void 0, void 0, function () {
|
154 | var query, result, msgClass;
|
155 | return __generator(this, function (_a) {
|
156 | switch (_a.label) {
|
157 | case 0:
|
158 | query = new loom_pb_1.ContractMethodCall();
|
159 | query.setMethod(method);
|
160 | query.setArgs(args.serializeBinary());
|
161 | return [4 , this._client.queryAsync(this.address, query.serializeBinary(), loom_pb_1.VMType.PLUGIN, this.caller)];
|
162 | case 1:
|
163 | result = _a.sent();
|
164 | if (result && output) {
|
165 | msgClass = output.constructor;
|
166 | google_protobuf_1.Message.copyInto(msgClass.deserializeBinary(crypto_utils_1.bufferToProtobufBytes(result)), output);
|
167 | }
|
168 | return [2 , output];
|
169 | }
|
170 | });
|
171 | });
|
172 | };
|
173 | Contract.prototype._emitContractEvent = function (event) {
|
174 | if (event.contractAddress.equals(this.address)) {
|
175 | this.emit(Contract.EVENT, event);
|
176 | }
|
177 | };
|
178 | Contract.EVENT = 'event';
|
179 | return Contract;
|
180 | }(events_1.default));
|
181 | exports.Contract = Contract;
|
182 |
|
\ | No newline at end of file |