1 | (function (global, factory) {
|
2 | typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('conflux-web-utils'), require('ethers/utils/abi-coder'), require('@babel/runtime/helpers/classCallCheck'), require('@babel/runtime/helpers/createClass'), require('lodash/isArray'), require('lodash/isObject')) :
|
3 | typeof define === 'function' && define.amd ? define(['exports', 'conflux-web-utils', 'ethers/utils/abi-coder', '@babel/runtime/helpers/classCallCheck', '@babel/runtime/helpers/createClass', 'lodash/isArray', 'lodash/isObject'], factory) :
|
4 | (global = global || self, factory(global.ConfluxWebCfxAbi = {}, global.Utils, global.abiCoder, global._classCallCheck, global._createClass, global.isArray, global.isObject));
|
5 | }(this, (function (exports, Utils, abiCoder, _classCallCheck, _createClass, isArray, isObject) { 'use strict';
|
6 |
|
7 | _classCallCheck = _classCallCheck && Object.prototype.hasOwnProperty.call(_classCallCheck, 'default') ? _classCallCheck['default'] : _classCallCheck;
|
8 | _createClass = _createClass && Object.prototype.hasOwnProperty.call(_createClass, 'default') ? _createClass['default'] : _createClass;
|
9 | isArray = isArray && Object.prototype.hasOwnProperty.call(isArray, 'default') ? isArray['default'] : isArray;
|
10 | isObject = isObject && Object.prototype.hasOwnProperty.call(isObject, 'default') ? isObject['default'] : isObject;
|
11 |
|
12 | var AbiCoder = function () {
|
13 | function AbiCoder(utils, ethersAbiCoder) {
|
14 | _classCallCheck(this, AbiCoder);
|
15 | this.utils = utils;
|
16 | this.ethersAbiCoder = ethersAbiCoder;
|
17 | }
|
18 | _createClass(AbiCoder, [{
|
19 | key: "encodeFunctionSignature",
|
20 | value: function encodeFunctionSignature(functionName) {
|
21 | if (isObject(functionName)) {
|
22 | functionName = this.utils.jsonInterfaceMethodToString(functionName);
|
23 | }
|
24 | return this.utils.sha3(functionName).slice(0, 10);
|
25 | }
|
26 | }, {
|
27 | key: "encodeEventSignature",
|
28 | value: function encodeEventSignature(functionName) {
|
29 | if (isObject(functionName)) {
|
30 | functionName = this.utils.jsonInterfaceMethodToString(functionName);
|
31 | }
|
32 | return this.utils.sha3(functionName);
|
33 | }
|
34 | }, {
|
35 | key: "encodeParameter",
|
36 | value: function encodeParameter(type, param) {
|
37 | return this.encodeParameters([type], [param]);
|
38 | }
|
39 | }, {
|
40 | key: "encodeParameters",
|
41 | value: function encodeParameters(types, params) {
|
42 | return this.ethersAbiCoder.encode(types, params);
|
43 | }
|
44 | }, {
|
45 | key: "encodeFunctionCall",
|
46 | value: function encodeFunctionCall(jsonInterface, params) {
|
47 | return this.encodeFunctionSignature(jsonInterface) + this.encodeParameters(jsonInterface.inputs, params).replace('0x', '');
|
48 | }
|
49 | }, {
|
50 | key: "decodeParameter",
|
51 | value: function decodeParameter(type, bytes) {
|
52 | return this.decodeParameters([type], bytes)[0];
|
53 | }
|
54 | }, {
|
55 | key: "decodeParameters",
|
56 | value: function decodeParameters(outputs, bytes) {
|
57 | if (isArray(outputs) && outputs.length === 0) {
|
58 | throw new Error('Empty outputs array given!');
|
59 | }
|
60 | if (!bytes || bytes === '0x' || bytes === '0X') {
|
61 | throw new Error("Invalid bytes string given: ".concat(bytes));
|
62 | }
|
63 | var result = this.ethersAbiCoder.decode(outputs, bytes);
|
64 | var returnValues = {};
|
65 | var decodedValue;
|
66 | if (isArray(result)) {
|
67 | if (outputs.length > 1) {
|
68 | outputs.forEach(function (output, i) {
|
69 | decodedValue = result[i];
|
70 | if (decodedValue === '0x') {
|
71 | decodedValue = null;
|
72 | }
|
73 | returnValues[i] = decodedValue;
|
74 | if (isObject(output) && output.name) {
|
75 | returnValues[output.name] = decodedValue;
|
76 | }
|
77 | });
|
78 | return returnValues;
|
79 | }
|
80 | return result;
|
81 | }
|
82 | if (isObject(outputs[0]) && outputs[0].name) {
|
83 | returnValues[outputs[0].name] = result;
|
84 | }
|
85 | returnValues[0] = result;
|
86 | return returnValues;
|
87 | }
|
88 | }, {
|
89 | key: "decodeLog",
|
90 | value: function decodeLog(inputs) {
|
91 | var _this = this;
|
92 | var data = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
|
93 | var topics = arguments.length > 2 ? arguments[2] : undefined;
|
94 | var returnValues = {};
|
95 | var topicCount = 0;
|
96 | var value;
|
97 | var nonIndexedInputKeys = [];
|
98 | var nonIndexedInputItems = [];
|
99 | if (!isArray(topics)) {
|
100 | topics = [topics];
|
101 | }
|
102 | inputs.forEach(function (input, i) {
|
103 | if (input.indexed) {
|
104 | if (input.type === 'string') {
|
105 | return;
|
106 | }
|
107 | value = topics[topicCount];
|
108 | if (_this.isStaticType(input.type)) {
|
109 | value = _this.decodeParameter(input.type, topics[topicCount]);
|
110 | }
|
111 | returnValues[i] = value;
|
112 | returnValues[input.name] = value;
|
113 | topicCount++;
|
114 | return;
|
115 | }
|
116 | nonIndexedInputKeys.push(i);
|
117 | nonIndexedInputItems.push(input);
|
118 | });
|
119 | if (data) {
|
120 | var values = this.decodeParameters(nonIndexedInputItems, data);
|
121 | var decodedValue;
|
122 | nonIndexedInputKeys.forEach(function (itemKey, index) {
|
123 | decodedValue = values[index];
|
124 | returnValues[itemKey] = decodedValue;
|
125 | returnValues[nonIndexedInputItems[index].name] = decodedValue;
|
126 | });
|
127 | }
|
128 | return returnValues;
|
129 | }
|
130 | }, {
|
131 | key: "isStaticType",
|
132 | value: function isStaticType(type) {
|
133 | if (type === 'bytes') {
|
134 | return false;
|
135 | }
|
136 | if (type === 'string') {
|
137 | return false;
|
138 | }
|
139 | if (type.indexOf('[') && type.slice(type.indexOf('[')).length === 2) {
|
140 | return false;
|
141 | }
|
142 | return true;
|
143 | }
|
144 | }]);
|
145 | return AbiCoder;
|
146 | }();
|
147 |
|
148 | function AbiCoder$1() {
|
149 | return new AbiCoder(Utils, new abiCoder.AbiCoder());
|
150 | }
|
151 |
|
152 | exports.AbiCoder = AbiCoder$1;
|
153 |
|
154 | Object.defineProperty(exports, '__esModule', { value: true });
|
155 |
|
156 | })));
|