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,965 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Trx = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const tronweb_js_1 = require("../tronweb.js");
6
+ const index_js_1 = tslib_1.__importDefault(require("../utils/index.js"));
7
+ const ethersUtils_js_1 = require("../utils/ethersUtils.js");
8
+ const address_js_1 = require("../utils/address.js");
9
+ const index_js_2 = require("../paramValidator/index.js");
10
+ const transaction_js_1 = require("../utils/transaction.js");
11
+ const crypto_js_1 = require("../utils/crypto.js");
12
+ const TRX_MESSAGE_HEADER = '\x19TRON Signed Message:\n32';
13
+ // it should be: '\x15TRON Signed Message:\n32';
14
+ const ETH_MESSAGE_HEADER = '\x19Ethereum Signed Message:\n32';
15
+ function toHex(value) {
16
+ return tronweb_js_1.TronWeb.address.toHex(value);
17
+ }
18
+ class Trx {
19
+ tronWeb;
20
+ cache;
21
+ validator;
22
+ signMessage;
23
+ sendAsset;
24
+ send;
25
+ sendTrx;
26
+ broadcast;
27
+ broadcastHex;
28
+ signTransaction;
29
+ constructor(tronWeb) {
30
+ this.tronWeb = tronWeb;
31
+ this.cache = {
32
+ contracts: {},
33
+ };
34
+ this.validator = new index_js_2.Validator();
35
+ this.signMessage = this.sign;
36
+ this.sendAsset = this.sendToken;
37
+ this.send = this.sendTransaction;
38
+ this.sendTrx = this.sendTransaction;
39
+ this.broadcast = this.sendRawTransaction;
40
+ this.broadcastHex = this.sendHexTransaction;
41
+ this.signTransaction = this.sign;
42
+ }
43
+ _parseToken(token) {
44
+ return {
45
+ ...token,
46
+ name: this.tronWeb.toUtf8(token.name),
47
+ abbr: token.abbr && this.tronWeb.toUtf8(token.abbr),
48
+ description: token.description && this.tronWeb.toUtf8(token.description),
49
+ url: token.url && this.tronWeb.toUtf8(token.url),
50
+ };
51
+ }
52
+ getCurrentBlock() {
53
+ return this.tronWeb.fullNode.request('wallet/getnowblock');
54
+ }
55
+ getConfirmedCurrentBlock() {
56
+ return this.tronWeb.solidityNode.request('walletsolidity/getnowblock');
57
+ }
58
+ async getBlock(block = this.tronWeb.defaultBlock) {
59
+ if (block === false) {
60
+ throw new Error('No block identifier provided');
61
+ }
62
+ if (block == 'earliest')
63
+ block = 0;
64
+ if (block == 'latest')
65
+ return this.getCurrentBlock();
66
+ if (isNaN(+block) && index_js_1.default.isHex(block.toString()))
67
+ return this.getBlockByHash(block);
68
+ return this.getBlockByNumber(block);
69
+ }
70
+ async getBlockByHash(blockHash) {
71
+ const block = await this.tronWeb.fullNode.request('wallet/getblockbyid', {
72
+ value: blockHash,
73
+ }, 'post');
74
+ if (!Object.keys(block).length) {
75
+ throw new Error('Block not found');
76
+ }
77
+ return block;
78
+ }
79
+ async getBlockByNumber(blockID) {
80
+ if (!index_js_1.default.isInteger(blockID) || blockID < 0) {
81
+ throw new Error('Invalid block number provided');
82
+ }
83
+ return this.tronWeb.fullNode
84
+ .request('wallet/getblockbynum', {
85
+ num: parseInt(blockID),
86
+ }, 'post')
87
+ .then((block) => {
88
+ if (!Object.keys(block).length) {
89
+ throw new Error('Block not found');
90
+ }
91
+ return block;
92
+ });
93
+ }
94
+ async getBlockTransactionCount(block = this.tronWeb.defaultBlock) {
95
+ const { transactions = [] } = await this.getBlock(block);
96
+ return transactions.length;
97
+ }
98
+ async getTransactionFromBlock(block = this.tronWeb.defaultBlock, index) {
99
+ const { transactions } = await this.getBlock(block);
100
+ if (!transactions) {
101
+ throw new Error('Transaction not found in block');
102
+ }
103
+ if (index >= 0 && index < transactions.length)
104
+ return transactions[index];
105
+ else
106
+ throw new Error('Invalid transaction index provided');
107
+ }
108
+ async getTransactionsFromBlock(block = this.tronWeb.defaultBlock) {
109
+ const { transactions } = await this.getBlock(block);
110
+ if (!transactions) {
111
+ throw new Error('Transaction not found in block');
112
+ }
113
+ return transactions;
114
+ }
115
+ async getTransaction(transactionID) {
116
+ const transaction = await this.tronWeb.fullNode.request('wallet/gettransactionbyid', {
117
+ value: transactionID,
118
+ }, 'post');
119
+ if (!Object.keys(transaction).length) {
120
+ throw new Error('Transaction not found');
121
+ }
122
+ return transaction;
123
+ }
124
+ async getConfirmedTransaction(transactionID) {
125
+ const transaction = await this.tronWeb.solidityNode.request('walletsolidity/gettransactionbyid', {
126
+ value: transactionID,
127
+ }, 'post');
128
+ if (!Object.keys(transaction).length) {
129
+ throw new Error('Transaction not found');
130
+ }
131
+ return transaction;
132
+ }
133
+ getUnconfirmedTransactionInfo(transactionID) {
134
+ return this.tronWeb.fullNode.request('wallet/gettransactioninfobyid', { value: transactionID }, 'post');
135
+ }
136
+ getTransactionInfo(transactionID) {
137
+ return this.tronWeb.solidityNode.request('walletsolidity/gettransactioninfobyid', { value: transactionID }, 'post');
138
+ }
139
+ getTransactionsToAddress(address = this.tronWeb.defaultAddress.hex, limit = 30, offset = 0) {
140
+ return this.getTransactionsRelated(this.tronWeb.address.toHex(address), 'to', limit, offset);
141
+ }
142
+ getTransactionsFromAddress(address = this.tronWeb.defaultAddress.hex, limit = 30, offset = 0) {
143
+ return this.getTransactionsRelated(this.tronWeb.address.toHex(address), 'from', limit, offset);
144
+ }
145
+ async getTransactionsRelated(address = this.tronWeb.defaultAddress.hex, direction = 'all', limit = 30, offset = 0) {
146
+ if (!['to', 'from', 'all'].includes(direction)) {
147
+ throw new Error('Invalid direction provided: Expected "to", "from" or "all"');
148
+ }
149
+ if (direction == 'all') {
150
+ const [from, to] = await Promise.all([
151
+ this.getTransactionsRelated(address, 'from', limit, offset),
152
+ this.getTransactionsRelated(address, 'to', limit, offset),
153
+ ]);
154
+ return [
155
+ ...from.map((tx) => ((tx.direction = 'from'), tx)),
156
+ ...to.map((tx) => ((tx.direction = 'to'), tx)),
157
+ ].sort((a, b) => {
158
+ return b.raw_data.timestamp - a.raw_data.timestamp;
159
+ });
160
+ }
161
+ if (!this.tronWeb.isAddress(address)) {
162
+ throw new Error('Invalid address provided');
163
+ }
164
+ if (!index_js_1.default.isInteger(limit) || limit < 0 || (offset && limit < 1)) {
165
+ throw new Error('Invalid limit provided');
166
+ }
167
+ if (!index_js_1.default.isInteger(offset) || offset < 0) {
168
+ throw new Error('Invalid offset provided');
169
+ }
170
+ address = this.tronWeb.address.toHex(address);
171
+ return this.tronWeb.solidityNode
172
+ .request(`walletextension/gettransactions${direction}this`, {
173
+ account: {
174
+ address,
175
+ },
176
+ offset,
177
+ limit,
178
+ }, 'post')
179
+ .then(({ transaction }) => {
180
+ return transaction;
181
+ });
182
+ }
183
+ async getAccount(address = this.tronWeb.defaultAddress.hex) {
184
+ if (!this.tronWeb.isAddress(address)) {
185
+ throw new Error('Invalid address provided');
186
+ }
187
+ address = this.tronWeb.address.toHex(address);
188
+ return this.tronWeb.solidityNode.request('walletsolidity/getaccount', {
189
+ address,
190
+ }, 'post');
191
+ }
192
+ getAccountById(id) {
193
+ return this.getAccountInfoById(id, { confirmed: true });
194
+ }
195
+ async getAccountInfoById(id, options) {
196
+ this.validator.notValid([
197
+ {
198
+ name: 'accountId',
199
+ type: 'hex',
200
+ value: id,
201
+ },
202
+ {
203
+ name: 'accountId',
204
+ type: 'string',
205
+ lte: 32,
206
+ gte: 8,
207
+ value: id,
208
+ },
209
+ ]);
210
+ if (id.startsWith('0x')) {
211
+ id = id.slice(2);
212
+ }
213
+ return this.tronWeb[options.confirmed ? 'solidityNode' : 'fullNode'].request(`wallet${options.confirmed ? 'solidity' : ''}/getaccountbyid`, {
214
+ account_id: id,
215
+ }, 'post');
216
+ }
217
+ async getBalance(address = this.tronWeb.defaultAddress.hex) {
218
+ const { balance = 0 } = await this.getAccount(address);
219
+ return balance;
220
+ }
221
+ async getUnconfirmedAccount(address = this.tronWeb.defaultAddress.hex) {
222
+ if (!this.tronWeb.isAddress(address)) {
223
+ throw new Error('Invalid address provided');
224
+ }
225
+ address = this.tronWeb.address.toHex(address);
226
+ return this.tronWeb.fullNode.request('wallet/getaccount', {
227
+ address,
228
+ }, 'post');
229
+ }
230
+ getUnconfirmedAccountById(id) {
231
+ return this.getAccountInfoById(id, { confirmed: false });
232
+ }
233
+ async getUnconfirmedBalance(address = this.tronWeb.defaultAddress.hex) {
234
+ const { balance = 0 } = await this.getUnconfirmedAccount(address);
235
+ return balance;
236
+ }
237
+ async getBandwidth(address = this.tronWeb.defaultAddress.hex) {
238
+ if (!this.tronWeb.isAddress(address)) {
239
+ throw new Error('Invalid address provided');
240
+ }
241
+ address = this.tronWeb.address.toHex(address);
242
+ return this.tronWeb.fullNode
243
+ .request('wallet/getaccountnet', {
244
+ address,
245
+ }, 'post')
246
+ .then(({ freeNetUsed = 0, freeNetLimit = 0, NetUsed = 0, NetLimit = 0 }) => {
247
+ return freeNetLimit - freeNetUsed + (NetLimit - NetUsed);
248
+ });
249
+ }
250
+ async getTokensIssuedByAddress(address = this.tronWeb.defaultAddress.hex) {
251
+ if (!this.tronWeb.isAddress(address)) {
252
+ throw new Error('Invalid address provided');
253
+ }
254
+ address = this.tronWeb.address.toHex(address);
255
+ return this.tronWeb.fullNode
256
+ .request('wallet/getassetissuebyaccount', {
257
+ address,
258
+ }, 'post')
259
+ .then(({ assetIssue }) => {
260
+ if (!assetIssue)
261
+ return {};
262
+ const tokens = assetIssue
263
+ .map((token) => {
264
+ return this._parseToken(token);
265
+ })
266
+ .reduce((tokens, token) => {
267
+ return (tokens[token.name] = token), tokens;
268
+ }, {});
269
+ return tokens;
270
+ });
271
+ }
272
+ async getTokenFromID(tokenID) {
273
+ if (index_js_1.default.isInteger(tokenID))
274
+ tokenID = tokenID.toString();
275
+ if (!index_js_1.default.isString(tokenID) || !tokenID.length) {
276
+ throw new Error('Invalid token ID provided');
277
+ }
278
+ return this.tronWeb.fullNode
279
+ .request('wallet/getassetissuebyname', {
280
+ value: this.tronWeb.fromUtf8(tokenID),
281
+ }, 'post')
282
+ .then((token) => {
283
+ if (!token.name) {
284
+ throw new Error('Token does not exist');
285
+ }
286
+ return this._parseToken(token);
287
+ });
288
+ }
289
+ async listNodes() {
290
+ const { nodes = [] } = await this.tronWeb.fullNode.request('wallet/listnodes');
291
+ return nodes.map(({ address: { host, port } }) => `${this.tronWeb.toUtf8(host)}:${port}`);
292
+ }
293
+ async getBlockRange(start = 0, end = 30) {
294
+ if (!index_js_1.default.isInteger(start) || start < 0) {
295
+ throw new Error('Invalid start of range provided');
296
+ }
297
+ if (!index_js_1.default.isInteger(end) || end < start) {
298
+ throw new Error('Invalid end of range provided');
299
+ }
300
+ if (end + 1 - start > 100) {
301
+ throw new Error('Invalid range size, which should be no more than 100.');
302
+ }
303
+ return this.tronWeb.fullNode
304
+ .request('wallet/getblockbylimitnext', {
305
+ startNum: parseInt(start),
306
+ endNum: parseInt(end) + 1,
307
+ }, 'post')
308
+ .then(({ block = [] }) => block);
309
+ }
310
+ async listSuperRepresentatives() {
311
+ const { witnesses = [] } = await this.tronWeb.fullNode.request('wallet/listwitnesses');
312
+ return witnesses;
313
+ }
314
+ async listTokens(limit = 0, offset = 0) {
315
+ if (!index_js_1.default.isInteger(limit) || limit < 0 || (offset && limit < 1)) {
316
+ throw new Error('Invalid limit provided');
317
+ }
318
+ if (!index_js_1.default.isInteger(offset) || offset < 0) {
319
+ throw new Error('Invalid offset provided');
320
+ }
321
+ if (!limit) {
322
+ return this.tronWeb.fullNode
323
+ .request('wallet/getassetissuelist')
324
+ .then(({ assetIssue = [] }) => assetIssue.map((token) => this._parseToken(token)));
325
+ }
326
+ return this.tronWeb.fullNode
327
+ .request('wallet/getpaginatedassetissuelist', {
328
+ offset: parseInt(offset),
329
+ limit: parseInt(limit),
330
+ }, 'post')
331
+ .then(({ assetIssue = [] }) => assetIssue.map((token) => this._parseToken(token)));
332
+ }
333
+ async timeUntilNextVoteCycle() {
334
+ const { num = -1 } = await this.tronWeb.fullNode.request('wallet/getnextmaintenancetime');
335
+ if (num == -1) {
336
+ throw new Error('Failed to get time until next vote cycle');
337
+ }
338
+ return Math.floor(num / 1000);
339
+ }
340
+ async getContract(contractAddress) {
341
+ if (!this.tronWeb.isAddress(contractAddress)) {
342
+ throw new Error('Invalid contract address provided');
343
+ }
344
+ if (this.cache.contracts[contractAddress]) {
345
+ return this.cache.contracts[contractAddress];
346
+ }
347
+ contractAddress = this.tronWeb.address.toHex(contractAddress);
348
+ const contract = await this.tronWeb.fullNode.request('wallet/getcontract', {
349
+ value: contractAddress,
350
+ });
351
+ if (contract.Error) {
352
+ throw new Error('Contract does not exist');
353
+ }
354
+ this.cache.contracts[contractAddress] = contract;
355
+ return contract;
356
+ }
357
+ ecRecover(transaction) {
358
+ return Trx.ecRecover(transaction);
359
+ }
360
+ static ecRecover(transaction) {
361
+ if (!(0, transaction_js_1.txCheck)(transaction)) {
362
+ throw new Error('Invalid transaction');
363
+ }
364
+ if (!transaction.signature?.length) {
365
+ throw new Error('Transaction is not signed');
366
+ }
367
+ if (transaction.signature.length === 1) {
368
+ const tronAddress = (0, crypto_js_1.ecRecover)(transaction.txID, transaction.signature[0]);
369
+ return tronweb_js_1.TronWeb.address.fromHex(tronAddress);
370
+ }
371
+ return transaction.signature.map((sig) => {
372
+ const tronAddress = (0, crypto_js_1.ecRecover)(transaction.txID, sig);
373
+ return tronweb_js_1.TronWeb.address.fromHex(tronAddress);
374
+ });
375
+ }
376
+ async verifyMessage(message, signature, address = this.tronWeb.defaultAddress.base58, useTronHeader = true) {
377
+ if (!index_js_1.default.isHex(message)) {
378
+ throw new Error('Expected hex message input');
379
+ }
380
+ if (Trx.verifySignature(message, address, signature, useTronHeader)) {
381
+ return true;
382
+ }
383
+ throw new Error('Signature does not match');
384
+ }
385
+ static verifySignature(message, address, signature, useTronHeader = true) {
386
+ message = message.replace(/^0x/, '');
387
+ const messageBytes = [
388
+ ...(0, ethersUtils_js_1.toUtf8Bytes)(useTronHeader ? TRX_MESSAGE_HEADER : ETH_MESSAGE_HEADER),
389
+ ...index_js_1.default.code.hexStr2byteArray(message),
390
+ ];
391
+ const messageDigest = (0, ethersUtils_js_1.keccak256)(new Uint8Array(messageBytes));
392
+ const recovered = (0, ethersUtils_js_1.recoverAddress)(messageDigest, ethersUtils_js_1.Signature.from(`0x${signature.replace(/^0x/, '')}`));
393
+ const tronAddress = address_js_1.ADDRESS_PREFIX + recovered.substr(2);
394
+ const base58Address = tronweb_js_1.TronWeb.address.fromHex(tronAddress);
395
+ return base58Address == tronweb_js_1.TronWeb.address.fromHex(address);
396
+ }
397
+ async verifyMessageV2(message, signature) {
398
+ return Trx.verifyMessageV2(message, signature);
399
+ }
400
+ static verifyMessageV2(message, signature) {
401
+ return index_js_1.default.message.verifyMessage(message, signature);
402
+ }
403
+ verifyTypedData(domain, types, value, signature, address = this.tronWeb.defaultAddress.base58) {
404
+ if (Trx.verifyTypedData(domain, types, value, signature, address))
405
+ return true;
406
+ throw new Error('Signature does not match');
407
+ }
408
+ static verifyTypedData(domain, types, value, signature, address) {
409
+ const messageDigest = index_js_1.default._TypedDataEncoder.hash(domain, types, value);
410
+ const recovered = (0, ethersUtils_js_1.recoverAddress)(messageDigest, ethersUtils_js_1.Signature.from(`0x${signature.replace(/^0x/, '')}`));
411
+ const tronAddress = address_js_1.ADDRESS_PREFIX + recovered.substr(2);
412
+ const base58Address = tronweb_js_1.TronWeb.address.fromHex(tronAddress);
413
+ return base58Address == tronweb_js_1.TronWeb.address.fromHex(address);
414
+ }
415
+ async sign(transaction, privateKey = this.tronWeb.defaultPrivateKey, useTronHeader = true, multisig = false) {
416
+ // Message signing
417
+ if (index_js_1.default.isString(transaction)) {
418
+ if (!index_js_1.default.isHex(transaction)) {
419
+ throw new Error('Expected hex message input');
420
+ }
421
+ return Trx.signString(transaction, privateKey, useTronHeader);
422
+ }
423
+ if (!index_js_1.default.isObject(transaction)) {
424
+ throw new Error('Invalid transaction provided');
425
+ }
426
+ if (!multisig && transaction.signature) {
427
+ throw new Error('Transaction is already signed');
428
+ }
429
+ if (!multisig) {
430
+ const address = this.tronWeb.address
431
+ .toHex(this.tronWeb.address.fromPrivateKey(privateKey))
432
+ .toLowerCase();
433
+ if (address !== this.tronWeb.address.toHex(transaction.raw_data.contract[0].parameter.value.owner_address)) {
434
+ throw new Error('Private key does not match address in transaction');
435
+ }
436
+ if (!(0, transaction_js_1.txCheck)(transaction)) {
437
+ throw new Error('Invalid transaction');
438
+ }
439
+ }
440
+ return index_js_1.default.crypto.signTransaction(privateKey, transaction);
441
+ }
442
+ static signString(message, privateKey, useTronHeader = true) {
443
+ message = message.replace(/^0x/, '');
444
+ const value = `0x${privateKey.replace(/^0x/, '')}`;
445
+ const signingKey = new ethersUtils_js_1.SigningKey(value);
446
+ const messageBytes = [
447
+ ...(0, ethersUtils_js_1.toUtf8Bytes)(useTronHeader ? TRX_MESSAGE_HEADER : ETH_MESSAGE_HEADER),
448
+ ...index_js_1.default.code.hexStr2byteArray(message),
449
+ ];
450
+ const messageDigest = (0, ethersUtils_js_1.keccak256)(new Uint8Array(messageBytes));
451
+ const signature = signingKey.sign(messageDigest);
452
+ const signatureHex = ['0x', signature.r.substring(2), signature.s.substring(2), Number(signature.v).toString(16)].join('');
453
+ return signatureHex;
454
+ }
455
+ /**
456
+ * sign message v2 for verified header length
457
+ *
458
+ * @param {message to be signed, should be Bytes or string} message
459
+ * @param {privateKey for signature} privateKey
460
+ * @param {reserved} options
461
+ */
462
+ signMessageV2(message, privateKey = this.tronWeb.defaultPrivateKey) {
463
+ return Trx.signMessageV2(message, privateKey);
464
+ }
465
+ static signMessageV2(message, privateKey) {
466
+ return index_js_1.default.message.signMessage(message, privateKey);
467
+ }
468
+ _signTypedData(domain, types, value, privateKey = this.tronWeb.defaultPrivateKey) {
469
+ return Trx._signTypedData(domain, types, value, privateKey);
470
+ }
471
+ static _signTypedData(domain, types, value, privateKey) {
472
+ return index_js_1.default.crypto._signTypedData(domain, types, value, privateKey);
473
+ }
474
+ async multiSign(transaction, privateKey = this.tronWeb.defaultPrivateKey, permissionId = 0) {
475
+ if (!index_js_1.default.isObject(transaction) || !transaction.raw_data || !transaction.raw_data.contract) {
476
+ throw new Error('Invalid transaction provided');
477
+ }
478
+ // If owner permission or permission id exists in transaction, do sign directly
479
+ // If no permission id inside transaction or user passes permission id, use old way to reset permission id
480
+ if (!transaction.raw_data.contract[0].Permission_id && permissionId > 0) {
481
+ // set permission id
482
+ transaction.raw_data.contract[0].Permission_id = permissionId;
483
+ // check if private key insides permission list
484
+ const address = this.tronWeb.address
485
+ .toHex(this.tronWeb.address.fromPrivateKey(privateKey))
486
+ .toLowerCase();
487
+ const signWeight = await this.getSignWeight(transaction, permissionId);
488
+ if (signWeight.result.code === 'PERMISSION_ERROR') {
489
+ throw new Error(signWeight.result.message);
490
+ }
491
+ let foundKey = false;
492
+ signWeight.permission.keys.map((key) => {
493
+ if (key.address === address)
494
+ foundKey = true;
495
+ });
496
+ if (!foundKey) {
497
+ throw new Error(privateKey + ' has no permission to sign');
498
+ }
499
+ if (signWeight.approved_list && signWeight.approved_list.indexOf(address) != -1) {
500
+ throw new Error(privateKey + ' already sign transaction');
501
+ }
502
+ // reset transaction
503
+ if (signWeight.transaction && signWeight.transaction.transaction) {
504
+ transaction = signWeight.transaction.transaction;
505
+ if (permissionId > 0) {
506
+ transaction.raw_data.contract[0].Permission_id = permissionId;
507
+ }
508
+ }
509
+ else {
510
+ throw new Error('Invalid transaction provided');
511
+ }
512
+ }
513
+ // sign
514
+ if (!(0, transaction_js_1.txCheck)(transaction)) {
515
+ throw new Error('Invalid transaction');
516
+ }
517
+ return index_js_1.default.crypto.signTransaction(privateKey, transaction);
518
+ }
519
+ async getApprovedList(transaction) {
520
+ if (!index_js_1.default.isObject(transaction)) {
521
+ throw new Error('Invalid transaction provided');
522
+ }
523
+ return this.tronWeb.fullNode.request('wallet/getapprovedlist', transaction, 'post');
524
+ }
525
+ async getSignWeight(transaction, permissionId) {
526
+ if (!index_js_1.default.isObject(transaction) || !transaction.raw_data || !transaction.raw_data.contract)
527
+ throw new Error('Invalid transaction provided');
528
+ if (index_js_1.default.isInteger(permissionId)) {
529
+ transaction.raw_data.contract[0].Permission_id = parseInt(permissionId);
530
+ }
531
+ else if (typeof transaction.raw_data.contract[0].Permission_id !== 'number') {
532
+ transaction.raw_data.contract[0].Permission_id = 0;
533
+ }
534
+ return this.tronWeb.fullNode.request('wallet/getsignweight', transaction, 'post');
535
+ }
536
+ async sendRawTransaction(signedTransaction) {
537
+ if (!index_js_1.default.isObject(signedTransaction)) {
538
+ throw new Error('Invalid transaction provided');
539
+ }
540
+ if (!signedTransaction.signature || !index_js_1.default.isArray(signedTransaction.signature)) {
541
+ throw new Error('Transaction is not signed');
542
+ }
543
+ const result = await this.tronWeb.fullNode.request('wallet/broadcasttransaction', signedTransaction, 'post');
544
+ return {
545
+ ...result,
546
+ transaction: signedTransaction,
547
+ };
548
+ }
549
+ async sendHexTransaction(signedHexTransaction) {
550
+ if (!index_js_1.default.isHex(signedHexTransaction)) {
551
+ throw new Error('Invalid hex transaction provided');
552
+ }
553
+ const params = {
554
+ transaction: signedHexTransaction,
555
+ };
556
+ const result = await this.tronWeb.fullNode.request('wallet/broadcasthex', params, 'post');
557
+ if (result.result) {
558
+ return {
559
+ ...result,
560
+ transaction: JSON.parse(result.transaction),
561
+ hexTransaction: signedHexTransaction,
562
+ };
563
+ }
564
+ return result;
565
+ }
566
+ async sendTransaction(to, amount, options = {}) {
567
+ if (typeof options === 'string')
568
+ options = { privateKey: options };
569
+ if (!this.tronWeb.isAddress(to)) {
570
+ throw new Error('Invalid recipient provided');
571
+ }
572
+ if (!index_js_1.default.isInteger(amount) || amount <= 0) {
573
+ throw new Error('Invalid amount provided');
574
+ }
575
+ options = {
576
+ privateKey: this.tronWeb.defaultPrivateKey,
577
+ address: this.tronWeb.defaultAddress.hex,
578
+ ...options,
579
+ };
580
+ if (!options.privateKey && !options.address) {
581
+ throw new Error('Function requires either a private key or address to be set');
582
+ }
583
+ const address = options.privateKey ? this.tronWeb.address.fromPrivateKey(options.privateKey) : options.address;
584
+ const transaction = await this.tronWeb.transactionBuilder.sendTrx(to, amount, address);
585
+ const signedTransaction = await this.sign(transaction, options.privateKey);
586
+ const result = await this.sendRawTransaction(signedTransaction);
587
+ return result;
588
+ }
589
+ async sendToken(to, amount, tokenID, options = {}) {
590
+ if (typeof options === 'string')
591
+ options = { privateKey: options };
592
+ if (!this.tronWeb.isAddress(to)) {
593
+ throw new Error('Invalid recipient provided');
594
+ }
595
+ if (!index_js_1.default.isInteger(amount) || amount <= 0) {
596
+ throw new Error('Invalid amount provided');
597
+ }
598
+ if (index_js_1.default.isInteger(tokenID))
599
+ tokenID = tokenID.toString();
600
+ if (!index_js_1.default.isString(tokenID)) {
601
+ throw new Error('Invalid token ID provided');
602
+ }
603
+ options = {
604
+ privateKey: this.tronWeb.defaultPrivateKey,
605
+ address: this.tronWeb.defaultAddress.hex,
606
+ ...options,
607
+ };
608
+ if (!options.privateKey && !options.address) {
609
+ throw new Error('Function requires either a private key or address to be set');
610
+ }
611
+ const address = options.privateKey ? this.tronWeb.address.fromPrivateKey(options.privateKey) : options.address;
612
+ const transaction = await this.tronWeb.transactionBuilder.sendToken(to, amount, tokenID, address);
613
+ const signedTransaction = await this.sign(transaction, options.privateKey);
614
+ const result = await this.sendRawTransaction(signedTransaction);
615
+ return result;
616
+ }
617
+ /**
618
+ * Freezes an amount of TRX.
619
+ * Will give bandwidth OR Energy and TRON Power(voting rights)
620
+ * to the owner of the frozen tokens.
621
+ *
622
+ * @param amount - is the number of frozen trx
623
+ * @param duration - is the duration in days to be frozen
624
+ * @param resource - is the type, must be either "ENERGY" or "BANDWIDTH"
625
+ * @param options
626
+ */
627
+ async freezeBalance(amount = 0, duration = 3, resource = 'BANDWIDTH', options = {}, receiverAddress) {
628
+ if (typeof options === 'string')
629
+ options = { privateKey: options };
630
+ if (!['BANDWIDTH', 'ENERGY'].includes(resource)) {
631
+ throw new Error('Invalid resource provided: Expected "BANDWIDTH" or "ENERGY"');
632
+ }
633
+ if (!index_js_1.default.isInteger(amount) || amount <= 0) {
634
+ throw new Error('Invalid amount provided');
635
+ }
636
+ if (!index_js_1.default.isInteger(duration) || duration < 3) {
637
+ throw new Error('Invalid duration provided, minimum of 3 days');
638
+ }
639
+ options = {
640
+ privateKey: this.tronWeb.defaultPrivateKey,
641
+ address: this.tronWeb.defaultAddress.hex,
642
+ ...options,
643
+ };
644
+ if (!options.privateKey && !options.address) {
645
+ throw new Error('Function requires either a private key or address to be set');
646
+ }
647
+ const address = options.privateKey ? this.tronWeb.address.fromPrivateKey(options.privateKey) : options.address;
648
+ const freezeBalance = await this.tronWeb.transactionBuilder.freezeBalance(amount, duration, resource, address, receiverAddress);
649
+ const signedTransaction = await this.sign(freezeBalance, options.privateKey);
650
+ const result = await this.sendRawTransaction(signedTransaction);
651
+ return result;
652
+ }
653
+ /**
654
+ * Unfreeze TRX that has passed the minimum freeze duration.
655
+ * Unfreezing will remove bandwidth and TRON Power.
656
+ *
657
+ * @param resource - is the type, must be either "ENERGY" or "BANDWIDTH"
658
+ * @param options
659
+ */
660
+ async unfreezeBalance(resource = 'BANDWIDTH', options = {}, receiverAddress) {
661
+ if (typeof options === 'string')
662
+ options = { privateKey: options };
663
+ if (!['BANDWIDTH', 'ENERGY'].includes(resource)) {
664
+ throw new Error('Invalid resource provided: Expected "BANDWIDTH" or "ENERGY"');
665
+ }
666
+ options = {
667
+ privateKey: this.tronWeb.defaultPrivateKey,
668
+ address: this.tronWeb.defaultAddress.hex,
669
+ ...options,
670
+ };
671
+ if (!options.privateKey && !options.address) {
672
+ throw new Error('Function requires either a private key or address to be set');
673
+ }
674
+ const address = options.privateKey ? this.tronWeb.address.fromPrivateKey(options.privateKey) : options.address;
675
+ const unfreezeBalance = await this.tronWeb.transactionBuilder.unfreezeBalance(resource, address, receiverAddress);
676
+ const signedTransaction = await this.sign(unfreezeBalance, options.privateKey);
677
+ const result = await this.sendRawTransaction(signedTransaction);
678
+ return result;
679
+ }
680
+ /**
681
+ * Modify account name
682
+ * Note: Username is allowed to edit only once.
683
+ *
684
+ * @param privateKey - Account private Key
685
+ * @param accountName - name of the account
686
+ *
687
+ * @return modified Transaction Object
688
+ */
689
+ async updateAccount(accountName, options = {}) {
690
+ if (typeof options === 'string')
691
+ options = { privateKey: options };
692
+ if (!index_js_1.default.isString(accountName) || !accountName.length) {
693
+ throw new Error('Name must be a string');
694
+ }
695
+ options = {
696
+ privateKey: this.tronWeb.defaultPrivateKey,
697
+ address: this.tronWeb.defaultAddress.hex,
698
+ ...options,
699
+ };
700
+ if (!options.privateKey && !options.address)
701
+ throw Error('Function requires either a private key or address to be set');
702
+ const address = options.privateKey ? this.tronWeb.address.fromPrivateKey(options.privateKey) : options.address;
703
+ const updateAccount = await this.tronWeb.transactionBuilder.updateAccount(accountName, address);
704
+ const signedTransaction = await this.sign(updateAccount, options.privateKey);
705
+ const result = await this.sendRawTransaction(signedTransaction);
706
+ return result;
707
+ }
708
+ /**
709
+ * Gets a network modification proposal by ID.
710
+ */
711
+ async getProposal(proposalID) {
712
+ if (!index_js_1.default.isInteger(proposalID) || proposalID < 0) {
713
+ throw new Error('Invalid proposalID provided');
714
+ }
715
+ return this.tronWeb.fullNode.request('wallet/getproposalbyid', {
716
+ id: parseInt(proposalID),
717
+ }, 'post');
718
+ }
719
+ /**
720
+ * Lists all network modification proposals.
721
+ */
722
+ async listProposals() {
723
+ const { proposals = [] } = await this.tronWeb.fullNode.request('wallet/listproposals', {}, 'post');
724
+ return proposals;
725
+ }
726
+ /**
727
+ * Lists all parameters available for network modification proposals.
728
+ */
729
+ async getChainParameters() {
730
+ const { chainParameter = [] } = await this.tronWeb.fullNode.request('wallet/getchainparameters', {}, 'post');
731
+ return chainParameter;
732
+ }
733
+ /**
734
+ * Get the account resources
735
+ */
736
+ async getAccountResources(address = this.tronWeb.defaultAddress.hex) {
737
+ if (!this.tronWeb.isAddress(address)) {
738
+ throw new Error('Invalid address provided');
739
+ }
740
+ return this.tronWeb.fullNode.request('wallet/getaccountresource', {
741
+ address: this.tronWeb.address.toHex(address),
742
+ }, 'post');
743
+ }
744
+ /**
745
+ * Query the amount of resources of a specific resourceType delegated by fromAddress to toAddress
746
+ */
747
+ async getDelegatedResourceV2(fromAddress = this.tronWeb.defaultAddress.hex, toAddress = this.tronWeb.defaultAddress.hex, options = { confirmed: true }) {
748
+ if (!this.tronWeb.isAddress(fromAddress)) {
749
+ throw new Error('Invalid address provided');
750
+ }
751
+ if (!this.tronWeb.isAddress(toAddress)) {
752
+ throw new Error('Invalid address provided');
753
+ }
754
+ return this.tronWeb[options.confirmed ? 'solidityNode' : 'fullNode'].request(`wallet${options.confirmed ? 'solidity' : ''}/getdelegatedresourcev2`, {
755
+ fromAddress: toHex(fromAddress),
756
+ toAddress: toHex(toAddress),
757
+ }, 'post');
758
+ }
759
+ /**
760
+ * Query the resource delegation index by an account
761
+ */
762
+ async getDelegatedResourceAccountIndexV2(address = this.tronWeb.defaultAddress.hex, options = { confirmed: true }) {
763
+ if (!this.tronWeb.isAddress(address)) {
764
+ throw new Error('Invalid address provided');
765
+ }
766
+ return this.tronWeb[options.confirmed ? 'solidityNode' : 'fullNode'].request(`wallet${options.confirmed ? 'solidity' : ''}/getdelegatedresourceaccountindexv2`, {
767
+ value: toHex(address),
768
+ }, 'post');
769
+ }
770
+ /**
771
+ * Query the amount of delegatable resources of the specified resource Type for target address, unit is sun.
772
+ */
773
+ async getCanDelegatedMaxSize(address = this.tronWeb.defaultAddress.hex, resource = 'BANDWIDTH', options = { confirmed: true }) {
774
+ if (!this.tronWeb.isAddress(address)) {
775
+ throw new Error('Invalid address provided');
776
+ }
777
+ this.validator.notValid([
778
+ {
779
+ name: 'resource',
780
+ type: 'resource',
781
+ value: resource,
782
+ msg: 'Invalid resource provided: Expected "BANDWIDTH" or "ENERGY"',
783
+ },
784
+ ]);
785
+ return this.tronWeb[options.confirmed ? 'solidityNode' : 'fullNode'].request(`wallet${options.confirmed ? 'solidity' : ''}/getcandelegatedmaxsize`, {
786
+ owner_address: toHex(address),
787
+ type: resource === 'ENERGY' ? 1 : 0,
788
+ }, 'post');
789
+ }
790
+ /**
791
+ * Remaining times of available unstaking API
792
+ */
793
+ async getAvailableUnfreezeCount(address = this.tronWeb.defaultAddress.hex, options = { confirmed: true }) {
794
+ if (!this.tronWeb.isAddress(address)) {
795
+ throw new Error('Invalid address provided');
796
+ }
797
+ return this.tronWeb[options.confirmed ? 'solidityNode' : 'fullNode'].request(`wallet${options.confirmed ? 'solidity' : ''}/getavailableunfreezecount`, {
798
+ owner_address: toHex(address),
799
+ }, 'post');
800
+ }
801
+ /**
802
+ * Query the withdrawable balance at the specified timestamp
803
+ */
804
+ async getCanWithdrawUnfreezeAmount(address = this.tronWeb.defaultAddress.hex, timestamp = Date.now(), options = { confirmed: true }) {
805
+ if (!this.tronWeb.isAddress(address)) {
806
+ throw new Error('Invalid address provided');
807
+ }
808
+ if (!index_js_1.default.isInteger(timestamp) || timestamp < 0) {
809
+ throw new Error('Invalid timestamp provided');
810
+ }
811
+ return this.tronWeb[options.confirmed ? 'solidityNode' : 'fullNode'].request(`wallet${options.confirmed ? 'solidity' : ''}/getcanwithdrawunfreezeamount`, {
812
+ owner_address: toHex(address),
813
+ timestamp: timestamp,
814
+ }, 'post');
815
+ }
816
+ /**
817
+ * Get the exchange ID.
818
+ */
819
+ async getExchangeByID(exchangeID) {
820
+ if (!index_js_1.default.isInteger(exchangeID) || exchangeID < 0) {
821
+ throw new Error('Invalid exchangeID provided');
822
+ }
823
+ return this.tronWeb.fullNode.request('wallet/getexchangebyid', {
824
+ id: exchangeID,
825
+ }, 'post');
826
+ }
827
+ /**
828
+ * Lists the exchanges
829
+ */
830
+ async listExchanges() {
831
+ return this.tronWeb.fullNode
832
+ .request('wallet/listexchanges', {}, 'post')
833
+ .then(({ exchanges = [] }) => exchanges);
834
+ }
835
+ /**
836
+ * Lists all network modification proposals.
837
+ */
838
+ async listExchangesPaginated(limit = 10, offset = 0) {
839
+ return this.tronWeb.fullNode
840
+ .request('wallet/getpaginatedexchangelist', {
841
+ limit,
842
+ offset,
843
+ }, 'post')
844
+ .then(({ exchanges = [] }) => exchanges);
845
+ }
846
+ /**
847
+ * Get info about thre node
848
+ */
849
+ async getNodeInfo() {
850
+ return this.tronWeb.fullNode.request('wallet/getnodeinfo', {}, 'post');
851
+ }
852
+ async getTokenListByName(tokenID) {
853
+ if (index_js_1.default.isInteger(tokenID))
854
+ tokenID = tokenID.toString();
855
+ if (!index_js_1.default.isString(tokenID) || !tokenID.length) {
856
+ throw new Error('Invalid token ID provided');
857
+ }
858
+ return this.tronWeb.fullNode
859
+ .request('wallet/getassetissuelistbyname', {
860
+ value: this.tronWeb.fromUtf8(tokenID),
861
+ }, 'post')
862
+ .then((token) => {
863
+ if (Array.isArray(token.assetIssue)) {
864
+ return token.assetIssue.map((t) => this._parseToken(t));
865
+ }
866
+ else if (!token.name) {
867
+ throw new Error('Token does not exist');
868
+ }
869
+ return this._parseToken(token);
870
+ });
871
+ }
872
+ getTokenByID(tokenID) {
873
+ if (index_js_1.default.isInteger(tokenID))
874
+ tokenID = tokenID.toString();
875
+ if (!index_js_1.default.isString(tokenID) || !tokenID.length) {
876
+ throw new Error('Invalid token ID provided');
877
+ }
878
+ return this.tronWeb.fullNode
879
+ .request('wallet/getassetissuebyid', {
880
+ value: tokenID,
881
+ }, 'post')
882
+ .then((token) => {
883
+ if (!token.name) {
884
+ throw new Error('Token does not exist');
885
+ }
886
+ return this._parseToken(token);
887
+ });
888
+ }
889
+ async getReward(address, options = {}) {
890
+ options.confirmed = true;
891
+ return this._getReward(address, options);
892
+ }
893
+ async getUnconfirmedReward(address, options = {}) {
894
+ options.confirmed = false;
895
+ return this._getReward(address, options);
896
+ }
897
+ async getBrokerage(address, options = {}) {
898
+ options.confirmed = true;
899
+ return this._getBrokerage(address, options);
900
+ }
901
+ async getUnconfirmedBrokerage(address, options = {}) {
902
+ options.confirmed = false;
903
+ return this._getBrokerage(address, options);
904
+ }
905
+ async _getReward(address = this.tronWeb.defaultAddress.hex, options) {
906
+ this.validator.notValid([
907
+ {
908
+ name: 'origin',
909
+ type: 'address',
910
+ value: address,
911
+ },
912
+ ]);
913
+ const data = {
914
+ address: toHex(address),
915
+ };
916
+ return this.tronWeb[options.confirmed ? 'solidityNode' : 'fullNode']
917
+ .request(`wallet${options.confirmed ? 'solidity' : ''}/getReward`, data, 'post')
918
+ .then((result = { reward: undefined }) => {
919
+ if (typeof result.reward === 'undefined') {
920
+ throw new Error('Not found.');
921
+ }
922
+ return result.reward;
923
+ });
924
+ }
925
+ async _getBrokerage(address = this.tronWeb.defaultAddress.hex, options) {
926
+ this.validator.notValid([
927
+ {
928
+ name: 'origin',
929
+ type: 'address',
930
+ value: address,
931
+ },
932
+ ]);
933
+ const data = {
934
+ address: toHex(address),
935
+ };
936
+ return this.tronWeb[options.confirmed ? 'solidityNode' : 'fullNode']
937
+ .request(`wallet${options.confirmed ? 'solidity' : ''}/getBrokerage`, data, 'post')
938
+ .then((result = {}) => {
939
+ if (typeof result.brokerage === 'undefined') {
940
+ throw new Error('Not found.');
941
+ }
942
+ return result.brokerage;
943
+ });
944
+ }
945
+ async getBandwidthPrices() {
946
+ return this.tronWeb.fullNode.request('wallet/getbandwidthprices', {}, 'post')
947
+ .then((result = {}) => {
948
+ if (typeof result.prices === 'undefined') {
949
+ throw new Error('Not found.');
950
+ }
951
+ return result.prices;
952
+ });
953
+ }
954
+ async getEnergyPrices() {
955
+ return this.tronWeb.fullNode.request('wallet/getenergyprices', {}, 'post')
956
+ .then((result = {}) => {
957
+ if (typeof result.prices === 'undefined') {
958
+ throw new Error('Not found.');
959
+ }
960
+ return result.prices;
961
+ });
962
+ }
963
+ }
964
+ exports.Trx = Trx;
965
+ //# sourceMappingURL=trx.js.map