UNPKG

9.85 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 google_protobuf_1 = require("google-protobuf");
52var events_1 = __importDefault(require("events"));
53var client_1 = require("./client");
54var loom_pb_1 = require("./proto/loom_pb");
55var crypto_utils_1 = require("./crypto-utils");
56/**
57 * The Contract class streamlines interaction with a contract that was deployed on a Loom DAppChain.
58 * Each instance of this class is bound to a specific contract, and provides a simple way of calling
59 * into and querying that contract.
60 *
61 * A contract instance can be used to listen to events emitted by the corresponding smart contract,
62 * there is currently only one type of event. The event subscription API matches the NodeJS
63 * EventEmitter API. For example...
64 *
65 * function subscribeToEvents(contract: Contract) {
66 * contract.on(Contract.EVENT, (event: IChainEventArgs) => {
67 * const dataStr = Buffer.from(event.data as Buffer).toString('utf8')
68 * const dataObj = JSON.parse(dataStr)
69 * console.log('Contract Event: ' + dataStr)
70 * })
71 * }
72 */
73var Contract = /** @class */ (function (_super) {
74 __extends(Contract, _super);
75 /**
76 * @param params Parameters.
77 * @param params.contractAddr Address of a contract on the Loom DAppChain.
78 * @param params.contractName Name of the contract.
79 * @param params.callerAddr: Address of the caller, generated from the public key of the tx signer,
80 * e.g. `new Address(client.chainId, LocalAddress.fromPublicKey(pubKey))`
81 * @param params.client: Client to use to communicate with the contract.
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 * Calls a contract method that mutates state.
104 * The call into the contract is accomplished by committing a tx to the DAppChain.
105 * @param method Contract method to call.
106 * @param args Arguments to pass to the contract method.
107 * @returns A promise that will be resolved with return value (if any) of the contract method.
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 /*yield*/, 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 /*return*/, output];
141 }
142 });
143 });
144 };
145 /**
146 * Calls a contract method that doesn't mutate state.
147 * This method is usually used to query the current contract state, it doesn't commit any txs.
148 * @param method Contract method to call.
149 * @param args Arguments to pass to the contract method.
150 * @returns A promise that will be resolved with the return value of the contract method.
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 /*yield*/, 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 /*return*/, 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));
181exports.Contract = Contract;
182//# sourceMappingURL=contract.js.map
\No newline at end of file