interface.js 28 KB

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