@getomnichain/omnichain 0.5.1 → 0.6.1

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 (300) hide show
  1. package/CHANGELOG.md +123 -0
  2. package/README.md +5 -3
  3. package/dist/cjs/address.factory.js +7 -4
  4. package/dist/cjs/address.factory.js.map +1 -1
  5. package/dist/cjs/asset_balance.js +7 -0
  6. package/dist/cjs/asset_balance.js.map +1 -0
  7. package/dist/cjs/asset_map.js +45 -0
  8. package/dist/cjs/asset_map.js.map +1 -0
  9. package/dist/cjs/asset_registry.js +21 -0
  10. package/dist/cjs/asset_registry.js.map +1 -0
  11. package/dist/cjs/bip39.js +88 -0
  12. package/dist/cjs/bip39.js.map +1 -0
  13. package/dist/cjs/bytes_from_hex.js +34 -0
  14. package/dist/cjs/bytes_from_hex.js.map +1 -0
  15. package/dist/cjs/chain_type.js +35 -0
  16. package/dist/cjs/chain_type.js.map +1 -0
  17. package/dist/cjs/evm/evm_chain.js +16 -0
  18. package/dist/cjs/evm/evm_chain.js.map +1 -1
  19. package/dist/cjs/index.js +12 -0
  20. package/dist/cjs/index.js.map +1 -1
  21. package/dist/cjs/network_type.js +5 -2
  22. package/dist/cjs/network_type.js.map +1 -1
  23. package/dist/cjs/python_builtins.js +225 -0
  24. package/dist/cjs/python_builtins.js.map +1 -0
  25. package/dist/cjs/python_json.js +120 -0
  26. package/dist/cjs/python_json.js.map +1 -0
  27. package/dist/cjs/python_pydantic.js +50 -0
  28. package/dist/cjs/python_pydantic.js.map +1 -0
  29. package/dist/cjs/python_repr.js +172 -0
  30. package/dist/cjs/python_repr.js.map +1 -0
  31. package/dist/cjs/signed_transaction.js +24 -0
  32. package/dist/cjs/signed_transaction.js.map +1 -0
  33. package/dist/cjs/stellar/index.js +26 -0
  34. package/dist/cjs/stellar/index.js.map +1 -0
  35. package/dist/cjs/stellar/stellar_address.js +25 -0
  36. package/dist/cjs/stellar/stellar_address.js.map +1 -0
  37. package/dist/cjs/stellar/stellar_asset.js +155 -0
  38. package/dist/cjs/stellar/stellar_asset.js.map +1 -0
  39. package/dist/cjs/stellar/stellar_assets.js +26 -0
  40. package/dist/cjs/stellar/stellar_assets.js.map +1 -0
  41. package/dist/cjs/stellar/stellar_chain.js +1334 -0
  42. package/dist/cjs/stellar/stellar_chain.js.map +1 -0
  43. package/dist/cjs/stellar/stellar_chains.js +28 -0
  44. package/dist/cjs/stellar/stellar_chains.js.map +1 -0
  45. package/dist/cjs/stellar/stellar_gas_pricing.js +22 -0
  46. package/dist/cjs/stellar/stellar_gas_pricing.js.map +1 -0
  47. package/dist/cjs/stellar/stellar_strkey.js +71 -0
  48. package/dist/cjs/stellar/stellar_strkey.js.map +1 -0
  49. package/dist/cjs/stellar/stellar_transaction_status.js +72 -0
  50. package/dist/cjs/stellar/stellar_transaction_status.js.map +1 -0
  51. package/dist/cjs/stellar/stellar_transactions.js +475 -0
  52. package/dist/cjs/stellar/stellar_transactions.js.map +1 -0
  53. package/dist/cjs/stellar/stellar_wallet.js +195 -0
  54. package/dist/cjs/stellar/stellar_wallet.js.map +1 -0
  55. package/dist/cjs/token.js +3 -1
  56. package/dist/cjs/token.js.map +1 -1
  57. package/dist/cjs/transaction_json.js +85 -0
  58. package/dist/cjs/transaction_json.js.map +1 -0
  59. package/dist/cjs/transaction_prerequisite.js +19 -0
  60. package/dist/cjs/transaction_prerequisite.js.map +1 -0
  61. package/dist/cjs/transaction_simulation.js +21 -0
  62. package/dist/cjs/transaction_simulation.js.map +1 -0
  63. package/dist/cjs/tron/index.js +32 -0
  64. package/dist/cjs/tron/index.js.map +1 -0
  65. package/dist/cjs/tron/tron_abi.js +287 -0
  66. package/dist/cjs/tron/tron_abi.js.map +1 -0
  67. package/dist/cjs/tron/tron_address.js +38 -0
  68. package/dist/cjs/tron/tron_address.js.map +1 -0
  69. package/dist/cjs/tron/tron_asset.js +56 -0
  70. package/dist/cjs/tron/tron_asset.js.map +1 -0
  71. package/dist/cjs/tron/tron_assets.js +17 -0
  72. package/dist/cjs/tron/tron_assets.js.map +1 -0
  73. package/dist/cjs/tron/tron_base58.js +95 -0
  74. package/dist/cjs/tron/tron_base58.js.map +1 -0
  75. package/dist/cjs/tron/tron_canonical_transaction.js +268 -0
  76. package/dist/cjs/tron/tron_canonical_transaction.js.map +1 -0
  77. package/dist/cjs/tron/tron_chain.js +809 -0
  78. package/dist/cjs/tron/tron_chain.js.map +1 -0
  79. package/dist/cjs/tron/tron_chains.js +19 -0
  80. package/dist/cjs/tron/tron_chains.js.map +1 -0
  81. package/dist/cjs/tron/tron_client.js +327 -0
  82. package/dist/cjs/tron/tron_client.js.map +1 -0
  83. package/dist/cjs/tron/tron_contract.js +195 -0
  84. package/dist/cjs/tron/tron_contract.js.map +1 -0
  85. package/dist/cjs/tron/tron_gas_pricing.js +21 -0
  86. package/dist/cjs/tron/tron_gas_pricing.js.map +1 -0
  87. package/dist/cjs/tron/tron_keys.js +251 -0
  88. package/dist/cjs/tron/tron_keys.js.map +1 -0
  89. package/dist/cjs/tron/tron_transaction_builder.js +196 -0
  90. package/dist/cjs/tron/tron_transaction_builder.js.map +1 -0
  91. package/dist/cjs/tron/tron_transaction_status.js +118 -0
  92. package/dist/cjs/tron/tron_transaction_status.js.map +1 -0
  93. package/dist/cjs/tron/tron_transactions.js +192 -0
  94. package/dist/cjs/tron/tron_transactions.js.map +1 -0
  95. package/dist/cjs/tron/tron_wallet.js +152 -0
  96. package/dist/cjs/tron/tron_wallet.js.map +1 -0
  97. package/dist/cjs/unsigned_transaction.js +12 -0
  98. package/dist/cjs/unsigned_transaction.js.map +1 -1
  99. package/dist/cjs/wallet.base.js +29 -0
  100. package/dist/cjs/wallet.base.js.map +1 -0
  101. package/dist/esm/address.factory.d.ts.map +1 -1
  102. package/dist/esm/address.factory.js +7 -4
  103. package/dist/esm/address.factory.js.map +1 -1
  104. package/dist/esm/asset_balance.d.ts +5 -0
  105. package/dist/esm/asset_balance.d.ts.map +1 -0
  106. package/dist/esm/asset_balance.js +3 -0
  107. package/dist/esm/asset_balance.js.map +1 -0
  108. package/dist/esm/asset_map.d.ts +12 -0
  109. package/dist/esm/asset_map.d.ts.map +1 -0
  110. package/dist/esm/asset_map.js +40 -0
  111. package/dist/esm/asset_map.js.map +1 -0
  112. package/dist/esm/asset_registry.d.ts +6 -0
  113. package/dist/esm/asset_registry.d.ts.map +1 -0
  114. package/dist/esm/asset_registry.js +15 -0
  115. package/dist/esm/asset_registry.js.map +1 -0
  116. package/dist/esm/bip39.d.ts +4 -0
  117. package/dist/esm/bip39.d.ts.map +1 -0
  118. package/dist/esm/bip39.js +83 -0
  119. package/dist/esm/bip39.js.map +1 -0
  120. package/dist/esm/bytes_from_hex.d.ts +2 -0
  121. package/dist/esm/bytes_from_hex.d.ts.map +1 -0
  122. package/dist/esm/bytes_from_hex.js +31 -0
  123. package/dist/esm/bytes_from_hex.js.map +1 -0
  124. package/dist/esm/chain_type.d.ts +29 -0
  125. package/dist/esm/chain_type.d.ts.map +1 -0
  126. package/dist/esm/chain_type.js +32 -0
  127. package/dist/esm/chain_type.js.map +1 -0
  128. package/dist/esm/evm/evm_chain.d.ts +1 -0
  129. package/dist/esm/evm/evm_chain.d.ts.map +1 -1
  130. package/dist/esm/evm/evm_chain.js +17 -1
  131. package/dist/esm/evm/evm_chain.js.map +1 -1
  132. package/dist/esm/index.d.ts +12 -0
  133. package/dist/esm/index.d.ts.map +1 -1
  134. package/dist/esm/index.js +12 -0
  135. package/dist/esm/index.js.map +1 -1
  136. package/dist/esm/network_type.d.ts +3 -2
  137. package/dist/esm/network_type.d.ts.map +1 -1
  138. package/dist/esm/network_type.js +6 -3
  139. package/dist/esm/network_type.js.map +1 -1
  140. package/dist/esm/python_builtins.d.ts +13 -0
  141. package/dist/esm/python_builtins.d.ts.map +1 -0
  142. package/dist/esm/python_builtins.js +212 -0
  143. package/dist/esm/python_builtins.js.map +1 -0
  144. package/dist/esm/python_json.d.ts +9 -0
  145. package/dist/esm/python_json.d.ts.map +1 -0
  146. package/dist/esm/python_json.js +117 -0
  147. package/dist/esm/python_json.js.map +1 -0
  148. package/dist/esm/python_pydantic.d.ts +11 -0
  149. package/dist/esm/python_pydantic.d.ts.map +1 -0
  150. package/dist/esm/python_pydantic.js +39 -0
  151. package/dist/esm/python_pydantic.js.map +1 -0
  152. package/dist/esm/python_repr.d.ts +17 -0
  153. package/dist/esm/python_repr.d.ts.map +1 -0
  154. package/dist/esm/python_repr.js +160 -0
  155. package/dist/esm/python_repr.js.map +1 -0
  156. package/dist/esm/signed_transaction.d.ts +17 -0
  157. package/dist/esm/signed_transaction.d.ts.map +1 -0
  158. package/dist/esm/signed_transaction.js +19 -0
  159. package/dist/esm/signed_transaction.js.map +1 -0
  160. package/dist/esm/stellar/index.d.ts +10 -0
  161. package/dist/esm/stellar/index.d.ts.map +1 -0
  162. package/dist/esm/stellar/index.js +10 -0
  163. package/dist/esm/stellar/index.js.map +1 -0
  164. package/dist/esm/stellar/stellar_address.d.ts +8 -0
  165. package/dist/esm/stellar/stellar_address.d.ts.map +1 -0
  166. package/dist/esm/stellar/stellar_address.js +21 -0
  167. package/dist/esm/stellar/stellar_address.js.map +1 -0
  168. package/dist/esm/stellar/stellar_asset.d.ts +52 -0
  169. package/dist/esm/stellar/stellar_asset.d.ts.map +1 -0
  170. package/dist/esm/stellar/stellar_asset.js +147 -0
  171. package/dist/esm/stellar/stellar_asset.js.map +1 -0
  172. package/dist/esm/stellar/stellar_assets.d.ts +11 -0
  173. package/dist/esm/stellar/stellar_assets.d.ts.map +1 -0
  174. package/dist/esm/stellar/stellar_assets.js +23 -0
  175. package/dist/esm/stellar/stellar_assets.js.map +1 -0
  176. package/dist/esm/stellar/stellar_chain.d.ts +173 -0
  177. package/dist/esm/stellar/stellar_chain.d.ts.map +1 -0
  178. package/dist/esm/stellar/stellar_chain.js +1325 -0
  179. package/dist/esm/stellar/stellar_chain.js.map +1 -0
  180. package/dist/esm/stellar/stellar_chains.d.ts +5 -0
  181. package/dist/esm/stellar/stellar_chains.d.ts.map +1 -0
  182. package/dist/esm/stellar/stellar_chains.js +25 -0
  183. package/dist/esm/stellar/stellar_chains.js.map +1 -0
  184. package/dist/esm/stellar/stellar_gas_pricing.d.ts +13 -0
  185. package/dist/esm/stellar/stellar_gas_pricing.d.ts.map +1 -0
  186. package/dist/esm/stellar/stellar_gas_pricing.js +18 -0
  187. package/dist/esm/stellar/stellar_gas_pricing.js.map +1 -0
  188. package/dist/esm/stellar/stellar_strkey.d.ts +3 -0
  189. package/dist/esm/stellar/stellar_strkey.d.ts.map +1 -0
  190. package/dist/esm/stellar/stellar_strkey.js +67 -0
  191. package/dist/esm/stellar/stellar_strkey.js.map +1 -0
  192. package/dist/esm/stellar/stellar_transaction_status.d.ts +39 -0
  193. package/dist/esm/stellar/stellar_transaction_status.d.ts.map +1 -0
  194. package/dist/esm/stellar/stellar_transaction_status.js +68 -0
  195. package/dist/esm/stellar/stellar_transaction_status.js.map +1 -0
  196. package/dist/esm/stellar/stellar_transactions.d.ts +166 -0
  197. package/dist/esm/stellar/stellar_transactions.d.ts.map +1 -0
  198. package/dist/esm/stellar/stellar_transactions.js +451 -0
  199. package/dist/esm/stellar/stellar_transactions.js.map +1 -0
  200. package/dist/esm/stellar/stellar_wallet.d.ts +43 -0
  201. package/dist/esm/stellar/stellar_wallet.d.ts.map +1 -0
  202. package/dist/esm/stellar/stellar_wallet.js +190 -0
  203. package/dist/esm/stellar/stellar_wallet.js.map +1 -0
  204. package/dist/esm/token.d.ts +1 -0
  205. package/dist/esm/token.d.ts.map +1 -1
  206. package/dist/esm/token.js +3 -1
  207. package/dist/esm/token.js.map +1 -1
  208. package/dist/esm/transaction_json.d.ts +21 -0
  209. package/dist/esm/transaction_json.d.ts.map +1 -0
  210. package/dist/esm/transaction_json.js +75 -0
  211. package/dist/esm/transaction_json.js.map +1 -0
  212. package/dist/esm/transaction_prerequisite.d.ts +17 -0
  213. package/dist/esm/transaction_prerequisite.d.ts.map +1 -0
  214. package/dist/esm/transaction_prerequisite.js +13 -0
  215. package/dist/esm/transaction_prerequisite.js.map +1 -0
  216. package/dist/esm/transaction_simulation.d.ts +20 -0
  217. package/dist/esm/transaction_simulation.d.ts.map +1 -0
  218. package/dist/esm/transaction_simulation.js +17 -0
  219. package/dist/esm/transaction_simulation.js.map +1 -0
  220. package/dist/esm/tron/index.d.ts +16 -0
  221. package/dist/esm/tron/index.d.ts.map +1 -0
  222. package/dist/esm/tron/index.js +16 -0
  223. package/dist/esm/tron/index.js.map +1 -0
  224. package/dist/esm/tron/tron_abi.d.ts +3 -0
  225. package/dist/esm/tron/tron_abi.d.ts.map +1 -0
  226. package/dist/esm/tron/tron_abi.js +283 -0
  227. package/dist/esm/tron/tron_abi.js.map +1 -0
  228. package/dist/esm/tron/tron_address.d.ts +10 -0
  229. package/dist/esm/tron/tron_address.d.ts.map +1 -0
  230. package/dist/esm/tron/tron_address.js +34 -0
  231. package/dist/esm/tron/tron_address.js.map +1 -0
  232. package/dist/esm/tron/tron_asset.d.ts +19 -0
  233. package/dist/esm/tron/tron_asset.d.ts.map +1 -0
  234. package/dist/esm/tron/tron_asset.js +51 -0
  235. package/dist/esm/tron/tron_asset.js.map +1 -0
  236. package/dist/esm/tron/tron_assets.d.ts +10 -0
  237. package/dist/esm/tron/tron_assets.d.ts.map +1 -0
  238. package/dist/esm/tron/tron_assets.js +14 -0
  239. package/dist/esm/tron/tron_assets.js.map +1 -0
  240. package/dist/esm/tron/tron_base58.d.ts +7 -0
  241. package/dist/esm/tron/tron_base58.d.ts.map +1 -0
  242. package/dist/esm/tron/tron_base58.js +84 -0
  243. package/dist/esm/tron/tron_base58.js.map +1 -0
  244. package/dist/esm/tron/tron_canonical_transaction.d.ts +164 -0
  245. package/dist/esm/tron/tron_canonical_transaction.d.ts.map +1 -0
  246. package/dist/esm/tron/tron_canonical_transaction.js +262 -0
  247. package/dist/esm/tron/tron_canonical_transaction.js.map +1 -0
  248. package/dist/esm/tron/tron_chain.d.ts +136 -0
  249. package/dist/esm/tron/tron_chain.d.ts.map +1 -0
  250. package/dist/esm/tron/tron_chain.js +801 -0
  251. package/dist/esm/tron/tron_chain.js.map +1 -0
  252. package/dist/esm/tron/tron_chains.d.ts +5 -0
  253. package/dist/esm/tron/tron_chains.d.ts.map +1 -0
  254. package/dist/esm/tron/tron_chains.js +16 -0
  255. package/dist/esm/tron/tron_chains.js.map +1 -0
  256. package/dist/esm/tron/tron_client.d.ts +56 -0
  257. package/dist/esm/tron/tron_client.d.ts.map +1 -0
  258. package/dist/esm/tron/tron_client.js +319 -0
  259. package/dist/esm/tron/tron_client.js.map +1 -0
  260. package/dist/esm/tron/tron_contract.d.ts +69 -0
  261. package/dist/esm/tron/tron_contract.d.ts.map +1 -0
  262. package/dist/esm/tron/tron_contract.js +190 -0
  263. package/dist/esm/tron/tron_contract.js.map +1 -0
  264. package/dist/esm/tron/tron_gas_pricing.d.ts +12 -0
  265. package/dist/esm/tron/tron_gas_pricing.d.ts.map +1 -0
  266. package/dist/esm/tron/tron_gas_pricing.js +17 -0
  267. package/dist/esm/tron/tron_gas_pricing.js.map +1 -0
  268. package/dist/esm/tron/tron_keys.d.ts +61 -0
  269. package/dist/esm/tron/tron_keys.d.ts.map +1 -0
  270. package/dist/esm/tron/tron_keys.js +228 -0
  271. package/dist/esm/tron/tron_keys.js.map +1 -0
  272. package/dist/esm/tron/tron_transaction_builder.d.ts +56 -0
  273. package/dist/esm/tron/tron_transaction_builder.d.ts.map +1 -0
  274. package/dist/esm/tron/tron_transaction_builder.js +188 -0
  275. package/dist/esm/tron/tron_transaction_builder.js.map +1 -0
  276. package/dist/esm/tron/tron_transaction_status.d.ts +54 -0
  277. package/dist/esm/tron/tron_transaction_status.d.ts.map +1 -0
  278. package/dist/esm/tron/tron_transaction_status.js +113 -0
  279. package/dist/esm/tron/tron_transaction_status.js.map +1 -0
  280. package/dist/esm/tron/tron_transactions.d.ts +96 -0
  281. package/dist/esm/tron/tron_transactions.d.ts.map +1 -0
  282. package/dist/esm/tron/tron_transactions.js +181 -0
  283. package/dist/esm/tron/tron_transactions.js.map +1 -0
  284. package/dist/esm/tron/tron_wallet.d.ts +43 -0
  285. package/dist/esm/tron/tron_wallet.d.ts.map +1 -0
  286. package/dist/esm/tron/tron_wallet.js +148 -0
  287. package/dist/esm/tron/tron_wallet.js.map +1 -0
  288. package/dist/esm/unsigned_transaction.d.ts +5 -0
  289. package/dist/esm/unsigned_transaction.d.ts.map +1 -1
  290. package/dist/esm/unsigned_transaction.js +12 -0
  291. package/dist/esm/unsigned_transaction.js.map +1 -1
  292. package/dist/esm/wallet.base.d.ts +32 -0
  293. package/dist/esm/wallet.base.d.ts.map +1 -0
  294. package/dist/esm/wallet.base.js +23 -0
  295. package/dist/esm/wallet.base.js.map +1 -0
  296. package/docs/CONNECTIONS.md +1 -0
  297. package/docs/README.md +16 -7
  298. package/docs/stellar.md +113 -0
  299. package/docs/tron.md +103 -0
  300. package/package.json +14 -2
@@ -0,0 +1,801 @@
1
+ import { Decimal } from 'decimal.js';
2
+ import { isAbstractGasPricing } from "../abstract_gas_pricing.js";
3
+ import { Chain, resolveTransferAmount, } from "../chain.base.js";
4
+ import { CHAIN_ID_TRON_MAINNET } from "../chain_ids.js";
5
+ import { ChainType } from "../chain_type.js";
6
+ import { ChainError, ChainErrorKinds, isChainError } from "../errors.js";
7
+ import { isPyInt, pyDecodeUtf8, pyEncodeUtf8, pyInt, pyItem, pyTruthy } from "../python_builtins.js";
8
+ import { pyRepr, pyTypeRepr } from "../python_repr.js";
9
+ import { NetworkType } from "../network_type.js";
10
+ import { FeePriority } from "../priority.js";
11
+ import { coerceJsonDict, JSON_TRANSACTION_TYPE_KEY } from "../transaction_json.js";
12
+ import { UnsignedTransactionWithPrerequisites } from "../transaction_prerequisite.js";
13
+ import { TransactionSimulationStatusTypes } from "../transaction_simulation.js";
14
+ import { AssetBalanceChange, hrDecimalToMinorUnits, minorUnitsToHrString, } from "../transaction_status.js";
15
+ import { AbstractSignedMessage } from "../wallet.base.js";
16
+ import { TronAddress } from "./tron_address.js";
17
+ import { TronAsset } from "./tron_asset.js";
18
+ import { TronAddressNotFoundError, TronApiError, TronClient, TronTransactionNotFoundError, } from "./tron_client.js";
19
+ import { TronContract } from "./tron_contract.js";
20
+ import { TronGasPricing } from "./tron_gas_pricing.js";
21
+ import { TronPublicKey, TronSignature, isBase58CheckAddress, toBase58CheckAddress, } from "./tron_keys.js";
22
+ import { TronTrx } from "./tron_transaction_builder.js";
23
+ import { TronTransactionFees, TronTransactionStatus } from "./tron_transaction_status.js";
24
+ import { TronBroadcastTransactionResponse, TronSignedTransaction, TronTransactionSimulationResult, TronUnsignedTransaction, assertJsonSafeIntegers, tronTransactionFromJson, } from "./tron_transactions.js";
25
+ export const TRC20_ABI = [
26
+ {
27
+ constant: true,
28
+ inputs: [],
29
+ name: 'decimals',
30
+ outputs: [{ name: '', type: 'uint8' }],
31
+ stateMutability: 'view',
32
+ type: 'function',
33
+ },
34
+ {
35
+ constant: true,
36
+ inputs: [],
37
+ name: 'symbol',
38
+ outputs: [{ name: '', type: 'string' }],
39
+ stateMutability: 'view',
40
+ type: 'function',
41
+ },
42
+ {
43
+ constant: true,
44
+ inputs: [{ name: '_owner', type: 'address' }],
45
+ name: 'balanceOf',
46
+ outputs: [{ name: 'balance', type: 'uint256' }],
47
+ stateMutability: 'view',
48
+ type: 'function',
49
+ },
50
+ {
51
+ constant: false,
52
+ inputs: [
53
+ { name: '_to', type: 'address' },
54
+ { name: '_value', type: 'uint256' },
55
+ ],
56
+ name: 'transfer',
57
+ outputs: [{ name: '', type: 'bool' }],
58
+ stateMutability: 'nonpayable',
59
+ type: 'function',
60
+ },
61
+ {
62
+ constant: false,
63
+ inputs: [
64
+ { name: '_spender', type: 'address' },
65
+ { name: '_value', type: 'uint256' },
66
+ ],
67
+ name: 'approve',
68
+ outputs: [{ name: '', type: 'bool' }],
69
+ stateMutability: 'nonpayable',
70
+ type: 'function',
71
+ },
72
+ {
73
+ constant: true,
74
+ inputs: [
75
+ { name: '_owner', type: 'address' },
76
+ { name: '_spender', type: 'address' },
77
+ ],
78
+ name: 'allowance',
79
+ outputs: [{ name: '', type: 'uint256' }],
80
+ stateMutability: 'view',
81
+ type: 'function',
82
+ },
83
+ ];
84
+ export const TRC20_TRANSFER_TOPIC = 'ddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef';
85
+ export const DEFAULT_TRX_FEE_LIMIT_SUN = 300_000;
86
+ export const DEFAULT_TRC20_TRANSFER_FEE_LIMIT_SUN = 15_000_000;
87
+ export const DEFAULT_TRC20_APPROVE_FEE_LIMIT_SUN = 25_000_000;
88
+ export function zeroResetApprovalKey(chainId, contractAddress) {
89
+ return `${chainId}:${contractAddress}`;
90
+ }
91
+ export const ZERO_RESET_APPROVAL_TRC20_ADDRESSES = new Set([
92
+ zeroResetApprovalKey(CHAIN_ID_TRON_MAINNET, 'TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t'),
93
+ ]);
94
+ export class TronAddressUtils {
95
+ static isHex(value) {
96
+ return isPyInt(value, 16);
97
+ }
98
+ static hexToVisible(hexAddress) {
99
+ if (hexAddress.startsWith('T')) {
100
+ throw new ChainError(ChainErrorKinds.InvalidAddress, `Tron hex address must not start with "T", got ${hexAddress}`, { address: hexAddress });
101
+ }
102
+ if (!TronAddressUtils.isHex(hexAddress)) {
103
+ throw new ChainError(ChainErrorKinds.InvalidAddress, `Invalid hex address: ${hexAddress}`, { address: hexAddress });
104
+ }
105
+ if (!hexAddress.startsWith('41')) {
106
+ throw new ChainError(ChainErrorKinds.InvalidAddress, `Tron hex address must start with 41, got ${hexAddress}`, { address: hexAddress });
107
+ }
108
+ return toBase58CheckAddress(hexAddress);
109
+ }
110
+ }
111
+ export class TronSignedMessage extends AbstractSignedMessage {
112
+ constructor(signature) {
113
+ super(signature);
114
+ }
115
+ }
116
+ export class TronChain extends Chain {
117
+ chainAgnosticNamespace = null;
118
+ defaultRpcUrl;
119
+ rpcUrl;
120
+ #trongridApiKey;
121
+ _client = null;
122
+ _chainParameters = null;
123
+ _nativeAsset;
124
+ assetDecimalsByIdentifier = new Map();
125
+ constructor(init) {
126
+ super(init.chainId, init.name, NetworkType.TRON, 3.0, 'TRX', init.explorerUrl);
127
+ this.defaultRpcUrl = init.defaultRpcUrl;
128
+ this.rpcUrl = init.rpcUrl ?? null;
129
+ this.#trongridApiKey = init.trongridApiKey ?? null;
130
+ this._nativeAsset = new TronAsset(init.chainId, 'TRX', null, TronAsset.NATIVE_DECIMALS);
131
+ }
132
+ get client() {
133
+ if (this._client === null) {
134
+ const env = readEnv();
135
+ if (this.#trongridApiKey === null && env?.TRONGRID_API_KEY !== undefined) {
136
+ this.#trongridApiKey = env.TRONGRID_API_KEY;
137
+ }
138
+ this.rpcUrl = this.rpcUrl || this._loadRpcUrl();
139
+ this._client = new TronClient({
140
+ endpointUri: this.rpcUrl,
141
+ apiKey: this.#trongridApiKey ?? undefined,
142
+ });
143
+ }
144
+ return this._client;
145
+ }
146
+ get trx() {
147
+ return new TronTrx(this.client);
148
+ }
149
+ static rpcUrlEnvForChainId(chainId) {
150
+ return `TRON_${chainId}_RPC_URL`;
151
+ }
152
+ _loadRpcUrl() {
153
+ const env = readEnv();
154
+ const candidates = [`${this.name.replace(/ /g, '_').toUpperCase()}_RPC_URL`, TronChain.rpcUrlEnvForChainId(this.chainId)];
155
+ for (const name of candidates) {
156
+ const value = env?.[name];
157
+ if (value)
158
+ return value;
159
+ }
160
+ return this.defaultRpcUrl;
161
+ }
162
+ get chainType() {
163
+ return ChainType.TRON;
164
+ }
165
+ get nativeAsset() {
166
+ return this._nativeAsset;
167
+ }
168
+ get nativeToken() {
169
+ return this._nativeAsset;
170
+ }
171
+ toString() {
172
+ return `TronChain[chain_id:${this.chainId}]`;
173
+ }
174
+ getTrc20Asset(symbol, contractAddress, decimals) {
175
+ return new TronAsset(this.chainId, symbol, contractAddress, decimals);
176
+ }
177
+ getAssetExplorerUrl(asset) {
178
+ if (!(asset instanceof TronAsset)) {
179
+ throw new ChainError(ChainErrorKinds.InvalidArgument, `Invalid asset ${String(asset)}, expected TronAsset`, { chainId: this.chainId });
180
+ }
181
+ if (asset.isNative())
182
+ return null;
183
+ return `${this.explorerBaseUrl}/#/token20/${asset.contractAddress}`;
184
+ }
185
+ getWalletAddressExplorerUrl(walletAddress) {
186
+ return `${this.explorerBaseUrl}/#/address/${walletAddress}`;
187
+ }
188
+ getTransactionExplorerUrl(txHash) {
189
+ return `${this.explorerBaseUrl}/#/transaction/${txHash}`;
190
+ }
191
+ getWalletExplorerUrl(address) {
192
+ return this.getWalletAddressExplorerUrl(address);
193
+ }
194
+ getTokenExplorerUrl(tokenIdentifier) {
195
+ if (!tokenIdentifier)
196
+ return this.explorerBaseUrl;
197
+ return `${this.explorerBaseUrl}/#/token20/${tokenIdentifier}`;
198
+ }
199
+ static formatAssetIdentifier(identifier) {
200
+ if (identifier === null || identifier === undefined)
201
+ return null;
202
+ return toBase58CheckAddress(identifier);
203
+ }
204
+ static validateAssetIdentifier(identifier) {
205
+ if (identifier !== null && identifier !== undefined && !isBase58CheckAddress(identifier)) {
206
+ throw new ChainError(ChainErrorKinds.InvalidTokenIdentifier, `Invalid Tron asset contract address ${identifier}`, { identifier });
207
+ }
208
+ }
209
+ static formatWalletAddress(walletAddress) {
210
+ return toBase58CheckAddress(walletAddress);
211
+ }
212
+ static validateWalletAddress(walletAddress) {
213
+ if (!isBase58CheckAddress(walletAddress)) {
214
+ throw new ChainError(ChainErrorKinds.InvalidAddress, `Invalid Tron wallet address ${walletAddress}`, { address: walletAddress });
215
+ }
216
+ }
217
+ validateAddress(raw) {
218
+ return typeof raw === 'string' && TronAddress.validatedBase58(raw) !== null;
219
+ }
220
+ validateTokenIdentifier(raw) {
221
+ if (raw === undefined)
222
+ return true;
223
+ try {
224
+ TronChain.validateAssetIdentifier(raw);
225
+ return true;
226
+ }
227
+ catch {
228
+ return false;
229
+ }
230
+ }
231
+ async resolveAsset(identifier) {
232
+ if (identifier === null || identifier === undefined)
233
+ return this._nativeAsset;
234
+ const contract = await this.getTrc20Contract(identifier);
235
+ const decimals = Number(await contract.callView('decimals'));
236
+ const symbol = String(await contract.callView('symbol'));
237
+ return new TronAsset(this.chainId, symbol, identifier, decimals);
238
+ }
239
+ createAsset(symbol, identifier, decimals) {
240
+ if (identifier === null || identifier === undefined) {
241
+ if (symbol !== 'TRX') {
242
+ throw new ChainError(ChainErrorKinds.InvalidArgument, `Invalid symbol ${symbol} for native TRX, expected TRX`, { chainId: this.chainId });
243
+ }
244
+ if (decimals !== TronAsset.NATIVE_DECIMALS) {
245
+ throw new ChainError(ChainErrorKinds.InvalidArgument, `Invalid decimals ${decimals} for native TRX, expected ${TronAsset.NATIVE_DECIMALS}`, { chainId: this.chainId });
246
+ }
247
+ return this._nativeAsset;
248
+ }
249
+ return new TronAsset(this.chainId, symbol, identifier, decimals);
250
+ }
251
+ async getTrc20Contract(contractAddress) {
252
+ const address = toBase58CheckAddress(contractAddress);
253
+ const info = await this.client.getContract(address);
254
+ const contract = TronContract.fromContractInfo(address, info, this.client);
255
+ contract.abi = TRC20_ABI;
256
+ return contract;
257
+ }
258
+ async getCachedAssetDecimals(symbol, identifier) {
259
+ if (identifier === null || identifier === undefined) {
260
+ if (symbol !== this._nativeAsset.symbol) {
261
+ throw new ChainError(ChainErrorKinds.InvalidArgument, `Invalid native asset symbol ${symbol}`, { chainId: this.chainId });
262
+ }
263
+ return this._nativeAsset.decimals;
264
+ }
265
+ let decimals = this.assetDecimalsByIdentifier.get(identifier);
266
+ if (decimals === undefined) {
267
+ decimals = await this._getAssetDecimalsInternal(symbol, identifier);
268
+ this.assetDecimalsByIdentifier.set(identifier, decimals);
269
+ }
270
+ return decimals;
271
+ }
272
+ async _getAssetDecimalsInternal(symbol, identifier) {
273
+ if (identifier === null || identifier === undefined) {
274
+ if (symbol !== 'TRX') {
275
+ throw new ChainError(ChainErrorKinds.InvalidArgument, `Invalid symbol ${symbol} for native TRX`, { chainId: this.chainId });
276
+ }
277
+ return TronAsset.NATIVE_DECIMALS;
278
+ }
279
+ const contract = await this.getTrc20Contract(identifier);
280
+ return Number(await contract.callView('decimals'));
281
+ }
282
+ async _getTrc20SymbolInternal(address) {
283
+ const contract = await this.getTrc20Contract(address);
284
+ return String(await contract.callView('symbol'));
285
+ }
286
+ async getAssetBalance(asset, ownerAddress) {
287
+ this.assertOwnAsset(asset);
288
+ const owner = TronChain.formatWalletAddress(ownerAddress);
289
+ if (asset.isNative()) {
290
+ try {
291
+ return await this.client.getAccountBalance(owner);
292
+ }
293
+ catch (err) {
294
+ if (err instanceof TronAddressNotFoundError)
295
+ return new Decimal(0);
296
+ throw err;
297
+ }
298
+ }
299
+ const contract = await this.getTrc20Contract(asset.contractAddress);
300
+ const balanceMr = await contract.callView('balanceOf', owner);
301
+ return new Decimal(minorUnitsToHrString(BigInt(balanceMr), asset.decimals));
302
+ }
303
+ async createTransferTransaction(req) {
304
+ this.assertOwnAsset(req.asset);
305
+ const sender = TronChain.formatWalletAddress(req.senderAddress);
306
+ const receiver = TronChain.formatWalletAddress(req.receiverAddress);
307
+ const feeLimitSun = this._resolveGasPricing(req.gasPricing === undefined ? FeePriority.NORMAL : req.gasPricing);
308
+ let amountMr = hrDecimalToMinorUnits(new Decimal(req.amountHr.toString()), req.asset.decimals);
309
+ let builder;
310
+ if (req.asset.isNative()) {
311
+ if (req.isFullBalance === true) {
312
+ amountMr = amountMr - BigInt(DEFAULT_TRX_FEE_LIMIT_SUN);
313
+ }
314
+ builder = this.trx.transfer(sender, receiver, amountMr);
315
+ }
316
+ else {
317
+ const contract = await this.getTrc20Contract(req.asset.contractAddress);
318
+ builder = (await contract.buildCall('transfer', receiver, amountMr)).withOwner(sender).feeLimit(feeLimitSun);
319
+ }
320
+ if (req.memo !== undefined && req.memo !== null) {
321
+ builder = builder.memo(req.memo);
322
+ }
323
+ const transaction = await builder.build();
324
+ return new UnsignedTransactionWithPrerequisites({
325
+ transaction: new TronUnsignedTransaction({ chainId: this.chainId, transaction }),
326
+ prerequisites: [],
327
+ });
328
+ }
329
+ _resolveGasPricing(gasPricing) {
330
+ if (isAbstractGasPricing(gasPricing)) {
331
+ if (!(gasPricing instanceof TronGasPricing)) {
332
+ throw new ChainError(ChainErrorKinds.InvalidArgument, `Expected TronGasPricing for a Tron chain, got ${gasPricing.constructor.name}`, { chainId: this.chainId });
333
+ }
334
+ return gasPricing.feeLimitSun;
335
+ }
336
+ if (!Object.values(FeePriority).includes(gasPricing)) {
337
+ throw new ChainError(ChainErrorKinds.InvalidArgument, `Unsupported gas_pricing ${pyRepr(gasPricing)}`, { chainId: this.chainId });
338
+ }
339
+ return DEFAULT_TRC20_TRANSFER_FEE_LIMIT_SUN;
340
+ }
341
+ async _simulateTriggerSmartContract(ownerAddressB58, contractAddressB58, callDataHex) {
342
+ return this.client.makeRequest('wallet/triggerconstantcontract', {
343
+ owner_address: ownerAddressB58,
344
+ contract_address: contractAddressB58,
345
+ data: callDataHex,
346
+ visible: true,
347
+ });
348
+ }
349
+ static _interpretTriggerConstantResponse(response) {
350
+ const resultObj = response.result;
351
+ let apiOk;
352
+ let apiCode = null;
353
+ let apiMessage = null;
354
+ if (resultObj !== null && typeof resultObj === 'object' && !Array.isArray(resultObj)) {
355
+ const result = resultObj;
356
+ apiOk = pyTruthy(result.result ?? false);
357
+ apiCode = result.code ?? null;
358
+ apiMessage = result.message ?? null;
359
+ }
360
+ else {
361
+ apiOk = pyTruthy(resultObj);
362
+ }
363
+ const energyUsedRaw = response.energy_used;
364
+ const energyUsed = typeof energyUsedRaw === 'number' ? Math.trunc(energyUsedRaw) : typeof energyUsedRaw === 'boolean' ? Number(energyUsedRaw) : null;
365
+ const ok = apiOk && apiCode === null && apiMessage === null;
366
+ let error = null;
367
+ if (!ok) {
368
+ let message = apiMessage;
369
+ if (typeof message === 'string' && /^[0-9a-fA-F]*$/.test(message) && message.length % 2 === 0) {
370
+ message = pyDecodeUtf8(Buffer.from(message, 'hex'), 'replace');
371
+ }
372
+ error = new Error(`Tron constant call failed: code=${pyRepr(apiCode)} message=${pyRepr(message)}`);
373
+ }
374
+ return [ok, energyUsed, error];
375
+ }
376
+ async getChainParameters() {
377
+ if (this._chainParameters === null) {
378
+ this._chainParameters = (async () => {
379
+ const params = await this.client.getChainParameters();
380
+ const energyFee = params.filter((kv) => kv.key === 'getEnergyFee').map((kv) => kv.value);
381
+ if (energyFee.length === 0 || energyFee[0] === undefined || energyFee[0] === null) {
382
+ throw new ChainError(ChainErrorKinds.RpcError, 'Tron chain parameters are missing getEnergyFee', { chainId: this.chainId });
383
+ }
384
+ return { energyFee: Math.trunc(Number(energyFee[0])) };
385
+ })();
386
+ this._chainParameters.catch(() => {
387
+ this._chainParameters = null;
388
+ });
389
+ }
390
+ return this._chainParameters;
391
+ }
392
+ async supportsFullTransactionSimulation(unsignedTransaction) {
393
+ if (!(unsignedTransaction instanceof TronUnsignedTransaction)) {
394
+ throw new ChainError(ChainErrorKinds.InvalidArgument, 'Expected TronUnsignedTransaction', { chainId: this.chainId });
395
+ }
396
+ return false;
397
+ }
398
+ async simulateTransaction(req) {
399
+ const transaction = req.transaction;
400
+ if (!(transaction instanceof TronUnsignedTransaction)) {
401
+ throw new ChainError(ChainErrorKinds.InvalidArgument, `Invalid transaction ${String(transaction)}, expected TronUnsignedTransaction`, { chainId: this.chainId });
402
+ }
403
+ const contracts = pyItem(transaction.transaction.rawData, 'contract');
404
+ if (!pyTruthy(contracts)) {
405
+ throw new ChainError(ChainErrorKinds.InvalidArgument, `Tron unsigned transaction ${transaction.txId} has no contract entries`, { chainId: this.chainId });
406
+ }
407
+ const entry = contracts[0];
408
+ if (pyItem(entry, 'type') === 'TriggerSmartContract') {
409
+ const triggerValue = pyItem(pyItem(entry, 'parameter'), 'value');
410
+ const rawData = String(pyItem(triggerValue, 'data') || '').toLowerCase();
411
+ const callDataHex = rawData.startsWith('0x') ? rawData.slice(2) : rawData;
412
+ let ownerB58;
413
+ let contractB58;
414
+ try {
415
+ ownerB58 = TronAddressUtils.hexToVisible(pyItem(triggerValue, 'owner_address'));
416
+ contractB58 = TronAddressUtils.hexToVisible(pyItem(triggerValue, 'contract_address'));
417
+ }
418
+ catch (err) {
419
+ return new TronTransactionSimulationResult({
420
+ chainId: this.chainId,
421
+ statusType: TransactionSimulationStatusTypes.Failed,
422
+ balanceChanges: new Map(),
423
+ error: new Error(`Malformed TriggerSmartContract addresses in canonical tx: ${err.message}`),
424
+ energyUsed: null,
425
+ });
426
+ }
427
+ let response;
428
+ try {
429
+ response = await this._simulateTriggerSmartContract(ownerB58, contractB58, callDataHex);
430
+ }
431
+ catch (err) {
432
+ return new TronTransactionSimulationResult({
433
+ chainId: this.chainId,
434
+ statusType: TransactionSimulationStatusTypes.Failed,
435
+ balanceChanges: new Map(),
436
+ error: err instanceof Error ? err : new Error(String(err)),
437
+ energyUsed: null,
438
+ });
439
+ }
440
+ const [ok, energyUsed, error] = TronChain._interpretTriggerConstantResponse(response);
441
+ return new TronTransactionSimulationResult({
442
+ chainId: this.chainId,
443
+ statusType: ok ? TransactionSimulationStatusTypes.Success : TransactionSimulationStatusTypes.Failed,
444
+ balanceChanges: new Map(),
445
+ error,
446
+ energyUsed,
447
+ });
448
+ }
449
+ throw new ChainError(ChainErrorKinds.FeatureNotSupported, `Tron does not expose a gas-reporting simulation primitive for contract type ${pyRepr(entry.type)}. Only TriggerSmartContract can be dry-run via wallet/triggerconstantcontract — every other contract type is a deterministic protocol operation with no VM execution and no energy usage to report.`, { chainId: this.chainId });
450
+ }
451
+ static _toBase58CheckAny(addressStr) {
452
+ if (!addressStr) {
453
+ throw new ChainError(ChainErrorKinds.InvalidAddress, 'empty address');
454
+ }
455
+ try {
456
+ return toBase58CheckAddress(addressStr);
457
+ }
458
+ catch {
459
+ return toBase58CheckAddress(`41${addressStr.startsWith('0x') ? addressStr.slice(2) : addressStr}`);
460
+ }
461
+ }
462
+ static _parseTrc20TransferLog(log) {
463
+ const topics = log.topics || [];
464
+ if (topics.length === 0 || topics[0].toLowerCase() !== TRC20_TRANSFER_TOPIC)
465
+ return null;
466
+ if (topics.length < 3)
467
+ return null;
468
+ let from;
469
+ let to;
470
+ try {
471
+ from = TronChain._toBase58CheckAny(topics[1].slice(-40));
472
+ to = TronChain._toBase58CheckAny(topics[2].slice(-40));
473
+ }
474
+ catch {
475
+ return null;
476
+ }
477
+ const dataHex = String(log.data || '').replace(/^[0x]+/, '');
478
+ if (!dataHex)
479
+ return null;
480
+ const value = pyInt(dataHex, 16);
481
+ let contract;
482
+ try {
483
+ contract = TronChain._toBase58CheckAny(String(log.address || ''));
484
+ }
485
+ catch {
486
+ contract = null;
487
+ }
488
+ return { contract, from, to, value };
489
+ }
490
+ async _balanceChangesFromInfo(args) {
491
+ const balanceChanges = new Map();
492
+ const native = this._nativeAsset;
493
+ const add = (wallet, asset, change) => {
494
+ if (args.filteredWallets !== null && !args.filteredWallets.has(wallet))
495
+ return;
496
+ if (args.filteredAssets !== null && !args.filteredAssets.some((a) => a.strictEquals(asset)))
497
+ return;
498
+ AssetBalanceChange.upsert(balanceChanges, wallet, asset, change);
499
+ };
500
+ const rawContracts = (args.txData.raw_data || {}).contract || [];
501
+ for (const c of rawContracts) {
502
+ if (c.type !== 'TransferContract')
503
+ continue;
504
+ const value = (c.parameter || {}).value || {};
505
+ let owner;
506
+ let to;
507
+ try {
508
+ owner = TronChain._toBase58CheckAny(String(value.owner_address ?? ''));
509
+ to = TronChain._toBase58CheckAny(String(value.to_address ?? ''));
510
+ }
511
+ catch {
512
+ continue;
513
+ }
514
+ const amountMr = BigInt(String(value.amount ?? 0));
515
+ add(owner, native, AssetBalanceChange.fromMr(-amountMr, native.decimals));
516
+ add(to, native, AssetBalanceChange.fromMr(amountMr, native.decimals));
517
+ }
518
+ for (const internalTransaction of args.info.internal_transactions || []) {
519
+ const callValueInfo = internalTransaction.callValueInfo;
520
+ if (callValueInfo === undefined || callValueInfo === null)
521
+ continue;
522
+ if (callValueInfo.length === 0) {
523
+ throw new ChainError(ChainErrorKinds.TransactionDecodeFailed, 'Tron internal transaction has an empty callValueInfo list', { chainId: this.chainId });
524
+ }
525
+ if (callValueInfo[0].callValue === undefined || callValueInfo[0].callValue === null)
526
+ continue;
527
+ const callValue = BigInt(String(callValueInfo[0].callValue));
528
+ const senderAddress = TronChain._toBase58CheckAny(String(internalTransaction.caller_address));
529
+ const receiverAddress = TronChain._toBase58CheckAny(String(internalTransaction.transferTo_address));
530
+ add(senderAddress, native, AssetBalanceChange.fromMr(-callValue, native.decimals));
531
+ add(receiverAddress, native, AssetBalanceChange.fromMr(callValue, native.decimals));
532
+ }
533
+ for (const log of args.info.log || []) {
534
+ const parsed = TronChain._parseTrc20TransferLog(log);
535
+ if (parsed === null || parsed.contract === null)
536
+ continue;
537
+ const contractAddress = parsed.contract;
538
+ let asset = TronAsset.searchRegisteredAsset(this.chainId, contractAddress);
539
+ if (asset === null) {
540
+ const symbol = await this._getTrc20SymbolInternal(contractAddress);
541
+ asset = TronAsset.getRegisteredAsset(this.chainId, symbol, contractAddress);
542
+ if (asset === null) {
543
+ const decimals = await this._getAssetDecimalsInternal(symbol, contractAddress);
544
+ asset = this.getTrc20Asset(symbol, contractAddress, decimals);
545
+ }
546
+ }
547
+ add(parsed.from, asset, AssetBalanceChange.fromMr(-parsed.value, asset.decimals));
548
+ add(parsed.to, asset, AssetBalanceChange.fromMr(parsed.value, asset.decimals));
549
+ }
550
+ if (args.feeInSun > 0 && rawContracts.length > 0) {
551
+ const payerHex = ((rawContracts[0].parameter || {}).value || {}).owner_address;
552
+ if (payerHex) {
553
+ try {
554
+ const payer = TronChain._toBase58CheckAny(String(payerHex));
555
+ add(payer, native, AssetBalanceChange.fromMr(-BigInt(args.feeInSun), native.decimals));
556
+ }
557
+ catch {
558
+ void 0;
559
+ }
560
+ }
561
+ }
562
+ return balanceChanges;
563
+ }
564
+ async getTransactionStatus(txHash, opts) {
565
+ if (opts?.wait || (opts?.confirmations !== undefined && opts.confirmations > 1)) {
566
+ throw new ChainError(ChainErrorKinds.FeatureNotSupported, 'TronChain.getTransactionStatus does not honor wait/confirmations opts (would silently return immediately). Poll consumer-side or omit the opts.', { chainId: this.chainId });
567
+ }
568
+ if (Array.isArray(txHash)) {
569
+ return runBatch(txHash, (hash) => this.getTransactionStatusOnce(hash, opts));
570
+ }
571
+ return this.getTransactionStatusOnce(txHash, opts);
572
+ }
573
+ async getTransactionStatusOnce(txHash, opts) {
574
+ let info;
575
+ try {
576
+ info = await this.client.getTransactionInfo(txHash);
577
+ }
578
+ catch (err) {
579
+ if (err instanceof TronTransactionNotFoundError || isChainError(err, ChainErrorKinds.InvalidArgument)) {
580
+ return TronTransactionStatus.notFound(this.chainId, { code: 'NOT_FOUND', reason: err.message });
581
+ }
582
+ throw err;
583
+ }
584
+ if (Object.keys(info).length === 0 || info.id === undefined || info.id === null) {
585
+ return TronTransactionStatus.pending(this.chainId);
586
+ }
587
+ const receipt = info.receipt || {};
588
+ const contractRet = receipt.result ?? null;
589
+ const hasFailure = info.result === 'FAILED' || (contractRet !== null && contractRet !== 'SUCCESS');
590
+ let txData;
591
+ try {
592
+ txData = await this.client.getTransaction(txHash);
593
+ }
594
+ catch (err) {
595
+ if (!(err instanceof TronTransactionNotFoundError))
596
+ throw err;
597
+ txData = {};
598
+ }
599
+ const filteredWallets = opts?.filteredWallets === undefined || opts.filteredWallets === null
600
+ ? null
601
+ : new Set([...opts.filteredWallets].map((w) => TronChain.formatWalletAddress(w)));
602
+ const filteredAssets = opts?.filteredAssets === undefined || opts.filteredAssets === null ? null : [...opts.filteredAssets];
603
+ const chainParameters = await this.getChainParameters();
604
+ if (info.blockTimeStamp === undefined || info.blockTimeStamp === null) {
605
+ throw new ChainError(ChainErrorKinds.TransactionDecodeFailed, `Tron transaction info for ${txHash} has no blockTimeStamp`, {
606
+ chainId: this.chainId,
607
+ txHash,
608
+ });
609
+ }
610
+ const inclusionAt = new Date(Math.trunc(Number(info.blockTimeStamp)));
611
+ const fees = TronTransactionFees.fromTransactionInfo(info, chainParameters.energyFee);
612
+ const balanceChanges = await this._balanceChangesFromInfo({
613
+ txData,
614
+ info,
615
+ feeInSun: fees.feeInSun,
616
+ filteredWallets,
617
+ filteredAssets,
618
+ });
619
+ if (!hasFailure) {
620
+ return TronTransactionStatus.successful({ chainId: this.chainId, inclusionAt, balanceChanges, fees });
621
+ }
622
+ return TronTransactionStatus.failed({
623
+ chainId: this.chainId,
624
+ inclusionAt,
625
+ error: {
626
+ code: String(contractRet ?? info.result ?? 'FAILED'),
627
+ reason: `Tron transaction failed: result=${pyRepr(info.result ?? null)}, contractRet=${pyRepr(contractRet)}`,
628
+ },
629
+ fees,
630
+ });
631
+ }
632
+ async broadcastSignedTransaction(signedTransaction) {
633
+ if (!(signedTransaction instanceof TronSignedTransaction)) {
634
+ throw new ChainError(ChainErrorKinds.InvalidArgument, `Invalid transaction ${String(signedTransaction)}, expected TronSignedTransaction`, { chainId: this.chainId });
635
+ }
636
+ if (this.chainId !== signedTransaction.chainId) {
637
+ throw new ChainError(ChainErrorKinds.InvalidArgument, `Invalid chain for signed transaction chain for ${String(signedTransaction)} with chain id ${signedTransaction.chainId}, expected ${this.chainId}`, { chainId: this.chainId });
638
+ }
639
+ if (signedTransaction.signedTransaction.client === null) {
640
+ signedTransaction.signedTransaction.client = this.client;
641
+ }
642
+ try {
643
+ const result = await signedTransaction.signedTransaction.broadcast();
644
+ const txHash = pyTruthy(result.txid) ? result.txid : signedTransaction.txHash;
645
+ return new TronBroadcastTransactionResponse({ chain: this, txHash });
646
+ }
647
+ catch (err) {
648
+ return new TronBroadcastTransactionResponse({
649
+ chain: this,
650
+ txHash: signedTransaction.txHash,
651
+ broadcastError: err instanceof Error ? err : new Error(String(err)),
652
+ });
653
+ }
654
+ }
655
+ static verifySignature(publicKey, message, signedMessage) {
656
+ if (!(signedMessage instanceof TronSignedMessage)) {
657
+ throw new ChainError(ChainErrorKinds.InvalidArgument, `TronChain expected a TronSignedMessage, got ${pyTypeRepr(signedMessage)}`);
658
+ }
659
+ try {
660
+ const resolvedPublicKey = publicKey instanceof TronPublicKey
661
+ ? publicKey
662
+ : TronPublicKey.fromHex(String(publicKey).startsWith('0x') ? String(publicKey).slice(2) : String(publicKey));
663
+ const signature = TronSignature.fromHex(signedMessage.signature);
664
+ return resolvedPublicKey.verifyMsg(pyEncodeUtf8(message), signature);
665
+ }
666
+ catch {
667
+ return false;
668
+ }
669
+ }
670
+ verifySignature(publicKey, message, signedMessage) {
671
+ return TronChain.verifySignature(publicKey, message, signedMessage);
672
+ }
673
+ async getBalance(owner, tokenIdentifier) {
674
+ const ownerAddress = TronChain.formatWalletAddress(owner);
675
+ if (tokenIdentifier === undefined) {
676
+ try {
677
+ const account = await this.client.getAccount(ownerAddress);
678
+ return BigInt(String(account.balance ?? 0));
679
+ }
680
+ catch (err) {
681
+ if (err instanceof TronAddressNotFoundError)
682
+ return 0n;
683
+ throw err;
684
+ }
685
+ }
686
+ const contract = await this.getTrc20Contract(tokenIdentifier);
687
+ return BigInt(await contract.callView('balanceOf', ownerAddress));
688
+ }
689
+ async createTransferUnsignedTransaction(req) {
690
+ if (!req.from) {
691
+ throw new ChainError(ChainErrorKinds.InvalidArgument, 'Tron transfers require an explicit `from` (no implicit signer)', {
692
+ chainId: this.chainId,
693
+ });
694
+ }
695
+ const asset = await this.resolveAsset(req.tokenIdentifier);
696
+ const resolved = resolveTransferAmount(req, asset.decimals);
697
+ const amountHr = resolved.kind === 'exact'
698
+ ? new Decimal(minorUnitsToHrString(resolved.amountMr, asset.decimals))
699
+ : await this.getAssetBalance(asset, req.from);
700
+ const bundle = await this.createTransferTransaction({
701
+ asset,
702
+ amountHr,
703
+ senderAddress: req.from,
704
+ receiverAddress: req.to,
705
+ isFullBalance: resolved.kind === 'full',
706
+ gasPricing: req.gasPricing,
707
+ memo: req.memo ?? null,
708
+ });
709
+ return bundle.transaction;
710
+ }
711
+ async broadcast(signed, opts) {
712
+ if (opts && opts.signal !== undefined) {
713
+ throw new ChainError(ChainErrorKinds.FeatureNotSupported, "Tron broadcast: signal is not honored (silently ignoring would let a caller conclude 'not sent' while the tx still lands)", { chainId: this.chainId });
714
+ }
715
+ const signedTransaction = parseTronSignedInput(signed, this.chainId);
716
+ const response = await this.broadcastSignedTransaction(signedTransaction);
717
+ const error = response.broadcastError;
718
+ if (error === null) {
719
+ const signedHash = signedTransaction.txHash.toLowerCase();
720
+ if (typeof response.txHash !== 'string' || response.txHash.toLowerCase() !== signedHash) {
721
+ throw new ChainError(ChainErrorKinds.RpcError, `Tron node answered txid ${pyRepr(response.txHash)} for the signed transaction ${signedTransaction.txHash}`, { chainId: this.chainId, txHash: signedTransaction.txHash });
722
+ }
723
+ return signedHash;
724
+ }
725
+ throw new ChainError(tronBroadcastErrorKind(error), `Tron broadcast failed: ${error.message}`, { chainId: this.chainId, txHash: response.txHash }, error);
726
+ }
727
+ async getChainTipHeight() {
728
+ return this.client.getLatestBlockNumber();
729
+ }
730
+ async verifyMessageSignature(req) {
731
+ try {
732
+ const signature = TronSignature.fromHex(req.signature.startsWith('0x') ? req.signature.slice(2) : req.signature);
733
+ const recovered = signature.recoverPublicKeyFromMsg(pyEncodeUtf8(req.message));
734
+ return recovered.toBase58CheckAddress() === toBase58CheckAddress(req.signer);
735
+ }
736
+ catch {
737
+ return false;
738
+ }
739
+ }
740
+ assertOwnAsset(asset) {
741
+ if (!(asset instanceof TronAsset)) {
742
+ throw new ChainError(ChainErrorKinds.InvalidArgument, `Invalid asset ${String(asset)} of type ${pyTypeRepr(asset)}, expected TronAsset`, {
743
+ chainId: this.chainId,
744
+ });
745
+ }
746
+ if (asset.chainId !== this.chainId) {
747
+ throw new ChainError(ChainErrorKinds.InvalidArgument, `Invalid chain id ${asset.chainId} for asset ${String(asset)}, expected ${this.chainId}`, { chainId: this.chainId });
748
+ }
749
+ }
750
+ }
751
+ const TRON_NOT_ACCEPTED_BROADCAST_CODES = new Set(['SIGERROR', 'CONTRACT_VALIDATE_ERROR', 'CONTRACT_EXE_ERROR', 'BANDWITH_ERROR']);
752
+ export function tronBroadcastErrorKind(error) {
753
+ if (!(error instanceof TronApiError) || error.code === null)
754
+ return ChainErrorKinds.RpcError;
755
+ if (error.code === 'TOO_BIG_TRANSACTION_ERROR')
756
+ return ChainErrorKinds.TransactionTooLarge;
757
+ return TRON_NOT_ACCEPTED_BROADCAST_CODES.has(error.code) ? ChainErrorKinds.BroadcastRejected : ChainErrorKinds.RpcError;
758
+ }
759
+ function parseTronSignedInput(signed, chainId) {
760
+ let payload;
761
+ try {
762
+ payload = coerceJsonDict(signed);
763
+ }
764
+ catch (err) {
765
+ throw new ChainError(ChainErrorKinds.InvalidArgument, 'Tron broadcast: signed must be the JSON of a TronSignedTransaction or of a signed Tron transaction ({txID, raw_data, signature})', { chainId }, err);
766
+ }
767
+ if (payload[JSON_TRANSACTION_TYPE_KEY] === TronSignedTransaction.JSON_TYPE) {
768
+ return TronSignedTransaction.fromJson(payload);
769
+ }
770
+ if (typeof payload.txID !== 'string' || payload.raw_data === undefined) {
771
+ throw new ChainError(ChainErrorKinds.InvalidArgument, 'Tron broadcast: signed transaction JSON must carry txID and raw_data', { chainId });
772
+ }
773
+ assertJsonSafeIntegers(payload);
774
+ return new TronSignedTransaction({ chainId, signedTransaction: tronTransactionFromJson(payload) });
775
+ }
776
+ async function runBatch(items, fetchOne) {
777
+ const results = new Array(items.length);
778
+ let cursor = 0;
779
+ let aborted = false;
780
+ const spawn = async () => {
781
+ while (!aborted) {
782
+ const idx = cursor++;
783
+ if (idx >= items.length)
784
+ return;
785
+ try {
786
+ results[idx] = await fetchOne(items[idx]);
787
+ }
788
+ catch (err) {
789
+ aborted = true;
790
+ throw err;
791
+ }
792
+ }
793
+ };
794
+ await Promise.all(Array.from({ length: Math.min(8, items.length) }, spawn));
795
+ return results;
796
+ }
797
+ function readEnv() {
798
+ return globalThis.process?.env;
799
+ }
800
+ export { pyTruthy };
801
+ //# sourceMappingURL=tron_chain.js.map