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
package/src/lib/trx.ts ADDED
@@ -0,0 +1,1463 @@
1
+ import { TronWeb } from '../tronweb.js';
2
+ import utils from '../utils/index.js';
3
+ import { keccak256, toUtf8Bytes, recoverAddress, SigningKey, Signature } from '../utils/ethersUtils.js';
4
+ import { ADDRESS_PREFIX } from '../utils/address.js';
5
+ import { Validator } from '../paramValidator/index.js';
6
+ import { txCheck } from '../utils/transaction.js';
7
+ import { ecRecover } from '../utils/crypto.js';
8
+ import { Block } from '../types/APIResponse.js';
9
+ import {
10
+ Token,
11
+ Account,
12
+ AccountNetMessage,
13
+ Witness,
14
+ TransactionSignWeight,
15
+ BroadcastReturn,
16
+ AddressOptions,
17
+ Proposal,
18
+ ChainParameter,
19
+ BroadcastHexReturn,
20
+ AccountResourceMessage,
21
+ Address,
22
+ Exchange,
23
+ TransactionInfo,
24
+ } from '../types/Trx.js';
25
+ import { SignedTransaction, Transaction } from '../types/Transaction.js';
26
+ import { TypedDataDomain, TypedDataField } from '../utils/typedData.js';
27
+ import { Resource } from '../types/TransactionBuilder.js';
28
+
29
+ const TRX_MESSAGE_HEADER = '\x19TRON Signed Message:\n32';
30
+ // it should be: '\x15TRON Signed Message:\n32';
31
+ const ETH_MESSAGE_HEADER = '\x19Ethereum Signed Message:\n32';
32
+
33
+ function toHex(value: string) {
34
+ return TronWeb.address.toHex(value);
35
+ }
36
+
37
+ type SignedStringOrSignedTransaction<T extends string | Transaction | SignedTransaction> = T extends string
38
+ ? string
39
+ : SignedTransaction & T;
40
+
41
+ export class Trx {
42
+ private tronWeb: TronWeb;
43
+ private cache: { contracts: Record<string, any> };
44
+ private validator: Validator;
45
+
46
+ signMessage;
47
+ sendAsset;
48
+ send;
49
+ sendTrx;
50
+ broadcast;
51
+ broadcastHex;
52
+ signTransaction;
53
+
54
+ constructor(tronWeb: TronWeb) {
55
+ this.tronWeb = tronWeb;
56
+
57
+ this.cache = {
58
+ contracts: {},
59
+ };
60
+ this.validator = new Validator();
61
+ this.signMessage = this.sign;
62
+ this.sendAsset = this.sendToken;
63
+ this.send = this.sendTransaction;
64
+ this.sendTrx = this.sendTransaction;
65
+ this.broadcast = this.sendRawTransaction;
66
+ this.broadcastHex = this.sendHexTransaction;
67
+ this.signTransaction = this.sign;
68
+ }
69
+
70
+ _parseToken(token: any): Token {
71
+ return {
72
+ ...token,
73
+ name: this.tronWeb.toUtf8(token.name),
74
+ abbr: token.abbr && this.tronWeb.toUtf8(token.abbr),
75
+ description: token.description && this.tronWeb.toUtf8(token.description),
76
+ url: token.url && this.tronWeb.toUtf8(token.url),
77
+ };
78
+ }
79
+
80
+ getCurrentBlock(): Promise<Block> {
81
+ return this.tronWeb.fullNode.request('wallet/getnowblock');
82
+ }
83
+
84
+ getConfirmedCurrentBlock(): Promise<Block> {
85
+ return this.tronWeb.solidityNode.request('walletsolidity/getnowblock');
86
+ }
87
+
88
+ async getBlock(block: 'earliest' | 'latest' | number | string | false = this.tronWeb.defaultBlock): Promise<Block> {
89
+ if (block === false) {
90
+ throw new Error('No block identifier provided');
91
+ }
92
+
93
+ if (block == 'earliest') block = 0;
94
+
95
+ if (block == 'latest') return this.getCurrentBlock();
96
+
97
+ if (isNaN(+block) && utils.isHex(block.toString())) return this.getBlockByHash(block as string);
98
+
99
+ return this.getBlockByNumber(block as number);
100
+ }
101
+
102
+ async getBlockByHash(blockHash: string): Promise<Block> {
103
+ const block = await this.tronWeb.fullNode.request<Block>(
104
+ 'wallet/getblockbyid',
105
+ {
106
+ value: blockHash,
107
+ },
108
+ 'post'
109
+ );
110
+ if (!Object.keys(block).length) {
111
+ throw new Error('Block not found');
112
+ }
113
+ return block;
114
+ }
115
+
116
+ async getBlockByNumber(blockID: number): Promise<Block> {
117
+ if (!utils.isInteger(blockID) || blockID < 0) {
118
+ throw new Error('Invalid block number provided');
119
+ }
120
+
121
+ return this.tronWeb.fullNode
122
+ .request<Block>(
123
+ 'wallet/getblockbynum',
124
+ {
125
+ num: parseInt(blockID),
126
+ },
127
+ 'post'
128
+ )
129
+ .then((block) => {
130
+ if (!Object.keys(block).length) {
131
+ throw new Error('Block not found');
132
+ }
133
+
134
+ return block;
135
+ });
136
+ }
137
+
138
+ async getBlockTransactionCount(
139
+ block: 'earliest' | 'latest' | number | string | false = this.tronWeb.defaultBlock
140
+ ): Promise<number> {
141
+ const { transactions = [] } = await this.getBlock(block);
142
+ return transactions.length;
143
+ }
144
+
145
+ async getTransactionFromBlock(
146
+ block: 'earliest' | 'latest' | number | string | false = this.tronWeb.defaultBlock,
147
+ index: number
148
+ ): Promise<Transaction> {
149
+ const { transactions } = await this.getBlock(block);
150
+ if (!transactions) {
151
+ throw new Error('Transaction not found in block');
152
+ }
153
+ if (index >= 0 && index < transactions.length) return transactions[index];
154
+ else throw new Error('Invalid transaction index provided');
155
+ }
156
+
157
+ async getTransactionsFromBlock(
158
+ block: 'earliest' | 'latest' | number | string | false = this.tronWeb.defaultBlock
159
+ ): Promise<Transaction[]> {
160
+ const { transactions } = await this.getBlock(block);
161
+ if (!transactions) {
162
+ throw new Error('Transaction not found in block');
163
+ }
164
+ return transactions;
165
+ }
166
+
167
+ async getTransaction(transactionID: string): Promise<Transaction> {
168
+ const transaction = await this.tronWeb.fullNode.request<Transaction>(
169
+ 'wallet/gettransactionbyid',
170
+ {
171
+ value: transactionID,
172
+ },
173
+ 'post'
174
+ );
175
+ if (!Object.keys(transaction).length) {
176
+ throw new Error('Transaction not found');
177
+ }
178
+ return transaction;
179
+ }
180
+
181
+ async getConfirmedTransaction(transactionID: string): Promise<Transaction> {
182
+ const transaction = await this.tronWeb.solidityNode.request<Transaction>(
183
+ 'walletsolidity/gettransactionbyid',
184
+ {
185
+ value: transactionID,
186
+ },
187
+ 'post'
188
+ );
189
+ if (!Object.keys(transaction).length) {
190
+ throw new Error('Transaction not found');
191
+ }
192
+ return transaction;
193
+ }
194
+
195
+ getUnconfirmedTransactionInfo(transactionID: string): Promise<TransactionInfo> {
196
+ return this.tronWeb.fullNode.request('wallet/gettransactioninfobyid', { value: transactionID }, 'post');
197
+ }
198
+
199
+ getTransactionInfo(transactionID: string): Promise<TransactionInfo> {
200
+ return this.tronWeb.solidityNode.request('walletsolidity/gettransactioninfobyid', { value: transactionID }, 'post');
201
+ }
202
+
203
+ getTransactionsToAddress(address = this.tronWeb.defaultAddress.hex, limit = 30, offset = 0): Promise<Transaction[]> {
204
+ return this.getTransactionsRelated(this.tronWeb.address.toHex(address as string), 'to', limit, offset);
205
+ }
206
+
207
+ getTransactionsFromAddress(address = this.tronWeb.defaultAddress.hex, limit = 30, offset = 0): Promise<Transaction[]> {
208
+ return this.getTransactionsRelated(this.tronWeb.address.toHex(address as string), 'from', limit, offset);
209
+ }
210
+
211
+ async getTransactionsRelated(
212
+ address = this.tronWeb.defaultAddress.hex,
213
+ direction = 'all',
214
+ limit = 30,
215
+ offset = 0
216
+ ): Promise<Transaction[]> {
217
+ if (!['to', 'from', 'all'].includes(direction)) {
218
+ throw new Error('Invalid direction provided: Expected "to", "from" or "all"');
219
+ }
220
+
221
+ if (direction == 'all') {
222
+ const [from, to] = await Promise.all([
223
+ this.getTransactionsRelated(address, 'from', limit, offset),
224
+ this.getTransactionsRelated(address, 'to', limit, offset),
225
+ ]);
226
+
227
+ return [
228
+ ...from.map((tx) => (((tx as any).direction = 'from'), tx)),
229
+ ...to.map((tx) => (((tx as any).direction = 'to'), tx)),
230
+ ].sort((a, b) => {
231
+ return b.raw_data.timestamp - a.raw_data.timestamp;
232
+ });
233
+ }
234
+
235
+ if (!this.tronWeb.isAddress(address as string)) {
236
+ throw new Error('Invalid address provided');
237
+ }
238
+
239
+ if (!utils.isInteger(limit) || limit < 0 || (offset && limit < 1)) {
240
+ throw new Error('Invalid limit provided');
241
+ }
242
+
243
+ if (!utils.isInteger(offset) || offset < 0) {
244
+ throw new Error('Invalid offset provided');
245
+ }
246
+
247
+ address = this.tronWeb.address.toHex(address as string);
248
+
249
+ return this.tronWeb.solidityNode
250
+ .request<{ transaction: Transaction[] }>(
251
+ `walletextension/gettransactions${direction}this`,
252
+ {
253
+ account: {
254
+ address,
255
+ },
256
+ offset,
257
+ limit,
258
+ },
259
+ 'post'
260
+ )
261
+ .then(({ transaction }) => {
262
+ return transaction;
263
+ });
264
+ }
265
+
266
+ async getAccount(address = this.tronWeb.defaultAddress.hex): Promise<Account> {
267
+ if (!this.tronWeb.isAddress(address as Address)) {
268
+ throw new Error('Invalid address provided');
269
+ }
270
+
271
+ address = this.tronWeb.address.toHex(address as string);
272
+
273
+ return this.tronWeb.solidityNode.request(
274
+ 'walletsolidity/getaccount',
275
+ {
276
+ address,
277
+ },
278
+ 'post'
279
+ );
280
+ }
281
+
282
+ getAccountById(id: string): Promise<Account> {
283
+ return this.getAccountInfoById(id, { confirmed: true });
284
+ }
285
+
286
+ async getAccountInfoById(id: string, options: { confirmed: boolean }): Promise<Account> {
287
+ this.validator.notValid([
288
+ {
289
+ name: 'accountId',
290
+ type: 'hex',
291
+ value: id,
292
+ },
293
+ {
294
+ name: 'accountId',
295
+ type: 'string',
296
+ lte: 32,
297
+ gte: 8,
298
+ value: id,
299
+ },
300
+ ]);
301
+
302
+ if (id.startsWith('0x')) {
303
+ id = id.slice(2);
304
+ }
305
+
306
+ return this.tronWeb[options.confirmed ? 'solidityNode' : 'fullNode'].request(
307
+ `wallet${options.confirmed ? 'solidity' : ''}/getaccountbyid`,
308
+ {
309
+ account_id: id,
310
+ },
311
+ 'post'
312
+ );
313
+ }
314
+
315
+ async getBalance(address = this.tronWeb.defaultAddress.hex): Promise<number> {
316
+ const { balance = 0 } = await this.getAccount(address);
317
+ return balance;
318
+ }
319
+
320
+ async getUnconfirmedAccount(address = this.tronWeb.defaultAddress.hex): Promise<Account> {
321
+ if (!this.tronWeb.isAddress(address as Address)) {
322
+ throw new Error('Invalid address provided');
323
+ }
324
+
325
+ address = this.tronWeb.address.toHex(address as string);
326
+
327
+ return this.tronWeb.fullNode.request(
328
+ 'wallet/getaccount',
329
+ {
330
+ address,
331
+ },
332
+ 'post'
333
+ );
334
+ }
335
+
336
+ getUnconfirmedAccountById(id: string): Promise<Account> {
337
+ return this.getAccountInfoById(id, { confirmed: false });
338
+ }
339
+
340
+ async getUnconfirmedBalance(address = this.tronWeb.defaultAddress.hex): Promise<number> {
341
+ const { balance = 0 } = await this.getUnconfirmedAccount(address);
342
+ return balance;
343
+ }
344
+
345
+ async getBandwidth(address = this.tronWeb.defaultAddress.hex): Promise<number> {
346
+ if (!this.tronWeb.isAddress(address as Address)) {
347
+ throw new Error('Invalid address provided');
348
+ }
349
+
350
+ address = this.tronWeb.address.toHex(address as string);
351
+
352
+ return this.tronWeb.fullNode
353
+ .request<AccountNetMessage>(
354
+ 'wallet/getaccountnet',
355
+ {
356
+ address,
357
+ },
358
+ 'post'
359
+ )
360
+ .then(({ freeNetUsed = 0, freeNetLimit = 0, NetUsed = 0, NetLimit = 0 }) => {
361
+ return freeNetLimit - freeNetUsed + (NetLimit - NetUsed);
362
+ });
363
+ }
364
+
365
+ async getTokensIssuedByAddress(address = this.tronWeb.defaultAddress.hex): Promise<Record<string, Token>> {
366
+ if (!this.tronWeb.isAddress(address as Address)) {
367
+ throw new Error('Invalid address provided');
368
+ }
369
+
370
+ address = this.tronWeb.address.toHex(address as string);
371
+
372
+ return this.tronWeb.fullNode
373
+ .request<{ assetIssue: Token[] }>(
374
+ 'wallet/getassetissuebyaccount',
375
+ {
376
+ address,
377
+ },
378
+ 'post'
379
+ )
380
+ .then(({ assetIssue }) => {
381
+ if (!assetIssue) return {};
382
+
383
+ const tokens = assetIssue
384
+ .map((token) => {
385
+ return this._parseToken(token);
386
+ })
387
+ .reduce((tokens, token) => {
388
+ return (tokens[token.name] = token), tokens;
389
+ }, {} as Record<string, Token>);
390
+
391
+ return tokens;
392
+ });
393
+ }
394
+
395
+ async getTokenFromID(tokenID: string | number): Promise<Token> {
396
+ if (utils.isInteger(tokenID)) tokenID = tokenID.toString();
397
+
398
+ if (!utils.isString(tokenID) || !tokenID.length) {
399
+ throw new Error('Invalid token ID provided');
400
+ }
401
+
402
+ return this.tronWeb.fullNode
403
+ .request<Token>(
404
+ 'wallet/getassetissuebyname',
405
+ {
406
+ value: this.tronWeb.fromUtf8(tokenID),
407
+ },
408
+ 'post'
409
+ )
410
+ .then((token) => {
411
+ if (!token.name) {
412
+ throw new Error('Token does not exist');
413
+ }
414
+
415
+ return this._parseToken(token);
416
+ });
417
+ }
418
+
419
+ async listNodes(): Promise<string[]> {
420
+ const { nodes = [] } = await this.tronWeb.fullNode.request<{ nodes: { address: { host: string; port: number } }[] }>(
421
+ 'wallet/listnodes'
422
+ );
423
+ return nodes.map(({ address: { host, port } }) => `${this.tronWeb.toUtf8(host)}:${port}`);
424
+ }
425
+
426
+ async getBlockRange(start = 0, end = 30): Promise<Block[]> {
427
+ if (!utils.isInteger(start) || start < 0) {
428
+ throw new Error('Invalid start of range provided');
429
+ }
430
+
431
+ if (!utils.isInteger(end) || end < start) {
432
+ throw new Error('Invalid end of range provided');
433
+ }
434
+
435
+ if (end + 1 - start > 100) {
436
+ throw new Error('Invalid range size, which should be no more than 100.');
437
+ }
438
+
439
+ return this.tronWeb.fullNode
440
+ .request<{ block: Block[] }>(
441
+ 'wallet/getblockbylimitnext',
442
+ {
443
+ startNum: parseInt(start),
444
+ endNum: parseInt(end) + 1,
445
+ },
446
+ 'post'
447
+ )
448
+ .then(({ block = [] }) => block);
449
+ }
450
+
451
+ async listSuperRepresentatives(): Promise<Witness[]> {
452
+ const { witnesses = [] } = await this.tronWeb.fullNode.request<{ witnesses: Witness[] }>('wallet/listwitnesses');
453
+ return witnesses;
454
+ }
455
+
456
+ async listTokens(limit = 0, offset = 0): Promise<Token[]> {
457
+ if (!utils.isInteger(limit) || limit < 0 || (offset && limit < 1)) {
458
+ throw new Error('Invalid limit provided');
459
+ }
460
+
461
+ if (!utils.isInteger(offset) || offset < 0) {
462
+ throw new Error('Invalid offset provided');
463
+ }
464
+
465
+ if (!limit) {
466
+ return this.tronWeb.fullNode
467
+ .request<{ assetIssue: Token[] }>('wallet/getassetissuelist')
468
+ .then(({ assetIssue = [] }) => assetIssue.map((token) => this._parseToken(token)));
469
+ }
470
+
471
+ return this.tronWeb.fullNode
472
+ .request<{ assetIssue: Token[] }>(
473
+ 'wallet/getpaginatedassetissuelist',
474
+ {
475
+ offset: parseInt(offset),
476
+ limit: parseInt(limit),
477
+ },
478
+ 'post'
479
+ )
480
+ .then(({ assetIssue = [] }) => assetIssue.map((token) => this._parseToken(token)));
481
+ }
482
+
483
+ async timeUntilNextVoteCycle(): Promise<number> {
484
+ const { num = -1 } = await this.tronWeb.fullNode.request<{ num: number }>('wallet/getnextmaintenancetime');
485
+ if (num == -1) {
486
+ throw new Error('Failed to get time until next vote cycle');
487
+ }
488
+ return Math.floor(num / 1000);
489
+ }
490
+
491
+ async getContract(contractAddress: string): Promise<any> {
492
+ if (!this.tronWeb.isAddress(contractAddress)) {
493
+ throw new Error('Invalid contract address provided');
494
+ }
495
+
496
+ if (this.cache.contracts[contractAddress]) {
497
+ return this.cache.contracts[contractAddress];
498
+ }
499
+
500
+ contractAddress = this.tronWeb.address.toHex(contractAddress);
501
+
502
+ const contract = await this.tronWeb.fullNode.request<any>('wallet/getcontract', {
503
+ value: contractAddress,
504
+ });
505
+ if (contract.Error) {
506
+ throw new Error('Contract does not exist');
507
+ }
508
+ this.cache.contracts[contractAddress] = contract;
509
+ return contract;
510
+ }
511
+
512
+ ecRecover(transaction: SignedTransaction) {
513
+ return Trx.ecRecover(transaction);
514
+ }
515
+
516
+ static ecRecover(transaction: SignedTransaction): Address | Address[] {
517
+ if (!txCheck(transaction)) {
518
+ throw new Error('Invalid transaction');
519
+ }
520
+ if (!transaction.signature?.length) {
521
+ throw new Error('Transaction is not signed');
522
+ }
523
+ if (transaction.signature.length === 1) {
524
+ const tronAddress = ecRecover(transaction.txID, transaction.signature[0]);
525
+ return TronWeb.address.fromHex(tronAddress);
526
+ }
527
+ return transaction.signature.map((sig) => {
528
+ const tronAddress = ecRecover(transaction.txID, sig);
529
+ return TronWeb.address.fromHex(tronAddress);
530
+ });
531
+ }
532
+
533
+ async verifyMessage(message: string, signature: string, address = this.tronWeb.defaultAddress.base58, useTronHeader = true) {
534
+ if (!utils.isHex(message)) {
535
+ throw new Error('Expected hex message input');
536
+ }
537
+
538
+ if (Trx.verifySignature(message, address as string, signature, useTronHeader)) {
539
+ return true;
540
+ }
541
+
542
+ throw new Error('Signature does not match');
543
+ }
544
+
545
+ static verifySignature(message: string, address: string, signature: string, useTronHeader = true) {
546
+ message = message.replace(/^0x/, '');
547
+ const messageBytes = [
548
+ ...toUtf8Bytes(useTronHeader ? TRX_MESSAGE_HEADER : ETH_MESSAGE_HEADER),
549
+ ...utils.code.hexStr2byteArray(message),
550
+ ];
551
+
552
+ const messageDigest = keccak256(new Uint8Array(messageBytes));
553
+ const recovered = recoverAddress(messageDigest, Signature.from(`0x${signature.replace(/^0x/, '')}`));
554
+
555
+ const tronAddress = ADDRESS_PREFIX + recovered.substr(2);
556
+ const base58Address = TronWeb.address.fromHex(tronAddress);
557
+
558
+ return base58Address == TronWeb.address.fromHex(address);
559
+ }
560
+
561
+ async verifyMessageV2(message: string | Uint8Array | Array<number>, signature: string) {
562
+ return Trx.verifyMessageV2(message, signature);
563
+ }
564
+
565
+ static verifyMessageV2(message: string | Uint8Array | Array<number>, signature: string) {
566
+ return utils.message.verifyMessage(message, signature);
567
+ }
568
+
569
+ verifyTypedData(
570
+ domain: TypedDataDomain,
571
+ types: Record<string, TypedDataField[]>,
572
+ value: Record<string, any>,
573
+ signature: string,
574
+ address = this.tronWeb.defaultAddress.base58
575
+ ) {
576
+ if (Trx.verifyTypedData(domain, types, value, signature, address as string)) return true;
577
+
578
+ throw new Error('Signature does not match');
579
+ }
580
+
581
+ static verifyTypedData(
582
+ domain: TypedDataDomain,
583
+ types: Record<string, TypedDataField[]>,
584
+ value: Record<string, any>,
585
+ signature: string,
586
+ address: string
587
+ ) {
588
+ const messageDigest = utils._TypedDataEncoder.hash(domain, types, value);
589
+ const recovered = recoverAddress(messageDigest, Signature.from(`0x${signature.replace(/^0x/, '')}`));
590
+
591
+ const tronAddress = ADDRESS_PREFIX + recovered.substr(2);
592
+ const base58Address = TronWeb.address.fromHex(tronAddress);
593
+
594
+ return base58Address == TronWeb.address.fromHex(address);
595
+ }
596
+
597
+ async sign<T extends SignedTransaction | Transaction | string>(
598
+ transaction: T,
599
+ privateKey = this.tronWeb.defaultPrivateKey,
600
+ useTronHeader = true,
601
+ multisig = false
602
+ ): Promise<SignedStringOrSignedTransaction<T>> {
603
+ // Message signing
604
+ if (utils.isString(transaction)) {
605
+ if (!utils.isHex(transaction)) {
606
+ throw new Error('Expected hex message input');
607
+ }
608
+
609
+ return Trx.signString(transaction, privateKey as string, useTronHeader) as SignedStringOrSignedTransaction<T>;
610
+ }
611
+
612
+ if (!utils.isObject(transaction)) {
613
+ throw new Error('Invalid transaction provided');
614
+ }
615
+
616
+ if (!multisig && (transaction as SignedTransaction).signature) {
617
+ throw new Error('Transaction is already signed');
618
+ }
619
+
620
+ if (!multisig) {
621
+ const address = this.tronWeb.address
622
+ .toHex(this.tronWeb.address.fromPrivateKey(privateKey as string) as string)
623
+ .toLowerCase();
624
+
625
+ if (address !== this.tronWeb.address.toHex(transaction.raw_data.contract[0].parameter.value.owner_address)) {
626
+ throw new Error('Private key does not match address in transaction');
627
+ }
628
+
629
+ if (!txCheck(transaction)) {
630
+ throw new Error('Invalid transaction');
631
+ }
632
+ }
633
+ return utils.crypto.signTransaction(privateKey as string, transaction) as SignedStringOrSignedTransaction<T>;
634
+ }
635
+
636
+ static signString(message: string, privateKey: string, useTronHeader = true) {
637
+ message = message.replace(/^0x/, '');
638
+ const value = `0x${privateKey.replace(/^0x/, '')}`;
639
+ const signingKey = new SigningKey(value);
640
+ const messageBytes = [
641
+ ...toUtf8Bytes(useTronHeader ? TRX_MESSAGE_HEADER : ETH_MESSAGE_HEADER),
642
+ ...utils.code.hexStr2byteArray(message),
643
+ ];
644
+ const messageDigest = keccak256(new Uint8Array(messageBytes));
645
+ const signature = signingKey.sign(messageDigest);
646
+ const signatureHex = ['0x', signature.r.substring(2), signature.s.substring(2), Number(signature.v).toString(16)].join(
647
+ ''
648
+ );
649
+ return signatureHex;
650
+ }
651
+
652
+ /**
653
+ * sign message v2 for verified header length
654
+ *
655
+ * @param {message to be signed, should be Bytes or string} message
656
+ * @param {privateKey for signature} privateKey
657
+ * @param {reserved} options
658
+ */
659
+ signMessageV2(message: string | Uint8Array | Array<number>, privateKey = this.tronWeb.defaultPrivateKey) {
660
+ return Trx.signMessageV2(message, privateKey as string);
661
+ }
662
+
663
+ static signMessageV2(message: string | Uint8Array | Array<number>, privateKey: string) {
664
+ return utils.message.signMessage(message, privateKey);
665
+ }
666
+
667
+ _signTypedData(
668
+ domain: TypedDataDomain,
669
+ types: Record<string, TypedDataField[]>,
670
+ value: Record<string, any>,
671
+ privateKey = this.tronWeb.defaultPrivateKey
672
+ ) {
673
+ return Trx._signTypedData(domain, types, value, privateKey as string);
674
+ }
675
+
676
+ static _signTypedData(
677
+ domain: TypedDataDomain,
678
+ types: Record<string, TypedDataField[]>,
679
+ value: Record<string, any>,
680
+ privateKey: string
681
+ ) {
682
+ return utils.crypto._signTypedData(domain, types, value, privateKey);
683
+ }
684
+
685
+ async multiSign(transaction: Transaction, privateKey = this.tronWeb.defaultPrivateKey, permissionId = 0) {
686
+ if (!utils.isObject(transaction) || !transaction.raw_data || !transaction.raw_data.contract) {
687
+ throw new Error('Invalid transaction provided');
688
+ }
689
+
690
+ // If owner permission or permission id exists in transaction, do sign directly
691
+ // If no permission id inside transaction or user passes permission id, use old way to reset permission id
692
+ if (!transaction.raw_data.contract[0].Permission_id && permissionId > 0) {
693
+ // set permission id
694
+ transaction.raw_data.contract[0].Permission_id = permissionId;
695
+
696
+ // check if private key insides permission list
697
+ const address = this.tronWeb.address
698
+ .toHex(this.tronWeb.address.fromPrivateKey(privateKey as string) as string)
699
+ .toLowerCase();
700
+ const signWeight = await this.getSignWeight(transaction, permissionId);
701
+
702
+ if (signWeight.result.code === 'PERMISSION_ERROR') {
703
+ throw new Error(signWeight.result.message);
704
+ }
705
+
706
+ let foundKey = false;
707
+ signWeight.permission.keys.map((key) => {
708
+ if (key.address === address) foundKey = true;
709
+ });
710
+
711
+ if (!foundKey) {
712
+ throw new Error(privateKey + ' has no permission to sign');
713
+ }
714
+
715
+ if (signWeight.approved_list && signWeight.approved_list.indexOf(address) != -1) {
716
+ throw new Error(privateKey + ' already sign transaction');
717
+ }
718
+
719
+ // reset transaction
720
+ if (signWeight.transaction && signWeight.transaction.transaction) {
721
+ transaction = signWeight.transaction.transaction;
722
+ if (permissionId > 0) {
723
+ transaction.raw_data.contract[0].Permission_id = permissionId;
724
+ }
725
+ } else {
726
+ throw new Error('Invalid transaction provided');
727
+ }
728
+ }
729
+
730
+ // sign
731
+ if (!txCheck(transaction)) {
732
+ throw new Error('Invalid transaction');
733
+ }
734
+ return utils.crypto.signTransaction(privateKey as string, transaction);
735
+ }
736
+
737
+ async getApprovedList(transaction: Transaction): Promise<{ approved_list: string[] }> {
738
+ if (!utils.isObject(transaction)) {
739
+ throw new Error('Invalid transaction provided');
740
+ }
741
+
742
+ return this.tronWeb.fullNode.request('wallet/getapprovedlist', transaction, 'post');
743
+ }
744
+
745
+ async getSignWeight(transaction: Transaction, permissionId?: number): Promise<TransactionSignWeight> {
746
+ if (!utils.isObject(transaction) || !transaction.raw_data || !transaction.raw_data.contract)
747
+ throw new Error('Invalid transaction provided');
748
+
749
+ if (utils.isInteger(permissionId)) {
750
+ transaction.raw_data.contract[0].Permission_id = parseInt(permissionId);
751
+ } else if (typeof transaction.raw_data.contract[0].Permission_id !== 'number') {
752
+ transaction.raw_data.contract[0].Permission_id = 0;
753
+ }
754
+
755
+ return this.tronWeb.fullNode.request('wallet/getsignweight', transaction, 'post');
756
+ }
757
+
758
+ async sendRawTransaction<T extends SignedTransaction>(signedTransaction: T): Promise<BroadcastReturn<T>> {
759
+ if (!utils.isObject(signedTransaction)) {
760
+ throw new Error('Invalid transaction provided');
761
+ }
762
+
763
+ if (!signedTransaction.signature || !utils.isArray(signedTransaction.signature)) {
764
+ throw new Error('Transaction is not signed');
765
+ }
766
+
767
+ const result = await this.tronWeb.fullNode.request<Omit<BroadcastReturn<T>, 'transaction'>>(
768
+ 'wallet/broadcasttransaction',
769
+ signedTransaction,
770
+ 'post'
771
+ );
772
+ return {
773
+ ...result,
774
+ transaction: signedTransaction,
775
+ };
776
+ }
777
+
778
+ async sendHexTransaction(signedHexTransaction: string) {
779
+ if (!utils.isHex(signedHexTransaction)) {
780
+ throw new Error('Invalid hex transaction provided');
781
+ }
782
+
783
+ const params = {
784
+ transaction: signedHexTransaction,
785
+ };
786
+
787
+ const result = await this.tronWeb.fullNode.request<BroadcastHexReturn>('wallet/broadcasthex', params, 'post');
788
+ if (result.result) {
789
+ return {
790
+ ...result,
791
+ transaction: JSON.parse(result.transaction) as Transaction,
792
+ hexTransaction: signedHexTransaction,
793
+ };
794
+ }
795
+ return result;
796
+ }
797
+
798
+ async sendTransaction(to: string, amount: number, options: AddressOptions = {}): Promise<BroadcastReturn<SignedTransaction>> {
799
+ if (typeof options === 'string') options = { privateKey: options };
800
+
801
+ if (!this.tronWeb.isAddress(to)) {
802
+ throw new Error('Invalid recipient provided');
803
+ }
804
+
805
+ if (!utils.isInteger(amount) || amount <= 0) {
806
+ throw new Error('Invalid amount provided');
807
+ }
808
+
809
+ options = {
810
+ privateKey: this.tronWeb.defaultPrivateKey as string,
811
+ address: this.tronWeb.defaultAddress.hex as string,
812
+ ...options,
813
+ };
814
+
815
+ if (!options.privateKey && !options.address) {
816
+ throw new Error('Function requires either a private key or address to be set');
817
+ }
818
+
819
+ const address = options.privateKey ? this.tronWeb.address.fromPrivateKey(options.privateKey) : options.address;
820
+ const transaction = await this.tronWeb.transactionBuilder.sendTrx(to, amount, address as Address);
821
+ const signedTransaction = await this.sign(transaction, options.privateKey);
822
+ const result = await this.sendRawTransaction(signedTransaction);
823
+ return result;
824
+ }
825
+
826
+ async sendToken(
827
+ to: string,
828
+ amount: number,
829
+ tokenID: string | number,
830
+ options: AddressOptions = {}
831
+ ): Promise<BroadcastReturn<SignedTransaction>> {
832
+ if (typeof options === 'string') options = { privateKey: options };
833
+
834
+ if (!this.tronWeb.isAddress(to)) {
835
+ throw new Error('Invalid recipient provided');
836
+ }
837
+
838
+ if (!utils.isInteger(amount) || amount <= 0) {
839
+ throw new Error('Invalid amount provided');
840
+ }
841
+
842
+ if (utils.isInteger(tokenID)) tokenID = tokenID.toString();
843
+
844
+ if (!utils.isString(tokenID)) {
845
+ throw new Error('Invalid token ID provided');
846
+ }
847
+
848
+ options = {
849
+ privateKey: this.tronWeb.defaultPrivateKey as string,
850
+ address: this.tronWeb.defaultAddress.hex as string,
851
+ ...options,
852
+ };
853
+
854
+ if (!options.privateKey && !options.address) {
855
+ throw new Error('Function requires either a private key or address to be set');
856
+ }
857
+
858
+ const address = options.privateKey ? this.tronWeb.address.fromPrivateKey(options.privateKey) : options.address;
859
+ const transaction = await this.tronWeb.transactionBuilder.sendToken(to, amount, tokenID, address as Address);
860
+ const signedTransaction = await this.sign(transaction, options.privateKey);
861
+ const result = await this.sendRawTransaction(signedTransaction);
862
+ return result;
863
+ }
864
+
865
+ /**
866
+ * Freezes an amount of TRX.
867
+ * Will give bandwidth OR Energy and TRON Power(voting rights)
868
+ * to the owner of the frozen tokens.
869
+ *
870
+ * @param amount - is the number of frozen trx
871
+ * @param duration - is the duration in days to be frozen
872
+ * @param resource - is the type, must be either "ENERGY" or "BANDWIDTH"
873
+ * @param options
874
+ */
875
+ async freezeBalance(
876
+ amount = 0,
877
+ duration = 3,
878
+ resource: Resource = 'BANDWIDTH',
879
+ options: AddressOptions = {},
880
+ receiverAddress?: string
881
+ ): Promise<BroadcastReturn<SignedTransaction>> {
882
+ if (typeof options === 'string') options = { privateKey: options };
883
+
884
+ if (!['BANDWIDTH', 'ENERGY'].includes(resource)) {
885
+ throw new Error('Invalid resource provided: Expected "BANDWIDTH" or "ENERGY"');
886
+ }
887
+
888
+ if (!utils.isInteger(amount) || amount <= 0) {
889
+ throw new Error('Invalid amount provided');
890
+ }
891
+
892
+ if (!utils.isInteger(duration) || duration < 3) {
893
+ throw new Error('Invalid duration provided, minimum of 3 days');
894
+ }
895
+
896
+ options = {
897
+ privateKey: this.tronWeb.defaultPrivateKey as string,
898
+ address: this.tronWeb.defaultAddress.hex as string,
899
+ ...options,
900
+ };
901
+
902
+ if (!options.privateKey && !options.address) {
903
+ throw new Error('Function requires either a private key or address to be set');
904
+ }
905
+
906
+ const address = options.privateKey ? this.tronWeb.address.fromPrivateKey(options.privateKey) : options.address;
907
+ const freezeBalance = await this.tronWeb.transactionBuilder.freezeBalance(
908
+ amount,
909
+ duration,
910
+ resource,
911
+ address as Address,
912
+ receiverAddress
913
+ );
914
+ const signedTransaction = await this.sign(freezeBalance, options.privateKey);
915
+ const result = await this.sendRawTransaction(signedTransaction);
916
+ return result;
917
+ }
918
+
919
+ /**
920
+ * Unfreeze TRX that has passed the minimum freeze duration.
921
+ * Unfreezing will remove bandwidth and TRON Power.
922
+ *
923
+ * @param resource - is the type, must be either "ENERGY" or "BANDWIDTH"
924
+ * @param options
925
+ */
926
+ async unfreezeBalance(
927
+ resource: Resource = 'BANDWIDTH',
928
+ options: AddressOptions = {},
929
+ receiverAddress: string
930
+ ): Promise<BroadcastReturn<SignedTransaction>> {
931
+ if (typeof options === 'string') options = { privateKey: options };
932
+
933
+ if (!['BANDWIDTH', 'ENERGY'].includes(resource)) {
934
+ throw new Error('Invalid resource provided: Expected "BANDWIDTH" or "ENERGY"');
935
+ }
936
+
937
+ options = {
938
+ privateKey: this.tronWeb.defaultPrivateKey as string,
939
+ address: this.tronWeb.defaultAddress.hex as string,
940
+ ...options,
941
+ };
942
+
943
+ if (!options.privateKey && !options.address) {
944
+ throw new Error('Function requires either a private key or address to be set');
945
+ }
946
+
947
+ const address = options.privateKey ? this.tronWeb.address.fromPrivateKey(options.privateKey) : options.address;
948
+ const unfreezeBalance = await this.tronWeb.transactionBuilder.unfreezeBalance(
949
+ resource,
950
+ address as Address,
951
+ receiverAddress
952
+ );
953
+ const signedTransaction = await this.sign(unfreezeBalance, options.privateKey);
954
+ const result = await this.sendRawTransaction(signedTransaction);
955
+ return result;
956
+ }
957
+
958
+ /**
959
+ * Modify account name
960
+ * Note: Username is allowed to edit only once.
961
+ *
962
+ * @param privateKey - Account private Key
963
+ * @param accountName - name of the account
964
+ *
965
+ * @return modified Transaction Object
966
+ */
967
+ async updateAccount(accountName: string, options: AddressOptions = {}): Promise<BroadcastReturn<SignedTransaction>> {
968
+ if (typeof options === 'string') options = { privateKey: options };
969
+
970
+ if (!utils.isString(accountName) || !accountName.length) {
971
+ throw new Error('Name must be a string');
972
+ }
973
+
974
+ options = {
975
+ privateKey: this.tronWeb.defaultPrivateKey as string,
976
+ address: this.tronWeb.defaultAddress.hex as string,
977
+ ...options,
978
+ };
979
+
980
+ if (!options.privateKey && !options.address) throw Error('Function requires either a private key or address to be set');
981
+
982
+ const address = options.privateKey ? this.tronWeb.address.fromPrivateKey(options.privateKey) : options.address;
983
+ const updateAccount = await this.tronWeb.transactionBuilder.updateAccount(accountName, address as Address);
984
+ const signedTransaction = await this.sign(updateAccount, options.privateKey);
985
+ const result = await this.sendRawTransaction(signedTransaction);
986
+ return result;
987
+ }
988
+
989
+ /**
990
+ * Gets a network modification proposal by ID.
991
+ */
992
+ async getProposal(proposalID: number): Promise<Proposal> {
993
+ if (!utils.isInteger(proposalID) || proposalID < 0) {
994
+ throw new Error('Invalid proposalID provided');
995
+ }
996
+
997
+ return this.tronWeb.fullNode.request(
998
+ 'wallet/getproposalbyid',
999
+ {
1000
+ id: parseInt(proposalID),
1001
+ },
1002
+ 'post'
1003
+ );
1004
+ }
1005
+
1006
+ /**
1007
+ * Lists all network modification proposals.
1008
+ */
1009
+ async listProposals(): Promise<Proposal[]> {
1010
+ const { proposals = [] } = await this.tronWeb.fullNode.request<{ proposals: Proposal[] }>(
1011
+ 'wallet/listproposals',
1012
+ {},
1013
+ 'post'
1014
+ );
1015
+ return proposals;
1016
+ }
1017
+
1018
+ /**
1019
+ * Lists all parameters available for network modification proposals.
1020
+ */
1021
+ async getChainParameters(): Promise<ChainParameter[]> {
1022
+ const { chainParameter = [] } = await this.tronWeb.fullNode.request<{ chainParameter: ChainParameter[] }>(
1023
+ 'wallet/getchainparameters',
1024
+ {},
1025
+ 'post'
1026
+ );
1027
+ return chainParameter;
1028
+ }
1029
+
1030
+ /**
1031
+ * Get the account resources
1032
+ */
1033
+ async getAccountResources(address = this.tronWeb.defaultAddress.hex): Promise<AccountResourceMessage> {
1034
+ if (!this.tronWeb.isAddress(address as Address)) {
1035
+ throw new Error('Invalid address provided');
1036
+ }
1037
+
1038
+ return this.tronWeb.fullNode.request(
1039
+ 'wallet/getaccountresource',
1040
+ {
1041
+ address: this.tronWeb.address.toHex(address as string),
1042
+ },
1043
+ 'post'
1044
+ );
1045
+ }
1046
+
1047
+ /**
1048
+ * Query the amount of resources of a specific resourceType delegated by fromAddress to toAddress
1049
+ */
1050
+ async getDelegatedResourceV2(
1051
+ fromAddress = this.tronWeb.defaultAddress.hex,
1052
+ toAddress = this.tronWeb.defaultAddress.hex,
1053
+ options = { confirmed: true }
1054
+ ): Promise<{
1055
+ delegatedResource: {
1056
+ from: string;
1057
+ to: string;
1058
+ frozen_balance_for_bandwidth: number;
1059
+ frozen_balance_for_energy: number;
1060
+ expire_time_for_bandwidth: number;
1061
+ expire_time_for_energy: number;
1062
+ };
1063
+ }> {
1064
+ if (!this.tronWeb.isAddress(fromAddress as Address)) {
1065
+ throw new Error('Invalid address provided');
1066
+ }
1067
+
1068
+ if (!this.tronWeb.isAddress(toAddress as Address)) {
1069
+ throw new Error('Invalid address provided');
1070
+ }
1071
+
1072
+ return this.tronWeb[options.confirmed ? 'solidityNode' : 'fullNode'].request(
1073
+ `wallet${options.confirmed ? 'solidity' : ''}/getdelegatedresourcev2`,
1074
+ {
1075
+ fromAddress: toHex(fromAddress as string),
1076
+ toAddress: toHex(toAddress as string),
1077
+ },
1078
+ 'post'
1079
+ );
1080
+ }
1081
+
1082
+ /**
1083
+ * Query the resource delegation index by an account
1084
+ */
1085
+ async getDelegatedResourceAccountIndexV2(
1086
+ address = this.tronWeb.defaultAddress.hex,
1087
+ options = { confirmed: true }
1088
+ ): Promise<{
1089
+ account: Address;
1090
+ fromAccounts: Address[];
1091
+ toAccounts: Address[];
1092
+ }> {
1093
+ if (!this.tronWeb.isAddress(address as Address)) {
1094
+ throw new Error('Invalid address provided');
1095
+ }
1096
+
1097
+ return this.tronWeb[options.confirmed ? 'solidityNode' : 'fullNode'].request(
1098
+ `wallet${options.confirmed ? 'solidity' : ''}/getdelegatedresourceaccountindexv2`,
1099
+ {
1100
+ value: toHex(address as Address),
1101
+ },
1102
+ 'post'
1103
+ );
1104
+ }
1105
+
1106
+ /**
1107
+ * Query the amount of delegatable resources of the specified resource Type for target address, unit is sun.
1108
+ */
1109
+ async getCanDelegatedMaxSize(
1110
+ address = this.tronWeb.defaultAddress.hex,
1111
+ resource: Resource = 'BANDWIDTH',
1112
+ options = { confirmed: true }
1113
+ ): Promise<{
1114
+ max_size: number;
1115
+ }> {
1116
+ if (!this.tronWeb.isAddress(address as Address)) {
1117
+ throw new Error('Invalid address provided');
1118
+ }
1119
+
1120
+ this.validator.notValid([
1121
+ {
1122
+ name: 'resource',
1123
+ type: 'resource',
1124
+ value: resource,
1125
+ msg: 'Invalid resource provided: Expected "BANDWIDTH" or "ENERGY"',
1126
+ },
1127
+ ]);
1128
+
1129
+ return this.tronWeb[options.confirmed ? 'solidityNode' : 'fullNode'].request(
1130
+ `wallet${options.confirmed ? 'solidity' : ''}/getcandelegatedmaxsize`,
1131
+ {
1132
+ owner_address: toHex(address as Address),
1133
+ type: resource === 'ENERGY' ? 1 : 0,
1134
+ },
1135
+ 'post'
1136
+ );
1137
+ }
1138
+
1139
+ /**
1140
+ * Remaining times of available unstaking API
1141
+ */
1142
+ async getAvailableUnfreezeCount(
1143
+ address = this.tronWeb.defaultAddress.hex,
1144
+ options = { confirmed: true }
1145
+ ): Promise<{
1146
+ count: number;
1147
+ }> {
1148
+ if (!this.tronWeb.isAddress(address as Address)) {
1149
+ throw new Error('Invalid address provided');
1150
+ }
1151
+
1152
+ return this.tronWeb[options.confirmed ? 'solidityNode' : 'fullNode'].request(
1153
+ `wallet${options.confirmed ? 'solidity' : ''}/getavailableunfreezecount`,
1154
+ {
1155
+ owner_address: toHex(address as Address),
1156
+ },
1157
+ 'post'
1158
+ );
1159
+ }
1160
+
1161
+ /**
1162
+ * Query the withdrawable balance at the specified timestamp
1163
+ */
1164
+ async getCanWithdrawUnfreezeAmount(
1165
+ address = this.tronWeb.defaultAddress.hex,
1166
+ timestamp = Date.now(),
1167
+ options = { confirmed: true }
1168
+ ): Promise<{
1169
+ amount: number;
1170
+ }> {
1171
+ if (!this.tronWeb.isAddress(address as Address)) {
1172
+ throw new Error('Invalid address provided');
1173
+ }
1174
+
1175
+ if (!utils.isInteger(timestamp) || timestamp < 0) {
1176
+ throw new Error('Invalid timestamp provided');
1177
+ }
1178
+
1179
+ return this.tronWeb[options.confirmed ? 'solidityNode' : 'fullNode'].request(
1180
+ `wallet${options.confirmed ? 'solidity' : ''}/getcanwithdrawunfreezeamount`,
1181
+ {
1182
+ owner_address: toHex(address as Address),
1183
+ timestamp: timestamp,
1184
+ },
1185
+ 'post'
1186
+ );
1187
+ }
1188
+
1189
+ /**
1190
+ * Get the exchange ID.
1191
+ */
1192
+ async getExchangeByID(exchangeID: number): Promise<Exchange> {
1193
+ if (!utils.isInteger(exchangeID) || exchangeID < 0) {
1194
+ throw new Error('Invalid exchangeID provided');
1195
+ }
1196
+
1197
+ return this.tronWeb.fullNode.request(
1198
+ 'wallet/getexchangebyid',
1199
+ {
1200
+ id: exchangeID,
1201
+ },
1202
+ 'post'
1203
+ );
1204
+ }
1205
+
1206
+ /**
1207
+ * Lists the exchanges
1208
+ */
1209
+ async listExchanges() {
1210
+ return this.tronWeb.fullNode
1211
+ .request<{ exchanges: Exchange[] }>('wallet/listexchanges', {}, 'post')
1212
+ .then(({ exchanges = [] }) => exchanges);
1213
+ }
1214
+
1215
+ /**
1216
+ * Lists all network modification proposals.
1217
+ */
1218
+ async listExchangesPaginated(limit = 10, offset = 0) {
1219
+ return this.tronWeb.fullNode
1220
+ .request<{ exchanges: Exchange[] }>(
1221
+ 'wallet/getpaginatedexchangelist',
1222
+ {
1223
+ limit,
1224
+ offset,
1225
+ },
1226
+ 'post'
1227
+ )
1228
+ .then(({ exchanges = [] }) => exchanges);
1229
+ }
1230
+
1231
+ /**
1232
+ * Get info about thre node
1233
+ */
1234
+ async getNodeInfo(): Promise<{
1235
+ beginSyncNum: number;
1236
+ block: string;
1237
+ solidityBlock: string;
1238
+ currentConnectCount: number;
1239
+ activeConnectCount: number;
1240
+ passiveConnectCount: number;
1241
+ totalFlow: number;
1242
+ peerInfoList: {
1243
+ lastSyncBlock: string;
1244
+ remainNum: number;
1245
+ lastBlockUpdateTime: number;
1246
+ syncFlag: boolean;
1247
+ headBlockTimeWeBothHave: number;
1248
+ needSyncFromPeer: boolean;
1249
+ needSyncFromUs: boolean;
1250
+ host: string;
1251
+ port: number;
1252
+ nodeId: string;
1253
+ connectTime: number;
1254
+ avgLatency: number;
1255
+ syncToFetchSize: number;
1256
+ syncToFetchSizePeekNum: number;
1257
+ syncBlockRequestedSize: number;
1258
+ unFetchSynNum: number;
1259
+ blockInPorcSize: number;
1260
+ headBlockWeBothHave: string;
1261
+ isActive: boolean;
1262
+ score: number;
1263
+ nodeCount: number;
1264
+ inFlow: number;
1265
+ disconnectTimes: number;
1266
+ localDisconnectReason: string;
1267
+ remoteDisconnectReason: string;
1268
+ };
1269
+ configNodeInfo: {
1270
+ codeVersion: string;
1271
+ p2pVersion: string;
1272
+ listenPort: number;
1273
+ discoverEnable: boolean;
1274
+ activeNodeSize: number;
1275
+ passiveNodeSize: number;
1276
+ sendNodeSize: number;
1277
+ maxConnectCount: number;
1278
+ sameIpMaxConnectCount: number;
1279
+ backupListenPort: number;
1280
+ backupMemberSize: number;
1281
+ backupPriority: number;
1282
+ dbVersion: number;
1283
+ minParticipationRate: number;
1284
+ supportConstant: boolean;
1285
+ minTimeRatio: number;
1286
+ maxTimeRatio: number;
1287
+ allowCreationOfContracts: number;
1288
+ allowAdaptiveEnergy: number;
1289
+ };
1290
+ machineInfo: {
1291
+ threadCount: number;
1292
+ deadLockThreadCount: number;
1293
+ cpuCount: number;
1294
+ totalMemory: number;
1295
+ freeMemory: number;
1296
+ cpuRate: number;
1297
+ javaVersion: string;
1298
+ osName: string;
1299
+ jvmTotalMemory: number;
1300
+ jvmFreeMemory: number;
1301
+ processCpuRate: number;
1302
+ memoryDescInfoList: {
1303
+ name: string;
1304
+ initSize: number;
1305
+ useSize: number;
1306
+ maxSize: number;
1307
+ useRate: number;
1308
+ };
1309
+ deadLockThreadInfoList: {
1310
+ name: string;
1311
+ lockName: string;
1312
+ lockOwner: string;
1313
+ state: string;
1314
+ blockTime: number;
1315
+ waitTime: number;
1316
+ stackTrace: string;
1317
+ };
1318
+ };
1319
+ cheatWitnessInfoMap: Map<string, string>;
1320
+ }> {
1321
+ return this.tronWeb.fullNode.request('wallet/getnodeinfo', {}, 'post');
1322
+ }
1323
+
1324
+ async getTokenListByName(tokenID: string | number): Promise<Token | Token[]> {
1325
+ if (utils.isInteger(tokenID)) tokenID = tokenID.toString();
1326
+
1327
+ if (!utils.isString(tokenID) || !tokenID.length) {
1328
+ throw new Error('Invalid token ID provided');
1329
+ }
1330
+
1331
+ return this.tronWeb.fullNode
1332
+ .request<({ assetIssue: Token[] } & { name: undefined }) | (Token & { assetIssue: undefined })>(
1333
+ 'wallet/getassetissuelistbyname',
1334
+ {
1335
+ value: this.tronWeb.fromUtf8(tokenID),
1336
+ },
1337
+ 'post'
1338
+ )
1339
+ .then((token) => {
1340
+ if (Array.isArray(token.assetIssue)) {
1341
+ return token.assetIssue.map((t) => this._parseToken(t));
1342
+ } else if (!token.name) {
1343
+ throw new Error('Token does not exist');
1344
+ }
1345
+
1346
+ return this._parseToken(token);
1347
+ });
1348
+ }
1349
+
1350
+ getTokenByID(tokenID: number | string): Promise<Token> {
1351
+ if (utils.isInteger(tokenID)) tokenID = tokenID.toString();
1352
+
1353
+ if (!utils.isString(tokenID) || !tokenID.length) {
1354
+ throw new Error('Invalid token ID provided');
1355
+ }
1356
+
1357
+ return this.tronWeb.fullNode
1358
+ .request<Token>(
1359
+ 'wallet/getassetissuebyid',
1360
+ {
1361
+ value: tokenID,
1362
+ },
1363
+ 'post'
1364
+ )
1365
+ .then((token) => {
1366
+ if (!token.name) {
1367
+ throw new Error('Token does not exist');
1368
+ }
1369
+
1370
+ return this._parseToken(token);
1371
+ });
1372
+ }
1373
+
1374
+ async getReward(address: Address, options: { confirmed?: boolean } = {}) {
1375
+ options.confirmed = true;
1376
+ return this._getReward(address, options);
1377
+ }
1378
+
1379
+ async getUnconfirmedReward(address: Address, options: { confirmed?: boolean } = {}) {
1380
+ options.confirmed = false;
1381
+ return this._getReward(address, options);
1382
+ }
1383
+
1384
+ async getBrokerage(address: Address, options: { confirmed?: boolean } = {}) {
1385
+ options.confirmed = true;
1386
+ return this._getBrokerage(address, options);
1387
+ }
1388
+
1389
+ async getUnconfirmedBrokerage(address: Address, options: { confirmed?: boolean } = {}) {
1390
+ options.confirmed = false;
1391
+ return this._getBrokerage(address, options);
1392
+ }
1393
+
1394
+ async _getReward(address = this.tronWeb.defaultAddress.hex, options: { confirmed?: boolean }): Promise<number> {
1395
+ this.validator.notValid([
1396
+ {
1397
+ name: 'origin',
1398
+ type: 'address',
1399
+ value: address,
1400
+ },
1401
+ ]);
1402
+
1403
+ const data = {
1404
+ address: toHex(address as Address),
1405
+ };
1406
+
1407
+ return this.tronWeb[options.confirmed ? 'solidityNode' : 'fullNode']
1408
+ .request<{ reward?: number }>(`wallet${options.confirmed ? 'solidity' : ''}/getReward`, data, 'post')
1409
+ .then((result = { reward: undefined }) => {
1410
+ if (typeof result.reward === 'undefined') {
1411
+ throw new Error('Not found.');
1412
+ }
1413
+
1414
+ return result.reward;
1415
+ });
1416
+ }
1417
+
1418
+ private async _getBrokerage(address = this.tronWeb.defaultAddress.hex, options: { confirmed?: boolean }): Promise<number> {
1419
+ this.validator.notValid([
1420
+ {
1421
+ name: 'origin',
1422
+ type: 'address',
1423
+ value: address,
1424
+ },
1425
+ ]);
1426
+
1427
+ const data = {
1428
+ address: toHex(address as Address),
1429
+ };
1430
+
1431
+ return this.tronWeb[options.confirmed ? 'solidityNode' : 'fullNode']
1432
+ .request<{ brokerage?: number }>(`wallet${options.confirmed ? 'solidity' : ''}/getBrokerage`, data, 'post')
1433
+ .then((result = {}) => {
1434
+ if (typeof result.brokerage === 'undefined') {
1435
+ throw new Error('Not found.');
1436
+ }
1437
+
1438
+ return result.brokerage;
1439
+ });
1440
+ }
1441
+
1442
+ async getBandwidthPrices(): Promise<string> {
1443
+ return this.tronWeb.fullNode.request<{ prices?: string }>('wallet/getbandwidthprices', {}, 'post')
1444
+ .then((result = {}) => {
1445
+ if (typeof result.prices === 'undefined') {
1446
+ throw new Error('Not found.');
1447
+ }
1448
+
1449
+ return result.prices;
1450
+ });
1451
+ }
1452
+
1453
+ async getEnergyPrices(): Promise<string> {
1454
+ return this.tronWeb.fullNode.request<{ prices?: string }>('wallet/getenergyprices', {}, 'post')
1455
+ .then((result = {}) => {
1456
+ if (typeof result.prices === 'undefined') {
1457
+ throw new Error('Not found.');
1458
+ }
1459
+
1460
+ return result.prices;
1461
+ });
1462
+ }
1463
+ }