UNPKG

7.49 kBJavaScriptView Raw
1"use strict";
2var __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})();
12var __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};
20var __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};
47var __importDefault = (this && this.__importDefault) || function (mod) {
48 return (mod && mod.__esModule) ? mod : { "default": mod };
49};
50Object.defineProperty(exports, "__esModule", { value: true });
51var events_1 = __importDefault(require("events"));
52var client_1 = require("./client");
53var loom_pb_1 = require("./proto/loom_pb");
54/**
55 * The EvmContract class streamlines interaction with a contract that was
56 * deployed on a Loom DAppChain EVM.
57 * Each instance of this class is bound to a specific contract, and provides a simple way of calling
58 * into and querying that contract.
59 *
60 * A contract instance can be used to listen to events emitted by the corresponding smart contract,
61 * there is currently only one type of event. The event subscription API matches the NodeJS
62 * EventEmitter API.
63 */
64var EvmContract = /** @class */ (function (_super) {
65 __extends(EvmContract, _super);
66 /**
67 * @param params Parameters.
68 * @param params.contractAddr Address of a contract on the Loom DAppChain EVM.
69 * @param params.callerAddr: Address of the caller, generated from the public key of the tx signer,
70 * e.g. `new Address(client.chainId, LocalAddress.fromPublicKey(pubKey))`
71 * @param params.client: Client to use to communicate with the contract.
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 * Calls a contract method that mutates state.
93 * The call into the contract is accomplished by committing a tx to the DAppChain.
94 * @param args ABI encoded function signature and input paramters.
95 * @returns A promise that will be resolved with return value (if any) of the contract method.
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 /*return*/, this._client.commitTxAsync(tx)];
113 });
114 });
115 };
116 /**
117 * Calls a method of a contract running on an EVM that doesn't mutate state.
118 * This method is usually used to query the current contract state, it doesn't commit any txs.
119 * @param args ABI encoded function signature and input paramters.
120 * @returns A promise that will be resolved with the return value of the contract method.
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 /*return*/, 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));
139exports.EvmContract = EvmContract;
140//# sourceMappingURL=evm-contract.js.map
\No newline at end of file