interface.js 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664
  1. "use strict";
  2. var __extends = (this && this.__extends) || (function () {
  3. var extendStatics = function (d, b) {
  4. extendStatics = Object.setPrototypeOf ||
  5. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  6. function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
  7. return extendStatics(d, b);
  8. };
  9. return function (d, b) {
  10. if (typeof b !== "function" && b !== null)
  11. throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
  12. extendStatics(d, b);
  13. function __() { this.constructor = d; }
  14. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  15. };
  16. })();
  17. Object.defineProperty(exports, "__esModule", { value: true });
  18. exports.Interface = exports.Indexed = exports.ErrorDescription = exports.TransactionDescription = exports.LogDescription = exports.checkResultErrors = void 0;
  19. var address_1 = require("@ethersproject/address");
  20. var bignumber_1 = require("@ethersproject/bignumber");
  21. var bytes_1 = require("@ethersproject/bytes");
  22. var hash_1 = require("@ethersproject/hash");
  23. var keccak256_1 = require("@ethersproject/keccak256");
  24. var properties_1 = require("@ethersproject/properties");
  25. var abi_coder_1 = require("./abi-coder");
  26. var abstract_coder_1 = require("./coders/abstract-coder");
  27. Object.defineProperty(exports, "checkResultErrors", { enumerable: true, get: function () { return abstract_coder_1.checkResultErrors; } });
  28. var fragments_1 = require("./fragments");
  29. var logger_1 = require("@ethersproject/logger");
  30. var _version_1 = require("./_version");
  31. var logger = new logger_1.Logger(_version_1.version);
  32. var LogDescription = /** @class */ (function (_super) {
  33. __extends(LogDescription, _super);
  34. function LogDescription() {
  35. return _super !== null && _super.apply(this, arguments) || this;
  36. }
  37. return LogDescription;
  38. }(properties_1.Description));
  39. exports.LogDescription = LogDescription;
  40. var TransactionDescription = /** @class */ (function (_super) {
  41. __extends(TransactionDescription, _super);
  42. function TransactionDescription() {
  43. return _super !== null && _super.apply(this, arguments) || this;
  44. }
  45. return TransactionDescription;
  46. }(properties_1.Description));
  47. exports.TransactionDescription = TransactionDescription;
  48. var ErrorDescription = /** @class */ (function (_super) {
  49. __extends(ErrorDescription, _super);
  50. function ErrorDescription() {
  51. return _super !== null && _super.apply(this, arguments) || this;
  52. }
  53. return ErrorDescription;
  54. }(properties_1.Description));
  55. exports.ErrorDescription = ErrorDescription;
  56. var Indexed = /** @class */ (function (_super) {
  57. __extends(Indexed, _super);
  58. function Indexed() {
  59. return _super !== null && _super.apply(this, arguments) || this;
  60. }
  61. Indexed.isIndexed = function (value) {
  62. return !!(value && value._isIndexed);
  63. };
  64. return Indexed;
  65. }(properties_1.Description));
  66. exports.Indexed = Indexed;
  67. var BuiltinErrors = {
  68. "0x08c379a0": { signature: "Error(string)", name: "Error", inputs: ["string"], reason: true },
  69. "0x4e487b71": { signature: "Panic(uint256)", name: "Panic", inputs: ["uint256"] }
  70. };
  71. function wrapAccessError(property, error) {
  72. var wrap = new Error("deferred error during ABI decoding triggered accessing " + property);
  73. wrap.error = error;
  74. return wrap;
  75. }
  76. /*
  77. function checkNames(fragment: Fragment, type: "input" | "output", params: Array<ParamType>): void {
  78. params.reduce((accum, param) => {
  79. if (param.name) {
  80. if (accum[param.name]) {
  81. logger.throwArgumentError(`duplicate ${ type } parameter ${ JSON.stringify(param.name) } in ${ fragment.format("full") }`, "fragment", fragment);
  82. }
  83. accum[param.name] = true;
  84. }
  85. return accum;
  86. }, <{ [ name: string ]: boolean }>{ });
  87. }
  88. */
  89. var Interface = /** @class */ (function () {
  90. function Interface(fragments) {
  91. var _newTarget = this.constructor;
  92. var _this = this;
  93. var abi = [];
  94. if (typeof (fragments) === "string") {
  95. abi = JSON.parse(fragments);
  96. }
  97. else {
  98. abi = fragments;
  99. }
  100. (0, properties_1.defineReadOnly)(this, "fragments", abi.map(function (fragment) {
  101. return fragments_1.Fragment.from(fragment);
  102. }).filter(function (fragment) { return (fragment != null); }));
  103. (0, properties_1.defineReadOnly)(this, "_abiCoder", (0, properties_1.getStatic)(_newTarget, "getAbiCoder")());
  104. (0, properties_1.defineReadOnly)(this, "functions", {});
  105. (0, properties_1.defineReadOnly)(this, "errors", {});
  106. (0, properties_1.defineReadOnly)(this, "events", {});
  107. (0, properties_1.defineReadOnly)(this, "structs", {});
  108. // Add all fragments by their signature
  109. this.fragments.forEach(function (fragment) {
  110. var bucket = null;
  111. switch (fragment.type) {
  112. case "constructor":
  113. if (_this.deploy) {
  114. logger.warn("duplicate definition - constructor");
  115. return;
  116. }
  117. //checkNames(fragment, "input", fragment.inputs);
  118. (0, properties_1.defineReadOnly)(_this, "deploy", fragment);
  119. return;
  120. case "function":
  121. //checkNames(fragment, "input", fragment.inputs);
  122. //checkNames(fragment, "output", (<FunctionFragment>fragment).outputs);
  123. bucket = _this.functions;
  124. break;
  125. case "event":
  126. //checkNames(fragment, "input", fragment.inputs);
  127. bucket = _this.events;
  128. break;
  129. case "error":
  130. bucket = _this.errors;
  131. break;
  132. default:
  133. return;
  134. }
  135. var signature = fragment.format();
  136. if (bucket[signature]) {
  137. logger.warn("duplicate definition - " + signature);
  138. return;
  139. }
  140. bucket[signature] = fragment;
  141. });
  142. // If we do not have a constructor add a default
  143. if (!this.deploy) {
  144. (0, properties_1.defineReadOnly)(this, "deploy", fragments_1.ConstructorFragment.from({
  145. payable: false,
  146. type: "constructor"
  147. }));
  148. }
  149. (0, properties_1.defineReadOnly)(this, "_isInterface", true);
  150. }
  151. Interface.prototype.format = function (format) {
  152. if (!format) {
  153. format = fragments_1.FormatTypes.full;
  154. }
  155. if (format === fragments_1.FormatTypes.sighash) {
  156. logger.throwArgumentError("interface does not support formatting sighash", "format", format);
  157. }
  158. var abi = this.fragments.map(function (fragment) { return fragment.format(format); });
  159. // We need to re-bundle the JSON fragments a bit
  160. if (format === fragments_1.FormatTypes.json) {
  161. return JSON.stringify(abi.map(function (j) { return JSON.parse(j); }));
  162. }
  163. return abi;
  164. };
  165. // Sub-classes can override these to handle other blockchains
  166. Interface.getAbiCoder = function () {
  167. return abi_coder_1.defaultAbiCoder;
  168. };
  169. Interface.getAddress = function (address) {
  170. return (0, address_1.getAddress)(address);
  171. };
  172. Interface.getSighash = function (fragment) {
  173. return (0, bytes_1.hexDataSlice)((0, hash_1.id)(fragment.format()), 0, 4);
  174. };
  175. Interface.getEventTopic = function (eventFragment) {
  176. return (0, hash_1.id)(eventFragment.format());
  177. };
  178. // Find a function definition by any means necessary (unless it is ambiguous)
  179. Interface.prototype.getFunction = function (nameOrSignatureOrSighash) {
  180. if ((0, bytes_1.isHexString)(nameOrSignatureOrSighash)) {
  181. for (var name_1 in this.functions) {
  182. if (nameOrSignatureOrSighash === this.getSighash(name_1)) {
  183. return this.functions[name_1];
  184. }
  185. }
  186. logger.throwArgumentError("no matching function", "sighash", nameOrSignatureOrSighash);
  187. }
  188. // It is a bare name, look up the function (will return null if ambiguous)
  189. if (nameOrSignatureOrSighash.indexOf("(") === -1) {
  190. var name_2 = nameOrSignatureOrSighash.trim();
  191. var matching = Object.keys(this.functions).filter(function (f) { return (f.split("(" /* fix:) */)[0] === name_2); });
  192. if (matching.length === 0) {
  193. logger.throwArgumentError("no matching function", "name", name_2);
  194. }
  195. else if (matching.length > 1) {
  196. logger.throwArgumentError("multiple matching functions", "name", name_2);
  197. }
  198. return this.functions[matching[0]];
  199. }
  200. // Normalize the signature and lookup the function
  201. var result = this.functions[fragments_1.FunctionFragment.fromString(nameOrSignatureOrSighash).format()];
  202. if (!result) {
  203. logger.throwArgumentError("no matching function", "signature", nameOrSignatureOrSighash);
  204. }
  205. return result;
  206. };
  207. // Find an event definition by any means necessary (unless it is ambiguous)
  208. Interface.prototype.getEvent = function (nameOrSignatureOrTopic) {
  209. if ((0, bytes_1.isHexString)(nameOrSignatureOrTopic)) {
  210. var topichash = nameOrSignatureOrTopic.toLowerCase();
  211. for (var name_3 in this.events) {
  212. if (topichash === this.getEventTopic(name_3)) {
  213. return this.events[name_3];
  214. }
  215. }
  216. logger.throwArgumentError("no matching event", "topichash", topichash);
  217. }
  218. // It is a bare name, look up the function (will return null if ambiguous)
  219. if (nameOrSignatureOrTopic.indexOf("(") === -1) {
  220. var name_4 = nameOrSignatureOrTopic.trim();
  221. var matching = Object.keys(this.events).filter(function (f) { return (f.split("(" /* fix:) */)[0] === name_4); });
  222. if (matching.length === 0) {
  223. logger.throwArgumentError("no matching event", "name", name_4);
  224. }
  225. else if (matching.length > 1) {
  226. logger.throwArgumentError("multiple matching events", "name", name_4);
  227. }
  228. return this.events[matching[0]];
  229. }
  230. // Normalize the signature and lookup the function
  231. var result = this.events[fragments_1.EventFragment.fromString(nameOrSignatureOrTopic).format()];
  232. if (!result) {
  233. logger.throwArgumentError("no matching event", "signature", nameOrSignatureOrTopic);
  234. }
  235. return result;
  236. };
  237. // Find a function definition by any means necessary (unless it is ambiguous)
  238. Interface.prototype.getError = function (nameOrSignatureOrSighash) {
  239. if ((0, bytes_1.isHexString)(nameOrSignatureOrSighash)) {
  240. var getSighash = (0, properties_1.getStatic)(this.constructor, "getSighash");
  241. for (var name_5 in this.errors) {
  242. var error = this.errors[name_5];
  243. if (nameOrSignatureOrSighash === getSighash(error)) {
  244. return this.errors[name_5];
  245. }
  246. }
  247. logger.throwArgumentError("no matching error", "sighash", nameOrSignatureOrSighash);
  248. }
  249. // It is a bare name, look up the function (will return null if ambiguous)
  250. if (nameOrSignatureOrSighash.indexOf("(") === -1) {
  251. var name_6 = nameOrSignatureOrSighash.trim();
  252. var matching = Object.keys(this.errors).filter(function (f) { return (f.split("(" /* fix:) */)[0] === name_6); });
  253. if (matching.length === 0) {
  254. logger.throwArgumentError("no matching error", "name", name_6);
  255. }
  256. else if (matching.length > 1) {
  257. logger.throwArgumentError("multiple matching errors", "name", name_6);
  258. }
  259. return this.errors[matching[0]];
  260. }
  261. // Normalize the signature and lookup the function
  262. var result = this.errors[fragments_1.FunctionFragment.fromString(nameOrSignatureOrSighash).format()];
  263. if (!result) {
  264. logger.throwArgumentError("no matching error", "signature", nameOrSignatureOrSighash);
  265. }
  266. return result;
  267. };
  268. // Get the sighash (the bytes4 selector) used by Solidity to identify a function
  269. Interface.prototype.getSighash = function (fragment) {
  270. if (typeof (fragment) === "string") {
  271. try {
  272. fragment = this.getFunction(fragment);
  273. }
  274. catch (error) {
  275. try {
  276. fragment = this.getError(fragment);
  277. }
  278. catch (_) {
  279. throw error;
  280. }
  281. }
  282. }
  283. return (0, properties_1.getStatic)(this.constructor, "getSighash")(fragment);
  284. };
  285. // Get the topic (the bytes32 hash) used by Solidity to identify an event
  286. Interface.prototype.getEventTopic = function (eventFragment) {
  287. if (typeof (eventFragment) === "string") {
  288. eventFragment = this.getEvent(eventFragment);
  289. }
  290. return (0, properties_1.getStatic)(this.constructor, "getEventTopic")(eventFragment);
  291. };
  292. Interface.prototype._decodeParams = function (params, data) {
  293. return this._abiCoder.decode(params, data);
  294. };
  295. Interface.prototype._encodeParams = function (params, values) {
  296. return this._abiCoder.encode(params, values);
  297. };
  298. Interface.prototype.encodeDeploy = function (values) {
  299. return this._encodeParams(this.deploy.inputs, values || []);
  300. };
  301. Interface.prototype.decodeErrorResult = function (fragment, data) {
  302. if (typeof (fragment) === "string") {
  303. fragment = this.getError(fragment);
  304. }
  305. var bytes = (0, bytes_1.arrayify)(data);
  306. if ((0, bytes_1.hexlify)(bytes.slice(0, 4)) !== this.getSighash(fragment)) {
  307. logger.throwArgumentError("data signature does not match error " + fragment.name + ".", "data", (0, bytes_1.hexlify)(bytes));
  308. }
  309. return this._decodeParams(fragment.inputs, bytes.slice(4));
  310. };
  311. Interface.prototype.encodeErrorResult = function (fragment, values) {
  312. if (typeof (fragment) === "string") {
  313. fragment = this.getError(fragment);
  314. }
  315. return (0, bytes_1.hexlify)((0, bytes_1.concat)([
  316. this.getSighash(fragment),
  317. this._encodeParams(fragment.inputs, values || [])
  318. ]));
  319. };
  320. // Decode the data for a function call (e.g. tx.data)
  321. Interface.prototype.decodeFunctionData = function (functionFragment, data) {
  322. if (typeof (functionFragment) === "string") {
  323. functionFragment = this.getFunction(functionFragment);
  324. }
  325. var bytes = (0, bytes_1.arrayify)(data);
  326. if ((0, bytes_1.hexlify)(bytes.slice(0, 4)) !== this.getSighash(functionFragment)) {
  327. logger.throwArgumentError("data signature does not match function " + functionFragment.name + ".", "data", (0, bytes_1.hexlify)(bytes));
  328. }
  329. return this._decodeParams(functionFragment.inputs, bytes.slice(4));
  330. };
  331. // Encode the data for a function call (e.g. tx.data)
  332. Interface.prototype.encodeFunctionData = function (functionFragment, values) {
  333. if (typeof (functionFragment) === "string") {
  334. functionFragment = this.getFunction(functionFragment);
  335. }
  336. return (0, bytes_1.hexlify)((0, bytes_1.concat)([
  337. this.getSighash(functionFragment),
  338. this._encodeParams(functionFragment.inputs, values || [])
  339. ]));
  340. };
  341. // Decode the result from a function call (e.g. from eth_call)
  342. Interface.prototype.decodeFunctionResult = function (functionFragment, data) {
  343. if (typeof (functionFragment) === "string") {
  344. functionFragment = this.getFunction(functionFragment);
  345. }
  346. var bytes = (0, bytes_1.arrayify)(data);
  347. var reason = null;
  348. var message = "";
  349. var errorArgs = null;
  350. var errorName = null;
  351. var errorSignature = null;
  352. switch (bytes.length % this._abiCoder._getWordSize()) {
  353. case 0:
  354. try {
  355. return this._abiCoder.decode(functionFragment.outputs, bytes);
  356. }
  357. catch (error) { }
  358. break;
  359. case 4: {
  360. var selector = (0, bytes_1.hexlify)(bytes.slice(0, 4));
  361. var builtin = BuiltinErrors[selector];
  362. if (builtin) {
  363. errorArgs = this._abiCoder.decode(builtin.inputs, bytes.slice(4));
  364. errorName = builtin.name;
  365. errorSignature = builtin.signature;
  366. if (builtin.reason) {
  367. reason = errorArgs[0];
  368. }
  369. if (errorName === "Error") {
  370. message = "; VM Exception while processing transaction: reverted with reason string " + JSON.stringify(errorArgs[0]);
  371. }
  372. else if (errorName === "Panic") {
  373. message = "; VM Exception while processing transaction: reverted with panic code " + errorArgs[0];
  374. }
  375. }
  376. else {
  377. try {
  378. var error = this.getError(selector);
  379. errorArgs = this._abiCoder.decode(error.inputs, bytes.slice(4));
  380. errorName = error.name;
  381. errorSignature = error.format();
  382. }
  383. catch (error) { }
  384. }
  385. break;
  386. }
  387. }
  388. return logger.throwError("call revert exception" + message, logger_1.Logger.errors.CALL_EXCEPTION, {
  389. method: functionFragment.format(),
  390. data: (0, bytes_1.hexlify)(data),
  391. errorArgs: errorArgs,
  392. errorName: errorName,
  393. errorSignature: errorSignature,
  394. reason: reason
  395. });
  396. };
  397. // Encode the result for a function call (e.g. for eth_call)
  398. Interface.prototype.encodeFunctionResult = function (functionFragment, values) {
  399. if (typeof (functionFragment) === "string") {
  400. functionFragment = this.getFunction(functionFragment);
  401. }
  402. return (0, bytes_1.hexlify)(this._abiCoder.encode(functionFragment.outputs, values || []));
  403. };
  404. // Create the filter for the event with search criteria (e.g. for eth_filterLog)
  405. Interface.prototype.encodeFilterTopics = function (eventFragment, values) {
  406. var _this = this;
  407. if (typeof (eventFragment) === "string") {
  408. eventFragment = this.getEvent(eventFragment);
  409. }
  410. if (values.length > eventFragment.inputs.length) {
  411. logger.throwError("too many arguments for " + eventFragment.format(), logger_1.Logger.errors.UNEXPECTED_ARGUMENT, {
  412. argument: "values",
  413. value: values
  414. });
  415. }
  416. var topics = [];
  417. if (!eventFragment.anonymous) {
  418. topics.push(this.getEventTopic(eventFragment));
  419. }
  420. var encodeTopic = function (param, value) {
  421. if (param.type === "string") {
  422. return (0, hash_1.id)(value);
  423. }
  424. else if (param.type === "bytes") {
  425. return (0, keccak256_1.keccak256)((0, bytes_1.hexlify)(value));
  426. }
  427. if (param.type === "bool" && typeof (value) === "boolean") {
  428. value = (value ? "0x01" : "0x00");
  429. }
  430. if (param.type.match(/^u?int/)) {
  431. value = bignumber_1.BigNumber.from(value).toHexString();
  432. }
  433. // Check addresses are valid
  434. if (param.type === "address") {
  435. _this._abiCoder.encode(["address"], [value]);
  436. }
  437. return (0, bytes_1.hexZeroPad)((0, bytes_1.hexlify)(value), 32);
  438. };
  439. values.forEach(function (value, index) {
  440. var param = eventFragment.inputs[index];
  441. if (!param.indexed) {
  442. if (value != null) {
  443. logger.throwArgumentError("cannot filter non-indexed parameters; must be null", ("contract." + param.name), value);
  444. }
  445. return;
  446. }
  447. if (value == null) {
  448. topics.push(null);
  449. }
  450. else if (param.baseType === "array" || param.baseType === "tuple") {
  451. logger.throwArgumentError("filtering with tuples or arrays not supported", ("contract." + param.name), value);
  452. }
  453. else if (Array.isArray(value)) {
  454. topics.push(value.map(function (value) { return encodeTopic(param, value); }));
  455. }
  456. else {
  457. topics.push(encodeTopic(param, value));
  458. }
  459. });
  460. // Trim off trailing nulls
  461. while (topics.length && topics[topics.length - 1] === null) {
  462. topics.pop();
  463. }
  464. return topics;
  465. };
  466. Interface.prototype.encodeEventLog = function (eventFragment, values) {
  467. var _this = this;
  468. if (typeof (eventFragment) === "string") {
  469. eventFragment = this.getEvent(eventFragment);
  470. }
  471. var topics = [];
  472. var dataTypes = [];
  473. var dataValues = [];
  474. if (!eventFragment.anonymous) {
  475. topics.push(this.getEventTopic(eventFragment));
  476. }
  477. if (values.length !== eventFragment.inputs.length) {
  478. logger.throwArgumentError("event arguments/values mismatch", "values", values);
  479. }
  480. eventFragment.inputs.forEach(function (param, index) {
  481. var value = values[index];
  482. if (param.indexed) {
  483. if (param.type === "string") {
  484. topics.push((0, hash_1.id)(value));
  485. }
  486. else if (param.type === "bytes") {
  487. topics.push((0, keccak256_1.keccak256)(value));
  488. }
  489. else if (param.baseType === "tuple" || param.baseType === "array") {
  490. // @TODO
  491. throw new Error("not implemented");
  492. }
  493. else {
  494. topics.push(_this._abiCoder.encode([param.type], [value]));
  495. }
  496. }
  497. else {
  498. dataTypes.push(param);
  499. dataValues.push(value);
  500. }
  501. });
  502. return {
  503. data: this._abiCoder.encode(dataTypes, dataValues),
  504. topics: topics
  505. };
  506. };
  507. // Decode a filter for the event and the search criteria
  508. Interface.prototype.decodeEventLog = function (eventFragment, data, topics) {
  509. if (typeof (eventFragment) === "string") {
  510. eventFragment = this.getEvent(eventFragment);
  511. }
  512. if (topics != null && !eventFragment.anonymous) {
  513. var topicHash = this.getEventTopic(eventFragment);
  514. if (!(0, bytes_1.isHexString)(topics[0], 32) || topics[0].toLowerCase() !== topicHash) {
  515. logger.throwError("fragment/topic mismatch", logger_1.Logger.errors.INVALID_ARGUMENT, { argument: "topics[0]", expected: topicHash, value: topics[0] });
  516. }
  517. topics = topics.slice(1);
  518. }
  519. var indexed = [];
  520. var nonIndexed = [];
  521. var dynamic = [];
  522. eventFragment.inputs.forEach(function (param, index) {
  523. if (param.indexed) {
  524. if (param.type === "string" || param.type === "bytes" || param.baseType === "tuple" || param.baseType === "array") {
  525. indexed.push(fragments_1.ParamType.fromObject({ type: "bytes32", name: param.name }));
  526. dynamic.push(true);
  527. }
  528. else {
  529. indexed.push(param);
  530. dynamic.push(false);
  531. }
  532. }
  533. else {
  534. nonIndexed.push(param);
  535. dynamic.push(false);
  536. }
  537. });
  538. var resultIndexed = (topics != null) ? this._abiCoder.decode(indexed, (0, bytes_1.concat)(topics)) : null;
  539. var resultNonIndexed = this._abiCoder.decode(nonIndexed, data, true);
  540. var result = [];
  541. var nonIndexedIndex = 0, indexedIndex = 0;
  542. eventFragment.inputs.forEach(function (param, index) {
  543. if (param.indexed) {
  544. if (resultIndexed == null) {
  545. result[index] = new Indexed({ _isIndexed: true, hash: null });
  546. }
  547. else if (dynamic[index]) {
  548. result[index] = new Indexed({ _isIndexed: true, hash: resultIndexed[indexedIndex++] });
  549. }
  550. else {
  551. try {
  552. result[index] = resultIndexed[indexedIndex++];
  553. }
  554. catch (error) {
  555. result[index] = error;
  556. }
  557. }
  558. }
  559. else {
  560. try {
  561. result[index] = resultNonIndexed[nonIndexedIndex++];
  562. }
  563. catch (error) {
  564. result[index] = error;
  565. }
  566. }
  567. // Add the keyword argument if named and safe
  568. if (param.name && result[param.name] == null) {
  569. var value_1 = result[index];
  570. // Make error named values throw on access
  571. if (value_1 instanceof Error) {
  572. Object.defineProperty(result, param.name, {
  573. enumerable: true,
  574. get: function () { throw wrapAccessError("property " + JSON.stringify(param.name), value_1); }
  575. });
  576. }
  577. else {
  578. result[param.name] = value_1;
  579. }
  580. }
  581. });
  582. var _loop_1 = function (i) {
  583. var value = result[i];
  584. if (value instanceof Error) {
  585. Object.defineProperty(result, i, {
  586. enumerable: true,
  587. get: function () { throw wrapAccessError("index " + i, value); }
  588. });
  589. }
  590. };
  591. // Make all error indexed values throw on access
  592. for (var i = 0; i < result.length; i++) {
  593. _loop_1(i);
  594. }
  595. return Object.freeze(result);
  596. };
  597. // Given a transaction, find the matching function fragment (if any) and
  598. // determine all its properties and call parameters
  599. Interface.prototype.parseTransaction = function (tx) {
  600. var fragment = this.getFunction(tx.data.substring(0, 10).toLowerCase());
  601. if (!fragment) {
  602. return null;
  603. }
  604. return new TransactionDescription({
  605. args: this._abiCoder.decode(fragment.inputs, "0x" + tx.data.substring(10)),
  606. functionFragment: fragment,
  607. name: fragment.name,
  608. signature: fragment.format(),
  609. sighash: this.getSighash(fragment),
  610. value: bignumber_1.BigNumber.from(tx.value || "0"),
  611. });
  612. };
  613. // @TODO
  614. //parseCallResult(data: BytesLike): ??
  615. // Given an event log, find the matching event fragment (if any) and
  616. // determine all its properties and values
  617. Interface.prototype.parseLog = function (log) {
  618. var fragment = this.getEvent(log.topics[0]);
  619. if (!fragment || fragment.anonymous) {
  620. return null;
  621. }
  622. // @TODO: If anonymous, and the only method, and the input count matches, should we parse?
  623. // Probably not, because just because it is the only event in the ABI does
  624. // not mean we have the full ABI; maybe just a fragment?
  625. return new LogDescription({
  626. eventFragment: fragment,
  627. name: fragment.name,
  628. signature: fragment.format(),
  629. topic: this.getEventTopic(fragment),
  630. args: this.decodeEventLog(fragment, log.data, log.topics)
  631. });
  632. };
  633. Interface.prototype.parseError = function (data) {
  634. var hexData = (0, bytes_1.hexlify)(data);
  635. var fragment = this.getError(hexData.substring(0, 10).toLowerCase());
  636. if (!fragment) {
  637. return null;
  638. }
  639. return new ErrorDescription({
  640. args: this._abiCoder.decode(fragment.inputs, "0x" + hexData.substring(10)),
  641. errorFragment: fragment,
  642. name: fragment.name,
  643. signature: fragment.format(),
  644. sighash: this.getSighash(fragment),
  645. });
  646. };
  647. /*
  648. static from(value: Array<Fragment | string | JsonAbi> | string | Interface) {
  649. if (Interface.isInterface(value)) {
  650. return value;
  651. }
  652. if (typeof(value) === "string") {
  653. return new Interface(JSON.parse(value));
  654. }
  655. return new Interface(value);
  656. }
  657. */
  658. Interface.isInterface = function (value) {
  659. return !!(value && value._isInterface);
  660. };
  661. return Interface;
  662. }());
  663. exports.Interface = Interface;
  664. //# sourceMappingURL=interface.js.map