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,1771 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.TransactionBuilder = void 0;
4
+ const tronweb_js_1 = require("../../tronweb.js");
5
+ const ethersUtils_js_1 = require("../../utils/ethersUtils.js");
6
+ const address_js_1 = require("../../utils/address.js");
7
+ const abi_js_1 = require("../../utils/abi.js");
8
+ const index_js_1 = require("../../paramValidator/index.js");
9
+ const validations_js_1 = require("../../utils/validations.js");
10
+ const Contract_js_1 = require("../../types/Contract.js");
11
+ const helper_js_1 = require("./helper.js");
12
+ class TransactionBuilder {
13
+ tronWeb;
14
+ validator;
15
+ constructor(tronWeb) {
16
+ if (!tronWeb || !(tronWeb instanceof tronweb_js_1.TronWeb)) {
17
+ throw new Error('Expected instance of TronWeb');
18
+ }
19
+ this.tronWeb = tronWeb;
20
+ this.validator = new index_js_1.Validator();
21
+ }
22
+ async sendTrx(to, amount = 0, from = this.tronWeb.defaultAddress.hex, options = {}) {
23
+ // accept amounts passed as strings
24
+ amount = parseInt(amount);
25
+ this.validator.notValid([
26
+ {
27
+ name: 'recipient',
28
+ type: 'address',
29
+ value: to,
30
+ },
31
+ {
32
+ name: 'origin',
33
+ type: 'address',
34
+ value: from,
35
+ },
36
+ {
37
+ names: ['recipient', 'origin'],
38
+ type: 'notEqual',
39
+ msg: 'Cannot transfer TRX to the same account',
40
+ },
41
+ {
42
+ name: 'amount',
43
+ type: 'integer',
44
+ gt: 0,
45
+ value: amount,
46
+ },
47
+ ]);
48
+ const data = {
49
+ to_address: (0, address_js_1.toHex)(to),
50
+ owner_address: (0, address_js_1.toHex)(from),
51
+ amount: amount,
52
+ };
53
+ const transactionOptions = (0, helper_js_1.getTransactionOptions)(options);
54
+ return (0, helper_js_1.createTransaction)(this.tronWeb, Contract_js_1.ContractType.TransferContract, data, options?.permissionId, transactionOptions);
55
+ }
56
+ async sendToken(to, amount = 0, tokenId, from = this.tronWeb.defaultAddress.hex, options = {}) {
57
+ amount = parseInt(amount);
58
+ this.validator.notValid([
59
+ {
60
+ name: 'recipient',
61
+ type: 'address',
62
+ value: to,
63
+ },
64
+ {
65
+ name: 'origin',
66
+ type: 'address',
67
+ value: from,
68
+ },
69
+ {
70
+ names: ['recipient', 'origin'],
71
+ type: 'notEqual',
72
+ msg: 'Cannot transfer tokens to the same account',
73
+ },
74
+ {
75
+ name: 'amount',
76
+ type: 'integer',
77
+ gt: 0,
78
+ value: amount,
79
+ },
80
+ {
81
+ name: 'token ID',
82
+ type: 'tokenId',
83
+ value: tokenId,
84
+ },
85
+ ]);
86
+ const data = {
87
+ to_address: (0, address_js_1.toHex)(to),
88
+ owner_address: (0, address_js_1.toHex)(from),
89
+ asset_name: (0, helper_js_1.fromUtf8)(tokenId),
90
+ amount,
91
+ };
92
+ const transactionOptions = (0, helper_js_1.getTransactionOptions)(options);
93
+ return (0, helper_js_1.createTransaction)(this.tronWeb, Contract_js_1.ContractType.TransferAssetContract, data, options?.permissionId, transactionOptions);
94
+ }
95
+ async purchaseToken(issuerAddress, tokenId, amount = 0, buyer = this.tronWeb.defaultAddress.hex, options = {}) {
96
+ this.validator.notValid([
97
+ {
98
+ name: 'buyer',
99
+ type: 'address',
100
+ value: buyer,
101
+ },
102
+ {
103
+ name: 'issuer',
104
+ type: 'address',
105
+ value: issuerAddress,
106
+ },
107
+ {
108
+ names: ['buyer', 'issuer'],
109
+ type: 'notEqual',
110
+ msg: 'Cannot purchase tokens from same account',
111
+ },
112
+ {
113
+ name: 'amount',
114
+ type: 'integer',
115
+ gt: 0,
116
+ value: amount,
117
+ },
118
+ {
119
+ name: 'token ID',
120
+ type: 'tokenId',
121
+ value: tokenId,
122
+ },
123
+ ]);
124
+ const data = {
125
+ to_address: (0, address_js_1.toHex)(issuerAddress),
126
+ owner_address: (0, address_js_1.toHex)(buyer),
127
+ asset_name: (0, helper_js_1.fromUtf8)(tokenId),
128
+ amount: parseInt(amount),
129
+ };
130
+ const transactionOptions = (0, helper_js_1.getTransactionOptions)(options);
131
+ return (0, helper_js_1.createTransaction)(this.tronWeb, Contract_js_1.ContractType.ParticipateAssetIssueContract, data, options?.permissionId, transactionOptions);
132
+ }
133
+ async freezeBalance(amount = 0, duration = 3, resource = 'BANDWIDTH', ownerAddress = this.tronWeb.defaultAddress.hex, receiverAddress, options = {}) {
134
+ this.validator.notValid([
135
+ {
136
+ name: 'origin',
137
+ type: 'address',
138
+ value: ownerAddress,
139
+ },
140
+ {
141
+ name: 'receiver',
142
+ type: 'address',
143
+ value: receiverAddress,
144
+ optional: true,
145
+ },
146
+ {
147
+ name: 'amount',
148
+ type: 'integer',
149
+ gt: 0,
150
+ value: amount,
151
+ },
152
+ {
153
+ name: 'duration',
154
+ type: 'integer',
155
+ gte: 3,
156
+ value: duration,
157
+ },
158
+ {
159
+ name: 'resource',
160
+ type: 'resource',
161
+ value: resource,
162
+ msg: 'Invalid resource provided: Expected "BANDWIDTH" or "ENERGY"',
163
+ },
164
+ ]);
165
+ const data = {
166
+ owner_address: (0, address_js_1.toHex)(ownerAddress),
167
+ frozen_balance: parseInt(amount),
168
+ frozen_duration: parseInt(String(duration)),
169
+ };
170
+ if (resource !== 'BANDWIDTH') {
171
+ data.resource = resource;
172
+ }
173
+ if ((0, validations_js_1.isNotNullOrUndefined)(receiverAddress) && (0, address_js_1.toHex)(receiverAddress) !== (0, address_js_1.toHex)(ownerAddress)) {
174
+ data.receiver_address = (0, address_js_1.toHex)(receiverAddress);
175
+ }
176
+ const transactionOptions = (0, helper_js_1.getTransactionOptions)(options);
177
+ return (0, helper_js_1.createTransaction)(this.tronWeb, Contract_js_1.ContractType.FreezeBalanceContract, data, options?.permissionId, transactionOptions);
178
+ }
179
+ async unfreezeBalance(resource = 'BANDWIDTH', address = this.tronWeb.defaultAddress.hex, receiverAddress, options = {}) {
180
+ this.validator.notValid([
181
+ {
182
+ name: 'origin',
183
+ type: 'address',
184
+ value: address,
185
+ },
186
+ {
187
+ name: 'receiver',
188
+ type: 'address',
189
+ value: receiverAddress,
190
+ optional: true,
191
+ },
192
+ {
193
+ name: 'resource',
194
+ type: 'resource',
195
+ value: resource,
196
+ msg: 'Invalid resource provided: Expected "BANDWIDTH" or "ENERGY"',
197
+ },
198
+ ]);
199
+ const data = {
200
+ owner_address: (0, address_js_1.toHex)(address),
201
+ };
202
+ if (resource !== 'BANDWIDTH') {
203
+ data.resource = resource;
204
+ }
205
+ if ((0, validations_js_1.isNotNullOrUndefined)(receiverAddress) && (0, address_js_1.toHex)(receiverAddress) !== (0, address_js_1.toHex)(address)) {
206
+ data.receiver_address = (0, address_js_1.toHex)(receiverAddress);
207
+ }
208
+ const transactionOptions = (0, helper_js_1.getTransactionOptions)(options);
209
+ return (0, helper_js_1.createTransaction)(this.tronWeb, Contract_js_1.ContractType.UnfreezeBalanceContract, data, options?.permissionId, transactionOptions);
210
+ }
211
+ async freezeBalanceV2(amount = 0, resource = 'BANDWIDTH', address = this.tronWeb.defaultAddress.hex, options = {}) {
212
+ this.validator.notValid([
213
+ {
214
+ name: 'origin',
215
+ type: 'address',
216
+ value: address,
217
+ },
218
+ {
219
+ name: 'amount',
220
+ type: 'integer',
221
+ gt: 0,
222
+ value: amount,
223
+ },
224
+ {
225
+ name: 'resource',
226
+ type: 'resource',
227
+ value: resource,
228
+ msg: 'Invalid resource provided: Expected "BANDWIDTH" or "ENERGY"',
229
+ },
230
+ ]);
231
+ const data = {
232
+ owner_address: (0, address_js_1.toHex)(address),
233
+ frozen_balance: parseInt(amount),
234
+ };
235
+ if (resource !== 'BANDWIDTH') {
236
+ data.resource = resource;
237
+ }
238
+ const transactionOptions = (0, helper_js_1.getTransactionOptions)(options);
239
+ return (0, helper_js_1.createTransaction)(this.tronWeb, Contract_js_1.ContractType.FreezeBalanceV2Contract, data, options?.permissionId, transactionOptions);
240
+ }
241
+ async unfreezeBalanceV2(amount = 0, resource = 'BANDWIDTH', address = this.tronWeb.defaultAddress.hex, options = {}) {
242
+ this.validator.notValid([
243
+ {
244
+ name: 'origin',
245
+ type: 'address',
246
+ value: address,
247
+ },
248
+ {
249
+ name: 'amount',
250
+ type: 'integer',
251
+ gt: 0,
252
+ value: amount,
253
+ },
254
+ {
255
+ name: 'resource',
256
+ type: 'resource',
257
+ value: resource,
258
+ msg: 'Invalid resource provided: Expected "BANDWIDTH" or "ENERGY"',
259
+ },
260
+ ]);
261
+ const data = {
262
+ owner_address: (0, address_js_1.toHex)(address),
263
+ unfreeze_balance: parseInt(amount),
264
+ };
265
+ if (resource !== 'BANDWIDTH') {
266
+ data.resource = resource;
267
+ }
268
+ const transactionOptions = (0, helper_js_1.getTransactionOptions)(options);
269
+ return (0, helper_js_1.createTransaction)(this.tronWeb, Contract_js_1.ContractType.UnfreezeBalanceV2Contract, data, options?.permissionId, transactionOptions);
270
+ }
271
+ async cancelUnfreezeBalanceV2(address = this.tronWeb.defaultAddress.hex, options = {}) {
272
+ this.validator.notValid([
273
+ {
274
+ name: 'origin',
275
+ type: 'address',
276
+ value: address,
277
+ },
278
+ ]);
279
+ const data = {
280
+ owner_address: (0, address_js_1.toHex)(address),
281
+ };
282
+ const transactionOptions = (0, helper_js_1.getTransactionOptions)(options);
283
+ return (0, helper_js_1.createTransaction)(this.tronWeb, Contract_js_1.ContractType.CancelAllUnfreezeV2Contract, data, options?.permissionId, transactionOptions);
284
+ }
285
+ async delegateResource(amount = 0, receiverAddress, resource = 'BANDWIDTH', address = this.tronWeb.defaultAddress.hex, lock = false, lockPeriod, options = {}) {
286
+ this.validator.notValid([
287
+ {
288
+ name: 'amount',
289
+ type: 'integer',
290
+ gt: 0,
291
+ value: amount,
292
+ },
293
+ {
294
+ name: 'resource',
295
+ type: 'resource',
296
+ value: resource,
297
+ msg: 'Invalid resource provided: Expected "BANDWIDTH" or "ENERGY"',
298
+ },
299
+ {
300
+ name: 'receiver',
301
+ type: 'address',
302
+ value: receiverAddress,
303
+ },
304
+ {
305
+ name: 'origin',
306
+ type: 'address',
307
+ value: address,
308
+ },
309
+ {
310
+ name: 'lock',
311
+ type: 'boolean',
312
+ value: lock,
313
+ },
314
+ {
315
+ name: 'lock period',
316
+ type: 'integer',
317
+ gte: 0,
318
+ value: lockPeriod,
319
+ optional: true,
320
+ },
321
+ ]);
322
+ if ((0, address_js_1.toHex)(receiverAddress) === (0, address_js_1.toHex)(address)) {
323
+ throw new Error('Receiver address must not be the same as owner address');
324
+ }
325
+ const data = {
326
+ owner_address: (0, address_js_1.toHex)(address),
327
+ receiver_address: (0, address_js_1.toHex)(receiverAddress),
328
+ balance: parseInt(amount),
329
+ };
330
+ if (resource !== 'BANDWIDTH') {
331
+ data.resource = resource;
332
+ }
333
+ if (lock) {
334
+ data.lock = lock;
335
+ if ((0, validations_js_1.isNotNullOrUndefined)(lockPeriod)) {
336
+ data.lock_period = lockPeriod;
337
+ }
338
+ }
339
+ const transactionOptions = (0, helper_js_1.getTransactionOptions)(options);
340
+ return (0, helper_js_1.createTransaction)(this.tronWeb, Contract_js_1.ContractType.DelegateResourceContract, data, options?.permissionId, transactionOptions);
341
+ }
342
+ async undelegateResource(amount = 0, receiverAddress, resource = 'BANDWIDTH', address = this.tronWeb.defaultAddress.hex, options = {}) {
343
+ this.validator.notValid([
344
+ {
345
+ name: 'origin',
346
+ type: 'address',
347
+ value: address,
348
+ },
349
+ {
350
+ name: 'receiver',
351
+ type: 'address',
352
+ value: receiverAddress,
353
+ },
354
+ {
355
+ name: 'amount',
356
+ type: 'integer',
357
+ gt: 0,
358
+ value: amount,
359
+ },
360
+ {
361
+ name: 'resource',
362
+ type: 'resource',
363
+ value: resource,
364
+ msg: 'Invalid resource provided: Expected "BANDWIDTH" or "ENERGY"',
365
+ },
366
+ ]);
367
+ if ((0, address_js_1.toHex)(receiverAddress) === (0, address_js_1.toHex)(address)) {
368
+ throw new Error('Receiver address must not be the same as owner address');
369
+ }
370
+ const data = {
371
+ owner_address: (0, address_js_1.toHex)(address),
372
+ receiver_address: (0, address_js_1.toHex)(receiverAddress),
373
+ balance: parseInt(amount),
374
+ };
375
+ if (resource !== 'BANDWIDTH') {
376
+ data.resource = resource;
377
+ }
378
+ const transactionOptions = (0, helper_js_1.getTransactionOptions)(options);
379
+ return (0, helper_js_1.createTransaction)(this.tronWeb, Contract_js_1.ContractType.UnDelegateResourceContract, data, options?.permissionId, transactionOptions);
380
+ }
381
+ async withdrawExpireUnfreeze(address = this.tronWeb.defaultAddress.hex, options = {}) {
382
+ this.validator.notValid([
383
+ {
384
+ name: 'origin',
385
+ type: 'address',
386
+ value: address,
387
+ },
388
+ ]);
389
+ const data = {
390
+ owner_address: (0, address_js_1.toHex)(address),
391
+ };
392
+ const transactionOptions = (0, helper_js_1.getTransactionOptions)(options);
393
+ return (0, helper_js_1.createTransaction)(this.tronWeb, Contract_js_1.ContractType.WithdrawExpireUnfreezeContract, data, options?.permissionId, transactionOptions);
394
+ }
395
+ async withdrawBlockRewards(address = this.tronWeb.defaultAddress.hex, options = {}) {
396
+ this.validator.notValid([
397
+ {
398
+ name: 'origin',
399
+ type: 'address',
400
+ value: address,
401
+ },
402
+ ]);
403
+ const data = {
404
+ owner_address: (0, address_js_1.toHex)(address),
405
+ };
406
+ const transactionOptions = (0, helper_js_1.getTransactionOptions)(options);
407
+ return (0, helper_js_1.createTransaction)(this.tronWeb, Contract_js_1.ContractType.WithdrawBalanceContract, data, options?.permissionId, transactionOptions);
408
+ }
409
+ async applyForSR(address = this.tronWeb.defaultAddress.hex, url = '', options = {}) {
410
+ this.validator.notValid([
411
+ {
412
+ name: 'origin',
413
+ type: 'address',
414
+ value: address,
415
+ },
416
+ {
417
+ name: 'url',
418
+ type: 'url',
419
+ value: url,
420
+ msg: 'Invalid url provided',
421
+ },
422
+ {
423
+ name: 'url',
424
+ type: 'string',
425
+ value: url,
426
+ lte: 256,
427
+ msg: 'Invalid url provided',
428
+ },
429
+ ]);
430
+ const data = {
431
+ owner_address: (0, address_js_1.toHex)(address),
432
+ url: (0, helper_js_1.fromUtf8)(url),
433
+ };
434
+ const transactionOptions = (0, helper_js_1.getTransactionOptions)(options);
435
+ return (0, helper_js_1.createTransaction)(this.tronWeb, Contract_js_1.ContractType.WitnessCreateContract, data, options?.permissionId, transactionOptions);
436
+ }
437
+ async vote(votes = {}, voterAddress = this.tronWeb.defaultAddress.hex, options = {}) {
438
+ this.validator.notValid([
439
+ {
440
+ name: 'voter',
441
+ type: 'address',
442
+ value: voterAddress,
443
+ },
444
+ {
445
+ name: 'votes',
446
+ type: 'notEmptyObject',
447
+ value: votes,
448
+ },
449
+ ]);
450
+ const entries = Object.entries(votes);
451
+ for (const [srAddress, voteCount] of entries) {
452
+ this.validator.notValid([
453
+ {
454
+ name: 'SR',
455
+ type: 'address',
456
+ value: srAddress,
457
+ },
458
+ {
459
+ name: 'vote count',
460
+ type: 'integer',
461
+ gt: 0,
462
+ value: voteCount,
463
+ msg: 'Invalid vote count provided for SR: ' + srAddress,
464
+ },
465
+ ]);
466
+ }
467
+ const voteList = entries.map(([srAddress, voteCount]) => {
468
+ return {
469
+ vote_address: (0, address_js_1.toHex)(srAddress),
470
+ vote_count: parseInt(voteCount),
471
+ };
472
+ });
473
+ const data = {
474
+ owner_address: (0, address_js_1.toHex)(voterAddress),
475
+ votes: voteList,
476
+ };
477
+ const transactionOptions = (0, helper_js_1.getTransactionOptions)(options);
478
+ return (0, helper_js_1.createTransaction)(this.tronWeb, Contract_js_1.ContractType.VoteWitnessContract, data, options?.permissionId, transactionOptions);
479
+ }
480
+ async createSmartContract(options = {}, issuerAddress = this.tronWeb.defaultAddress.hex) {
481
+ const feeLimit = options.feeLimit || this.tronWeb.feeLimit;
482
+ let userFeePercentage = options.userFeePercentage;
483
+ if (typeof userFeePercentage !== 'number' && !userFeePercentage) {
484
+ userFeePercentage = 100;
485
+ }
486
+ const originEnergyLimit = options.originEnergyLimit || 10_000_000;
487
+ const callValue = options.callValue || 0;
488
+ const tokenValue = options.tokenValue;
489
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
490
+ // @ts-ignore
491
+ const tokenId = options.tokenId || options.token_id;
492
+ let { abi } = options;
493
+ const { parameters = [] } = options;
494
+ let parameter = '';
495
+ const { bytecode = false, name = '' } = options;
496
+ if (abi && (0, validations_js_1.isString)(abi)) {
497
+ try {
498
+ abi = JSON.parse(abi);
499
+ }
500
+ catch {
501
+ throw new Error('Invalid options.abi provided');
502
+ }
503
+ }
504
+ const newAbi = abi;
505
+ let entries = newAbi;
506
+ if (newAbi.entrys) {
507
+ entries = newAbi.entrys;
508
+ }
509
+ if (!(0, validations_js_1.isArray)(entries))
510
+ throw new Error('Invalid options.abi provided');
511
+ const payable = entries.some((func) => {
512
+ return func.type === 'constructor' && 'payable' === func.stateMutability.toLowerCase();
513
+ });
514
+ this.validator.notValid([
515
+ {
516
+ name: 'bytecode',
517
+ type: 'hex',
518
+ value: bytecode,
519
+ },
520
+ {
521
+ name: 'feeLimit',
522
+ type: 'integer',
523
+ value: feeLimit,
524
+ gt: 0,
525
+ },
526
+ {
527
+ name: 'callValue',
528
+ type: 'integer',
529
+ value: callValue,
530
+ gte: 0,
531
+ },
532
+ {
533
+ name: 'userFeePercentage',
534
+ type: 'integer',
535
+ value: userFeePercentage,
536
+ gte: 0,
537
+ lte: 100,
538
+ },
539
+ {
540
+ name: 'originEnergyLimit',
541
+ type: 'integer',
542
+ value: originEnergyLimit,
543
+ gte: 0,
544
+ lte: 10_000_000,
545
+ },
546
+ {
547
+ name: 'parameters',
548
+ type: 'array',
549
+ value: parameters,
550
+ },
551
+ {
552
+ name: 'issuer',
553
+ type: 'address',
554
+ value: issuerAddress,
555
+ },
556
+ {
557
+ name: 'tokenValue',
558
+ type: 'integer',
559
+ value: tokenValue,
560
+ gte: 0,
561
+ optional: true,
562
+ },
563
+ {
564
+ name: 'tokenId',
565
+ type: 'integer',
566
+ value: tokenId,
567
+ gte: 0,
568
+ optional: true,
569
+ },
570
+ ]);
571
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
572
+ // @ts-ignore
573
+ if (!payable && (callValue > 0 || tokenValue > 0))
574
+ throw new Error('When contract is not payable, options.callValue and options.tokenValue must be 0');
575
+ const { rawParameter, funcABIV2, parametersV2 } = options;
576
+ if (rawParameter && (0, validations_js_1.isString)(rawParameter)) {
577
+ parameter = rawParameter.replace(/^(0x)/, '');
578
+ }
579
+ else if (funcABIV2) {
580
+ parameter = (0, abi_js_1.encodeParamsV2ByABI)(funcABIV2, parametersV2).replace(/^(0x)/, '');
581
+ }
582
+ else {
583
+ let constructorParams = entries.find((it) => {
584
+ return it.type === 'constructor';
585
+ });
586
+ if (typeof constructorParams !== 'undefined' && constructorParams) {
587
+ const abiCoder = new ethersUtils_js_1.AbiCoder();
588
+ const types = [];
589
+ const values = [];
590
+ constructorParams = constructorParams.inputs;
591
+ if (parameters.length != constructorParams.length)
592
+ throw new Error(`constructor needs ${constructorParams.length} but ${parameters.length} provided`);
593
+ for (let i = 0; i < parameters.length; i++) {
594
+ let type = constructorParams[i].type;
595
+ let value = parameters[i];
596
+ if (!type || !(0, validations_js_1.isString)(type) || !type.length)
597
+ throw new Error('Invalid parameter type provided: ' + type);
598
+ const replaceAddressPrefix = (value) => {
599
+ if ((0, validations_js_1.isArray)(value)) {
600
+ return value.map((v) => replaceAddressPrefix(v));
601
+ }
602
+ return (0, address_js_1.toHex)(value).replace(address_js_1.ADDRESS_PREFIX_REGEX, '0x');
603
+ };
604
+ if (type === 'address')
605
+ value = replaceAddressPrefix(value);
606
+ else if (type.match(/^([^\x5b]*)(\x5b|$)/)?.[0] === 'address[')
607
+ value = replaceAddressPrefix(value);
608
+ else if (/trcToken/.test(type)) {
609
+ type = type.replace(/trcToken/, 'uint256');
610
+ }
611
+ types.push(type);
612
+ values.push(value);
613
+ }
614
+ try {
615
+ parameter = abiCoder.encode(types, values).replace(/^(0x)/, '');
616
+ }
617
+ catch (ex) {
618
+ throw new Error(ex);
619
+ }
620
+ }
621
+ else {
622
+ parameter = '';
623
+ }
624
+ }
625
+ const args = {
626
+ owner_address: (0, address_js_1.toHex)(issuerAddress),
627
+ fee_limit: parseInt(feeLimit),
628
+ call_value: parseInt(callValue),
629
+ consume_user_resource_percent: userFeePercentage,
630
+ origin_energy_limit: originEnergyLimit,
631
+ abi: JSON.stringify(abi),
632
+ bytecode,
633
+ parameter,
634
+ name,
635
+ };
636
+ // tokenValue and tokenId can cause errors if provided when the trx10 proposal has not been approved yet. So we set them only if they are passed to the method.
637
+ if ((0, validations_js_1.isNotNullOrUndefined)(tokenValue)) {
638
+ args.call_token_value = parseInt(tokenValue);
639
+ }
640
+ if ((0, validations_js_1.isNotNullOrUndefined)(tokenId)) {
641
+ args.token_id = parseInt(tokenId);
642
+ }
643
+ const contract = {};
644
+ contract.owner_address = args.owner_address;
645
+ if ((0, validations_js_1.isNotNullOrUndefined)(args.call_token_value)) {
646
+ contract.call_token_value = args.call_token_value;
647
+ }
648
+ if ((0, validations_js_1.isNotNullOrUndefined)(args.token_id)) {
649
+ contract.token_id = args.token_id;
650
+ }
651
+ const new_contract = (contract.new_contract = {});
652
+ if (args.abi) {
653
+ new_contract.abi = {
654
+ entrys: JSON.parse(args.abi),
655
+ };
656
+ }
657
+ else {
658
+ new_contract.abi = {};
659
+ }
660
+ if (args.call_value) {
661
+ new_contract.call_value = args.call_value;
662
+ }
663
+ new_contract.consume_user_resource_percent = args.consume_user_resource_percent;
664
+ new_contract.origin_energy_limit = args.origin_energy_limit;
665
+ new_contract.origin_address = args.origin_address ?? args.owner_address;
666
+ if (args.bytecode + args.parameter) {
667
+ new_contract.bytecode = (args.bytecode + args.parameter).replace(/^0x/, '');
668
+ }
669
+ if ((0, validations_js_1.isNotNullOrUndefined)(args.name)) {
670
+ new_contract.name = args.name;
671
+ }
672
+ const transactionOptions = (0, helper_js_1.getTransactionOptions)(options);
673
+ const tx = (await (0, helper_js_1.createTransaction)(this.tronWeb, Contract_js_1.ContractType.CreateSmartContract, contract, options?.permissionId, {
674
+ ...transactionOptions,
675
+ fee_limit: args.fee_limit,
676
+ }));
677
+ tx.contract_address = (0, helper_js_1.genContractAddress)(args.owner_address, tx.txID);
678
+ return tx;
679
+ }
680
+ async triggerSmartContract(contractAddress, functionSelector, options, parameters, issuerAddress) {
681
+ const params = [
682
+ contractAddress,
683
+ functionSelector,
684
+ options,
685
+ parameters,
686
+ issuerAddress,
687
+ ];
688
+ if (typeof params[2] !== 'object') {
689
+ params[2] = {
690
+ feeLimit: params[2],
691
+ callValue: params[3],
692
+ };
693
+ params.splice(3, 1);
694
+ }
695
+ if (params[2]?.txLocal) {
696
+ return this._triggerSmartContractLocal(...params);
697
+ }
698
+ return this._triggerSmartContract(...params);
699
+ }
700
+ async triggerConstantContract(contractAddress, functionSelector, options = {}, parameters = [], issuerAddress = this.tronWeb.defaultAddress.hex) {
701
+ options._isConstant = true;
702
+ return this._triggerSmartContract(contractAddress, functionSelector, options, parameters, issuerAddress);
703
+ }
704
+ async triggerConfirmedConstantContract(contractAddress, functionSelector, options = {}, parameters = [], issuerAddress = this.tronWeb.defaultAddress.hex) {
705
+ options._isConstant = true;
706
+ options.confirmed = true;
707
+ return this._triggerSmartContract(contractAddress, functionSelector, options, parameters, issuerAddress);
708
+ }
709
+ async estimateEnergy(contractAddress, functionSelector, options = {}, parameters = [], issuerAddress = this.tronWeb.defaultAddress.hex) {
710
+ options.estimateEnergy = true;
711
+ const result = await this._triggerSmartContract(contractAddress, functionSelector, options, parameters, issuerAddress);
712
+ return result;
713
+ }
714
+ async deployConstantContract(options = { input: '', ownerAddress: '' }) {
715
+ const { input, ownerAddress, tokenId, tokenValue, callValue = 0 } = options;
716
+ this.validator.notValid([
717
+ {
718
+ name: 'input',
719
+ type: 'not-empty-string',
720
+ value: input,
721
+ },
722
+ {
723
+ name: 'callValue',
724
+ type: 'integer',
725
+ value: callValue,
726
+ gte: 0,
727
+ },
728
+ {
729
+ name: 'owner',
730
+ type: 'address',
731
+ value: ownerAddress,
732
+ },
733
+ {
734
+ name: 'tokenValue',
735
+ type: 'integer',
736
+ value: tokenValue,
737
+ gte: 0,
738
+ optional: true,
739
+ },
740
+ {
741
+ name: 'tokenId',
742
+ type: 'integer',
743
+ value: tokenId,
744
+ gte: 0,
745
+ optional: true,
746
+ },
747
+ ]);
748
+ const args = {
749
+ data: input,
750
+ owner_address: (0, address_js_1.toHex)(ownerAddress),
751
+ call_value: callValue,
752
+ };
753
+ if (tokenId) {
754
+ args.token_id = tokenId;
755
+ }
756
+ if (tokenValue) {
757
+ args.call_token_value = tokenValue;
758
+ }
759
+ const pathInfo = `wallet${options.confirmed ? 'solidity' : ''}/estimateenergy`;
760
+ const transaction = await this.tronWeb[options.confirmed ? 'solidityNode' : 'fullNode'].request(pathInfo, args, 'post');
761
+ if (transaction.Error)
762
+ throw new Error(transaction.Error);
763
+ if (transaction.result && transaction.result.message) {
764
+ throw new Error(this.tronWeb.toUtf8(transaction.result.message));
765
+ }
766
+ return transaction;
767
+ }
768
+ _getTriggerSmartContractArgs(contractAddress, functionSelector, options, parameters, issuerAddress, tokenValue, tokenId, callValue, feeLimit) {
769
+ const args = {
770
+ contract_address: (0, address_js_1.toHex)(contractAddress),
771
+ owner_address: (0, address_js_1.toHex)(issuerAddress),
772
+ };
773
+ if (functionSelector && (0, validations_js_1.isString)(functionSelector)) {
774
+ functionSelector = functionSelector.replace(/\s*/g, '');
775
+ let parameterStr;
776
+ if (parameters.length) {
777
+ const abiCoder = new ethersUtils_js_1.AbiCoder();
778
+ let types = [];
779
+ const values = [];
780
+ for (let i = 0; i < parameters.length; i++) {
781
+ let { value } = parameters[i];
782
+ const { type } = parameters[i];
783
+ if (!type || !(0, validations_js_1.isString)(type) || !type.length)
784
+ throw new Error('Invalid parameter type provided: ' + type);
785
+ const replaceAddressPrefix = (value) => {
786
+ if ((0, validations_js_1.isArray)(value)) {
787
+ return value.map((v) => replaceAddressPrefix(v));
788
+ }
789
+ return (0, address_js_1.toHex)(value).replace(address_js_1.ADDRESS_PREFIX_REGEX, '0x');
790
+ };
791
+ if (type === 'address')
792
+ value = replaceAddressPrefix(value);
793
+ else if (type.match(/^([^\x5b]*)(\x5b|$)/)?.[0] === 'address[')
794
+ value = replaceAddressPrefix(value);
795
+ types.push(type);
796
+ values.push(value);
797
+ }
798
+ try {
799
+ // workaround for unsupported trcToken type
800
+ types = types.map((type) => {
801
+ if (/trcToken/.test(type)) {
802
+ type = type.replace(/trcToken/, 'uint256');
803
+ }
804
+ return type;
805
+ });
806
+ parameterStr = abiCoder.encode(types, values).replace(/^(0x)/, '');
807
+ }
808
+ catch (ex) {
809
+ throw new Error(ex);
810
+ }
811
+ }
812
+ else
813
+ parameterStr = '';
814
+ // work for abiv2 if passed the function abi in options
815
+ if (options.funcABIV2) {
816
+ parameterStr = (0, abi_js_1.encodeParamsV2ByABI)(options.funcABIV2, options.parametersV2).replace(/^(0x)/, '');
817
+ }
818
+ if (options.shieldedParameter && (0, validations_js_1.isString)(options.shieldedParameter)) {
819
+ parameterStr = options.shieldedParameter.replace(/^(0x)/, '');
820
+ }
821
+ if (options.rawParameter && (0, validations_js_1.isString)(options.rawParameter)) {
822
+ parameterStr = options.rawParameter.replace(/^(0x)/, '');
823
+ }
824
+ args.function_selector = functionSelector;
825
+ args.parameter = parameterStr;
826
+ }
827
+ else if (options.input) {
828
+ args.data = options.input;
829
+ }
830
+ args.call_value = parseInt(callValue);
831
+ if ((0, validations_js_1.isNotNullOrUndefined)(tokenValue))
832
+ args.call_token_value = parseInt(tokenValue);
833
+ if ((0, validations_js_1.isNotNullOrUndefined)(tokenId))
834
+ args.token_id = parseInt(tokenId);
835
+ if (!(options._isConstant || options.estimateEnergy)) {
836
+ args.fee_limit = parseInt(feeLimit);
837
+ }
838
+ if (options.permissionId) {
839
+ args.Permission_id = options.permissionId;
840
+ }
841
+ return args;
842
+ }
843
+ async _triggerSmartContractLocal(contractAddress, functionSelector, options = {}, parameters = [], issuerAddress = this.tronWeb.defaultAddress.hex) {
844
+ const { tokenValue, tokenId, callValue, feeLimit } = Object.assign({
845
+ callValue: 0,
846
+ feeLimit: this.tronWeb.feeLimit,
847
+ }, options);
848
+ this.validator.notValid([
849
+ {
850
+ name: 'feeLimit',
851
+ type: 'integer',
852
+ value: feeLimit,
853
+ gt: 0,
854
+ },
855
+ {
856
+ name: 'callValue',
857
+ type: 'integer',
858
+ value: callValue,
859
+ gte: 0,
860
+ },
861
+ {
862
+ name: 'parameters',
863
+ type: 'array',
864
+ value: parameters,
865
+ },
866
+ {
867
+ name: 'contract',
868
+ type: 'address',
869
+ value: contractAddress,
870
+ },
871
+ {
872
+ name: 'issuer',
873
+ type: 'address',
874
+ value: issuerAddress,
875
+ optional: true,
876
+ },
877
+ {
878
+ name: 'tokenValue',
879
+ type: 'integer',
880
+ value: tokenValue,
881
+ gte: 0,
882
+ optional: true,
883
+ },
884
+ {
885
+ name: 'tokenId',
886
+ type: 'integer',
887
+ value: tokenId,
888
+ gte: 0,
889
+ optional: true,
890
+ },
891
+ ]);
892
+ const args = this._getTriggerSmartContractArgs(contractAddress, functionSelector, options, parameters, issuerAddress, tokenValue, tokenId, callValue, feeLimit);
893
+ if (args.function_selector) {
894
+ args.data = (0, ethersUtils_js_1.keccak256)(Buffer.from(args.function_selector, 'utf-8')).toString().substring(2, 10) + args.parameter;
895
+ }
896
+ const value = {
897
+ data: args.data,
898
+ owner_address: args.owner_address,
899
+ contract_address: args.contract_address,
900
+ };
901
+ if (args.call_value) {
902
+ value.call_value = args.call_value;
903
+ }
904
+ if (args.call_token_value) {
905
+ value.call_token_value = args.call_token_value;
906
+ }
907
+ if (args.token_id) {
908
+ value.token_id = args.token_id;
909
+ }
910
+ const transactionOptions = (0, helper_js_1.getTransactionOptions)(options);
911
+ const transaction = await (0, helper_js_1.createTransaction)(this.tronWeb, Contract_js_1.ContractType.TriggerSmartContract, value, options.permissionId, {
912
+ ...transactionOptions,
913
+ fee_limit: args.fee_limit,
914
+ });
915
+ return {
916
+ result: {
917
+ result: true,
918
+ },
919
+ transaction,
920
+ };
921
+ }
922
+ async _triggerSmartContract(contractAddress, functionSelector, options = {}, parameters = [], issuerAddress = this.tronWeb.defaultAddress.hex) {
923
+ const { tokenValue, tokenId, callValue, feeLimit } = Object.assign({
924
+ callValue: 0,
925
+ feeLimit: this.tronWeb.feeLimit,
926
+ }, options);
927
+ this.validator.notValid([
928
+ {
929
+ name: 'feeLimit',
930
+ type: 'integer',
931
+ value: feeLimit,
932
+ gt: 0,
933
+ },
934
+ {
935
+ name: 'callValue',
936
+ type: 'integer',
937
+ value: callValue,
938
+ gte: 0,
939
+ },
940
+ {
941
+ name: 'parameters',
942
+ type: 'array',
943
+ value: parameters,
944
+ },
945
+ {
946
+ name: 'contract',
947
+ type: 'address',
948
+ value: contractAddress,
949
+ },
950
+ {
951
+ name: 'issuer',
952
+ type: 'address',
953
+ value: issuerAddress,
954
+ optional: true,
955
+ },
956
+ {
957
+ name: 'tokenValue',
958
+ type: 'integer',
959
+ value: tokenValue,
960
+ gte: 0,
961
+ optional: true,
962
+ },
963
+ {
964
+ name: 'tokenId',
965
+ type: 'integer',
966
+ value: tokenId,
967
+ gte: 0,
968
+ optional: true,
969
+ },
970
+ ]);
971
+ const args = this._getTriggerSmartContractArgs(contractAddress, functionSelector, options, parameters, issuerAddress, tokenValue, tokenId, callValue, feeLimit);
972
+ let pathInfo = 'triggersmartcontract';
973
+ if (options._isConstant) {
974
+ pathInfo = 'triggerconstantcontract';
975
+ }
976
+ else if (options.estimateEnergy) {
977
+ pathInfo = 'estimateenergy';
978
+ }
979
+ pathInfo = `wallet${options.confirmed ? 'solidity' : ''}/${pathInfo}`;
980
+ const transaction = await this.tronWeb[options.confirmed ? 'solidityNode' : 'fullNode'].request(pathInfo, args, 'post');
981
+ return (0, helper_js_1.resultManagerTriggerSmartContract)(transaction, args, options);
982
+ }
983
+ async clearABI(contractAddress, ownerAddress = this.tronWeb.defaultAddress.hex, options = {}) {
984
+ if (!tronweb_js_1.TronWeb.isAddress(contractAddress))
985
+ throw new Error('Invalid contract address provided');
986
+ if (!tronweb_js_1.TronWeb.isAddress(ownerAddress))
987
+ throw new Error('Invalid owner address provided');
988
+ const data = {
989
+ contract_address: (0, address_js_1.toHex)(contractAddress),
990
+ owner_address: (0, address_js_1.toHex)(ownerAddress),
991
+ };
992
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
993
+ // @ts-ignore
994
+ if (this.tronWeb.trx.cache.contracts[contractAddress]) {
995
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
996
+ // @ts-ignore
997
+ delete this.tronWeb.trx.cache.contracts[contractAddress];
998
+ }
999
+ const transactionOptions = (0, helper_js_1.getTransactionOptions)(options);
1000
+ return (0, helper_js_1.createTransaction)(this.tronWeb, Contract_js_1.ContractType.ClearABIContract, data, options?.permissionId, transactionOptions);
1001
+ }
1002
+ async updateBrokerage(brokerage, ownerAddress = this.tronWeb.defaultAddress.hex, options = {}) {
1003
+ if (!(0, validations_js_1.isNotNullOrUndefined)(brokerage))
1004
+ throw new Error('Invalid brokerage provided');
1005
+ if (!(0, validations_js_1.isInteger)(brokerage) || brokerage < 0 || brokerage > 100)
1006
+ throw new Error('Brokerage must be an integer between 0 and 100');
1007
+ if (!tronweb_js_1.TronWeb.isAddress(ownerAddress))
1008
+ throw new Error('Invalid owner address provided');
1009
+ const data = {
1010
+ brokerage: parseInt(brokerage),
1011
+ owner_address: (0, address_js_1.toHex)(ownerAddress),
1012
+ };
1013
+ const transactionOptions = (0, helper_js_1.getTransactionOptions)(options);
1014
+ return (0, helper_js_1.createTransaction)(this.tronWeb, Contract_js_1.ContractType.UpdateBrokerageContract, data, options?.permissionId, transactionOptions);
1015
+ }
1016
+ async createToken(options = {}, issuerAddress = this.tronWeb.defaultAddress.hex) {
1017
+ const { name = false, abbreviation = false, description = '', url = false, totalSupply = 0, trxRatio = 1, // How much TRX will `tokenRatio` cost
1018
+ tokenRatio = 1, // How many tokens will `trxRatio` afford
1019
+ saleStart = Date.now(), saleEnd = false, freeBandwidth = 0, // The creator's "donated" bandwidth for use by token holders
1020
+ freeBandwidthLimit = 0, // Out of `totalFreeBandwidth`, the amount each token holder get
1021
+ frozenAmount = 0, frozenDuration = 0,
1022
+ // for now there is no default for the following values
1023
+ voteScore, precision, } = options;
1024
+ this.validator.notValid([
1025
+ {
1026
+ name: 'Supply amount',
1027
+ type: 'positive-integer',
1028
+ value: totalSupply,
1029
+ },
1030
+ {
1031
+ name: 'TRX ratio',
1032
+ type: 'positive-integer',
1033
+ value: trxRatio,
1034
+ },
1035
+ {
1036
+ name: 'Token ratio',
1037
+ type: 'positive-integer',
1038
+ value: tokenRatio,
1039
+ },
1040
+ {
1041
+ name: 'token abbreviation',
1042
+ type: 'string',
1043
+ value: abbreviation,
1044
+ lte: 32,
1045
+ gt: 0,
1046
+ },
1047
+ {
1048
+ name: 'token name',
1049
+ type: 'not-empty-string',
1050
+ value: name,
1051
+ },
1052
+ {
1053
+ name: 'token description',
1054
+ type: 'string',
1055
+ value: description,
1056
+ lte: 200,
1057
+ },
1058
+ {
1059
+ name: 'token url',
1060
+ type: 'url',
1061
+ value: url,
1062
+ },
1063
+ {
1064
+ name: 'token url',
1065
+ type: 'string',
1066
+ value: url,
1067
+ lte: 256,
1068
+ },
1069
+ {
1070
+ name: 'issuer',
1071
+ type: 'address',
1072
+ value: issuerAddress,
1073
+ },
1074
+ {
1075
+ name: 'sale start timestamp',
1076
+ type: 'integer',
1077
+ value: saleStart,
1078
+ gte: Date.now(),
1079
+ },
1080
+ {
1081
+ name: 'sale end timestamp',
1082
+ type: 'integer',
1083
+ value: saleEnd,
1084
+ gt: saleStart,
1085
+ },
1086
+ {
1087
+ name: 'Frozen supply',
1088
+ type: 'integer',
1089
+ value: frozenAmount,
1090
+ gte: 0,
1091
+ },
1092
+ {
1093
+ name: 'Frozen duration',
1094
+ type: 'integer',
1095
+ value: frozenDuration,
1096
+ gte: 0,
1097
+ },
1098
+ ]);
1099
+ if ((0, validations_js_1.isNotNullOrUndefined)(voteScore) && (!(0, validations_js_1.isInteger)(voteScore) || voteScore <= 0))
1100
+ throw new Error('voteScore must be a positive integer greater than 0');
1101
+ if ((0, validations_js_1.isNotNullOrUndefined)(precision) && (!(0, validations_js_1.isInteger)(precision) || precision < 0 || precision > 6))
1102
+ throw new Error('precision must be a positive integer >= 0 and <= 6');
1103
+ const data = {
1104
+ owner_address: (0, address_js_1.toHex)(issuerAddress),
1105
+ name: (0, helper_js_1.fromUtf8)(name),
1106
+ abbr: (0, helper_js_1.fromUtf8)(abbreviation),
1107
+ description: (0, helper_js_1.fromUtf8)(description),
1108
+ url: (0, helper_js_1.fromUtf8)(url),
1109
+ total_supply: parseInt(totalSupply),
1110
+ trx_num: parseInt(trxRatio),
1111
+ num: parseInt(tokenRatio),
1112
+ start_time: parseInt(saleStart),
1113
+ end_time: parseInt(saleEnd),
1114
+ frozen_supply: [
1115
+ {
1116
+ frozen_amount: parseInt(frozenAmount),
1117
+ frozen_days: parseInt(frozenDuration),
1118
+ },
1119
+ ],
1120
+ };
1121
+ ['name', 'abbr', 'description', 'url'].forEach((key) => {
1122
+ if (!data[key]) {
1123
+ delete data[key];
1124
+ }
1125
+ });
1126
+ if (!(parseInt(frozenAmount) > 0)) {
1127
+ delete data.frozen_supply;
1128
+ }
1129
+ if (freeBandwidth && !isNaN(parseInt(freeBandwidth)) && parseInt(freeBandwidth) >= 0) {
1130
+ data.free_asset_net_limit = parseInt(freeBandwidth);
1131
+ }
1132
+ if (freeBandwidthLimit && !isNaN(parseInt(freeBandwidthLimit)) && parseInt(freeBandwidthLimit) >= 0) {
1133
+ data.public_free_asset_net_limit = parseInt(freeBandwidthLimit);
1134
+ }
1135
+ if (precision && !isNaN(parseInt(precision))) {
1136
+ data.precision = parseInt(precision);
1137
+ }
1138
+ if (voteScore && !isNaN(parseInt(voteScore))) {
1139
+ data.vote_score = parseInt(voteScore);
1140
+ }
1141
+ const transactionOptions = (0, helper_js_1.getTransactionOptions)(options);
1142
+ return (0, helper_js_1.createTransaction)(this.tronWeb, Contract_js_1.ContractType.AssetIssueContract, data, options?.permissionId, transactionOptions);
1143
+ }
1144
+ async createAccount(accountAddress, address = this.tronWeb.defaultAddress.hex, options = {}) {
1145
+ this.validator.notValid([
1146
+ {
1147
+ name: 'account',
1148
+ type: 'address',
1149
+ value: accountAddress,
1150
+ },
1151
+ {
1152
+ name: 'origin',
1153
+ type: 'address',
1154
+ value: address,
1155
+ },
1156
+ ]);
1157
+ const data = {
1158
+ owner_address: (0, address_js_1.toHex)(address),
1159
+ account_address: (0, address_js_1.toHex)(accountAddress),
1160
+ };
1161
+ const transactionOptions = (0, helper_js_1.getTransactionOptions)(options);
1162
+ return (0, helper_js_1.createTransaction)(this.tronWeb, Contract_js_1.ContractType.AccountCreateContract, data, options?.permissionId, transactionOptions);
1163
+ }
1164
+ async updateAccount(accountName, address = this.tronWeb.defaultAddress.hex, options = {}) {
1165
+ this.validator.notValid([
1166
+ {
1167
+ name: 'Name',
1168
+ type: 'string',
1169
+ lte: 200,
1170
+ gt: 0,
1171
+ value: accountName,
1172
+ msg: 'Invalid accountName',
1173
+ },
1174
+ {
1175
+ name: 'origin',
1176
+ type: 'address',
1177
+ value: address,
1178
+ },
1179
+ ]);
1180
+ const data = {
1181
+ account_name: (0, helper_js_1.fromUtf8)(accountName),
1182
+ owner_address: (0, address_js_1.toHex)(address),
1183
+ };
1184
+ const transactionOptions = (0, helper_js_1.getTransactionOptions)(options);
1185
+ return (0, helper_js_1.createTransaction)(this.tronWeb, Contract_js_1.ContractType.AccountUpdateContract, data, options?.permissionId, transactionOptions);
1186
+ }
1187
+ async setAccountId(accountId, address = this.tronWeb.defaultAddress.hex, options = {}) {
1188
+ if (accountId && (0, validations_js_1.isString)(accountId) && accountId.startsWith('0x')) {
1189
+ accountId = accountId.slice(2);
1190
+ }
1191
+ this.validator.notValid([
1192
+ {
1193
+ name: 'accountId',
1194
+ type: 'hex',
1195
+ value: accountId,
1196
+ },
1197
+ {
1198
+ name: 'accountId',
1199
+ type: 'string',
1200
+ lte: 32,
1201
+ gte: 8,
1202
+ value: accountId,
1203
+ },
1204
+ {
1205
+ name: 'origin',
1206
+ type: 'address',
1207
+ value: address,
1208
+ },
1209
+ ]);
1210
+ const data = {
1211
+ account_id: accountId,
1212
+ owner_address: (0, address_js_1.toHex)(address),
1213
+ };
1214
+ const transactionOptions = (0, helper_js_1.getTransactionOptions)(options);
1215
+ return (0, helper_js_1.createTransaction)(this.tronWeb, Contract_js_1.ContractType.SetAccountIdContract, data, options?.permissionId, transactionOptions);
1216
+ }
1217
+ async updateToken(options = {}, issuerAddress = this.tronWeb.defaultAddress.hex) {
1218
+ const { description = '', url = false, freeBandwidth = 0, // The creator's "donated" bandwidth for use by token holders
1219
+ freeBandwidthLimit = 0, // Out of `totalFreeBandwidth`, the amount each token holder get
1220
+ } = options;
1221
+ this.validator.notValid([
1222
+ {
1223
+ name: 'token description',
1224
+ type: 'string',
1225
+ value: description,
1226
+ lte: 200,
1227
+ },
1228
+ {
1229
+ name: 'token url',
1230
+ type: 'url',
1231
+ value: url,
1232
+ },
1233
+ {
1234
+ name: 'token url',
1235
+ type: 'string',
1236
+ value: url,
1237
+ lte: 256,
1238
+ },
1239
+ {
1240
+ name: 'issuer',
1241
+ type: 'address',
1242
+ value: issuerAddress,
1243
+ },
1244
+ ]);
1245
+ const data = {
1246
+ owner_address: (0, address_js_1.toHex)(issuerAddress),
1247
+ description: (0, helper_js_1.fromUtf8)(description),
1248
+ url: (0, helper_js_1.fromUtf8)(url),
1249
+ };
1250
+ if (freeBandwidth && !isNaN(parseInt(freeBandwidth)) && parseInt(freeBandwidth) >= 0) {
1251
+ data.new_limit = parseInt(freeBandwidth);
1252
+ }
1253
+ if (freeBandwidthLimit && !isNaN(parseInt(freeBandwidthLimit)) && parseInt(freeBandwidthLimit) >= 0) {
1254
+ data.new_public_limit = parseInt(freeBandwidthLimit);
1255
+ }
1256
+ const transactionOptions = (0, helper_js_1.getTransactionOptions)(options);
1257
+ return (0, helper_js_1.createTransaction)(this.tronWeb, Contract_js_1.ContractType.UpdateAssetContract, data, options?.permissionId, transactionOptions);
1258
+ }
1259
+ async sendAsset(to, amount = 0, tokenId, from = this.tronWeb.defaultAddress.hex, options = {}) {
1260
+ return this.sendToken(to, amount, tokenId, from, options);
1261
+ }
1262
+ async purchaseAsset(issuerAddress, tokenId, amount = 0, buyer = this.tronWeb.defaultAddress.hex, options = {}) {
1263
+ return this.purchaseToken(issuerAddress, tokenId, amount, buyer, options);
1264
+ }
1265
+ async createAsset(options, issuerAddress) {
1266
+ return this.createToken(options, issuerAddress);
1267
+ }
1268
+ async updateAsset(options = {}, issuerAddress = this.tronWeb.defaultAddress.hex) {
1269
+ return this.updateToken(options, issuerAddress);
1270
+ }
1271
+ /**
1272
+ * Creates a proposal to modify the network.
1273
+ * Can only be created by a current Super Representative.
1274
+ */
1275
+ async createProposal(parameters, issuerAddress = this.tronWeb.defaultAddress.hex, options = {}) {
1276
+ this.validator.notValid([
1277
+ {
1278
+ name: 'issuer',
1279
+ type: 'address',
1280
+ value: issuerAddress,
1281
+ },
1282
+ ]);
1283
+ const invalid = 'Invalid proposal parameters provided';
1284
+ if (!parameters)
1285
+ throw new Error(invalid);
1286
+ const newParams = (0, validations_js_1.isArray)(parameters) ? parameters : [parameters];
1287
+ for (const parameter of newParams) {
1288
+ if (!(0, validations_js_1.isObject)(parameter))
1289
+ throw new Error(invalid);
1290
+ }
1291
+ const data = {
1292
+ owner_address: (0, address_js_1.toHex)(issuerAddress),
1293
+ parameters: newParams,
1294
+ };
1295
+ const transactionOptions = (0, helper_js_1.getTransactionOptions)(options);
1296
+ return (0, helper_js_1.createTransaction)(this.tronWeb, Contract_js_1.ContractType.ProposalCreateContract, data, options?.permissionId, transactionOptions);
1297
+ }
1298
+ /**
1299
+ * Deletes a network modification proposal that the owner issued.
1300
+ * Only current Super Representative can vote on a proposal.
1301
+ */
1302
+ async deleteProposal(proposalID, issuerAddress = this.tronWeb.defaultAddress.hex, options = {}) {
1303
+ this.validator.notValid([
1304
+ {
1305
+ name: 'issuer',
1306
+ type: 'address',
1307
+ value: issuerAddress,
1308
+ },
1309
+ {
1310
+ name: 'proposalID',
1311
+ type: 'integer',
1312
+ value: proposalID,
1313
+ gte: 0,
1314
+ },
1315
+ ]);
1316
+ const data = {
1317
+ owner_address: (0, address_js_1.toHex)(issuerAddress),
1318
+ proposal_id: parseInt(proposalID),
1319
+ };
1320
+ const transactionOptions = (0, helper_js_1.getTransactionOptions)(options);
1321
+ return (0, helper_js_1.createTransaction)(this.tronWeb, Contract_js_1.ContractType.ProposalDeleteContract, data, options?.permissionId, transactionOptions);
1322
+ }
1323
+ /**
1324
+ * Adds a vote to an issued network modification proposal.
1325
+ * Only current Super Representative can vote on a proposal.
1326
+ */
1327
+ async voteProposal(proposalID, isApproval = false, voterAddress = this.tronWeb.defaultAddress.hex, options = {}) {
1328
+ this.validator.notValid([
1329
+ {
1330
+ name: 'voter',
1331
+ type: 'address',
1332
+ value: voterAddress,
1333
+ },
1334
+ {
1335
+ name: 'proposalID',
1336
+ type: 'integer',
1337
+ value: proposalID,
1338
+ gte: 0,
1339
+ },
1340
+ {
1341
+ name: 'has approval',
1342
+ type: 'boolean',
1343
+ value: isApproval,
1344
+ },
1345
+ ]);
1346
+ const data = {
1347
+ owner_address: (0, address_js_1.toHex)(voterAddress),
1348
+ proposal_id: parseInt(proposalID),
1349
+ is_add_approval: isApproval,
1350
+ };
1351
+ const transactionOptions = (0, helper_js_1.getTransactionOptions)(options);
1352
+ return (0, helper_js_1.createTransaction)(this.tronWeb, Contract_js_1.ContractType.ProposalApproveContract, data, options?.permissionId, transactionOptions);
1353
+ }
1354
+ /**
1355
+ * Create an exchange between a token and TRX.
1356
+ * Token Name should be a CASE SENSITIVE string.
1357
+ * PLEASE VERIFY THIS ON TRONSCAN.
1358
+ */
1359
+ async createTRXExchange(tokenName, tokenBalance, trxBalance, ownerAddress = this.tronWeb.defaultAddress.hex, options = {}) {
1360
+ this.validator.notValid([
1361
+ {
1362
+ name: 'owner',
1363
+ type: 'address',
1364
+ value: ownerAddress,
1365
+ },
1366
+ {
1367
+ name: 'token name',
1368
+ type: 'not-empty-string',
1369
+ value: tokenName,
1370
+ },
1371
+ {
1372
+ name: 'token balance',
1373
+ type: 'positive-integer',
1374
+ value: tokenBalance,
1375
+ },
1376
+ {
1377
+ name: 'trx balance',
1378
+ type: 'positive-integer',
1379
+ value: trxBalance,
1380
+ },
1381
+ ]);
1382
+ const data = {
1383
+ owner_address: (0, address_js_1.toHex)(ownerAddress),
1384
+ first_token_id: (0, helper_js_1.fromUtf8)(tokenName),
1385
+ first_token_balance: tokenBalance,
1386
+ second_token_id: '5f', // Constant for TRX.
1387
+ second_token_balance: trxBalance,
1388
+ };
1389
+ const transactionOptions = (0, helper_js_1.getTransactionOptions)(options);
1390
+ return (0, helper_js_1.createTransaction)(this.tronWeb, Contract_js_1.ContractType.ExchangeCreateContract, data, options?.permissionId, transactionOptions);
1391
+ }
1392
+ /**
1393
+ * Create an exchange between a token and another token.
1394
+ * DO NOT USE THIS FOR TRX.
1395
+ * Token Names should be a CASE SENSITIVE string.
1396
+ * PLEASE VERIFY THIS ON TRONSCAN.
1397
+ */
1398
+ async createTokenExchange(firstTokenName, firstTokenBalance, secondTokenName, secondTokenBalance, ownerAddress = this.tronWeb.defaultAddress.hex, options = {}) {
1399
+ this.validator.notValid([
1400
+ {
1401
+ name: 'owner',
1402
+ type: 'address',
1403
+ value: ownerAddress,
1404
+ },
1405
+ {
1406
+ name: 'first token name',
1407
+ type: 'not-empty-string',
1408
+ value: firstTokenName,
1409
+ },
1410
+ {
1411
+ name: 'second token name',
1412
+ type: 'not-empty-string',
1413
+ value: secondTokenName,
1414
+ },
1415
+ {
1416
+ name: 'first token balance',
1417
+ type: 'positive-integer',
1418
+ value: firstTokenBalance,
1419
+ },
1420
+ {
1421
+ name: 'second token balance',
1422
+ type: 'positive-integer',
1423
+ value: secondTokenBalance,
1424
+ },
1425
+ ]);
1426
+ const data = {
1427
+ owner_address: (0, address_js_1.toHex)(ownerAddress),
1428
+ first_token_id: (0, helper_js_1.fromUtf8)(firstTokenName),
1429
+ first_token_balance: firstTokenBalance,
1430
+ second_token_id: (0, helper_js_1.fromUtf8)(secondTokenName),
1431
+ second_token_balance: secondTokenBalance,
1432
+ };
1433
+ const transactionOptions = (0, helper_js_1.getTransactionOptions)(options);
1434
+ return (0, helper_js_1.createTransaction)(this.tronWeb, Contract_js_1.ContractType.ExchangeCreateContract, data, options?.permissionId, transactionOptions);
1435
+ }
1436
+ /**
1437
+ * Adds tokens into a bancor style exchange.
1438
+ * Will add both tokens at market rate.
1439
+ * Use "_" for the constant value for TRX.
1440
+ */
1441
+ async injectExchangeTokens(exchangeID, tokenName, tokenAmount, ownerAddress = this.tronWeb.defaultAddress.hex, options = {}) {
1442
+ this.validator.notValid([
1443
+ {
1444
+ name: 'owner',
1445
+ type: 'address',
1446
+ value: ownerAddress,
1447
+ },
1448
+ {
1449
+ name: 'token name',
1450
+ type: 'not-empty-string',
1451
+ value: tokenName,
1452
+ },
1453
+ {
1454
+ name: 'token amount',
1455
+ type: 'integer',
1456
+ value: tokenAmount,
1457
+ gte: 1,
1458
+ },
1459
+ {
1460
+ name: 'exchangeID',
1461
+ type: 'integer',
1462
+ value: exchangeID,
1463
+ gte: 0,
1464
+ },
1465
+ ]);
1466
+ const data = {
1467
+ owner_address: (0, address_js_1.toHex)(ownerAddress),
1468
+ exchange_id: parseInt(exchangeID),
1469
+ token_id: (0, helper_js_1.fromUtf8)(tokenName),
1470
+ quant: parseInt(tokenAmount),
1471
+ };
1472
+ const transactionOptions = (0, helper_js_1.getTransactionOptions)(options);
1473
+ return (0, helper_js_1.createTransaction)(this.tronWeb, Contract_js_1.ContractType.ExchangeInjectContract, data, options?.permissionId, transactionOptions);
1474
+ }
1475
+ /**
1476
+ * Withdraws tokens from a bancor style exchange.
1477
+ * Will withdraw at market rate both tokens.
1478
+ * Use "_" for the constant value for TRX.
1479
+ */
1480
+ async withdrawExchangeTokens(exchangeID, tokenName, tokenAmount, ownerAddress = this.tronWeb.defaultAddress.hex, options = {}) {
1481
+ this.validator.notValid([
1482
+ {
1483
+ name: 'owner',
1484
+ type: 'address',
1485
+ value: ownerAddress,
1486
+ },
1487
+ {
1488
+ name: 'token name',
1489
+ type: 'not-empty-string',
1490
+ value: tokenName,
1491
+ },
1492
+ {
1493
+ name: 'token amount',
1494
+ type: 'integer',
1495
+ value: tokenAmount,
1496
+ gte: 1,
1497
+ },
1498
+ {
1499
+ name: 'exchangeID',
1500
+ type: 'integer',
1501
+ value: exchangeID,
1502
+ gte: 0,
1503
+ },
1504
+ ]);
1505
+ const data = {
1506
+ owner_address: (0, address_js_1.toHex)(ownerAddress),
1507
+ exchange_id: parseInt(exchangeID),
1508
+ token_id: (0, helper_js_1.fromUtf8)(tokenName),
1509
+ quant: parseInt(tokenAmount),
1510
+ };
1511
+ const transactionOptions = (0, helper_js_1.getTransactionOptions)(options);
1512
+ return (0, helper_js_1.createTransaction)(this.tronWeb, Contract_js_1.ContractType.ExchangeWithdrawContract, data, options?.permissionId, transactionOptions);
1513
+ }
1514
+ /**
1515
+ * Trade tokens on a bancor style exchange.
1516
+ * Expected value is a validation and used to cap the total amt of token 2 spent.
1517
+ * Use "_" for the constant value for TRX.
1518
+ */
1519
+ async tradeExchangeTokens(exchangeID, tokenName, tokenAmountSold, tokenAmountExpected, ownerAddress = this.tronWeb.defaultAddress.hex, options = {}) {
1520
+ this.validator.notValid([
1521
+ {
1522
+ name: 'owner',
1523
+ type: 'address',
1524
+ value: ownerAddress,
1525
+ },
1526
+ {
1527
+ name: 'token name',
1528
+ type: 'not-empty-string',
1529
+ value: tokenName,
1530
+ },
1531
+ {
1532
+ name: 'tokenAmountSold',
1533
+ type: 'integer',
1534
+ value: tokenAmountSold,
1535
+ gte: 1,
1536
+ },
1537
+ {
1538
+ name: 'tokenAmountExpected',
1539
+ type: 'integer',
1540
+ value: tokenAmountExpected,
1541
+ gte: 1,
1542
+ },
1543
+ {
1544
+ name: 'exchangeID',
1545
+ type: 'integer',
1546
+ value: exchangeID,
1547
+ gte: 0,
1548
+ },
1549
+ ]);
1550
+ const data = {
1551
+ owner_address: (0, address_js_1.toHex)(ownerAddress),
1552
+ exchange_id: parseInt(exchangeID),
1553
+ token_id: tronweb_js_1.TronWeb.fromAscii(tokenName).replace(/^0x/, ''),
1554
+ quant: parseInt(tokenAmountSold),
1555
+ expected: parseInt(tokenAmountExpected),
1556
+ };
1557
+ const transactionOptions = (0, helper_js_1.getTransactionOptions)(options);
1558
+ return (0, helper_js_1.createTransaction)(this.tronWeb, Contract_js_1.ContractType.ExchangeTransactionContract, data, options?.permissionId, transactionOptions);
1559
+ }
1560
+ /**
1561
+ * Update userFeePercentage.
1562
+ */
1563
+ async updateSetting(contractAddress, userFeePercentage, ownerAddress = this.tronWeb.defaultAddress.hex, options = {}) {
1564
+ this.validator.notValid([
1565
+ {
1566
+ name: 'owner',
1567
+ type: 'address',
1568
+ value: ownerAddress,
1569
+ },
1570
+ {
1571
+ name: 'contract',
1572
+ type: 'address',
1573
+ value: contractAddress,
1574
+ },
1575
+ {
1576
+ name: 'userFeePercentage',
1577
+ type: 'integer',
1578
+ value: userFeePercentage,
1579
+ gte: 0,
1580
+ lte: 100,
1581
+ },
1582
+ ]);
1583
+ const data = {
1584
+ owner_address: (0, address_js_1.toHex)(ownerAddress),
1585
+ contract_address: (0, address_js_1.toHex)(contractAddress),
1586
+ consume_user_resource_percent: userFeePercentage,
1587
+ };
1588
+ const transactionOptions = (0, helper_js_1.getTransactionOptions)(options);
1589
+ return (0, helper_js_1.createTransaction)(this.tronWeb, Contract_js_1.ContractType.UpdateSettingContract, data, options?.permissionId, transactionOptions);
1590
+ }
1591
+ /**
1592
+ * Update energy limit.
1593
+ */
1594
+ async updateEnergyLimit(contractAddress, originEnergyLimit = 0, ownerAddress = this.tronWeb.defaultAddress.hex, options = {}) {
1595
+ this.validator.notValid([
1596
+ {
1597
+ name: 'owner',
1598
+ type: 'address',
1599
+ value: ownerAddress,
1600
+ },
1601
+ {
1602
+ name: 'contract',
1603
+ type: 'address',
1604
+ value: contractAddress,
1605
+ },
1606
+ {
1607
+ name: 'originEnergyLimit',
1608
+ type: 'integer',
1609
+ value: originEnergyLimit,
1610
+ gte: 0,
1611
+ lte: 10_000_000,
1612
+ },
1613
+ ]);
1614
+ const data = {
1615
+ owner_address: (0, address_js_1.toHex)(ownerAddress),
1616
+ contract_address: (0, address_js_1.toHex)(contractAddress),
1617
+ origin_energy_limit: originEnergyLimit,
1618
+ };
1619
+ const transactionOptions = (0, helper_js_1.getTransactionOptions)(options);
1620
+ return (0, helper_js_1.createTransaction)(this.tronWeb, Contract_js_1.ContractType.UpdateEnergyLimitContract, data, options?.permissionId, transactionOptions);
1621
+ }
1622
+ checkPermissions(permissions, type) {
1623
+ if (permissions) {
1624
+ if (permissions.type !== type ||
1625
+ !permissions.permission_name ||
1626
+ !(0, validations_js_1.isString)(permissions.permission_name) ||
1627
+ !(0, validations_js_1.isInteger)(permissions.threshold) ||
1628
+ permissions.threshold < 1 ||
1629
+ !permissions.keys) {
1630
+ return false;
1631
+ }
1632
+ for (const key of permissions.keys) {
1633
+ if (!tronweb_js_1.TronWeb.isAddress(key.address) ||
1634
+ !(0, validations_js_1.isInteger)(key.weight) ||
1635
+ key.weight > permissions.threshold ||
1636
+ key.weight < 1 ||
1637
+ (type === 2 && !permissions.operations)) {
1638
+ return false;
1639
+ }
1640
+ }
1641
+ }
1642
+ return true;
1643
+ }
1644
+ async updateAccountPermissions(ownerAddress = this.tronWeb.defaultAddress.hex, ownerPermission, witnessPermission, activesPermissions, options = {}) {
1645
+ if (!tronweb_js_1.TronWeb.isAddress(ownerAddress))
1646
+ throw new Error('Invalid ownerAddress provided');
1647
+ if (!this.checkPermissions(ownerPermission, 0)) {
1648
+ throw new Error('Invalid ownerPermissions provided');
1649
+ }
1650
+ if (!this.checkPermissions(witnessPermission, 1)) {
1651
+ throw new Error('Invalid witnessPermissions provided');
1652
+ }
1653
+ if (!Array.isArray(activesPermissions)) {
1654
+ activesPermissions = [activesPermissions];
1655
+ }
1656
+ for (const activesPermission of activesPermissions) {
1657
+ if (!this.checkPermissions(activesPermission, 2)) {
1658
+ throw new Error('Invalid activesPermissions provided');
1659
+ }
1660
+ }
1661
+ const data = {
1662
+ owner_address: (0, address_js_1.toHex)(ownerAddress),
1663
+ };
1664
+ if (ownerPermission) {
1665
+ const _ownerPermissions = (0, helper_js_1.deepCopyJson)(ownerPermission);
1666
+ // for compatible with old way of building transaction from chain which type prop is omitted
1667
+ if ('type' in _ownerPermissions) {
1668
+ delete _ownerPermissions.type;
1669
+ }
1670
+ _ownerPermissions.keys = _ownerPermissions.keys?.map(({ address, weight }) => ({
1671
+ address: this.tronWeb.address.toHex(address),
1672
+ weight,
1673
+ }));
1674
+ data.owner = _ownerPermissions;
1675
+ }
1676
+ if (witnessPermission) {
1677
+ const _witnessPermissions = (0, helper_js_1.deepCopyJson)(witnessPermission);
1678
+ // for compatible with old way of building transaction from chain which type prop is Witness
1679
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
1680
+ // @ts-ignore
1681
+ _witnessPermissions.type = 'Witness';
1682
+ _witnessPermissions.keys = _witnessPermissions.keys.map(({ address, weight }) => ({
1683
+ address: this.tronWeb.address.toHex(address),
1684
+ weight,
1685
+ }));
1686
+ data.witness = _witnessPermissions;
1687
+ }
1688
+ if (activesPermissions) {
1689
+ const _activesPermissions = (0, helper_js_1.deepCopyJson)(activesPermissions);
1690
+ // for compatible with old way of building transaction from chain which type prop is Active
1691
+ _activesPermissions.forEach((activePermissions) => {
1692
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
1693
+ // @ts-ignore
1694
+ activePermissions.type = 'Active';
1695
+ });
1696
+ _activesPermissions.forEach((_activesPermission) => {
1697
+ _activesPermission.keys = _activesPermission.keys.map(({ address, weight }) => ({
1698
+ address: this.tronWeb.address.toHex(address),
1699
+ weight,
1700
+ }));
1701
+ });
1702
+ data.actives = _activesPermissions;
1703
+ }
1704
+ const transactionOptions = (0, helper_js_1.getTransactionOptions)(options);
1705
+ return (0, helper_js_1.createTransaction)(this.tronWeb, Contract_js_1.ContractType.AccountPermissionUpdateContract, data, options?.permissionId, transactionOptions);
1706
+ }
1707
+ async newTxID(transaction, options = {}) {
1708
+ if (options?.txLocal) {
1709
+ const contract = transaction.raw_data.contract[0];
1710
+ try {
1711
+ const tx = await (0, helper_js_1.createTransaction)(this.tronWeb, contract.type, contract.parameter.value, contract.Permission_id, {
1712
+ fee_limit: transaction.raw_data.fee_limit,
1713
+ data: transaction.raw_data.data,
1714
+ ref_block_bytes: transaction.raw_data.ref_block_bytes,
1715
+ ref_block_hash: transaction.raw_data.ref_block_hash,
1716
+ expiration: transaction.raw_data.expiration,
1717
+ timestamp: transaction.raw_data.timestamp,
1718
+ });
1719
+ tx.signature = transaction.signature;
1720
+ tx.visible = transaction.visible;
1721
+ return tx;
1722
+ }
1723
+ catch (e) {
1724
+ throw new Error('Error generating a new transaction id.');
1725
+ }
1726
+ }
1727
+ try {
1728
+ const res = await this.tronWeb.fullNode.request('wallet/getsignweight', transaction, 'post');
1729
+ if (typeof transaction.visible === 'boolean') {
1730
+ res.transaction.transaction.visible = transaction.visible;
1731
+ }
1732
+ return (0, helper_js_1.resultManager)(res.transaction.transaction, {
1733
+ ...transaction.raw_data.contract[0].parameter.value,
1734
+ Permission_id: transaction.raw_data.contract[0].Permission_id,
1735
+ },
1736
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
1737
+ /* @ts-ignore */
1738
+ { data: transaction.raw_data.data, fee_limit: transaction.raw_data.fee_limit });
1739
+ }
1740
+ catch (e) {
1741
+ throw new Error('Error generating a new transaction id.');
1742
+ }
1743
+ }
1744
+ async alterTransaction(transaction, options = {}) {
1745
+ if (Reflect.has(transaction, 'signature'))
1746
+ throw new Error('You can not extend the expiration of a signed transaction.');
1747
+ if (options.data) {
1748
+ if (options.dataFormat !== 'hex')
1749
+ options.data = tronweb_js_1.TronWeb.toHex(options.data);
1750
+ options.data = options.data.replace(/^0x/, '');
1751
+ if (options.data.length === 0)
1752
+ throw new Error('Invalid data provided');
1753
+ transaction.raw_data.data = options.data;
1754
+ }
1755
+ if (options.extension) {
1756
+ options.extension = parseInt(options.extension * 1000);
1757
+ if (isNaN(options.extension) || transaction.raw_data.expiration + options.extension <= Date.now() + 3000)
1758
+ throw new Error('Invalid extension provided');
1759
+ transaction.raw_data.expiration += options.extension;
1760
+ }
1761
+ return await this.newTxID(transaction, { txLocal: options.txLocal });
1762
+ }
1763
+ async extendExpiration(transaction, extension, options = {}) {
1764
+ return await this.alterTransaction(transaction, { extension, txLocal: options?.txLocal });
1765
+ }
1766
+ async addUpdateData(transaction, data, dataFormat = 'utf8', options = {}) {
1767
+ return this.alterTransaction(transaction, { data, dataFormat: dataFormat, txLocal: options?.txLocal });
1768
+ }
1769
+ }
1770
+ exports.TransactionBuilder = TransactionBuilder;
1771
+ //# sourceMappingURL=TransactionBuilder.js.map