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,1330 @@
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
+ var _a;
13
+ import { defineProperties, getBigInt, getNumber, assert, assertPrivate, assertArgument } from 'ethers';
14
+ import { id } from 'ethers';
15
+ // [ "a", "b" ] => { "a": 1, "b": 1 }
16
+ function setify(items) {
17
+ const result = new Set();
18
+ items.forEach((k) => result.add(k));
19
+ return Object.freeze(result);
20
+ }
21
+ // Visibility Keywords
22
+ const _kwVisib = 'constant external internal payable private public pure view';
23
+ const KwVisib = setify(_kwVisib.split(' '));
24
+ const _kwTypes = 'constructor error event fallback function receive struct';
25
+ const KwTypes = setify(_kwTypes.split(' '));
26
+ const _kwModifiers = 'calldata memory storage payable indexed';
27
+ const KwModifiers = setify(_kwModifiers.split(' '));
28
+ const _kwOther = 'tuple returns';
29
+ // All Keywords
30
+ const _keywords = [_kwTypes, _kwModifiers, _kwOther, _kwVisib].join(' ');
31
+ const Keywords = setify(_keywords.split(' '));
32
+ // Single character tokens
33
+ const SimpleTokens = {
34
+ '(': 'OPEN_PAREN',
35
+ ')': 'CLOSE_PAREN',
36
+ '[': 'OPEN_BRACKET',
37
+ ']': 'CLOSE_BRACKET',
38
+ ',': 'COMMA',
39
+ '@': 'AT',
40
+ };
41
+ // Parser regexes to consume the next token
42
+ const regexWhitespacePrefix = new RegExp('^(\\s*)');
43
+ const regexNumberPrefix = new RegExp('^([0-9]+)');
44
+ const regexIdPrefix = new RegExp('^([a-zA-Z$_][a-zA-Z0-9$_]*)');
45
+ // Parser regexs to check validity
46
+ const regexId = new RegExp('^([a-zA-Z$_][a-zA-Z0-9$_]*)$');
47
+ const regexType = new RegExp('^(trcToken|address|bool|bytes([0-9]*)|string|u?int([0-9]*))$');
48
+ class TokenString {
49
+ #offset;
50
+ #tokens;
51
+ get offset() {
52
+ return this.#offset;
53
+ }
54
+ get length() {
55
+ return this.#tokens.length - this.#offset;
56
+ }
57
+ constructor(tokens) {
58
+ this.#offset = 0;
59
+ this.#tokens = tokens.slice();
60
+ }
61
+ clone() {
62
+ return new _a(this.#tokens);
63
+ }
64
+ reset() {
65
+ this.#offset = 0;
66
+ }
67
+ #subTokenString(from = 0, to = 0) {
68
+ return new _a(this.#tokens.slice(from, to).map((t) => {
69
+ return Object.freeze(Object.assign({}, t, {
70
+ match: t.match - from,
71
+ linkBack: t.linkBack - from,
72
+ linkNext: t.linkNext - from,
73
+ }));
74
+ }));
75
+ }
76
+ // Pops and returns the value of the next token, if it is a keyword in allowed; throws if out of tokens
77
+ popKeyword(allowed) {
78
+ const top = this.peek();
79
+ if (top.type !== 'KEYWORD' || !allowed.has(top.text)) {
80
+ throw new Error(`expected keyword ${top.text}`);
81
+ }
82
+ return this.pop().text;
83
+ }
84
+ // Pops and returns the value of the next token if it is `type`; throws if out of tokens
85
+ popType(type) {
86
+ if (this.peek().type !== type) {
87
+ throw new Error(`expected ${type}; got ${JSON.stringify(this.peek())}`);
88
+ }
89
+ return this.pop().text;
90
+ }
91
+ // Pops and returns a "(" TOKENS ")"
92
+ popParen() {
93
+ const top = this.peek();
94
+ if (top.type !== 'OPEN_PAREN') {
95
+ throw new Error('bad start');
96
+ }
97
+ const result = this.#subTokenString(this.#offset + 1, top.match + 1);
98
+ this.#offset = top.match + 1;
99
+ return result;
100
+ }
101
+ // Pops and returns the items within "(" ITEM1 "," ITEM2 "," ... ")"
102
+ popParams() {
103
+ const top = this.peek();
104
+ if (top.type !== 'OPEN_PAREN') {
105
+ throw new Error('bad start');
106
+ }
107
+ const result = [];
108
+ while (this.#offset < top.match - 1) {
109
+ const link = this.peek().linkNext;
110
+ result.push(this.#subTokenString(this.#offset + 1, link));
111
+ this.#offset = link;
112
+ }
113
+ this.#offset = top.match + 1;
114
+ return result;
115
+ }
116
+ // Returns the top Token, throwing if out of tokens
117
+ peek() {
118
+ if (this.#offset >= this.#tokens.length) {
119
+ throw new Error('out-of-bounds');
120
+ }
121
+ return this.#tokens[this.#offset];
122
+ }
123
+ // Returns the next value, if it is a keyword in `allowed`
124
+ peekKeyword(allowed) {
125
+ const top = this.peekType('KEYWORD');
126
+ return top != null && allowed.has(top) ? top : null;
127
+ }
128
+ // Returns the value of the next token if it is `type`
129
+ peekType(type) {
130
+ if (this.length === 0) {
131
+ return null;
132
+ }
133
+ const top = this.peek();
134
+ return top.type === type ? top.text : null;
135
+ }
136
+ // Returns the next token; throws if out of tokens
137
+ pop() {
138
+ const result = this.peek();
139
+ this.#offset++;
140
+ return result;
141
+ }
142
+ toString() {
143
+ const tokens = [];
144
+ for (let i = this.#offset; i < this.#tokens.length; i++) {
145
+ const token = this.#tokens[i];
146
+ tokens.push(`${token.type}:${token.text}`);
147
+ }
148
+ return `<TokenString ${tokens.join(' ')}>`;
149
+ }
150
+ }
151
+ _a = TokenString;
152
+ function lex(text) {
153
+ const tokens = [];
154
+ const throwError = (message) => {
155
+ const token = offset < text.length ? JSON.stringify(text[offset]) : '$EOI';
156
+ throw new Error(`invalid token ${token} at ${offset}: ${message}`);
157
+ };
158
+ const brackets = [];
159
+ const commas = [];
160
+ let offset = 0;
161
+ while (offset < text.length) {
162
+ // Strip off any leading whitespace
163
+ let cur = text.substring(offset);
164
+ let match = cur.match(regexWhitespacePrefix);
165
+ if (match) {
166
+ offset += match[1].length;
167
+ cur = text.substring(offset);
168
+ }
169
+ const token = {
170
+ depth: brackets.length,
171
+ linkBack: -1,
172
+ linkNext: -1,
173
+ match: -1,
174
+ type: '',
175
+ text: '',
176
+ offset,
177
+ value: -1,
178
+ };
179
+ tokens.push(token);
180
+ const type = SimpleTokens[cur[0]] || '';
181
+ if (type) {
182
+ token.type = type;
183
+ token.text = cur[0];
184
+ offset++;
185
+ if (type === 'OPEN_PAREN') {
186
+ brackets.push(tokens.length - 1);
187
+ commas.push(tokens.length - 1);
188
+ }
189
+ else if (type == 'CLOSE_PAREN') {
190
+ if (brackets.length === 0) {
191
+ throwError('no matching open bracket');
192
+ }
193
+ token.match = brackets.pop();
194
+ tokens[token.match].match = tokens.length - 1;
195
+ token.depth--;
196
+ token.linkBack = commas.pop();
197
+ tokens[token.linkBack].linkNext = tokens.length - 1;
198
+ }
199
+ else if (type === 'COMMA') {
200
+ token.linkBack = commas.pop();
201
+ tokens[token.linkBack].linkNext = tokens.length - 1;
202
+ commas.push(tokens.length - 1);
203
+ }
204
+ else if (type === 'OPEN_BRACKET') {
205
+ token.type = 'BRACKET';
206
+ }
207
+ else if (type === 'CLOSE_BRACKET') {
208
+ // Remove the CLOSE_BRACKET
209
+ let suffix = tokens.pop().text;
210
+ if (tokens.length > 0 && tokens[tokens.length - 1].type === 'NUMBER') {
211
+ const value = tokens.pop().text;
212
+ suffix = value + suffix;
213
+ tokens[tokens.length - 1].value = getNumber(value);
214
+ }
215
+ if (tokens.length === 0 || tokens[tokens.length - 1].type !== 'BRACKET') {
216
+ throw new Error('missing opening bracket');
217
+ }
218
+ tokens[tokens.length - 1].text += suffix;
219
+ }
220
+ continue;
221
+ }
222
+ match = cur.match(regexIdPrefix);
223
+ if (match) {
224
+ token.text = match[1];
225
+ offset += token.text.length;
226
+ if (Keywords.has(token.text)) {
227
+ token.type = 'KEYWORD';
228
+ continue;
229
+ }
230
+ if (token.text.match(regexType)) {
231
+ token.type = 'TYPE';
232
+ continue;
233
+ }
234
+ token.type = 'ID';
235
+ continue;
236
+ }
237
+ match = cur.match(regexNumberPrefix);
238
+ if (match) {
239
+ token.text = match[1];
240
+ token.type = 'NUMBER';
241
+ offset += token.text.length;
242
+ continue;
243
+ }
244
+ throw new Error(`unexpected token ${JSON.stringify(cur[0])} at position ${offset}`);
245
+ }
246
+ return new TokenString(tokens.map((t) => Object.freeze(t)));
247
+ }
248
+ // Check only one of `allowed` is in `set`
249
+ function allowSingle(set, allowed) {
250
+ const included = [];
251
+ for (const key in allowed.keys()) {
252
+ if (set.has(key)) {
253
+ included.push(key);
254
+ }
255
+ }
256
+ if (included.length > 1) {
257
+ throw new Error(`conflicting types: ${included.join(', ')}`);
258
+ }
259
+ }
260
+ // Functions to process a Solidity Signature TokenString from left-to-right for...
261
+ // ...the name with an optional type, returning the name
262
+ function consumeName(type, tokens) {
263
+ if (tokens.peekKeyword(KwTypes)) {
264
+ const keyword = tokens.pop().text;
265
+ if (keyword !== type) {
266
+ throw new Error(`expected ${type}, got ${keyword}`);
267
+ }
268
+ }
269
+ return tokens.popType('ID');
270
+ }
271
+ // ...all keywords matching allowed, returning the keywords
272
+ function consumeKeywords(tokens, allowed) {
273
+ const keywords = new Set();
274
+ // eslint-disable-next-line no-constant-condition
275
+ while (true) {
276
+ const keyword = tokens.peekType('KEYWORD');
277
+ if (keyword == null || (allowed && !allowed.has(keyword))) {
278
+ break;
279
+ }
280
+ tokens.pop();
281
+ if (keywords.has(keyword)) {
282
+ throw new Error(`duplicate keywords: ${JSON.stringify(keyword)}`);
283
+ }
284
+ keywords.add(keyword);
285
+ }
286
+ return Object.freeze(keywords);
287
+ }
288
+ // ...all visibility keywords, returning the coalesced mutability
289
+ function consumeMutability(tokens) {
290
+ const modifiers = consumeKeywords(tokens, KwVisib);
291
+ // Detect conflicting modifiers
292
+ allowSingle(modifiers, setify('constant payable nonpayable'.split(' ')));
293
+ allowSingle(modifiers, setify('pure view payable nonpayable'.split(' ')));
294
+ // Process mutability states
295
+ if (modifiers.has('view')) {
296
+ return 'view';
297
+ }
298
+ if (modifiers.has('pure')) {
299
+ return 'pure';
300
+ }
301
+ if (modifiers.has('payable')) {
302
+ return 'payable';
303
+ }
304
+ if (modifiers.has('nonpayable')) {
305
+ return 'nonpayable';
306
+ }
307
+ // Process legacy `constant` last
308
+ if (modifiers.has('constant')) {
309
+ return 'view';
310
+ }
311
+ return 'nonpayable';
312
+ }
313
+ // ...a parameter list, returning the ParamType list
314
+ function consumeParams(tokens, allowIndexed) {
315
+ return tokens.popParams().map((t) => ParamType.from(t, allowIndexed));
316
+ }
317
+ // ...a gas limit, returning a BigNumber or null if none
318
+ function consumeGas(tokens) {
319
+ if (tokens.peekType('AT')) {
320
+ tokens.pop();
321
+ if (tokens.peekType('NUMBER')) {
322
+ return getBigInt(tokens.pop().text);
323
+ }
324
+ throw new Error('invalid gas');
325
+ }
326
+ return null;
327
+ }
328
+ function consumeEoi(tokens) {
329
+ if (tokens.length) {
330
+ throw new Error(`unexpected tokens: ${tokens.toString()}`);
331
+ }
332
+ }
333
+ const regexArrayType = new RegExp(/^(.*)\[([0-9]*)\]$/);
334
+ function verifyBasicType(type) {
335
+ const match = type.match(regexType);
336
+ assertArgument(match, 'invalid type', 'type', type);
337
+ if (type === 'uint') {
338
+ return 'uint256';
339
+ }
340
+ if (type === 'int') {
341
+ return 'int256';
342
+ }
343
+ if (match[2]) {
344
+ // bytesXX
345
+ const length = parseInt(match[2]);
346
+ assertArgument(length !== 0 && length <= 32, 'invalid bytes length', 'type', type);
347
+ }
348
+ else if (match[3]) {
349
+ // intXX or uintXX
350
+ const size = parseInt(match[3]);
351
+ assertArgument(size !== 0 && size <= 256 && size % 8 === 0, 'invalid numeric width', 'type', type);
352
+ }
353
+ return type;
354
+ }
355
+ // Make the Fragment constructors effectively private
356
+ const _guard = {};
357
+ const internal = Symbol.for('_ethers_internal');
358
+ const ParamTypeInternal = '_ParamTypeInternal';
359
+ const ErrorFragmentInternal = '_ErrorInternal';
360
+ const EventFragmentInternal = '_EventInternal';
361
+ const ConstructorFragmentInternal = '_ConstructorInternal';
362
+ const FallbackFragmentInternal = '_FallbackInternal';
363
+ const FunctionFragmentInternal = '_FunctionInternal';
364
+ const StructFragmentInternal = '_StructInternal';
365
+ /**
366
+ * Each input and output of a [[Fragment]] is an Array of **ParamType**.
367
+ */
368
+ export class ParamType {
369
+ /**
370
+ * The local name of the parameter (or ``""`` if unbound)
371
+ */
372
+ name;
373
+ /**
374
+ * The fully qualified type (e.g. ``"address"``, ``"tuple(address)"``,
375
+ * ``"uint256[3][]"``)
376
+ */
377
+ type;
378
+ /**
379
+ * The base type (e.g. ``"address"``, ``"tuple"``, ``"array"``)
380
+ */
381
+ baseType;
382
+ /**
383
+ * True if the parameters is indexed.
384
+ *
385
+ * For non-indexable types this is ``null``.
386
+ */
387
+ indexed;
388
+ /**
389
+ * The components for the tuple.
390
+ *
391
+ * For non-tuple types this is ``null``.
392
+ */
393
+ components;
394
+ /**
395
+ * The array length, or ``-1`` for dynamic-lengthed arrays.
396
+ *
397
+ * For non-array types this is ``null``.
398
+ */
399
+ arrayLength;
400
+ /**
401
+ * The type of each child in the array.
402
+ *
403
+ * For non-array types this is ``null``.
404
+ */
405
+ arrayChildren;
406
+ /**
407
+ * @private
408
+ */
409
+ constructor(guard, name, type, baseType, indexed, components, arrayLength, arrayChildren) {
410
+ assertPrivate(guard, _guard, 'ParamType');
411
+ Object.defineProperty(this, internal, { value: ParamTypeInternal });
412
+ if (components) {
413
+ components = Object.freeze(components.slice());
414
+ }
415
+ if (baseType === 'array') {
416
+ if (arrayLength == null || arrayChildren == null) {
417
+ throw new Error('');
418
+ }
419
+ }
420
+ else if (arrayLength != null || arrayChildren != null) {
421
+ throw new Error('');
422
+ }
423
+ if (baseType === 'tuple') {
424
+ if (components == null) {
425
+ throw new Error('');
426
+ }
427
+ }
428
+ else if (components != null) {
429
+ throw new Error('');
430
+ }
431
+ defineProperties(this, {
432
+ name,
433
+ type,
434
+ baseType,
435
+ indexed,
436
+ components,
437
+ arrayLength,
438
+ arrayChildren,
439
+ });
440
+ }
441
+ /**
442
+ * Return a string representation of this type.
443
+ *
444
+ * For example,
445
+ *
446
+ * ``sighash" => "(uint256,address)"``
447
+ *
448
+ * ``"minimal" => "tuple(uint256,address) indexed"``
449
+ *
450
+ * ``"full" => "tuple(uint256 foo, address bar) indexed baz"``
451
+ */
452
+ format(format) {
453
+ if (format == null) {
454
+ format = 'sighash';
455
+ }
456
+ if (format === 'json') {
457
+ const result = {
458
+ type: this.baseType === 'tuple' ? 'tuple' : this.type,
459
+ name: this.name || undefined,
460
+ };
461
+ if (typeof this.indexed === 'boolean') {
462
+ result.indexed = this.indexed;
463
+ }
464
+ if (this.isTuple()) {
465
+ result.components = this.components.map((c) => JSON.parse(c.format(format)));
466
+ }
467
+ return JSON.stringify(result);
468
+ }
469
+ let result = '';
470
+ // Array
471
+ if (this.isArray()) {
472
+ result += this.arrayChildren.format(format);
473
+ result += `[${this.arrayLength < 0 ? '' : String(this.arrayLength)}]`;
474
+ }
475
+ else {
476
+ if (this.isTuple()) {
477
+ if (format !== 'sighash') {
478
+ result += this.type;
479
+ }
480
+ result += '(' + this.components.map((comp) => comp.format(format)).join(format === 'full' ? ', ' : ',') + ')';
481
+ }
482
+ else {
483
+ result += this.type;
484
+ }
485
+ }
486
+ if (format !== 'sighash') {
487
+ if (this.indexed === true) {
488
+ result += ' indexed';
489
+ }
490
+ if (format === 'full' && this.name) {
491
+ result += ' ' + this.name;
492
+ }
493
+ }
494
+ return result;
495
+ }
496
+ /**
497
+ * Returns true if %%this%% is an Array type.
498
+ *
499
+ * This provides a type gaurd ensuring that [[arrayChildren]]
500
+ * and [[arrayLength]] are non-null.
501
+ */
502
+ isArray() {
503
+ return this.baseType === 'array';
504
+ }
505
+ /**
506
+ * Returns true if %%this%% is a Tuple type.
507
+ *
508
+ * This provides a type gaurd ensuring that [[components]]
509
+ * is non-null.
510
+ */
511
+ isTuple() {
512
+ return this.baseType === 'tuple';
513
+ }
514
+ /**
515
+ * Returns true if %%this%% is an Indexable type.
516
+ *
517
+ * This provides a type gaurd ensuring that [[indexed]]
518
+ * is non-null.
519
+ */
520
+ isIndexable() {
521
+ return this.indexed != null;
522
+ }
523
+ /**
524
+ * Walks the **ParamType** with %%value%%, calling %%process%%
525
+ * on each type, destructing the %%value%% recursively.
526
+ */
527
+ walk(value, process) {
528
+ if (this.isArray()) {
529
+ if (!Array.isArray(value)) {
530
+ throw new Error('invalid array value');
531
+ }
532
+ if (this.arrayLength !== -1 && value.length !== this.arrayLength) {
533
+ throw new Error('array is wrong length');
534
+ }
535
+ return value.map((v) => this.arrayChildren?.walk(v, process));
536
+ }
537
+ if (this.isTuple()) {
538
+ if (!Array.isArray(value)) {
539
+ throw new Error('invalid tuple value');
540
+ }
541
+ if (value.length !== this.components.length) {
542
+ throw new Error('array is wrong length');
543
+ }
544
+ return value.map((v, i) => this.components?.[i].walk(v, process));
545
+ }
546
+ return process(this.type, value);
547
+ }
548
+ #walkAsync(promises, value, process, setValue) {
549
+ if (this.isArray()) {
550
+ if (!Array.isArray(value)) {
551
+ throw new Error('invalid array value');
552
+ }
553
+ if (this.arrayLength !== -1 && value.length !== this.arrayLength) {
554
+ throw new Error('array is wrong length');
555
+ }
556
+ const childType = this.arrayChildren;
557
+ const result = value.slice();
558
+ result.forEach((value, index) => {
559
+ childType.#walkAsync(promises, value, process, (value) => {
560
+ result[index] = value;
561
+ });
562
+ });
563
+ setValue(result);
564
+ return;
565
+ }
566
+ if (this.isTuple()) {
567
+ const components = this.components;
568
+ // Convert the object into an array
569
+ let result;
570
+ if (Array.isArray(value)) {
571
+ result = value.slice();
572
+ }
573
+ else {
574
+ if (value == null || typeof value !== 'object') {
575
+ throw new Error('invalid tuple value');
576
+ }
577
+ result = components.map((param) => {
578
+ if (!param.name) {
579
+ throw new Error('cannot use object value with unnamed components');
580
+ }
581
+ if (!(param.name in value)) {
582
+ throw new Error(`missing value for component ${param.name}`);
583
+ }
584
+ return value[param.name];
585
+ });
586
+ }
587
+ if (result.length !== this.components.length) {
588
+ throw new Error('array is wrong length');
589
+ }
590
+ result.forEach((value, index) => {
591
+ components[index].#walkAsync(promises, value, process, (value) => {
592
+ result[index] = value;
593
+ });
594
+ });
595
+ setValue(result);
596
+ return;
597
+ }
598
+ const result = process(this.type, value);
599
+ if (result.then) {
600
+ promises.push((async function () {
601
+ setValue(await result);
602
+ })());
603
+ }
604
+ else {
605
+ setValue(result);
606
+ }
607
+ }
608
+ /**
609
+ * Walks the **ParamType** with %%value%%, asynchronously calling
610
+ * %%process%% on each type, destructing the %%value%% recursively.
611
+ *
612
+ * This can be used to resolve ENS naes by walking and resolving each
613
+ * ``"address"`` type.
614
+ */
615
+ async walkAsync(value, process) {
616
+ const promises = [];
617
+ const result = [value];
618
+ this.#walkAsync(promises, value, process, (value) => {
619
+ result[0] = value;
620
+ });
621
+ if (promises.length) {
622
+ await Promise.all(promises);
623
+ }
624
+ return result[0];
625
+ }
626
+ /**
627
+ * Creates a new **ParamType** for %%obj%%.
628
+ *
629
+ * If %%allowIndexed%% then the ``indexed`` keyword is permitted,
630
+ * otherwise the ``indexed`` keyword will throw an error.
631
+ */
632
+ static from(obj, allowIndexed) {
633
+ if (ParamType.isParamType(obj)) {
634
+ return obj;
635
+ }
636
+ if (typeof obj === 'string') {
637
+ return ParamType.from(lex(obj), allowIndexed);
638
+ }
639
+ else if (obj instanceof TokenString) {
640
+ let type = '', baseType = '';
641
+ let comps = null;
642
+ if (consumeKeywords(obj, setify(['tuple'])).has('tuple') || obj.peekType('OPEN_PAREN')) {
643
+ // Tuple
644
+ baseType = 'tuple';
645
+ comps = obj.popParams().map((t) => ParamType.from(t));
646
+ type = `tuple(${comps.map((c) => c.format()).join(',')})`;
647
+ }
648
+ else {
649
+ // Normal
650
+ type = verifyBasicType(obj.popType('TYPE'));
651
+ baseType = type;
652
+ }
653
+ // Check for Array
654
+ let arrayChildren = null;
655
+ let arrayLength = null;
656
+ while (obj.length && obj.peekType('BRACKET')) {
657
+ const bracket = obj.pop(); //arrays[i];
658
+ arrayChildren = new ParamType(_guard, '', type, baseType, null, comps, arrayLength, arrayChildren);
659
+ arrayLength = bracket.value;
660
+ type += bracket.text;
661
+ baseType = 'array';
662
+ comps = null;
663
+ }
664
+ let indexed = null;
665
+ const keywords = consumeKeywords(obj, KwModifiers);
666
+ if (keywords.has('indexed')) {
667
+ if (!allowIndexed) {
668
+ throw new Error('');
669
+ }
670
+ indexed = true;
671
+ }
672
+ const name = obj.peekType('ID') ? obj.pop().text : '';
673
+ if (obj.length) {
674
+ throw new Error('leftover tokens');
675
+ }
676
+ return new ParamType(_guard, name, type, baseType, indexed, comps, arrayLength, arrayChildren);
677
+ }
678
+ const name = obj.name;
679
+ assertArgument(!name || (typeof name === 'string' && name.match(regexId)), 'invalid name', 'obj.name', name);
680
+ let indexed = obj.indexed;
681
+ if (indexed != null) {
682
+ assertArgument(allowIndexed, 'parameter cannot be indexed', 'obj.indexed', obj.indexed);
683
+ indexed = !!indexed;
684
+ }
685
+ let type = obj.type;
686
+ const arrayMatch = type.match(regexArrayType);
687
+ if (arrayMatch) {
688
+ const arrayLength = parseInt(arrayMatch[2] || '-1');
689
+ const arrayChildren = ParamType.from({
690
+ type: arrayMatch[1],
691
+ components: obj.components,
692
+ });
693
+ return new ParamType(_guard, name || '', type, 'array', indexed, null, arrayLength, arrayChildren);
694
+ }
695
+ if (type === 'tuple' || type.startsWith('tuple(' /* fix: ) */) || type.startsWith('(' /* fix: ) */)) {
696
+ const comps = obj.components != null ? obj.components.map((c) => ParamType.from(c)) : null;
697
+ const tuple = new ParamType(_guard, name || '', type, 'tuple', indexed, comps, null, null);
698
+ // @TODO: use lexer to validate and normalize type
699
+ return tuple;
700
+ }
701
+ type = verifyBasicType(obj.type);
702
+ return new ParamType(_guard, name || '', type, type, indexed, null, null, null);
703
+ }
704
+ /**
705
+ * Returns true if %%value%% is a **ParamType**.
706
+ */
707
+ static isParamType(value) {
708
+ return value && value[internal] === ParamTypeInternal;
709
+ }
710
+ }
711
+ /**
712
+ * An abstract class to represent An individual fragment from a parse ABI.
713
+ */
714
+ export class Fragment {
715
+ /**
716
+ * The type of the fragment.
717
+ */
718
+ type;
719
+ /**
720
+ * The inputs for the fragment.
721
+ */
722
+ inputs;
723
+ /**
724
+ * @private
725
+ */
726
+ constructor(guard, type, inputs) {
727
+ assertPrivate(guard, _guard, 'Fragment');
728
+ inputs = Object.freeze(inputs.slice());
729
+ defineProperties(this, { type, inputs });
730
+ }
731
+ /**
732
+ * Creates a new **Fragment** for %%obj%%, wich can be any supported
733
+ * ABI frgament type.
734
+ */
735
+ static from(obj) {
736
+ if (typeof obj === 'string') {
737
+ // Try parsing JSON...
738
+ try {
739
+ Fragment.from(JSON.parse(obj));
740
+ }
741
+ catch (e) {
742
+ //
743
+ }
744
+ // ...otherwise, use the human-readable lexer
745
+ return Fragment.from(lex(obj));
746
+ }
747
+ if (obj instanceof TokenString) {
748
+ // Human-readable ABI (already lexed)
749
+ const type = obj.peekKeyword(KwTypes);
750
+ switch (type) {
751
+ case 'constructor':
752
+ return ConstructorFragment.from(obj);
753
+ case 'error':
754
+ return ErrorFragment.from(obj);
755
+ case 'event':
756
+ return EventFragment.from(obj);
757
+ case 'fallback':
758
+ case 'receive':
759
+ return FallbackFragment.from(obj);
760
+ case 'function':
761
+ return FunctionFragment.from(obj);
762
+ case 'struct':
763
+ return StructFragment.from(obj);
764
+ }
765
+ }
766
+ else if (typeof obj === 'object') {
767
+ // JSON ABI
768
+ switch (obj.type) {
769
+ case 'constructor':
770
+ return ConstructorFragment.from(obj);
771
+ case 'error':
772
+ return ErrorFragment.from(obj);
773
+ case 'event':
774
+ return EventFragment.from(obj);
775
+ case 'fallback':
776
+ case 'receive':
777
+ return FallbackFragment.from(obj);
778
+ case 'function':
779
+ return FunctionFragment.from(obj);
780
+ case 'struct':
781
+ return StructFragment.from(obj);
782
+ }
783
+ assert(false, `unsupported type: ${obj.type}`, 'UNSUPPORTED_OPERATION', {
784
+ operation: 'Fragment.from',
785
+ });
786
+ }
787
+ assertArgument(false, 'unsupported frgament object', 'obj', obj);
788
+ }
789
+ /**
790
+ * Returns true if %%value%% is a [[ConstructorFragment]].
791
+ */
792
+ static isConstructor(value) {
793
+ return ConstructorFragment.isFragment(value);
794
+ }
795
+ /**
796
+ * Returns true if %%value%% is an [[ErrorFragment]].
797
+ */
798
+ static isError(value) {
799
+ return ErrorFragment.isFragment(value);
800
+ }
801
+ /**
802
+ * Returns true if %%value%% is an [[EventFragment]].
803
+ */
804
+ static isEvent(value) {
805
+ return EventFragment.isFragment(value);
806
+ }
807
+ /**
808
+ * Returns true if %%value%% is a [[FunctionFragment]].
809
+ */
810
+ static isFunction(value) {
811
+ return FunctionFragment.isFragment(value);
812
+ }
813
+ /**
814
+ * Returns true if %%value%% is a [[StructFragment]].
815
+ */
816
+ static isStruct(value) {
817
+ return StructFragment.isFragment(value);
818
+ }
819
+ }
820
+ /**
821
+ * An abstract class to represent An individual fragment
822
+ * which has a name from a parse ABI.
823
+ */
824
+ export class NamedFragment extends Fragment {
825
+ /**
826
+ * The name of the fragment.
827
+ */
828
+ name;
829
+ /**
830
+ * @private
831
+ */
832
+ constructor(guard, type, name, inputs) {
833
+ super(guard, type, inputs);
834
+ assertArgument(typeof name === 'string' && name.match(regexId), 'invalid identifier', 'name', name);
835
+ inputs = Object.freeze(inputs.slice());
836
+ defineProperties(this, { name });
837
+ }
838
+ }
839
+ function joinParams(format, params) {
840
+ return '(' + params.map((p) => p.format(format)).join(format === 'full' ? ', ' : ',') + ')';
841
+ }
842
+ /**
843
+ * A Fragment which represents a //Custom Error//.
844
+ */
845
+ export class ErrorFragment extends NamedFragment {
846
+ /**
847
+ * @private
848
+ */
849
+ constructor(guard, name, inputs) {
850
+ super(guard, 'error', name, inputs);
851
+ Object.defineProperty(this, internal, { value: ErrorFragmentInternal });
852
+ }
853
+ /**
854
+ * The Custom Error selector.
855
+ */
856
+ get selector() {
857
+ return id(this.format('sighash')).substring(0, 10);
858
+ }
859
+ /**
860
+ * Returns a string representation of this fragment as %%format%%.
861
+ */
862
+ format(format) {
863
+ if (format == null) {
864
+ format = 'sighash';
865
+ }
866
+ if (format === 'json') {
867
+ return JSON.stringify({
868
+ type: 'error',
869
+ name: this.name,
870
+ inputs: this.inputs.map((input) => JSON.parse(input.format(format))),
871
+ });
872
+ }
873
+ const result = [];
874
+ if (format !== 'sighash') {
875
+ result.push('error');
876
+ }
877
+ result.push(this.name + joinParams(format, this.inputs));
878
+ return result.join(' ');
879
+ }
880
+ /**
881
+ * Returns a new **ErrorFragment** for %%obj%%.
882
+ */
883
+ static from(obj) {
884
+ if (ErrorFragment.isFragment(obj)) {
885
+ return obj;
886
+ }
887
+ if (typeof obj === 'string') {
888
+ return ErrorFragment.from(lex(obj));
889
+ }
890
+ else if (obj instanceof TokenString) {
891
+ const name = consumeName('error', obj);
892
+ const inputs = consumeParams(obj);
893
+ consumeEoi(obj);
894
+ return new ErrorFragment(_guard, name, inputs);
895
+ }
896
+ return new ErrorFragment(_guard, obj.name, obj.inputs ? obj.inputs.map(ParamType.from) : []);
897
+ }
898
+ /**
899
+ * Returns ``true`` and provides a type guard if %%value%% is an
900
+ * **ErrorFragment**.
901
+ */
902
+ static isFragment(value) {
903
+ return value && value[internal] === ErrorFragmentInternal;
904
+ }
905
+ }
906
+ /**
907
+ * A Fragment which represents an Event.
908
+ */
909
+ export class EventFragment extends NamedFragment {
910
+ /**
911
+ * Whether this event is anonymous.
912
+ */
913
+ anonymous;
914
+ /**
915
+ * @private
916
+ */
917
+ constructor(guard, name, inputs, anonymous) {
918
+ super(guard, 'event', name, inputs);
919
+ Object.defineProperty(this, internal, { value: EventFragmentInternal });
920
+ defineProperties(this, { anonymous });
921
+ }
922
+ /**
923
+ * The Event topic hash.
924
+ */
925
+ get topicHash() {
926
+ return id(this.format('sighash'));
927
+ }
928
+ /**
929
+ * Returns a string representation of this event as %%format%%.
930
+ */
931
+ format(format) {
932
+ if (format == null) {
933
+ format = 'sighash';
934
+ }
935
+ if (format === 'json') {
936
+ return JSON.stringify({
937
+ type: 'event',
938
+ anonymous: this.anonymous,
939
+ name: this.name,
940
+ inputs: this.inputs.map((i) => JSON.parse(i.format(format))),
941
+ });
942
+ }
943
+ const result = [];
944
+ if (format !== 'sighash') {
945
+ result.push('event');
946
+ }
947
+ result.push(this.name + joinParams(format, this.inputs));
948
+ if (format !== 'sighash' && this.anonymous) {
949
+ result.push('anonymous');
950
+ }
951
+ return result.join(' ');
952
+ }
953
+ /**
954
+ * Return the topic hash for an event with %%name%% and %%params%%.
955
+ */
956
+ static getTopicHash(name, params) {
957
+ params = (params || []).map((p) => ParamType.from(p));
958
+ const fragment = new EventFragment(_guard, name, params, false);
959
+ return fragment.topicHash;
960
+ }
961
+ /**
962
+ * Returns a new **EventFragment** for %%obj%%.
963
+ */
964
+ static from(obj) {
965
+ if (EventFragment.isFragment(obj)) {
966
+ return obj;
967
+ }
968
+ if (typeof obj === 'string') {
969
+ return EventFragment.from(lex(obj));
970
+ }
971
+ else if (obj instanceof TokenString) {
972
+ const name = consumeName('event', obj);
973
+ const inputs = consumeParams(obj, true);
974
+ const anonymous = !!consumeKeywords(obj, setify(['anonymous'])).has('anonymous');
975
+ consumeEoi(obj);
976
+ return new EventFragment(_guard, name, inputs, anonymous);
977
+ }
978
+ return new EventFragment(_guard, obj.name, obj.inputs ? obj.inputs.map((p) => ParamType.from(p, true)) : [], !!obj.anonymous);
979
+ }
980
+ /**
981
+ * Returns ``true`` and provides a type guard if %%value%% is an
982
+ * **EventFragment**.
983
+ */
984
+ static isFragment(value) {
985
+ return value && value[internal] === EventFragmentInternal;
986
+ }
987
+ }
988
+ /**
989
+ * A Fragment which represents a constructor.
990
+ */
991
+ export class ConstructorFragment extends Fragment {
992
+ /**
993
+ * Whether the constructor can receive an endowment.
994
+ */
995
+ payable;
996
+ /**
997
+ * The recommended gas limit for deployment or ``null``.
998
+ */
999
+ gas;
1000
+ /**
1001
+ * @private
1002
+ */
1003
+ constructor(guard, type, inputs, payable, gas) {
1004
+ super(guard, type, inputs);
1005
+ Object.defineProperty(this, internal, {
1006
+ value: ConstructorFragmentInternal,
1007
+ });
1008
+ defineProperties(this, { payable, gas });
1009
+ }
1010
+ /**
1011
+ * Returns a string representation of this constructor as %%format%%.
1012
+ */
1013
+ format(format) {
1014
+ assert(format != null && format !== 'sighash', 'cannot format a constructor for sighash', 'UNSUPPORTED_OPERATION', {
1015
+ operation: 'format(sighash)',
1016
+ });
1017
+ if (format === 'json') {
1018
+ return JSON.stringify({
1019
+ type: 'constructor',
1020
+ stateMutability: this.payable ? 'payable' : 'undefined',
1021
+ payable: this.payable,
1022
+ gas: this.gas != null ? this.gas : undefined,
1023
+ inputs: this.inputs.map((i) => JSON.parse(i.format(format))),
1024
+ });
1025
+ }
1026
+ const result = [`constructor${joinParams(format, this.inputs)}`];
1027
+ result.push(this.payable ? 'payable' : 'nonpayable');
1028
+ if (this.gas != null) {
1029
+ result.push(`@${this.gas.toString()}`);
1030
+ }
1031
+ return result.join(' ');
1032
+ }
1033
+ /**
1034
+ * Returns a new **ConstructorFragment** for %%obj%%.
1035
+ */
1036
+ static from(obj) {
1037
+ if (ConstructorFragment.isFragment(obj)) {
1038
+ return obj;
1039
+ }
1040
+ if (typeof obj === 'string') {
1041
+ return ConstructorFragment.from(lex(obj));
1042
+ }
1043
+ else if (obj instanceof TokenString) {
1044
+ consumeKeywords(obj, setify(['constructor']));
1045
+ const inputs = consumeParams(obj);
1046
+ const payable = !!consumeKeywords(obj, setify(['payable'])).has('payable');
1047
+ const gas = consumeGas(obj);
1048
+ consumeEoi(obj);
1049
+ return new ConstructorFragment(_guard, 'constructor', inputs, payable, gas);
1050
+ }
1051
+ return new ConstructorFragment(_guard, 'constructor', obj.inputs ? obj.inputs.map(ParamType.from) : [], !!obj.payable, obj.gas != null ? obj.gas : null);
1052
+ }
1053
+ /**
1054
+ * Returns ``true`` and provides a type guard if %%value%% is a
1055
+ * **ConstructorFragment**.
1056
+ */
1057
+ static isFragment(value) {
1058
+ return value && value[internal] === ConstructorFragmentInternal;
1059
+ }
1060
+ }
1061
+ /**
1062
+ * A Fragment which represents a method.
1063
+ */
1064
+ export class FallbackFragment extends Fragment {
1065
+ /**
1066
+ * If the function can be sent value during invocation.
1067
+ */
1068
+ payable;
1069
+ constructor(guard, inputs, payable) {
1070
+ super(guard, 'fallback', inputs);
1071
+ Object.defineProperty(this, internal, { value: FallbackFragmentInternal });
1072
+ defineProperties(this, { payable });
1073
+ }
1074
+ /**
1075
+ * Returns a string representation of this fallback as %%format%%.
1076
+ */
1077
+ format(format) {
1078
+ const type = this.inputs.length === 0 ? 'receive' : 'fallback';
1079
+ if (format === 'json') {
1080
+ const stateMutability = this.payable ? 'payable' : 'nonpayable';
1081
+ return JSON.stringify({ type, stateMutability });
1082
+ }
1083
+ return `${type}()${this.payable ? ' payable' : ''}`;
1084
+ }
1085
+ /**
1086
+ * Returns a new **FallbackFragment** for %%obj%%.
1087
+ */
1088
+ static from(obj) {
1089
+ if (FallbackFragment.isFragment(obj)) {
1090
+ return obj;
1091
+ }
1092
+ if (typeof obj === 'string') {
1093
+ return FallbackFragment.from(lex(obj));
1094
+ }
1095
+ else if (obj instanceof TokenString) {
1096
+ const errorObj = obj.toString();
1097
+ const topIsValid = obj.peekKeyword(setify(['fallback', 'receive']));
1098
+ assertArgument(topIsValid, 'type must be fallback or receive', 'obj', errorObj);
1099
+ const type = obj.popKeyword(setify(['fallback', 'receive']));
1100
+ // receive()
1101
+ if (type === 'receive') {
1102
+ const inputs = consumeParams(obj);
1103
+ assertArgument(inputs.length === 0, `receive cannot have arguments`, 'obj.inputs', inputs);
1104
+ consumeKeywords(obj, setify(['payable']));
1105
+ consumeEoi(obj);
1106
+ return new FallbackFragment(_guard, [], true);
1107
+ }
1108
+ // fallback() [payable]
1109
+ // fallback(bytes) [payable] returns (bytes)
1110
+ let inputs = consumeParams(obj);
1111
+ if (inputs.length) {
1112
+ assertArgument(inputs.length === 1 && inputs[0].type === 'bytes', 'invalid fallback inputs', 'obj.inputs', inputs.map((i) => i.format('minimal')).join(', '));
1113
+ }
1114
+ else {
1115
+ inputs = [ParamType.from('bytes')];
1116
+ }
1117
+ const mutability = consumeMutability(obj);
1118
+ assertArgument(mutability === 'nonpayable' || mutability === 'payable', 'fallback cannot be constants', 'obj.stateMutability', mutability);
1119
+ if (consumeKeywords(obj, setify(['returns'])).has('returns')) {
1120
+ const outputs = consumeParams(obj);
1121
+ assertArgument(outputs.length === 1 && outputs[0].type === 'bytes', 'invalid fallback outputs', 'obj.outputs', outputs.map((i) => i.format('minimal')).join(', '));
1122
+ }
1123
+ consumeEoi(obj);
1124
+ return new FallbackFragment(_guard, inputs, mutability === 'payable');
1125
+ }
1126
+ if (obj.type === 'receive') {
1127
+ return new FallbackFragment(_guard, [], true);
1128
+ }
1129
+ if (obj.type === 'fallback') {
1130
+ const inputs = [ParamType.from('bytes')];
1131
+ const payable = obj.stateMutability === 'payable';
1132
+ return new FallbackFragment(_guard, inputs, payable);
1133
+ }
1134
+ assertArgument(false, 'invalid fallback description', 'obj', obj);
1135
+ }
1136
+ /**
1137
+ * Returns ``true`` and provides a type guard if %%value%% is a
1138
+ * **FallbackFragment**.
1139
+ */
1140
+ static isFragment(value) {
1141
+ return value && value[internal] === FallbackFragmentInternal;
1142
+ }
1143
+ }
1144
+ /**
1145
+ * A Fragment which represents a method.
1146
+ */
1147
+ export class FunctionFragment extends NamedFragment {
1148
+ /**
1149
+ * If the function is constant (e.g. ``pure`` or ``view`` functions).
1150
+ */
1151
+ constant;
1152
+ /**
1153
+ * The returned types for the result of calling this function.
1154
+ */
1155
+ outputs;
1156
+ /**
1157
+ * The state mutability (e.g. ``payable``, ``nonpayable``, ``view``
1158
+ * or ``pure``)
1159
+ */
1160
+ stateMutability;
1161
+ /**
1162
+ * If the function can be sent value during invocation.
1163
+ */
1164
+ payable;
1165
+ /**
1166
+ * The recommended gas limit to send when calling this function.
1167
+ */
1168
+ gas;
1169
+ /**
1170
+ * @private
1171
+ */
1172
+ constructor(guard, name, stateMutability, inputs, outputs, gas) {
1173
+ super(guard, 'function', name, inputs);
1174
+ Object.defineProperty(this, internal, { value: FunctionFragmentInternal });
1175
+ outputs = Object.freeze(outputs.slice());
1176
+ const constant = stateMutability === 'view' || stateMutability === 'pure';
1177
+ const payable = stateMutability === 'payable';
1178
+ defineProperties(this, {
1179
+ constant,
1180
+ gas,
1181
+ outputs,
1182
+ payable,
1183
+ stateMutability,
1184
+ });
1185
+ }
1186
+ /**
1187
+ * The Function selector.
1188
+ */
1189
+ get selector() {
1190
+ return id(this.format('sighash')).substring(0, 10);
1191
+ }
1192
+ /**
1193
+ * Returns a string representation of this function as %%format%%.
1194
+ */
1195
+ format(format) {
1196
+ if (format == null) {
1197
+ format = 'sighash';
1198
+ }
1199
+ if (format === 'json') {
1200
+ return JSON.stringify({
1201
+ type: 'function',
1202
+ name: this.name,
1203
+ constant: this.constant,
1204
+ stateMutability: this.stateMutability !== 'nonpayable' ? this.stateMutability : undefined,
1205
+ payable: this.payable,
1206
+ gas: this.gas != null ? this.gas : undefined,
1207
+ inputs: this.inputs.map((i) => JSON.parse(i.format(format))),
1208
+ outputs: this.outputs.map((o) => JSON.parse(o.format(format))),
1209
+ });
1210
+ }
1211
+ const result = [];
1212
+ if (format !== 'sighash') {
1213
+ result.push('function');
1214
+ }
1215
+ result.push(this.name + joinParams(format, this.inputs));
1216
+ if (format !== 'sighash') {
1217
+ if (this.stateMutability !== 'nonpayable') {
1218
+ result.push(this.stateMutability);
1219
+ }
1220
+ if (this.outputs && this.outputs.length) {
1221
+ result.push('returns');
1222
+ result.push(joinParams(format, this.outputs));
1223
+ }
1224
+ if (this.gas != null) {
1225
+ result.push(`@${this.gas.toString()}`);
1226
+ }
1227
+ }
1228
+ return result.join(' ');
1229
+ }
1230
+ /**
1231
+ * Return the selector for a function with %%name%% and %%params%%.
1232
+ */
1233
+ static getSelector(name, params) {
1234
+ params = (params || []).map((p) => ParamType.from(p));
1235
+ const fragment = new FunctionFragment(_guard, name, 'view', params, [], null);
1236
+ return fragment.selector;
1237
+ }
1238
+ /**
1239
+ * Returns a new **FunctionFragment** for %%obj%%.
1240
+ */
1241
+ static from(obj) {
1242
+ if (FunctionFragment.isFragment(obj)) {
1243
+ return obj;
1244
+ }
1245
+ if (typeof obj === 'string') {
1246
+ return FunctionFragment.from(lex(obj));
1247
+ }
1248
+ else if (obj instanceof TokenString) {
1249
+ const name = consumeName('function', obj);
1250
+ const inputs = consumeParams(obj);
1251
+ const mutability = consumeMutability(obj);
1252
+ let outputs = [];
1253
+ if (consumeKeywords(obj, setify(['returns'])).has('returns')) {
1254
+ outputs = consumeParams(obj);
1255
+ }
1256
+ const gas = consumeGas(obj);
1257
+ consumeEoi(obj);
1258
+ return new FunctionFragment(_guard, name, mutability, inputs, outputs, gas);
1259
+ }
1260
+ let stateMutability = obj.stateMutability;
1261
+ // Use legacy Solidity ABI logic if stateMutability is missing
1262
+ if (stateMutability == null) {
1263
+ stateMutability = 'payable';
1264
+ if (typeof obj.constant === 'boolean') {
1265
+ stateMutability = 'view';
1266
+ if (!obj.constant) {
1267
+ stateMutability = 'payable';
1268
+ if (typeof obj.payable === 'boolean' && !obj.payable) {
1269
+ stateMutability = 'nonpayable';
1270
+ }
1271
+ }
1272
+ }
1273
+ else if (typeof obj.payable === 'boolean' && !obj.payable) {
1274
+ stateMutability = 'nonpayable';
1275
+ }
1276
+ }
1277
+ // @TODO: verifyState for stateMutability (e.g. throw if
1278
+ // payable: false but stateMutability is "nonpayable")
1279
+ return new FunctionFragment(_guard, obj.name, stateMutability, obj.inputs ? obj.inputs.map(ParamType.from) : [], obj.outputs ? obj.outputs.map(ParamType.from) : [], obj.gas != null ? obj.gas : null);
1280
+ }
1281
+ /**
1282
+ * Returns ``true`` and provides a type guard if %%value%% is a
1283
+ * **FunctionFragment**.
1284
+ */
1285
+ static isFragment(value) {
1286
+ return value && value[internal] === FunctionFragmentInternal;
1287
+ }
1288
+ }
1289
+ /**
1290
+ * A Fragment which represents a structure.
1291
+ */
1292
+ export class StructFragment extends NamedFragment {
1293
+ /**
1294
+ * @private
1295
+ */
1296
+ constructor(guard, name, inputs) {
1297
+ super(guard, 'struct', name, inputs);
1298
+ Object.defineProperty(this, internal, { value: StructFragmentInternal });
1299
+ }
1300
+ /**
1301
+ * Returns a string representation of this struct as %%format%%.
1302
+ */
1303
+ format() {
1304
+ throw new Error('@TODO');
1305
+ }
1306
+ /**
1307
+ * Returns a new **StructFragment** for %%obj%%.
1308
+ */
1309
+ static from(obj) {
1310
+ if (typeof obj === 'string') {
1311
+ return StructFragment.from(lex(obj));
1312
+ }
1313
+ else if (obj instanceof TokenString) {
1314
+ const name = consumeName('struct', obj);
1315
+ const inputs = consumeParams(obj);
1316
+ consumeEoi(obj);
1317
+ return new StructFragment(_guard, name, inputs);
1318
+ }
1319
+ return new StructFragment(_guard, obj.name, obj.inputs ? obj.inputs.map(ParamType.from) : []);
1320
+ }
1321
+ // @TODO: fix this return type
1322
+ /**
1323
+ * Returns ``true`` and provides a type guard if %%value%% is a
1324
+ * **StructFragment**.
1325
+ */
1326
+ static isFragment(value) {
1327
+ return value && value[internal] === StructFragmentInternal;
1328
+ }
1329
+ }
1330
+ //# sourceMappingURL=fragments.js.map