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