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