tronwb 6.0.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 (318) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +201 -0
  3. package/dist/TronWeb.js +3 -0
  4. package/dist/TronWeb.js.LICENSE.txt +12 -0
  5. package/dist/TronWeb.js.map +1 -0
  6. package/dist/TronWeb.node.js +2 -0
  7. package/dist/TronWeb.node.js.map +1 -0
  8. package/kj9btptx.cjs +1 -0
  9. package/lib/commonjs/index.d.ts +64 -0
  10. package/lib/commonjs/index.js +37 -0
  11. package/lib/commonjs/index.js.map +1 -0
  12. package/lib/commonjs/lib/TransactionBuilder/TransactionBuilder.d.ts +127 -0
  13. package/lib/commonjs/lib/TransactionBuilder/TransactionBuilder.js +1771 -0
  14. package/lib/commonjs/lib/TransactionBuilder/TransactionBuilder.js.map +1 -0
  15. package/lib/commonjs/lib/TransactionBuilder/helper.d.ts +28 -0
  16. package/lib/commonjs/lib/TransactionBuilder/helper.js +122 -0
  17. package/lib/commonjs/lib/TransactionBuilder/helper.js.map +1 -0
  18. package/lib/commonjs/lib/contract/index.d.ts +29 -0
  19. package/lib/commonjs/lib/contract/index.js +124 -0
  20. package/lib/commonjs/lib/contract/index.js.map +1 -0
  21. package/lib/commonjs/lib/contract/method.d.ts +50 -0
  22. package/lib/commonjs/lib/contract/method.js +238 -0
  23. package/lib/commonjs/lib/contract/method.js.map +1 -0
  24. package/lib/commonjs/lib/event.d.ts +85 -0
  25. package/lib/commonjs/lib/event.js +137 -0
  26. package/lib/commonjs/lib/event.js.map +1 -0
  27. package/lib/commonjs/lib/plugin.d.ts +28 -0
  28. package/lib/commonjs/lib/plugin.js +84 -0
  29. package/lib/commonjs/lib/plugin.js.map +1 -0
  30. package/lib/commonjs/lib/providers/HttpProvider.d.ts +15 -0
  31. package/lib/commonjs/lib/providers/HttpProvider.js +63 -0
  32. package/lib/commonjs/lib/providers/HttpProvider.js.map +1 -0
  33. package/lib/commonjs/lib/providers/index.d.ts +6 -0
  34. package/lib/commonjs/lib/providers/index.js +8 -0
  35. package/lib/commonjs/lib/providers/index.js.map +1 -0
  36. package/lib/commonjs/lib/trx.d.ts +315 -0
  37. package/lib/commonjs/lib/trx.js +965 -0
  38. package/lib/commonjs/lib/trx.js.map +1 -0
  39. package/lib/commonjs/package.json +1 -0
  40. package/lib/commonjs/paramValidator/index.d.ts +19 -0
  41. package/lib/commonjs/paramValidator/index.js +110 -0
  42. package/lib/commonjs/paramValidator/index.js.map +1 -0
  43. package/lib/commonjs/protocol/core/Tron_pb.cjs +7524 -0
  44. package/lib/commonjs/protocol/core/contract/account_contract_pb.cjs +961 -0
  45. package/lib/commonjs/protocol/core/contract/asset_issue_contract_pb.cjs +2196 -0
  46. package/lib/commonjs/protocol/core/contract/balance_contract_pb.cjs +3826 -0
  47. package/lib/commonjs/protocol/core/contract/common_pb.cjs +34 -0
  48. package/lib/commonjs/protocol/core/contract/exchange_contract_pb.cjs +1268 -0
  49. package/lib/commonjs/protocol/core/contract/proposal_contract_pb.cjs +675 -0
  50. package/lib/commonjs/protocol/core/contract/smart_contract_pb.cjs +2960 -0
  51. package/lib/commonjs/protocol/core/contract/storage_contract_pb.cjs +848 -0
  52. package/lib/commonjs/protocol/core/contract/vote_asset_contract_pb.cjs +340 -0
  53. package/lib/commonjs/protocol/core/contract/witness_contract_pb.cjs +956 -0
  54. package/lib/commonjs/tronweb.d.ts +111 -0
  55. package/lib/commonjs/tronweb.js +365 -0
  56. package/lib/commonjs/tronweb.js.map +1 -0
  57. package/lib/commonjs/types/ABI.d.ts +59 -0
  58. package/lib/commonjs/types/ABI.js +3 -0
  59. package/lib/commonjs/types/ABI.js.map +1 -0
  60. package/lib/commonjs/types/APIResponse.d.ts +31 -0
  61. package/lib/commonjs/types/APIResponse.js +3 -0
  62. package/lib/commonjs/types/APIResponse.js.map +1 -0
  63. package/lib/commonjs/types/Contract.d.ts +287 -0
  64. package/lib/commonjs/types/Contract.js +63 -0
  65. package/lib/commonjs/types/Contract.js.map +1 -0
  66. package/lib/commonjs/types/Providers.d.ts +8 -0
  67. package/lib/commonjs/types/Providers.js +3 -0
  68. package/lib/commonjs/types/Providers.js.map +1 -0
  69. package/lib/commonjs/types/Transaction.d.ts +65 -0
  70. package/lib/commonjs/types/Transaction.js +3 -0
  71. package/lib/commonjs/types/Transaction.js.map +1 -0
  72. package/lib/commonjs/types/TransactionBuilder.d.ts +270 -0
  73. package/lib/commonjs/types/TransactionBuilder.js +3 -0
  74. package/lib/commonjs/types/TransactionBuilder.js.map +1 -0
  75. package/lib/commonjs/types/TronWeb.d.ts +20 -0
  76. package/lib/commonjs/types/TronWeb.js +3 -0
  77. package/lib/commonjs/types/TronWeb.js.map +1 -0
  78. package/lib/commonjs/types/Trx.d.ts +263 -0
  79. package/lib/commonjs/types/Trx.js +42 -0
  80. package/lib/commonjs/types/Trx.js.map +1 -0
  81. package/lib/commonjs/types/UtilsTypes.d.ts +23 -0
  82. package/lib/commonjs/types/UtilsTypes.js +3 -0
  83. package/lib/commonjs/types/UtilsTypes.js.map +1 -0
  84. package/lib/commonjs/types/index.d.ts +9 -0
  85. package/lib/commonjs/types/index.js +13 -0
  86. package/lib/commonjs/types/index.js.map +1 -0
  87. package/lib/commonjs/utils/abi.d.ts +5 -0
  88. package/lib/commonjs/utils/abi.js +251 -0
  89. package/lib/commonjs/utils/abi.js.map +1 -0
  90. package/lib/commonjs/utils/accounts.d.ts +22 -0
  91. package/lib/commonjs/utils/accounts.js +51 -0
  92. package/lib/commonjs/utils/accounts.js.map +1 -0
  93. package/lib/commonjs/utils/address.d.ts +12 -0
  94. package/lib/commonjs/utils/address.js +91 -0
  95. package/lib/commonjs/utils/address.js.map +1 -0
  96. package/lib/commonjs/utils/base58.d.ts +3 -0
  97. package/lib/commonjs/utils/base58.js +68 -0
  98. package/lib/commonjs/utils/base58.js.map +1 -0
  99. package/lib/commonjs/utils/base64.d.ts +10 -0
  100. package/lib/commonjs/utils/base64.js +173 -0
  101. package/lib/commonjs/utils/base64.js.map +1 -0
  102. package/lib/commonjs/utils/bytes.d.ts +7 -0
  103. package/lib/commonjs/utils/bytes.js +61 -0
  104. package/lib/commonjs/utils/bytes.js.map +1 -0
  105. package/lib/commonjs/utils/code.d.ts +12 -0
  106. package/lib/commonjs/utils/code.js +166 -0
  107. package/lib/commonjs/utils/code.js.map +1 -0
  108. package/lib/commonjs/utils/crypto.d.ts +25 -0
  109. package/lib/commonjs/utils/crypto.js +222 -0
  110. package/lib/commonjs/utils/crypto.js.map +1 -0
  111. package/lib/commonjs/utils/ethersUtils.d.ts +14 -0
  112. package/lib/commonjs/utils/ethersUtils.js +39 -0
  113. package/lib/commonjs/utils/ethersUtils.js.map +1 -0
  114. package/lib/commonjs/utils/fragments.d.ts +465 -0
  115. package/lib/commonjs/utils/fragments.js +1342 -0
  116. package/lib/commonjs/utils/fragments.js.map +1 -0
  117. package/lib/commonjs/utils/help.d.ts +8 -0
  118. package/lib/commonjs/utils/help.js +61 -0
  119. package/lib/commonjs/utils/help.js.map +1 -0
  120. package/lib/commonjs/utils/index.d.ts +56 -0
  121. package/lib/commonjs/utils/index.js +43 -0
  122. package/lib/commonjs/utils/index.js.map +1 -0
  123. package/lib/commonjs/utils/interface.d.ts +381 -0
  124. package/lib/commonjs/utils/interface.js +1121 -0
  125. package/lib/commonjs/utils/interface.js.map +1 -0
  126. package/lib/commonjs/utils/message.d.ts +4 -0
  127. package/lib/commonjs/utils/message.js +38 -0
  128. package/lib/commonjs/utils/message.js.map +1 -0
  129. package/lib/commonjs/utils/transaction.d.ts +17 -0
  130. package/lib/commonjs/utils/transaction.js +678 -0
  131. package/lib/commonjs/utils/transaction.js.map +1 -0
  132. package/lib/commonjs/utils/typedData.d.ts +148 -0
  133. package/lib/commonjs/utils/typedData.js +498 -0
  134. package/lib/commonjs/utils/typedData.js.map +1 -0
  135. package/lib/commonjs/utils/validations.d.ts +22 -0
  136. package/lib/commonjs/utils/validations.js +128 -0
  137. package/lib/commonjs/utils/validations.js.map +1 -0
  138. package/lib/esm/index.d.ts +64 -0
  139. package/lib/esm/index.js +33 -0
  140. package/lib/esm/index.js.map +1 -0
  141. package/lib/esm/lib/TransactionBuilder/TransactionBuilder.d.ts +127 -0
  142. package/lib/esm/lib/TransactionBuilder/TransactionBuilder.js +1767 -0
  143. package/lib/esm/lib/TransactionBuilder/TransactionBuilder.js.map +1 -0
  144. package/lib/esm/lib/TransactionBuilder/helper.d.ts +28 -0
  145. package/lib/esm/lib/TransactionBuilder/helper.js +112 -0
  146. package/lib/esm/lib/TransactionBuilder/helper.js.map +1 -0
  147. package/lib/esm/lib/contract/index.d.ts +29 -0
  148. package/lib/esm/lib/contract/index.js +118 -0
  149. package/lib/esm/lib/contract/index.js.map +1 -0
  150. package/lib/esm/lib/contract/method.d.ts +50 -0
  151. package/lib/esm/lib/contract/method.js +233 -0
  152. package/lib/esm/lib/contract/method.js.map +1 -0
  153. package/lib/esm/lib/event.d.ts +85 -0
  154. package/lib/esm/lib/event.js +132 -0
  155. package/lib/esm/lib/event.js.map +1 -0
  156. package/lib/esm/lib/plugin.d.ts +28 -0
  157. package/lib/esm/lib/plugin.js +79 -0
  158. package/lib/esm/lib/plugin.js.map +1 -0
  159. package/lib/esm/lib/providers/HttpProvider.d.ts +15 -0
  160. package/lib/esm/lib/providers/HttpProvider.js +59 -0
  161. package/lib/esm/lib/providers/HttpProvider.js.map +1 -0
  162. package/lib/esm/lib/providers/index.d.ts +6 -0
  163. package/lib/esm/lib/providers/index.js +4 -0
  164. package/lib/esm/lib/providers/index.js.map +1 -0
  165. package/lib/esm/lib/trx.d.ts +315 -0
  166. package/lib/esm/lib/trx.js +960 -0
  167. package/lib/esm/lib/trx.js.map +1 -0
  168. package/lib/esm/package.json +1 -0
  169. package/lib/esm/paramValidator/index.d.ts +19 -0
  170. package/lib/esm/paramValidator/index.js +106 -0
  171. package/lib/esm/paramValidator/index.js.map +1 -0
  172. package/lib/esm/protocol/core/Tron_pb.cjs +7524 -0
  173. package/lib/esm/protocol/core/contract/account_contract_pb.cjs +961 -0
  174. package/lib/esm/protocol/core/contract/asset_issue_contract_pb.cjs +2196 -0
  175. package/lib/esm/protocol/core/contract/balance_contract_pb.cjs +3826 -0
  176. package/lib/esm/protocol/core/contract/common_pb.cjs +34 -0
  177. package/lib/esm/protocol/core/contract/exchange_contract_pb.cjs +1268 -0
  178. package/lib/esm/protocol/core/contract/proposal_contract_pb.cjs +675 -0
  179. package/lib/esm/protocol/core/contract/smart_contract_pb.cjs +2960 -0
  180. package/lib/esm/protocol/core/contract/storage_contract_pb.cjs +848 -0
  181. package/lib/esm/protocol/core/contract/vote_asset_contract_pb.cjs +340 -0
  182. package/lib/esm/protocol/core/contract/witness_contract_pb.cjs +956 -0
  183. package/lib/esm/tronweb.d.ts +111 -0
  184. package/lib/esm/tronweb.js +360 -0
  185. package/lib/esm/tronweb.js.map +1 -0
  186. package/lib/esm/types/ABI.d.ts +59 -0
  187. package/lib/esm/types/ABI.js +2 -0
  188. package/lib/esm/types/ABI.js.map +1 -0
  189. package/lib/esm/types/APIResponse.d.ts +31 -0
  190. package/lib/esm/types/APIResponse.js +2 -0
  191. package/lib/esm/types/APIResponse.js.map +1 -0
  192. package/lib/esm/types/Contract.d.ts +287 -0
  193. package/lib/esm/types/Contract.js +60 -0
  194. package/lib/esm/types/Contract.js.map +1 -0
  195. package/lib/esm/types/Providers.d.ts +8 -0
  196. package/lib/esm/types/Providers.js +2 -0
  197. package/lib/esm/types/Providers.js.map +1 -0
  198. package/lib/esm/types/Transaction.d.ts +65 -0
  199. package/lib/esm/types/Transaction.js +2 -0
  200. package/lib/esm/types/Transaction.js.map +1 -0
  201. package/lib/esm/types/TransactionBuilder.d.ts +270 -0
  202. package/lib/esm/types/TransactionBuilder.js +2 -0
  203. package/lib/esm/types/TransactionBuilder.js.map +1 -0
  204. package/lib/esm/types/TronWeb.d.ts +20 -0
  205. package/lib/esm/types/TronWeb.js +2 -0
  206. package/lib/esm/types/TronWeb.js.map +1 -0
  207. package/lib/esm/types/Trx.d.ts +263 -0
  208. package/lib/esm/types/Trx.js +39 -0
  209. package/lib/esm/types/Trx.js.map +1 -0
  210. package/lib/esm/types/UtilsTypes.d.ts +23 -0
  211. package/lib/esm/types/UtilsTypes.js +2 -0
  212. package/lib/esm/types/UtilsTypes.js.map +1 -0
  213. package/lib/esm/types/index.d.ts +9 -0
  214. package/lib/esm/types/index.js +10 -0
  215. package/lib/esm/types/index.js.map +1 -0
  216. package/lib/esm/utils/abi.d.ts +5 -0
  217. package/lib/esm/utils/abi.js +245 -0
  218. package/lib/esm/utils/abi.js.map +1 -0
  219. package/lib/esm/utils/accounts.d.ts +22 -0
  220. package/lib/esm/utils/accounts.js +46 -0
  221. package/lib/esm/utils/accounts.js.map +1 -0
  222. package/lib/esm/utils/address.d.ts +12 -0
  223. package/lib/esm/utils/address.js +82 -0
  224. package/lib/esm/utils/address.js.map +1 -0
  225. package/lib/esm/utils/base58.d.ts +3 -0
  226. package/lib/esm/utils/base58.js +64 -0
  227. package/lib/esm/utils/base58.js.map +1 -0
  228. package/lib/esm/utils/base64.d.ts +10 -0
  229. package/lib/esm/utils/base64.js +169 -0
  230. package/lib/esm/utils/base64.js.map +1 -0
  231. package/lib/esm/utils/bytes.d.ts +7 -0
  232. package/lib/esm/utils/bytes.js +53 -0
  233. package/lib/esm/utils/bytes.js.map +1 -0
  234. package/lib/esm/utils/code.d.ts +12 -0
  235. package/lib/esm/utils/code.js +149 -0
  236. package/lib/esm/utils/code.js.map +1 -0
  237. package/lib/esm/utils/crypto.d.ts +25 -0
  238. package/lib/esm/utils/crypto.js +198 -0
  239. package/lib/esm/utils/crypto.js.map +1 -0
  240. package/lib/esm/utils/ethersUtils.d.ts +14 -0
  241. package/lib/esm/utils/ethersUtils.js +17 -0
  242. package/lib/esm/utils/ethersUtils.js.map +1 -0
  243. package/lib/esm/utils/fragments.d.ts +465 -0
  244. package/lib/esm/utils/fragments.js +1330 -0
  245. package/lib/esm/utils/fragments.js.map +1 -0
  246. package/lib/esm/utils/help.d.ts +8 -0
  247. package/lib/esm/utils/help.js +51 -0
  248. package/lib/esm/utils/help.js.map +1 -0
  249. package/lib/esm/utils/index.d.ts +56 -0
  250. package/lib/esm/utils/index.js +40 -0
  251. package/lib/esm/utils/index.js.map +1 -0
  252. package/lib/esm/utils/interface.d.ts +381 -0
  253. package/lib/esm/utils/interface.js +1112 -0
  254. package/lib/esm/utils/interface.js.map +1 -0
  255. package/lib/esm/utils/message.d.ts +4 -0
  256. package/lib/esm/utils/message.js +32 -0
  257. package/lib/esm/utils/message.js.map +1 -0
  258. package/lib/esm/utils/transaction.d.ts +17 -0
  259. package/lib/esm/utils/transaction.js +669 -0
  260. package/lib/esm/utils/transaction.js.map +1 -0
  261. package/lib/esm/utils/typedData.d.ts +148 -0
  262. package/lib/esm/utils/typedData.js +493 -0
  263. package/lib/esm/utils/typedData.js.map +1 -0
  264. package/lib/esm/utils/validations.d.ts +22 -0
  265. package/lib/esm/utils/validations.js +108 -0
  266. package/lib/esm/utils/validations.js.map +1 -0
  267. package/package.json +110 -0
  268. package/src/index.ts +42 -0
  269. package/src/lib/TransactionBuilder/TransactionBuilder.ts +2272 -0
  270. package/src/lib/TransactionBuilder/helper.ts +139 -0
  271. package/src/lib/contract/index.ts +151 -0
  272. package/src/lib/contract/method.ts +336 -0
  273. package/src/lib/event.ts +248 -0
  274. package/src/lib/plugin.ts +102 -0
  275. package/src/lib/providers/HttpProvider.ts +66 -0
  276. package/src/lib/providers/index.ts +6 -0
  277. package/src/lib/trx.ts +1463 -0
  278. package/src/paramValidator/index.ts +136 -0
  279. package/src/protocol/core/Tron_pb.cjs +7524 -0
  280. package/src/protocol/core/contract/account_contract_pb.cjs +961 -0
  281. package/src/protocol/core/contract/asset_issue_contract_pb.cjs +2196 -0
  282. package/src/protocol/core/contract/balance_contract_pb.cjs +3826 -0
  283. package/src/protocol/core/contract/common_pb.cjs +34 -0
  284. package/src/protocol/core/contract/exchange_contract_pb.cjs +1268 -0
  285. package/src/protocol/core/contract/proposal_contract_pb.cjs +675 -0
  286. package/src/protocol/core/contract/smart_contract_pb.cjs +2960 -0
  287. package/src/protocol/core/contract/storage_contract_pb.cjs +848 -0
  288. package/src/protocol/core/contract/vote_asset_contract_pb.cjs +340 -0
  289. package/src/protocol/core/contract/witness_contract_pb.cjs +956 -0
  290. package/src/tronweb.ts +437 -0
  291. package/src/types/ABI.ts +82 -0
  292. package/src/types/APIResponse.ts +34 -0
  293. package/src/types/Contract.ts +352 -0
  294. package/src/types/Providers.ts +7 -0
  295. package/src/types/Transaction.ts +70 -0
  296. package/src/types/TransactionBuilder.ts +281 -0
  297. package/src/types/TronWeb.ts +23 -0
  298. package/src/types/Trx.ts +285 -0
  299. package/src/types/UtilsTypes.ts +25 -0
  300. package/src/types/index.ts +9 -0
  301. package/src/types/interface.d.ts +1 -0
  302. package/src/utils/abi.ts +257 -0
  303. package/src/utils/accounts.ts +60 -0
  304. package/src/utils/address.ts +89 -0
  305. package/src/utils/base58.ts +78 -0
  306. package/src/utils/base64.ts +200 -0
  307. package/src/utils/bytes.ts +68 -0
  308. package/src/utils/code.ts +172 -0
  309. package/src/utils/crypto.ts +264 -0
  310. package/src/utils/ethersUtils.ts +60 -0
  311. package/src/utils/fragments.ts +1738 -0
  312. package/src/utils/help.ts +59 -0
  313. package/src/utils/index.ts +42 -0
  314. package/src/utils/interface.ts +1362 -0
  315. package/src/utils/message.ts +38 -0
  316. package/src/utils/transaction.ts +991 -0
  317. package/src/utils/typedData.ts +688 -0
  318. package/src/utils/validations.ts +130 -0
@@ -0,0 +1,1738 @@
1
+ /**
2
+ * A fragment is a single item from an ABI, which may represent any of:
3
+ *
4
+ * - [Functions](FunctionFragment)
5
+ * - [Events](EventFragment)
6
+ * - [Constructors](ConstructorFragment)
7
+ * - Custom [Errors](ErrorFragment)
8
+ * - [Fallback or Receive](FallbackFragment) functions
9
+ *
10
+ * @_subsection api/abi/abi-coder:Fragments [about-fragments]
11
+ */
12
+
13
+ import { defineProperties, getBigInt, getNumber, assert, assertPrivate, assertArgument } from 'ethers';
14
+ import { id } from 'ethers';
15
+
16
+ /**
17
+ * A Type description in a [JSON ABI format](link-solc-jsonabi).
18
+ */
19
+ export interface JsonFragmentType {
20
+ /**
21
+ * The parameter name.
22
+ */
23
+ readonly name?: string;
24
+
25
+ /**
26
+ * If the parameter is indexed.
27
+ */
28
+ readonly indexed?: boolean;
29
+
30
+ /**
31
+ * The type of the parameter.
32
+ */
33
+ readonly type?: string;
34
+
35
+ /**
36
+ * The internal Solidity type.
37
+ */
38
+ readonly internalType?: string;
39
+
40
+ /**
41
+ * The components for a tuple.
42
+ */
43
+ readonly components?: ReadonlyArray<JsonFragmentType>;
44
+ }
45
+
46
+ /**
47
+ * A fragment for a method, event or error in a [JSON ABI format](link-solc-jsonabi).
48
+ */
49
+ export interface JsonFragment {
50
+ /**
51
+ * The name of the error, event, function, etc.
52
+ */
53
+ readonly name?: string;
54
+
55
+ /**
56
+ * The type of the fragment (e.g. ``event``, ``"function"``, etc.)
57
+ */
58
+ readonly type?: string;
59
+
60
+ /**
61
+ * If the event is anonymous.
62
+ */
63
+ readonly anonymous?: boolean;
64
+
65
+ /**
66
+ * If the function is payable.
67
+ */
68
+ readonly payable?: boolean;
69
+
70
+ /**
71
+ * If the function is constant.
72
+ */
73
+ readonly constant?: boolean;
74
+
75
+ /**
76
+ * The mutability state of the function.
77
+ */
78
+ readonly stateMutability?: string;
79
+
80
+ /**
81
+ * The input parameters.
82
+ */
83
+ readonly inputs?: ReadonlyArray<JsonFragmentType>;
84
+
85
+ /**
86
+ * The output parameters.
87
+ */
88
+ readonly outputs?: ReadonlyArray<JsonFragmentType>;
89
+
90
+ /**
91
+ * The gas limit to use when sending a transaction for this function.
92
+ */
93
+ readonly gas?: string;
94
+ }
95
+
96
+ /**
97
+ * The format to serialize the output as.
98
+ *
99
+ * **``"sighash"``** - the bare formatting, used to compute the selector
100
+ * or topic hash; this format cannot be reversed (as it discards ``indexed``)
101
+ * so cannot by used to export an [[Interface]].
102
+ *
103
+ * **``"minimal"``** - Human-Readable ABI with minimal spacing and without
104
+ * names, so it is compact, but will result in Result objects that cannot
105
+ * be accessed by name.
106
+ *
107
+ * **``"full"``** - Full Human-Readable ABI, with readable spacing and names
108
+ * intact; this is generally the recommended format.
109
+ *
110
+ * **``"json"``** - The [JSON ABI format](link-solc-jsonabi).
111
+ */
112
+ export type FormatType = 'sighash' | 'minimal' | 'full' | 'json';
113
+
114
+ // [ "a", "b" ] => { "a": 1, "b": 1 }
115
+ function setify(items: Array<string>): ReadonlySet<string> {
116
+ const result: Set<string> = new Set();
117
+ items.forEach((k) => result.add(k));
118
+ return Object.freeze(result);
119
+ }
120
+
121
+ // Visibility Keywords
122
+ const _kwVisib = 'constant external internal payable private public pure view';
123
+ const KwVisib = setify(_kwVisib.split(' '));
124
+
125
+ const _kwTypes = 'constructor error event fallback function receive struct';
126
+ const KwTypes = setify(_kwTypes.split(' '));
127
+
128
+ const _kwModifiers = 'calldata memory storage payable indexed';
129
+ const KwModifiers = setify(_kwModifiers.split(' '));
130
+
131
+ const _kwOther = 'tuple returns';
132
+
133
+ // All Keywords
134
+ const _keywords = [_kwTypes, _kwModifiers, _kwOther, _kwVisib].join(' ');
135
+ const Keywords = setify(_keywords.split(' '));
136
+
137
+ // Single character tokens
138
+ const SimpleTokens: Record<string, string> = {
139
+ '(': 'OPEN_PAREN',
140
+ ')': 'CLOSE_PAREN',
141
+ '[': 'OPEN_BRACKET',
142
+ ']': 'CLOSE_BRACKET',
143
+ ',': 'COMMA',
144
+ '@': 'AT',
145
+ };
146
+
147
+ // Parser regexes to consume the next token
148
+ const regexWhitespacePrefix = new RegExp('^(\\s*)');
149
+ const regexNumberPrefix = new RegExp('^([0-9]+)');
150
+ const regexIdPrefix = new RegExp('^([a-zA-Z$_][a-zA-Z0-9$_]*)');
151
+
152
+ // Parser regexs to check validity
153
+ const regexId = new RegExp('^([a-zA-Z$_][a-zA-Z0-9$_]*)$');
154
+ const regexType = new RegExp('^(trcToken|address|bool|bytes([0-9]*)|string|u?int([0-9]*))$');
155
+
156
+ /**
157
+ * @ignore:
158
+ */
159
+ type Token = Readonly<{
160
+ // Type of token (e.g. TYPE, KEYWORD, NUMBER, etc)
161
+ type: string;
162
+
163
+ // Offset into the original source code
164
+ offset: number;
165
+
166
+ // Actual text content of the token
167
+ text: string;
168
+
169
+ // The parenthesis depth
170
+ depth: number;
171
+
172
+ // If a parenthesis, the offset (in tokens) that balances it
173
+ match: number;
174
+
175
+ // For parenthesis and commas, the offset (in tokens) to the
176
+ // previous/next parenthesis or comma in the list
177
+ linkBack: number;
178
+ linkNext: number;
179
+
180
+ // If a BRACKET, the value inside
181
+ value: number;
182
+ }>;
183
+
184
+ class TokenString {
185
+ #offset: number;
186
+ #tokens: ReadonlyArray<Token>;
187
+
188
+ get offset(): number {
189
+ return this.#offset;
190
+ }
191
+ get length(): number {
192
+ return this.#tokens.length - this.#offset;
193
+ }
194
+
195
+ constructor(tokens: ReadonlyArray<Token>) {
196
+ this.#offset = 0;
197
+ this.#tokens = tokens.slice();
198
+ }
199
+
200
+ clone(): TokenString {
201
+ return new TokenString(this.#tokens);
202
+ }
203
+ reset(): void {
204
+ this.#offset = 0;
205
+ }
206
+
207
+ #subTokenString(from = 0, to = 0): TokenString {
208
+ return new TokenString(
209
+ this.#tokens.slice(from, to).map((t) => {
210
+ return Object.freeze(
211
+ Object.assign({}, t, {
212
+ match: t.match - from,
213
+ linkBack: t.linkBack - from,
214
+ linkNext: t.linkNext - from,
215
+ })
216
+ );
217
+ })
218
+ );
219
+ }
220
+
221
+ // Pops and returns the value of the next token, if it is a keyword in allowed; throws if out of tokens
222
+ popKeyword(allowed: ReadonlySet<string>): string {
223
+ const top = this.peek();
224
+ if (top.type !== 'KEYWORD' || !allowed.has(top.text)) {
225
+ throw new Error(`expected keyword ${top.text}`);
226
+ }
227
+ return this.pop().text;
228
+ }
229
+
230
+ // Pops and returns the value of the next token if it is `type`; throws if out of tokens
231
+ popType(type: string): string {
232
+ if (this.peek().type !== type) {
233
+ throw new Error(`expected ${type}; got ${JSON.stringify(this.peek())}`);
234
+ }
235
+ return this.pop().text;
236
+ }
237
+
238
+ // Pops and returns a "(" TOKENS ")"
239
+ popParen(): TokenString {
240
+ const top = this.peek();
241
+ if (top.type !== 'OPEN_PAREN') {
242
+ throw new Error('bad start');
243
+ }
244
+ const result = this.#subTokenString(this.#offset + 1, top.match + 1);
245
+ this.#offset = top.match + 1;
246
+ return result;
247
+ }
248
+
249
+ // Pops and returns the items within "(" ITEM1 "," ITEM2 "," ... ")"
250
+ popParams(): Array<TokenString> {
251
+ const top = this.peek();
252
+
253
+ if (top.type !== 'OPEN_PAREN') {
254
+ throw new Error('bad start');
255
+ }
256
+
257
+ const result: Array<TokenString> = [];
258
+
259
+ while (this.#offset < top.match - 1) {
260
+ const link = this.peek().linkNext;
261
+ result.push(this.#subTokenString(this.#offset + 1, link));
262
+ this.#offset = link;
263
+ }
264
+
265
+ this.#offset = top.match + 1;
266
+
267
+ return result;
268
+ }
269
+
270
+ // Returns the top Token, throwing if out of tokens
271
+ peek(): Token {
272
+ if (this.#offset >= this.#tokens.length) {
273
+ throw new Error('out-of-bounds');
274
+ }
275
+ return this.#tokens[this.#offset];
276
+ }
277
+
278
+ // Returns the next value, if it is a keyword in `allowed`
279
+ peekKeyword(allowed: ReadonlySet<string>): null | string {
280
+ const top = this.peekType('KEYWORD');
281
+ return top != null && allowed.has(top) ? top : null;
282
+ }
283
+
284
+ // Returns the value of the next token if it is `type`
285
+ peekType(type: string): null | string {
286
+ if (this.length === 0) {
287
+ return null;
288
+ }
289
+ const top = this.peek();
290
+ return top.type === type ? top.text : null;
291
+ }
292
+
293
+ // Returns the next token; throws if out of tokens
294
+ pop(): Token {
295
+ const result = this.peek();
296
+ this.#offset++;
297
+ return result;
298
+ }
299
+
300
+ toString(): string {
301
+ const tokens: Array<string> = [];
302
+ for (let i = this.#offset; i < this.#tokens.length; i++) {
303
+ const token = this.#tokens[i];
304
+ tokens.push(`${token.type}:${token.text}`);
305
+ }
306
+ return `<TokenString ${tokens.join(' ')}>`;
307
+ }
308
+ }
309
+
310
+ type Writeable<T> = { -readonly [P in keyof T]: T[P] };
311
+
312
+ function lex(text: string): TokenString {
313
+ const tokens: Array<Token> = [];
314
+
315
+ const throwError = (message: string) => {
316
+ const token = offset < text.length ? JSON.stringify(text[offset]) : '$EOI';
317
+ throw new Error(`invalid token ${token} at ${offset}: ${message}`);
318
+ };
319
+
320
+ const brackets: Array<number> = [];
321
+ const commas: Array<number> = [];
322
+
323
+ let offset = 0;
324
+ while (offset < text.length) {
325
+ // Strip off any leading whitespace
326
+ let cur = text.substring(offset);
327
+ let match = cur.match(regexWhitespacePrefix);
328
+ if (match) {
329
+ offset += match[1].length;
330
+ cur = text.substring(offset);
331
+ }
332
+
333
+ const token = {
334
+ depth: brackets.length,
335
+ linkBack: -1,
336
+ linkNext: -1,
337
+ match: -1,
338
+ type: '',
339
+ text: '',
340
+ offset,
341
+ value: -1,
342
+ };
343
+ tokens.push(token);
344
+
345
+ const type = SimpleTokens[cur[0]] || '';
346
+ if (type) {
347
+ token.type = type;
348
+ token.text = cur[0];
349
+ offset++;
350
+
351
+ if (type === 'OPEN_PAREN') {
352
+ brackets.push(tokens.length - 1);
353
+ commas.push(tokens.length - 1);
354
+ } else if (type == 'CLOSE_PAREN') {
355
+ if (brackets.length === 0) {
356
+ throwError('no matching open bracket');
357
+ }
358
+
359
+ token.match = brackets.pop() as number;
360
+ (<Writeable<Token>>tokens[token.match]).match = tokens.length - 1;
361
+ token.depth--;
362
+
363
+ token.linkBack = commas.pop() as number;
364
+ (<Writeable<Token>>tokens[token.linkBack]).linkNext = tokens.length - 1;
365
+ } else if (type === 'COMMA') {
366
+ token.linkBack = commas.pop() as number;
367
+ (<Writeable<Token>>tokens[token.linkBack]).linkNext = tokens.length - 1;
368
+ commas.push(tokens.length - 1);
369
+ } else if (type === 'OPEN_BRACKET') {
370
+ token.type = 'BRACKET';
371
+ } else if (type === 'CLOSE_BRACKET') {
372
+ // Remove the CLOSE_BRACKET
373
+ let suffix = (tokens.pop() as Token).text;
374
+ if (tokens.length > 0 && tokens[tokens.length - 1].type === 'NUMBER') {
375
+ const value = (tokens.pop() as Token).text;
376
+ suffix = value + suffix;
377
+ (<Writeable<Token>>tokens[tokens.length - 1]).value = getNumber(value);
378
+ }
379
+ if (tokens.length === 0 || tokens[tokens.length - 1].type !== 'BRACKET') {
380
+ throw new Error('missing opening bracket');
381
+ }
382
+ (<Writeable<Token>>tokens[tokens.length - 1]).text += suffix;
383
+ }
384
+
385
+ continue;
386
+ }
387
+
388
+ match = cur.match(regexIdPrefix);
389
+ if (match) {
390
+ token.text = match[1];
391
+ offset += token.text.length;
392
+
393
+ if (Keywords.has(token.text)) {
394
+ token.type = 'KEYWORD';
395
+ continue;
396
+ }
397
+
398
+ if (token.text.match(regexType)) {
399
+ token.type = 'TYPE';
400
+ continue;
401
+ }
402
+
403
+ token.type = 'ID';
404
+ continue;
405
+ }
406
+
407
+ match = cur.match(regexNumberPrefix);
408
+ if (match) {
409
+ token.text = match[1];
410
+ token.type = 'NUMBER';
411
+ offset += token.text.length;
412
+ continue;
413
+ }
414
+
415
+ throw new Error(`unexpected token ${JSON.stringify(cur[0])} at position ${offset}`);
416
+ }
417
+
418
+ return new TokenString(tokens.map((t) => Object.freeze(t)));
419
+ }
420
+
421
+ // Check only one of `allowed` is in `set`
422
+ function allowSingle(set: ReadonlySet<string>, allowed: ReadonlySet<string>): void {
423
+ const included: Array<string> = [];
424
+ for (const key in allowed.keys()) {
425
+ if (set.has(key)) {
426
+ included.push(key);
427
+ }
428
+ }
429
+ if (included.length > 1) {
430
+ throw new Error(`conflicting types: ${included.join(', ')}`);
431
+ }
432
+ }
433
+
434
+ // Functions to process a Solidity Signature TokenString from left-to-right for...
435
+
436
+ // ...the name with an optional type, returning the name
437
+ function consumeName(type: string, tokens: TokenString): string {
438
+ if (tokens.peekKeyword(KwTypes)) {
439
+ const keyword = tokens.pop().text;
440
+ if (keyword !== type) {
441
+ throw new Error(`expected ${type}, got ${keyword}`);
442
+ }
443
+ }
444
+
445
+ return tokens.popType('ID');
446
+ }
447
+
448
+ // ...all keywords matching allowed, returning the keywords
449
+ function consumeKeywords(tokens: TokenString, allowed?: ReadonlySet<string>): ReadonlySet<string> {
450
+ const keywords: Set<string> = new Set();
451
+ // eslint-disable-next-line no-constant-condition
452
+ while (true) {
453
+ const keyword = tokens.peekType('KEYWORD');
454
+
455
+ if (keyword == null || (allowed && !allowed.has(keyword))) {
456
+ break;
457
+ }
458
+ tokens.pop();
459
+
460
+ if (keywords.has(keyword)) {
461
+ throw new Error(`duplicate keywords: ${JSON.stringify(keyword)}`);
462
+ }
463
+ keywords.add(keyword);
464
+ }
465
+
466
+ return Object.freeze(keywords);
467
+ }
468
+
469
+ // ...all visibility keywords, returning the coalesced mutability
470
+ function consumeMutability(tokens: TokenString): 'payable' | 'nonpayable' | 'view' | 'pure' {
471
+ const modifiers = consumeKeywords(tokens, KwVisib);
472
+
473
+ // Detect conflicting modifiers
474
+ allowSingle(modifiers, setify('constant payable nonpayable'.split(' ')));
475
+ allowSingle(modifiers, setify('pure view payable nonpayable'.split(' ')));
476
+
477
+ // Process mutability states
478
+ if (modifiers.has('view')) {
479
+ return 'view';
480
+ }
481
+ if (modifiers.has('pure')) {
482
+ return 'pure';
483
+ }
484
+ if (modifiers.has('payable')) {
485
+ return 'payable';
486
+ }
487
+ if (modifiers.has('nonpayable')) {
488
+ return 'nonpayable';
489
+ }
490
+
491
+ // Process legacy `constant` last
492
+ if (modifiers.has('constant')) {
493
+ return 'view';
494
+ }
495
+
496
+ return 'nonpayable';
497
+ }
498
+
499
+ // ...a parameter list, returning the ParamType list
500
+ function consumeParams(tokens: TokenString, allowIndexed?: boolean): Array<ParamType> {
501
+ return tokens.popParams().map((t) => ParamType.from(t, allowIndexed));
502
+ }
503
+
504
+ // ...a gas limit, returning a BigNumber or null if none
505
+ function consumeGas(tokens: TokenString): null | bigint {
506
+ if (tokens.peekType('AT')) {
507
+ tokens.pop();
508
+ if (tokens.peekType('NUMBER')) {
509
+ return getBigInt(tokens.pop().text);
510
+ }
511
+ throw new Error('invalid gas');
512
+ }
513
+ return null;
514
+ }
515
+
516
+ function consumeEoi(tokens: TokenString): void {
517
+ if (tokens.length) {
518
+ throw new Error(`unexpected tokens: ${tokens.toString()}`);
519
+ }
520
+ }
521
+
522
+ const regexArrayType = new RegExp(/^(.*)\[([0-9]*)\]$/);
523
+
524
+ function verifyBasicType(type: string): string {
525
+ const match = type.match(regexType);
526
+ assertArgument(match, 'invalid type', 'type', type);
527
+ if (type === 'uint') {
528
+ return 'uint256';
529
+ }
530
+ if (type === 'int') {
531
+ return 'int256';
532
+ }
533
+
534
+ if (match[2]) {
535
+ // bytesXX
536
+ const length = parseInt(match[2]);
537
+ assertArgument(length !== 0 && length <= 32, 'invalid bytes length', 'type', type);
538
+ } else if (match[3]) {
539
+ // intXX or uintXX
540
+ const size = parseInt(match[3] as string);
541
+ assertArgument(size !== 0 && size <= 256 && size % 8 === 0, 'invalid numeric width', 'type', type);
542
+ }
543
+
544
+ return type;
545
+ }
546
+
547
+ // Make the Fragment constructors effectively private
548
+ const _guard = {};
549
+
550
+ /**
551
+ * When [walking](ParamType-walk) a [[ParamType]], this is called
552
+ * on each component.
553
+ */
554
+ export type ParamTypeWalkFunc = (type: string, value: any) => any;
555
+
556
+ /**
557
+ * When [walking asynchronously](ParamType-walkAsync) a [[ParamType]],
558
+ * this is called on each component.
559
+ */
560
+ export type ParamTypeWalkAsyncFunc = (type: string, value: any) => any | Promise<any>;
561
+
562
+ const internal = Symbol.for('_ethers_internal');
563
+
564
+ const ParamTypeInternal = '_ParamTypeInternal';
565
+ const ErrorFragmentInternal = '_ErrorInternal';
566
+ const EventFragmentInternal = '_EventInternal';
567
+ const ConstructorFragmentInternal = '_ConstructorInternal';
568
+ const FallbackFragmentInternal = '_FallbackInternal';
569
+ const FunctionFragmentInternal = '_FunctionInternal';
570
+ const StructFragmentInternal = '_StructInternal';
571
+
572
+ /**
573
+ * Each input and output of a [[Fragment]] is an Array of **ParamType**.
574
+ */
575
+ export class ParamType {
576
+ /**
577
+ * The local name of the parameter (or ``""`` if unbound)
578
+ */
579
+ readonly name!: string;
580
+
581
+ /**
582
+ * The fully qualified type (e.g. ``"address"``, ``"tuple(address)"``,
583
+ * ``"uint256[3][]"``)
584
+ */
585
+ readonly type!: string;
586
+
587
+ /**
588
+ * The base type (e.g. ``"address"``, ``"tuple"``, ``"array"``)
589
+ */
590
+ readonly baseType!: string;
591
+
592
+ /**
593
+ * True if the parameters is indexed.
594
+ *
595
+ * For non-indexable types this is ``null``.
596
+ */
597
+ readonly indexed!: null | boolean;
598
+
599
+ /**
600
+ * The components for the tuple.
601
+ *
602
+ * For non-tuple types this is ``null``.
603
+ */
604
+ readonly components!: null | ReadonlyArray<ParamType>;
605
+
606
+ /**
607
+ * The array length, or ``-1`` for dynamic-lengthed arrays.
608
+ *
609
+ * For non-array types this is ``null``.
610
+ */
611
+ readonly arrayLength!: null | number;
612
+
613
+ /**
614
+ * The type of each child in the array.
615
+ *
616
+ * For non-array types this is ``null``.
617
+ */
618
+ readonly arrayChildren!: null | ParamType;
619
+
620
+ /**
621
+ * @private
622
+ */
623
+ constructor(
624
+ guard: any,
625
+ name: string,
626
+ type: string,
627
+ baseType: string,
628
+ indexed: null | boolean,
629
+ components: null | ReadonlyArray<ParamType>,
630
+ arrayLength: null | number,
631
+ arrayChildren: null | ParamType
632
+ ) {
633
+ assertPrivate(guard, _guard, 'ParamType');
634
+ Object.defineProperty(this, internal, { value: ParamTypeInternal });
635
+
636
+ if (components) {
637
+ components = Object.freeze(components.slice());
638
+ }
639
+
640
+ if (baseType === 'array') {
641
+ if (arrayLength == null || arrayChildren == null) {
642
+ throw new Error('');
643
+ }
644
+ } else if (arrayLength != null || arrayChildren != null) {
645
+ throw new Error('');
646
+ }
647
+
648
+ if (baseType === 'tuple') {
649
+ if (components == null) {
650
+ throw new Error('');
651
+ }
652
+ } else if (components != null) {
653
+ throw new Error('');
654
+ }
655
+
656
+ defineProperties<ParamType>(this, {
657
+ name,
658
+ type,
659
+ baseType,
660
+ indexed,
661
+ components,
662
+ arrayLength,
663
+ arrayChildren,
664
+ });
665
+ }
666
+
667
+ /**
668
+ * Return a string representation of this type.
669
+ *
670
+ * For example,
671
+ *
672
+ * ``sighash" => "(uint256,address)"``
673
+ *
674
+ * ``"minimal" => "tuple(uint256,address) indexed"``
675
+ *
676
+ * ``"full" => "tuple(uint256 foo, address bar) indexed baz"``
677
+ */
678
+ format(format?: FormatType): string {
679
+ if (format == null) {
680
+ format = 'sighash';
681
+ }
682
+ if (format === 'json') {
683
+ const result: any = {
684
+ type: this.baseType === 'tuple' ? 'tuple' : this.type,
685
+ name: this.name || undefined,
686
+ };
687
+ if (typeof this.indexed === 'boolean') {
688
+ result.indexed = this.indexed;
689
+ }
690
+ if (this.isTuple()) {
691
+ result.components = this.components.map((c) => JSON.parse(c.format(format)));
692
+ }
693
+ return JSON.stringify(result);
694
+ }
695
+
696
+ let result = '';
697
+
698
+ // Array
699
+ if (this.isArray()) {
700
+ result += this.arrayChildren.format(format);
701
+ result += `[${this.arrayLength < 0 ? '' : String(this.arrayLength)}]`;
702
+ } else {
703
+ if (this.isTuple()) {
704
+ if (format !== 'sighash') {
705
+ result += this.type;
706
+ }
707
+ result += '(' + this.components.map((comp) => comp.format(format)).join(format === 'full' ? ', ' : ',') + ')';
708
+ } else {
709
+ result += this.type;
710
+ }
711
+ }
712
+
713
+ if (format !== 'sighash') {
714
+ if (this.indexed === true) {
715
+ result += ' indexed';
716
+ }
717
+ if (format === 'full' && this.name) {
718
+ result += ' ' + this.name;
719
+ }
720
+ }
721
+
722
+ return result;
723
+ }
724
+
725
+ /**
726
+ * Returns true if %%this%% is an Array type.
727
+ *
728
+ * This provides a type gaurd ensuring that [[arrayChildren]]
729
+ * and [[arrayLength]] are non-null.
730
+ */
731
+ isArray(): this is ParamType & {
732
+ arrayChildren: ParamType;
733
+ arrayLength: number;
734
+ } {
735
+ return this.baseType === 'array';
736
+ }
737
+
738
+ /**
739
+ * Returns true if %%this%% is a Tuple type.
740
+ *
741
+ * This provides a type gaurd ensuring that [[components]]
742
+ * is non-null.
743
+ */
744
+ isTuple(): this is ParamType & { components: ReadonlyArray<ParamType> } {
745
+ return this.baseType === 'tuple';
746
+ }
747
+
748
+ /**
749
+ * Returns true if %%this%% is an Indexable type.
750
+ *
751
+ * This provides a type gaurd ensuring that [[indexed]]
752
+ * is non-null.
753
+ */
754
+ isIndexable(): this is ParamType & { indexed: boolean } {
755
+ return this.indexed != null;
756
+ }
757
+
758
+ /**
759
+ * Walks the **ParamType** with %%value%%, calling %%process%%
760
+ * on each type, destructing the %%value%% recursively.
761
+ */
762
+ walk(value: any, process: ParamTypeWalkFunc): any {
763
+ if (this.isArray()) {
764
+ if (!Array.isArray(value)) {
765
+ throw new Error('invalid array value');
766
+ }
767
+ if (this.arrayLength !== -1 && value.length !== this.arrayLength) {
768
+ throw new Error('array is wrong length');
769
+ }
770
+ return value.map((v) => this.arrayChildren?.walk(v, process));
771
+ }
772
+
773
+ if (this.isTuple()) {
774
+ if (!Array.isArray(value)) {
775
+ throw new Error('invalid tuple value');
776
+ }
777
+ if (value.length !== this.components.length) {
778
+ throw new Error('array is wrong length');
779
+ }
780
+ return value.map((v, i) => this.components?.[i].walk(v, process));
781
+ }
782
+
783
+ return process(this.type, value);
784
+ }
785
+
786
+ #walkAsync(
787
+ promises: Array<Promise<void>>,
788
+ value: any,
789
+ process: ParamTypeWalkAsyncFunc,
790
+ setValue: (value: any) => void
791
+ ): void {
792
+ if (this.isArray()) {
793
+ if (!Array.isArray(value)) {
794
+ throw new Error('invalid array value');
795
+ }
796
+ if (this.arrayLength !== -1 && value.length !== this.arrayLength) {
797
+ throw new Error('array is wrong length');
798
+ }
799
+ const childType = this.arrayChildren;
800
+
801
+ const result = value.slice();
802
+ result.forEach((value, index) => {
803
+ childType.#walkAsync(promises, value, process, (value: any) => {
804
+ result[index] = value;
805
+ });
806
+ });
807
+ setValue(result);
808
+ return;
809
+ }
810
+
811
+ if (this.isTuple()) {
812
+ const components = this.components;
813
+
814
+ // Convert the object into an array
815
+ let result: Array<any>;
816
+ if (Array.isArray(value)) {
817
+ result = value.slice();
818
+ } else {
819
+ if (value == null || typeof value !== 'object') {
820
+ throw new Error('invalid tuple value');
821
+ }
822
+
823
+ result = components.map((param) => {
824
+ if (!param.name) {
825
+ throw new Error('cannot use object value with unnamed components');
826
+ }
827
+ if (!(param.name in value)) {
828
+ throw new Error(`missing value for component ${param.name}`);
829
+ }
830
+ return value[param.name];
831
+ });
832
+ }
833
+
834
+ if (result.length !== this.components.length) {
835
+ throw new Error('array is wrong length');
836
+ }
837
+
838
+ result.forEach((value, index) => {
839
+ components[index].#walkAsync(promises, value, process, (value: any) => {
840
+ result[index] = value;
841
+ });
842
+ });
843
+ setValue(result);
844
+ return;
845
+ }
846
+
847
+ const result = process(this.type, value);
848
+ if (result.then) {
849
+ promises.push(
850
+ (async function () {
851
+ setValue(await result);
852
+ })()
853
+ );
854
+ } else {
855
+ setValue(result);
856
+ }
857
+ }
858
+
859
+ /**
860
+ * Walks the **ParamType** with %%value%%, asynchronously calling
861
+ * %%process%% on each type, destructing the %%value%% recursively.
862
+ *
863
+ * This can be used to resolve ENS naes by walking and resolving each
864
+ * ``"address"`` type.
865
+ */
866
+ async walkAsync(value: any, process: ParamTypeWalkAsyncFunc): Promise<any> {
867
+ const promises: Array<Promise<void>> = [];
868
+ const result: [any] = [value];
869
+ this.#walkAsync(promises, value, process, (value: any) => {
870
+ result[0] = value;
871
+ });
872
+ if (promises.length) {
873
+ await Promise.all(promises);
874
+ }
875
+ return result[0];
876
+ }
877
+
878
+ /**
879
+ * Creates a new **ParamType** for %%obj%%.
880
+ *
881
+ * If %%allowIndexed%% then the ``indexed`` keyword is permitted,
882
+ * otherwise the ``indexed`` keyword will throw an error.
883
+ */
884
+ static from(obj: any, allowIndexed?: boolean): ParamType {
885
+ if (ParamType.isParamType(obj)) {
886
+ return obj;
887
+ }
888
+
889
+ if (typeof obj === 'string') {
890
+ return ParamType.from(lex(obj), allowIndexed);
891
+ } else if (obj instanceof TokenString) {
892
+ let type = '',
893
+ baseType = '';
894
+ let comps: null | Array<ParamType> = null;
895
+
896
+ if (consumeKeywords(obj, setify(['tuple'])).has('tuple') || obj.peekType('OPEN_PAREN')) {
897
+ // Tuple
898
+ baseType = 'tuple';
899
+ comps = obj.popParams().map((t) => ParamType.from(t));
900
+ type = `tuple(${comps.map((c) => c.format()).join(',')})`;
901
+ } else {
902
+ // Normal
903
+ type = verifyBasicType(obj.popType('TYPE'));
904
+ baseType = type;
905
+ }
906
+
907
+ // Check for Array
908
+ let arrayChildren: null | ParamType = null;
909
+ let arrayLength: null | number = null;
910
+
911
+ while (obj.length && obj.peekType('BRACKET')) {
912
+ const bracket = obj.pop(); //arrays[i];
913
+ arrayChildren = new ParamType(_guard, '', type, baseType, null, comps, arrayLength, arrayChildren);
914
+ arrayLength = bracket.value;
915
+ type += bracket.text;
916
+ baseType = 'array';
917
+ comps = null;
918
+ }
919
+
920
+ let indexed: boolean | null = null;
921
+ const keywords = consumeKeywords(obj, KwModifiers);
922
+ if (keywords.has('indexed')) {
923
+ if (!allowIndexed) {
924
+ throw new Error('');
925
+ }
926
+ indexed = true;
927
+ }
928
+
929
+ const name = obj.peekType('ID') ? obj.pop().text : '';
930
+
931
+ if (obj.length) {
932
+ throw new Error('leftover tokens');
933
+ }
934
+
935
+ return new ParamType(_guard, name, type, baseType, indexed, comps, arrayLength, arrayChildren);
936
+ }
937
+
938
+ const name = obj.name;
939
+ assertArgument(!name || (typeof name === 'string' && name.match(regexId)), 'invalid name', 'obj.name', name);
940
+
941
+ let indexed = obj.indexed;
942
+ if (indexed != null) {
943
+ assertArgument(allowIndexed, 'parameter cannot be indexed', 'obj.indexed', obj.indexed);
944
+ indexed = !!indexed;
945
+ }
946
+
947
+ let type = obj.type;
948
+
949
+ const arrayMatch = type.match(regexArrayType);
950
+ if (arrayMatch) {
951
+ const arrayLength = parseInt(arrayMatch[2] || '-1');
952
+ const arrayChildren = ParamType.from({
953
+ type: arrayMatch[1],
954
+ components: obj.components,
955
+ });
956
+
957
+ return new ParamType(_guard, name || '', type, 'array', indexed, null, arrayLength, arrayChildren);
958
+ }
959
+
960
+ if (type === 'tuple' || type.startsWith('tuple(' /* fix: ) */) || type.startsWith('(' /* fix: ) */)) {
961
+ const comps = obj.components != null ? obj.components.map((c: any) => ParamType.from(c)) : null;
962
+ const tuple = new ParamType(_guard, name || '', type, 'tuple', indexed, comps, null, null);
963
+ // @TODO: use lexer to validate and normalize type
964
+ return tuple;
965
+ }
966
+
967
+ type = verifyBasicType(obj.type);
968
+
969
+ return new ParamType(_guard, name || '', type, type, indexed, null, null, null);
970
+ }
971
+
972
+ /**
973
+ * Returns true if %%value%% is a **ParamType**.
974
+ */
975
+ static isParamType(value: any): value is ParamType {
976
+ return value && value[internal] === ParamTypeInternal;
977
+ }
978
+ }
979
+
980
+ /**
981
+ * The type of a [[Fragment]].
982
+ */
983
+ export type FragmentType = 'constructor' | 'error' | 'event' | 'fallback' | 'function' | 'struct';
984
+
985
+ /**
986
+ * An abstract class to represent An individual fragment from a parse ABI.
987
+ */
988
+ export abstract class Fragment {
989
+ /**
990
+ * The type of the fragment.
991
+ */
992
+ readonly type!: FragmentType;
993
+
994
+ /**
995
+ * The inputs for the fragment.
996
+ */
997
+ readonly inputs!: ReadonlyArray<ParamType>;
998
+
999
+ /**
1000
+ * @private
1001
+ */
1002
+ constructor(guard: any, type: FragmentType, inputs: ReadonlyArray<ParamType>) {
1003
+ assertPrivate(guard, _guard, 'Fragment');
1004
+ inputs = Object.freeze(inputs.slice());
1005
+ defineProperties<Fragment>(this, { type, inputs });
1006
+ }
1007
+
1008
+ /**
1009
+ * Returns a string representation of this fragment as %%format%%.
1010
+ */
1011
+ abstract format(format?: FormatType): string;
1012
+
1013
+ /**
1014
+ * Creates a new **Fragment** for %%obj%%, wich can be any supported
1015
+ * ABI frgament type.
1016
+ */
1017
+ static from(obj: any): Fragment {
1018
+ if (typeof obj === 'string') {
1019
+ // Try parsing JSON...
1020
+ try {
1021
+ Fragment.from(JSON.parse(obj));
1022
+ } catch (e) {
1023
+ //
1024
+ }
1025
+
1026
+ // ...otherwise, use the human-readable lexer
1027
+ return Fragment.from(lex(obj));
1028
+ }
1029
+
1030
+ if (obj instanceof TokenString) {
1031
+ // Human-readable ABI (already lexed)
1032
+
1033
+ const type = obj.peekKeyword(KwTypes);
1034
+
1035
+ switch (type) {
1036
+ case 'constructor':
1037
+ return ConstructorFragment.from(obj);
1038
+ case 'error':
1039
+ return ErrorFragment.from(obj);
1040
+ case 'event':
1041
+ return EventFragment.from(obj);
1042
+ case 'fallback':
1043
+ case 'receive':
1044
+ return FallbackFragment.from(obj);
1045
+ case 'function':
1046
+ return FunctionFragment.from(obj);
1047
+ case 'struct':
1048
+ return StructFragment.from(obj);
1049
+ }
1050
+ } else if (typeof obj === 'object') {
1051
+ // JSON ABI
1052
+
1053
+ switch (obj.type) {
1054
+ case 'constructor':
1055
+ return ConstructorFragment.from(obj);
1056
+ case 'error':
1057
+ return ErrorFragment.from(obj);
1058
+ case 'event':
1059
+ return EventFragment.from(obj);
1060
+ case 'fallback':
1061
+ case 'receive':
1062
+ return FallbackFragment.from(obj);
1063
+ case 'function':
1064
+ return FunctionFragment.from(obj);
1065
+ case 'struct':
1066
+ return StructFragment.from(obj);
1067
+ }
1068
+
1069
+ assert(false, `unsupported type: ${obj.type}`, 'UNSUPPORTED_OPERATION', {
1070
+ operation: 'Fragment.from',
1071
+ });
1072
+ }
1073
+
1074
+ assertArgument(false, 'unsupported frgament object', 'obj', obj);
1075
+ }
1076
+
1077
+ /**
1078
+ * Returns true if %%value%% is a [[ConstructorFragment]].
1079
+ */
1080
+ static isConstructor(value: any): value is ConstructorFragment {
1081
+ return ConstructorFragment.isFragment(value);
1082
+ }
1083
+
1084
+ /**
1085
+ * Returns true if %%value%% is an [[ErrorFragment]].
1086
+ */
1087
+ static isError(value: any): value is ErrorFragment {
1088
+ return ErrorFragment.isFragment(value);
1089
+ }
1090
+
1091
+ /**
1092
+ * Returns true if %%value%% is an [[EventFragment]].
1093
+ */
1094
+ static isEvent(value: any): value is EventFragment {
1095
+ return EventFragment.isFragment(value);
1096
+ }
1097
+
1098
+ /**
1099
+ * Returns true if %%value%% is a [[FunctionFragment]].
1100
+ */
1101
+ static isFunction(value: any): value is FunctionFragment {
1102
+ return FunctionFragment.isFragment(value);
1103
+ }
1104
+
1105
+ /**
1106
+ * Returns true if %%value%% is a [[StructFragment]].
1107
+ */
1108
+ static isStruct(value: any): value is StructFragment {
1109
+ return StructFragment.isFragment(value);
1110
+ }
1111
+ }
1112
+
1113
+ /**
1114
+ * An abstract class to represent An individual fragment
1115
+ * which has a name from a parse ABI.
1116
+ */
1117
+ export abstract class NamedFragment extends Fragment {
1118
+ /**
1119
+ * The name of the fragment.
1120
+ */
1121
+ readonly name!: string;
1122
+
1123
+ /**
1124
+ * @private
1125
+ */
1126
+ constructor(guard: any, type: FragmentType, name: string, inputs: ReadonlyArray<ParamType>) {
1127
+ super(guard, type, inputs);
1128
+ assertArgument(typeof name === 'string' && name.match(regexId), 'invalid identifier', 'name', name);
1129
+ inputs = Object.freeze(inputs.slice());
1130
+ defineProperties<NamedFragment>(this, { name });
1131
+ }
1132
+ }
1133
+
1134
+ function joinParams(format: FormatType, params: ReadonlyArray<ParamType>): string {
1135
+ return '(' + params.map((p) => p.format(format)).join(format === 'full' ? ', ' : ',') + ')';
1136
+ }
1137
+
1138
+ /**
1139
+ * A Fragment which represents a //Custom Error//.
1140
+ */
1141
+ export class ErrorFragment extends NamedFragment {
1142
+ /**
1143
+ * @private
1144
+ */
1145
+ constructor(guard: any, name: string, inputs: ReadonlyArray<ParamType>) {
1146
+ super(guard, 'error', name, inputs);
1147
+ Object.defineProperty(this, internal, { value: ErrorFragmentInternal });
1148
+ }
1149
+
1150
+ /**
1151
+ * The Custom Error selector.
1152
+ */
1153
+ get selector(): string {
1154
+ return id(this.format('sighash')).substring(0, 10);
1155
+ }
1156
+
1157
+ /**
1158
+ * Returns a string representation of this fragment as %%format%%.
1159
+ */
1160
+ format(format?: FormatType): string {
1161
+ if (format == null) {
1162
+ format = 'sighash';
1163
+ }
1164
+ if (format === 'json') {
1165
+ return JSON.stringify({
1166
+ type: 'error',
1167
+ name: this.name,
1168
+ inputs: this.inputs.map((input) => JSON.parse(input.format(format))),
1169
+ });
1170
+ }
1171
+
1172
+ const result: string[] = [];
1173
+ if (format !== 'sighash') {
1174
+ result.push('error');
1175
+ }
1176
+ result.push(this.name + joinParams(format, this.inputs));
1177
+ return result.join(' ');
1178
+ }
1179
+
1180
+ /**
1181
+ * Returns a new **ErrorFragment** for %%obj%%.
1182
+ */
1183
+ static from(obj: any): ErrorFragment {
1184
+ if (ErrorFragment.isFragment(obj)) {
1185
+ return obj;
1186
+ }
1187
+
1188
+ if (typeof obj === 'string') {
1189
+ return ErrorFragment.from(lex(obj));
1190
+ } else if (obj instanceof TokenString) {
1191
+ const name = consumeName('error', obj);
1192
+ const inputs = consumeParams(obj);
1193
+ consumeEoi(obj);
1194
+
1195
+ return new ErrorFragment(_guard, name, inputs);
1196
+ }
1197
+
1198
+ return new ErrorFragment(_guard, obj.name, obj.inputs ? obj.inputs.map(ParamType.from) : []);
1199
+ }
1200
+
1201
+ /**
1202
+ * Returns ``true`` and provides a type guard if %%value%% is an
1203
+ * **ErrorFragment**.
1204
+ */
1205
+ static isFragment(value: any): value is ErrorFragment {
1206
+ return value && value[internal] === ErrorFragmentInternal;
1207
+ }
1208
+ }
1209
+
1210
+ /**
1211
+ * A Fragment which represents an Event.
1212
+ */
1213
+ export class EventFragment extends NamedFragment {
1214
+ /**
1215
+ * Whether this event is anonymous.
1216
+ */
1217
+ readonly anonymous!: boolean;
1218
+
1219
+ /**
1220
+ * @private
1221
+ */
1222
+ constructor(guard: any, name: string, inputs: ReadonlyArray<ParamType>, anonymous: boolean) {
1223
+ super(guard, 'event', name, inputs);
1224
+ Object.defineProperty(this, internal, { value: EventFragmentInternal });
1225
+ defineProperties<EventFragment>(this, { anonymous });
1226
+ }
1227
+
1228
+ /**
1229
+ * The Event topic hash.
1230
+ */
1231
+ get topicHash(): string {
1232
+ return id(this.format('sighash'));
1233
+ }
1234
+
1235
+ /**
1236
+ * Returns a string representation of this event as %%format%%.
1237
+ */
1238
+ format(format?: FormatType): string {
1239
+ if (format == null) {
1240
+ format = 'sighash';
1241
+ }
1242
+ if (format === 'json') {
1243
+ return JSON.stringify({
1244
+ type: 'event',
1245
+ anonymous: this.anonymous,
1246
+ name: this.name,
1247
+ inputs: this.inputs.map((i) => JSON.parse(i.format(format))),
1248
+ });
1249
+ }
1250
+
1251
+ const result: string[] = [];
1252
+ if (format !== 'sighash') {
1253
+ result.push('event');
1254
+ }
1255
+ result.push(this.name + joinParams(format, this.inputs));
1256
+ if (format !== 'sighash' && this.anonymous) {
1257
+ result.push('anonymous');
1258
+ }
1259
+ return result.join(' ');
1260
+ }
1261
+
1262
+ /**
1263
+ * Return the topic hash for an event with %%name%% and %%params%%.
1264
+ */
1265
+ static getTopicHash(name: string, params?: Array<any>): string {
1266
+ params = (params || []).map((p) => ParamType.from(p));
1267
+ const fragment = new EventFragment(_guard, name, params, false);
1268
+ return fragment.topicHash;
1269
+ }
1270
+
1271
+ /**
1272
+ * Returns a new **EventFragment** for %%obj%%.
1273
+ */
1274
+ static from(obj: any): EventFragment {
1275
+ if (EventFragment.isFragment(obj)) {
1276
+ return obj;
1277
+ }
1278
+
1279
+ if (typeof obj === 'string') {
1280
+ return EventFragment.from(lex(obj));
1281
+ } else if (obj instanceof TokenString) {
1282
+ const name = consumeName('event', obj);
1283
+ const inputs = consumeParams(obj, true);
1284
+ const anonymous = !!consumeKeywords(obj, setify(['anonymous'])).has('anonymous');
1285
+ consumeEoi(obj);
1286
+
1287
+ return new EventFragment(_guard, name, inputs, anonymous);
1288
+ }
1289
+
1290
+ return new EventFragment(
1291
+ _guard,
1292
+ obj.name,
1293
+ obj.inputs ? obj.inputs.map((p: any) => ParamType.from(p, true)) : [],
1294
+ !!obj.anonymous
1295
+ );
1296
+ }
1297
+
1298
+ /**
1299
+ * Returns ``true`` and provides a type guard if %%value%% is an
1300
+ * **EventFragment**.
1301
+ */
1302
+ static isFragment(value: any): value is EventFragment {
1303
+ return value && value[internal] === EventFragmentInternal;
1304
+ }
1305
+ }
1306
+
1307
+ /**
1308
+ * A Fragment which represents a constructor.
1309
+ */
1310
+ export class ConstructorFragment extends Fragment {
1311
+ /**
1312
+ * Whether the constructor can receive an endowment.
1313
+ */
1314
+ readonly payable!: boolean;
1315
+
1316
+ /**
1317
+ * The recommended gas limit for deployment or ``null``.
1318
+ */
1319
+ readonly gas!: null | bigint;
1320
+
1321
+ /**
1322
+ * @private
1323
+ */
1324
+ constructor(guard: any, type: FragmentType, inputs: ReadonlyArray<ParamType>, payable: boolean, gas: null | bigint) {
1325
+ super(guard, type, inputs);
1326
+ Object.defineProperty(this, internal, {
1327
+ value: ConstructorFragmentInternal,
1328
+ });
1329
+ defineProperties<ConstructorFragment>(this, { payable, gas });
1330
+ }
1331
+
1332
+ /**
1333
+ * Returns a string representation of this constructor as %%format%%.
1334
+ */
1335
+ format(format?: FormatType): string {
1336
+ assert(format != null && format !== 'sighash', 'cannot format a constructor for sighash', 'UNSUPPORTED_OPERATION', {
1337
+ operation: 'format(sighash)',
1338
+ });
1339
+
1340
+ if (format === 'json') {
1341
+ return JSON.stringify({
1342
+ type: 'constructor',
1343
+ stateMutability: this.payable ? 'payable' : 'undefined',
1344
+ payable: this.payable,
1345
+ gas: this.gas != null ? this.gas : undefined,
1346
+ inputs: this.inputs.map((i) => JSON.parse(i.format(format))),
1347
+ });
1348
+ }
1349
+
1350
+ const result = [`constructor${joinParams(format, this.inputs)}`];
1351
+ result.push(this.payable ? 'payable' : 'nonpayable');
1352
+ if (this.gas != null) {
1353
+ result.push(`@${this.gas.toString()}`);
1354
+ }
1355
+ return result.join(' ');
1356
+ }
1357
+
1358
+ /**
1359
+ * Returns a new **ConstructorFragment** for %%obj%%.
1360
+ */
1361
+ static from(obj: any): ConstructorFragment {
1362
+ if (ConstructorFragment.isFragment(obj)) {
1363
+ return obj;
1364
+ }
1365
+
1366
+ if (typeof obj === 'string') {
1367
+ return ConstructorFragment.from(lex(obj));
1368
+ } else if (obj instanceof TokenString) {
1369
+ consumeKeywords(obj, setify(['constructor']));
1370
+ const inputs = consumeParams(obj);
1371
+ const payable = !!consumeKeywords(obj, setify(['payable'])).has('payable');
1372
+ const gas = consumeGas(obj);
1373
+ consumeEoi(obj);
1374
+
1375
+ return new ConstructorFragment(_guard, 'constructor', inputs, payable, gas);
1376
+ }
1377
+
1378
+ return new ConstructorFragment(
1379
+ _guard,
1380
+ 'constructor',
1381
+ obj.inputs ? obj.inputs.map(ParamType.from) : [],
1382
+ !!obj.payable,
1383
+ obj.gas != null ? obj.gas : null
1384
+ );
1385
+ }
1386
+
1387
+ /**
1388
+ * Returns ``true`` and provides a type guard if %%value%% is a
1389
+ * **ConstructorFragment**.
1390
+ */
1391
+ static isFragment(value: any): value is ConstructorFragment {
1392
+ return value && value[internal] === ConstructorFragmentInternal;
1393
+ }
1394
+ }
1395
+
1396
+ /**
1397
+ * A Fragment which represents a method.
1398
+ */
1399
+ export class FallbackFragment extends Fragment {
1400
+ /**
1401
+ * If the function can be sent value during invocation.
1402
+ */
1403
+ readonly payable!: boolean;
1404
+
1405
+ constructor(guard: any, inputs: ReadonlyArray<ParamType>, payable: boolean) {
1406
+ super(guard, 'fallback', inputs);
1407
+ Object.defineProperty(this, internal, { value: FallbackFragmentInternal });
1408
+ defineProperties<FallbackFragment>(this, { payable });
1409
+ }
1410
+
1411
+ /**
1412
+ * Returns a string representation of this fallback as %%format%%.
1413
+ */
1414
+ format(format?: FormatType): string {
1415
+ const type = this.inputs.length === 0 ? 'receive' : 'fallback';
1416
+
1417
+ if (format === 'json') {
1418
+ const stateMutability = this.payable ? 'payable' : 'nonpayable';
1419
+ return JSON.stringify({ type, stateMutability });
1420
+ }
1421
+
1422
+ return `${type}()${this.payable ? ' payable' : ''}`;
1423
+ }
1424
+
1425
+ /**
1426
+ * Returns a new **FallbackFragment** for %%obj%%.
1427
+ */
1428
+ static from(obj: any): FallbackFragment {
1429
+ if (FallbackFragment.isFragment(obj)) {
1430
+ return obj;
1431
+ }
1432
+
1433
+ if (typeof obj === 'string') {
1434
+ return FallbackFragment.from(lex(obj));
1435
+ } else if (obj instanceof TokenString) {
1436
+ const errorObj = obj.toString();
1437
+
1438
+ const topIsValid = obj.peekKeyword(setify(['fallback', 'receive']));
1439
+ assertArgument(topIsValid, 'type must be fallback or receive', 'obj', errorObj);
1440
+
1441
+ const type = obj.popKeyword(setify(['fallback', 'receive']));
1442
+
1443
+ // receive()
1444
+ if (type === 'receive') {
1445
+ const inputs = consumeParams(obj);
1446
+ assertArgument(inputs.length === 0, `receive cannot have arguments`, 'obj.inputs', inputs);
1447
+ consumeKeywords(obj, setify(['payable']));
1448
+ consumeEoi(obj);
1449
+ return new FallbackFragment(_guard, [], true);
1450
+ }
1451
+
1452
+ // fallback() [payable]
1453
+ // fallback(bytes) [payable] returns (bytes)
1454
+ let inputs = consumeParams(obj);
1455
+ if (inputs.length) {
1456
+ assertArgument(
1457
+ inputs.length === 1 && inputs[0].type === 'bytes',
1458
+ 'invalid fallback inputs',
1459
+ 'obj.inputs',
1460
+ inputs.map((i) => i.format('minimal')).join(', ')
1461
+ );
1462
+ } else {
1463
+ inputs = [ParamType.from('bytes')];
1464
+ }
1465
+
1466
+ const mutability = consumeMutability(obj);
1467
+ assertArgument(
1468
+ mutability === 'nonpayable' || mutability === 'payable',
1469
+ 'fallback cannot be constants',
1470
+ 'obj.stateMutability',
1471
+ mutability
1472
+ );
1473
+
1474
+ if (consumeKeywords(obj, setify(['returns'])).has('returns')) {
1475
+ const outputs = consumeParams(obj);
1476
+ assertArgument(
1477
+ outputs.length === 1 && outputs[0].type === 'bytes',
1478
+ 'invalid fallback outputs',
1479
+ 'obj.outputs',
1480
+ outputs.map((i) => i.format('minimal')).join(', ')
1481
+ );
1482
+ }
1483
+
1484
+ consumeEoi(obj);
1485
+
1486
+ return new FallbackFragment(_guard, inputs, mutability === 'payable');
1487
+ }
1488
+
1489
+ if (obj.type === 'receive') {
1490
+ return new FallbackFragment(_guard, [], true);
1491
+ }
1492
+
1493
+ if (obj.type === 'fallback') {
1494
+ const inputs = [ParamType.from('bytes')];
1495
+ const payable = obj.stateMutability === 'payable';
1496
+ return new FallbackFragment(_guard, inputs, payable);
1497
+ }
1498
+
1499
+ assertArgument(false, 'invalid fallback description', 'obj', obj);
1500
+ }
1501
+
1502
+ /**
1503
+ * Returns ``true`` and provides a type guard if %%value%% is a
1504
+ * **FallbackFragment**.
1505
+ */
1506
+ static isFragment(value: any): value is FallbackFragment {
1507
+ return value && value[internal] === FallbackFragmentInternal;
1508
+ }
1509
+ }
1510
+
1511
+ /**
1512
+ * A Fragment which represents a method.
1513
+ */
1514
+ export class FunctionFragment extends NamedFragment {
1515
+ /**
1516
+ * If the function is constant (e.g. ``pure`` or ``view`` functions).
1517
+ */
1518
+ readonly constant!: boolean;
1519
+
1520
+ /**
1521
+ * The returned types for the result of calling this function.
1522
+ */
1523
+ readonly outputs!: ReadonlyArray<ParamType>;
1524
+
1525
+ /**
1526
+ * The state mutability (e.g. ``payable``, ``nonpayable``, ``view``
1527
+ * or ``pure``)
1528
+ */
1529
+ readonly stateMutability!: 'payable' | 'nonpayable' | 'view' | 'pure';
1530
+
1531
+ /**
1532
+ * If the function can be sent value during invocation.
1533
+ */
1534
+ readonly payable!: boolean;
1535
+
1536
+ /**
1537
+ * The recommended gas limit to send when calling this function.
1538
+ */
1539
+ readonly gas!: null | bigint;
1540
+
1541
+ /**
1542
+ * @private
1543
+ */
1544
+ constructor(
1545
+ guard: any,
1546
+ name: string,
1547
+ stateMutability: 'payable' | 'nonpayable' | 'view' | 'pure',
1548
+ inputs: ReadonlyArray<ParamType>,
1549
+ outputs: ReadonlyArray<ParamType>,
1550
+ gas: null | bigint
1551
+ ) {
1552
+ super(guard, 'function', name, inputs);
1553
+ Object.defineProperty(this, internal, { value: FunctionFragmentInternal });
1554
+ outputs = Object.freeze(outputs.slice());
1555
+ const constant = stateMutability === 'view' || stateMutability === 'pure';
1556
+ const payable = stateMutability === 'payable';
1557
+ defineProperties<FunctionFragment>(this, {
1558
+ constant,
1559
+ gas,
1560
+ outputs,
1561
+ payable,
1562
+ stateMutability,
1563
+ });
1564
+ }
1565
+
1566
+ /**
1567
+ * The Function selector.
1568
+ */
1569
+ get selector(): string {
1570
+ return id(this.format('sighash')).substring(0, 10);
1571
+ }
1572
+
1573
+ /**
1574
+ * Returns a string representation of this function as %%format%%.
1575
+ */
1576
+ format(format?: FormatType): string {
1577
+ if (format == null) {
1578
+ format = 'sighash';
1579
+ }
1580
+ if (format === 'json') {
1581
+ return JSON.stringify({
1582
+ type: 'function',
1583
+ name: this.name,
1584
+ constant: this.constant,
1585
+ stateMutability: this.stateMutability !== 'nonpayable' ? this.stateMutability : undefined,
1586
+ payable: this.payable,
1587
+ gas: this.gas != null ? this.gas : undefined,
1588
+ inputs: this.inputs.map((i) => JSON.parse(i.format(format))),
1589
+ outputs: this.outputs.map((o) => JSON.parse(o.format(format))),
1590
+ });
1591
+ }
1592
+
1593
+ const result: string[] = [];
1594
+
1595
+ if (format !== 'sighash') {
1596
+ result.push('function');
1597
+ }
1598
+
1599
+ result.push(this.name + joinParams(format, this.inputs));
1600
+
1601
+ if (format !== 'sighash') {
1602
+ if (this.stateMutability !== 'nonpayable') {
1603
+ result.push(this.stateMutability);
1604
+ }
1605
+
1606
+ if (this.outputs && this.outputs.length) {
1607
+ result.push('returns');
1608
+ result.push(joinParams(format, this.outputs));
1609
+ }
1610
+
1611
+ if (this.gas != null) {
1612
+ result.push(`@${this.gas.toString()}`);
1613
+ }
1614
+ }
1615
+ return result.join(' ');
1616
+ }
1617
+
1618
+ /**
1619
+ * Return the selector for a function with %%name%% and %%params%%.
1620
+ */
1621
+ static getSelector(name: string, params?: Array<any>): string {
1622
+ params = (params || []).map((p) => ParamType.from(p));
1623
+ const fragment = new FunctionFragment(_guard, name, 'view', params, [], null);
1624
+ return fragment.selector;
1625
+ }
1626
+
1627
+ /**
1628
+ * Returns a new **FunctionFragment** for %%obj%%.
1629
+ */
1630
+ static from(obj: any): FunctionFragment {
1631
+ if (FunctionFragment.isFragment(obj)) {
1632
+ return obj;
1633
+ }
1634
+
1635
+ if (typeof obj === 'string') {
1636
+ return FunctionFragment.from(lex(obj));
1637
+ } else if (obj instanceof TokenString) {
1638
+ const name = consumeName('function', obj);
1639
+ const inputs = consumeParams(obj);
1640
+ const mutability = consumeMutability(obj);
1641
+
1642
+ let outputs: Array<ParamType> = [];
1643
+ if (consumeKeywords(obj, setify(['returns'])).has('returns')) {
1644
+ outputs = consumeParams(obj);
1645
+ }
1646
+
1647
+ const gas = consumeGas(obj);
1648
+
1649
+ consumeEoi(obj);
1650
+
1651
+ return new FunctionFragment(_guard, name, mutability, inputs, outputs, gas);
1652
+ }
1653
+
1654
+ let stateMutability = obj.stateMutability;
1655
+
1656
+ // Use legacy Solidity ABI logic if stateMutability is missing
1657
+ if (stateMutability == null) {
1658
+ stateMutability = 'payable';
1659
+
1660
+ if (typeof obj.constant === 'boolean') {
1661
+ stateMutability = 'view';
1662
+ if (!obj.constant) {
1663
+ stateMutability = 'payable';
1664
+ if (typeof obj.payable === 'boolean' && !obj.payable) {
1665
+ stateMutability = 'nonpayable';
1666
+ }
1667
+ }
1668
+ } else if (typeof obj.payable === 'boolean' && !obj.payable) {
1669
+ stateMutability = 'nonpayable';
1670
+ }
1671
+ }
1672
+
1673
+ // @TODO: verifyState for stateMutability (e.g. throw if
1674
+ // payable: false but stateMutability is "nonpayable")
1675
+
1676
+ return new FunctionFragment(
1677
+ _guard,
1678
+ obj.name,
1679
+ stateMutability,
1680
+ obj.inputs ? obj.inputs.map(ParamType.from) : [],
1681
+ obj.outputs ? obj.outputs.map(ParamType.from) : [],
1682
+ obj.gas != null ? obj.gas : null
1683
+ );
1684
+ }
1685
+
1686
+ /**
1687
+ * Returns ``true`` and provides a type guard if %%value%% is a
1688
+ * **FunctionFragment**.
1689
+ */
1690
+ static isFragment(value: any): value is FunctionFragment {
1691
+ return value && value[internal] === FunctionFragmentInternal;
1692
+ }
1693
+ }
1694
+
1695
+ /**
1696
+ * A Fragment which represents a structure.
1697
+ */
1698
+ export class StructFragment extends NamedFragment {
1699
+ /**
1700
+ * @private
1701
+ */
1702
+ constructor(guard: any, name: string, inputs: ReadonlyArray<ParamType>) {
1703
+ super(guard, 'struct', name, inputs);
1704
+ Object.defineProperty(this, internal, { value: StructFragmentInternal });
1705
+ }
1706
+
1707
+ /**
1708
+ * Returns a string representation of this struct as %%format%%.
1709
+ */
1710
+ format(): string {
1711
+ throw new Error('@TODO');
1712
+ }
1713
+
1714
+ /**
1715
+ * Returns a new **StructFragment** for %%obj%%.
1716
+ */
1717
+ static from(obj: any): StructFragment {
1718
+ if (typeof obj === 'string') {
1719
+ return StructFragment.from(lex(obj));
1720
+ } else if (obj instanceof TokenString) {
1721
+ const name = consumeName('struct', obj);
1722
+ const inputs = consumeParams(obj);
1723
+ consumeEoi(obj);
1724
+ return new StructFragment(_guard, name, inputs);
1725
+ }
1726
+
1727
+ return new StructFragment(_guard, obj.name, obj.inputs ? obj.inputs.map(ParamType.from) : []);
1728
+ }
1729
+
1730
+ // @TODO: fix this return type
1731
+ /**
1732
+ * Returns ``true`` and provides a type guard if %%value%% is a
1733
+ * **StructFragment**.
1734
+ */
1735
+ static isFragment(value: any): value is FunctionFragment {
1736
+ return value && value[internal] === StructFragmentInternal;
1737
+ }
1738
+ }