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 events_1 = __importDefault(require("events"));
|
52 | var client_1 = require("./client");
|
53 | var loom_pb_1 = require("./proto/loom_pb");
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 | var EvmContract = (function (_super) {
|
65 | __extends(EvmContract, _super);
|
66 | |
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 | function EvmContract(params) {
|
74 | var _this = _super.call(this) || this;
|
75 | _this._client = params.client;
|
76 | _this.address = params.contractAddr;
|
77 | _this.caller = params.callerAddr;
|
78 | var emitContractEvent = _this._emitContractEvent.bind(_this);
|
79 | _this.on('newListener', function (event) {
|
80 | if (event === EvmContract.EVENT && _this.listenerCount(event) === 0) {
|
81 | _this._client.on(client_1.ClientEvent.Contract, emitContractEvent);
|
82 | }
|
83 | });
|
84 | _this.on('removeListener', function (event) {
|
85 | if (event === EvmContract.EVENT && _this.listenerCount(event) === 0) {
|
86 | _this._client.removeListener(client_1.ClientEvent.Contract, emitContractEvent);
|
87 | }
|
88 | });
|
89 | return _this;
|
90 | }
|
91 | |
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 | EvmContract.prototype.callAsync = function (args, output) {
|
98 | return __awaiter(this, void 0, void 0, function () {
|
99 | var ui8InData, callTx, msgTx, tx;
|
100 | return __generator(this, function (_a) {
|
101 | ui8InData = Uint8Array.from(args);
|
102 | callTx = new loom_pb_1.CallTx();
|
103 | callTx.setVmType(loom_pb_1.VMType.EVM);
|
104 | callTx.setInput(ui8InData);
|
105 | msgTx = new loom_pb_1.MessageTx();
|
106 | msgTx.setFrom(this.caller.MarshalPB());
|
107 | msgTx.setTo(this.address.MarshalPB());
|
108 | msgTx.setData(callTx.serializeBinary());
|
109 | tx = new loom_pb_1.Transaction();
|
110 | tx.setId(2);
|
111 | tx.setData(msgTx.serializeBinary());
|
112 | return [2 , this._client.commitTxAsync(tx)];
|
113 | });
|
114 | });
|
115 | };
|
116 | |
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 | EvmContract.prototype.staticCallAsync = function (args, output) {
|
123 | return __awaiter(this, void 0, void 0, function () {
|
124 | var ui8InData;
|
125 | return __generator(this, function (_a) {
|
126 | ui8InData = Uint8Array.from(args);
|
127 | return [2 , this._client.queryAsync(this.address, ui8InData, loom_pb_1.VMType.EVM, this.caller)];
|
128 | });
|
129 | });
|
130 | };
|
131 | EvmContract.prototype._emitContractEvent = function (event) {
|
132 | if (event.contractAddress.equals(this.address)) {
|
133 | this.emit(EvmContract.EVENT, event);
|
134 | }
|
135 | };
|
136 | EvmContract.EVENT = 'event';
|
137 | return EvmContract;
|
138 | }(events_1.default));
|
139 | exports.EvmContract = EvmContract;
|
140 |
|
\ | No newline at end of file |