UNPKG

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