@ledgerhq/coin-tron 0.0.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 (351) hide show
  1. package/.eslintrc.js +20 -0
  2. package/.turbo/turbo-build.log +4 -0
  3. package/.unimportedrc.json +25 -0
  4. package/LICENSE.txt +21 -0
  5. package/jest.config.js +8 -0
  6. package/jest.integ.config.js +7 -0
  7. package/lib/bridge/broadcast.d.ts +7 -0
  8. package/lib/bridge/broadcast.d.ts.map +1 -0
  9. package/lib/bridge/broadcast.js +26 -0
  10. package/lib/bridge/broadcast.js.map +1 -0
  11. package/lib/bridge/buildOptimisticOperation.d.ts +5 -0
  12. package/lib/bridge/buildOptimisticOperation.d.ts.map +1 -0
  13. package/lib/bridge/buildOptimisticOperation.js +101 -0
  14. package/lib/bridge/buildOptimisticOperation.js.map +1 -0
  15. package/lib/bridge/createTransaction.d.ts +4 -0
  16. package/lib/bridge/createTransaction.d.ts.map +1 -0
  17. package/lib/bridge/createTransaction.js +19 -0
  18. package/lib/bridge/createTransaction.js.map +1 -0
  19. package/lib/bridge/deviceTransactionConfig.d.ts +20 -0
  20. package/lib/bridge/deviceTransactionConfig.d.ts.map +1 -0
  21. package/lib/bridge/deviceTransactionConfig.js +65 -0
  22. package/lib/bridge/deviceTransactionConfig.js.map +1 -0
  23. package/lib/bridge/estimateMaxSpendable.d.ts +5 -0
  24. package/lib/bridge/estimateMaxSpendable.d.ts.map +1 -0
  25. package/lib/bridge/estimateMaxSpendable.js +30 -0
  26. package/lib/bridge/estimateMaxSpendable.js.map +1 -0
  27. package/lib/bridge/getEstimateFees.d.ts +6 -0
  28. package/lib/bridge/getEstimateFees.d.ts.map +1 -0
  29. package/lib/bridge/getEstimateFees.js +52 -0
  30. package/lib/bridge/getEstimateFees.js.map +1 -0
  31. package/lib/bridge/getTransactionStatus.d.ts +4 -0
  32. package/lib/bridge/getTransactionStatus.d.ts.map +1 -0
  33. package/lib/bridge/getTransactionStatus.js +218 -0
  34. package/lib/bridge/getTransactionStatus.js.map +1 -0
  35. package/lib/bridge/index.d.ts +10 -0
  36. package/lib/bridge/index.d.ts.map +1 -0
  37. package/lib/bridge/index.js +60 -0
  38. package/lib/bridge/index.js.map +1 -0
  39. package/lib/bridge/preload.d.ts +8 -0
  40. package/lib/bridge/preload.d.ts.map +1 -0
  41. package/lib/bridge/preload.js +32 -0
  42. package/lib/bridge/preload.js.map +1 -0
  43. package/lib/bridge/prepareTransaction.d.ts +4 -0
  44. package/lib/bridge/prepareTransaction.d.ts.map +1 -0
  45. package/lib/bridge/prepareTransaction.js +19 -0
  46. package/lib/bridge/prepareTransaction.js.map +1 -0
  47. package/lib/bridge/serialization.d.ts +9 -0
  48. package/lib/bridge/serialization.d.ts.map +1 -0
  49. package/lib/bridge/serialization.js +238 -0
  50. package/lib/bridge/serialization.js.map +1 -0
  51. package/lib/bridge/signOperation.d.ts +5 -0
  52. package/lib/bridge/signOperation.d.ts.map +1 -0
  53. package/lib/bridge/signOperation.js +92 -0
  54. package/lib/bridge/signOperation.js.map +1 -0
  55. package/lib/bridge/synchronization.d.ts +5 -0
  56. package/lib/bridge/synchronization.d.ts.map +1 -0
  57. package/lib/bridge/synchronization.integ.test.d.ts +3 -0
  58. package/lib/bridge/synchronization.integ.test.d.ts.map +1 -0
  59. package/lib/bridge/synchronization.integ.test.js +96 -0
  60. package/lib/bridge/synchronization.integ.test.js.map +1 -0
  61. package/lib/bridge/synchronization.js +216 -0
  62. package/lib/bridge/synchronization.js.map +1 -0
  63. package/lib/bridge/transaction.d.ts +15 -0
  64. package/lib/bridge/transaction.d.ts.map +1 -0
  65. package/lib/bridge/transaction.js +61 -0
  66. package/lib/bridge/transaction.js.map +1 -0
  67. package/lib/config.d.ts +10 -0
  68. package/lib/config.d.ts.map +1 -0
  69. package/lib/config.js +17 -0
  70. package/lib/config.js.map +1 -0
  71. package/lib/index.d.ts +3 -0
  72. package/lib/index.d.ts.map +1 -0
  73. package/lib/index.js +21 -0
  74. package/lib/index.js.map +1 -0
  75. package/lib/logic/constants.d.ts +4 -0
  76. package/lib/logic/constants.d.ts.map +1 -0
  77. package/lib/logic/constants.js +7 -0
  78. package/lib/logic/constants.js.map +1 -0
  79. package/lib/logic/pagination.d.ts +3 -0
  80. package/lib/logic/pagination.d.ts.map +1 -0
  81. package/lib/logic/pagination.js +19 -0
  82. package/lib/logic/pagination.js.map +1 -0
  83. package/lib/logic/utils.d.ts +12 -0
  84. package/lib/logic/utils.d.ts.map +1 -0
  85. package/lib/logic/utils.js +177 -0
  86. package/lib/logic/utils.js.map +1 -0
  87. package/lib/network/format.d.ts +9 -0
  88. package/lib/network/format.d.ts.map +1 -0
  89. package/lib/network/format.js +127 -0
  90. package/lib/network/format.js.map +1 -0
  91. package/lib/network/index.d.ts +30 -0
  92. package/lib/network/index.d.ts.map +1 -0
  93. package/lib/network/index.integ.test.d.ts +2 -0
  94. package/lib/network/index.integ.test.d.ts.map +1 -0
  95. package/lib/network/index.integ.test.js +38 -0
  96. package/lib/network/index.integ.test.js.map +1 -0
  97. package/lib/network/index.js +555 -0
  98. package/lib/network/index.js.map +1 -0
  99. package/lib/network/index.test.d.ts +2 -0
  100. package/lib/network/index.test.d.ts.map +1 -0
  101. package/lib/network/index.test.js +66 -0
  102. package/lib/network/index.test.js.map +1 -0
  103. package/lib/network/superRepresentativesData.mock.d.ts +69 -0
  104. package/lib/network/superRepresentativesData.mock.d.ts.map +1 -0
  105. package/lib/network/superRepresentativesData.mock.js +2208 -0
  106. package/lib/network/superRepresentativesData.mock.js.map +1 -0
  107. package/lib/network/types.d.ts +122 -0
  108. package/lib/network/types.d.ts.map +1 -0
  109. package/lib/network/types.fixture.d.ts +498 -0
  110. package/lib/network/types.fixture.d.ts.map +1 -0
  111. package/lib/network/types.fixture.js +4375 -0
  112. package/lib/network/types.fixture.js.map +1 -0
  113. package/lib/network/types.js +13 -0
  114. package/lib/network/types.js.map +1 -0
  115. package/lib/signer/getAddress.d.ts +6 -0
  116. package/lib/signer/getAddress.d.ts.map +1 -0
  117. package/lib/signer/getAddress.js +19 -0
  118. package/lib/signer/getAddress.js.map +1 -0
  119. package/lib/signer/index.d.ts +6 -0
  120. package/lib/signer/index.d.ts.map +1 -0
  121. package/lib/signer/index.js +11 -0
  122. package/lib/signer/index.js.map +1 -0
  123. package/lib/test/bot-deviceActions.d.ts +4 -0
  124. package/lib/test/bot-deviceActions.d.ts.map +1 -0
  125. package/lib/test/bot-deviceActions.js +103 -0
  126. package/lib/test/bot-deviceActions.js.map +1 -0
  127. package/lib/test/bot-specs.d.ts +7 -0
  128. package/lib/test/bot-specs.d.ts.map +1 -0
  129. package/lib/test/bot-specs.js +433 -0
  130. package/lib/test/bot-specs.js.map +1 -0
  131. package/lib/test/bridgeDatasetTest.d.ts +4 -0
  132. package/lib/test/bridgeDatasetTest.d.ts.map +1 -0
  133. package/lib/test/bridgeDatasetTest.js +1284 -0
  134. package/lib/test/bridgeDatasetTest.js.map +1 -0
  135. package/lib/test/cli.d.ts +52 -0
  136. package/lib/test/cli.d.ts.map +1 -0
  137. package/lib/test/cli.js +155 -0
  138. package/lib/test/cli.js.map +1 -0
  139. package/lib/test/index.d.ts +6 -0
  140. package/lib/test/index.d.ts.map +1 -0
  141. package/lib/test/index.js +26 -0
  142. package/lib/test/index.js.map +1 -0
  143. package/lib/types/bridge.d.ts +245 -0
  144. package/lib/types/bridge.d.ts.map +1 -0
  145. package/lib/types/bridge.js +16 -0
  146. package/lib/types/bridge.js.map +1 -0
  147. package/lib/types/errors.d.ts +49 -0
  148. package/lib/types/errors.d.ts.map +1 -0
  149. package/lib/types/errors.js +21 -0
  150. package/lib/types/errors.js.map +1 -0
  151. package/lib/types/index.d.ts +4 -0
  152. package/lib/types/index.d.ts.map +1 -0
  153. package/lib/types/index.js +21 -0
  154. package/lib/types/index.js.map +1 -0
  155. package/lib/types/signer.d.ts +10 -0
  156. package/lib/types/signer.d.ts.map +1 -0
  157. package/lib/types/signer.js +3 -0
  158. package/lib/types/signer.js.map +1 -0
  159. package/lib-es/bridge/broadcast.d.ts +7 -0
  160. package/lib-es/bridge/broadcast.d.ts.map +1 -0
  161. package/lib-es/bridge/broadcast.js +24 -0
  162. package/lib-es/bridge/broadcast.js.map +1 -0
  163. package/lib-es/bridge/buildOptimisticOperation.d.ts +5 -0
  164. package/lib-es/bridge/buildOptimisticOperation.d.ts.map +1 -0
  165. package/lib-es/bridge/buildOptimisticOperation.js +94 -0
  166. package/lib-es/bridge/buildOptimisticOperation.js.map +1 -0
  167. package/lib-es/bridge/createTransaction.d.ts +4 -0
  168. package/lib-es/bridge/createTransaction.d.ts.map +1 -0
  169. package/lib-es/bridge/createTransaction.js +14 -0
  170. package/lib-es/bridge/createTransaction.js.map +1 -0
  171. package/lib-es/bridge/deviceTransactionConfig.d.ts +20 -0
  172. package/lib-es/bridge/deviceTransactionConfig.d.ts.map +1 -0
  173. package/lib-es/bridge/deviceTransactionConfig.js +63 -0
  174. package/lib-es/bridge/deviceTransactionConfig.js.map +1 -0
  175. package/lib-es/bridge/estimateMaxSpendable.d.ts +5 -0
  176. package/lib-es/bridge/estimateMaxSpendable.d.ts.map +1 -0
  177. package/lib-es/bridge/estimateMaxSpendable.js +25 -0
  178. package/lib-es/bridge/estimateMaxSpendable.js.map +1 -0
  179. package/lib-es/bridge/getEstimateFees.d.ts +6 -0
  180. package/lib-es/bridge/getEstimateFees.d.ts.map +1 -0
  181. package/lib-es/bridge/getEstimateFees.js +47 -0
  182. package/lib-es/bridge/getEstimateFees.js.map +1 -0
  183. package/lib-es/bridge/getTransactionStatus.d.ts +4 -0
  184. package/lib-es/bridge/getTransactionStatus.d.ts.map +1 -0
  185. package/lib-es/bridge/getTransactionStatus.js +213 -0
  186. package/lib-es/bridge/getTransactionStatus.js.map +1 -0
  187. package/lib-es/bridge/index.d.ts +10 -0
  188. package/lib-es/bridge/index.d.ts.map +1 -0
  189. package/lib-es/bridge/index.js +53 -0
  190. package/lib-es/bridge/index.js.map +1 -0
  191. package/lib-es/bridge/preload.d.ts +8 -0
  192. package/lib-es/bridge/preload.d.ts.map +1 -0
  193. package/lib-es/bridge/preload.js +27 -0
  194. package/lib-es/bridge/preload.js.map +1 -0
  195. package/lib-es/bridge/prepareTransaction.d.ts +4 -0
  196. package/lib-es/bridge/prepareTransaction.d.ts.map +1 -0
  197. package/lib-es/bridge/prepareTransaction.js +15 -0
  198. package/lib-es/bridge/prepareTransaction.js.map +1 -0
  199. package/lib-es/bridge/serialization.d.ts +9 -0
  200. package/lib-es/bridge/serialization.d.ts.map +1 -0
  201. package/lib-es/bridge/serialization.js +229 -0
  202. package/lib-es/bridge/serialization.js.map +1 -0
  203. package/lib-es/bridge/signOperation.d.ts +5 -0
  204. package/lib-es/bridge/signOperation.d.ts.map +1 -0
  205. package/lib-es/bridge/signOperation.js +85 -0
  206. package/lib-es/bridge/signOperation.js.map +1 -0
  207. package/lib-es/bridge/synchronization.d.ts +5 -0
  208. package/lib-es/bridge/synchronization.d.ts.map +1 -0
  209. package/lib-es/bridge/synchronization.integ.test.d.ts +3 -0
  210. package/lib-es/bridge/synchronization.integ.test.d.ts.map +1 -0
  211. package/lib-es/bridge/synchronization.integ.test.js +89 -0
  212. package/lib-es/bridge/synchronization.integ.test.js.map +1 -0
  213. package/lib-es/bridge/synchronization.js +209 -0
  214. package/lib-es/bridge/synchronization.js.map +1 -0
  215. package/lib-es/bridge/transaction.d.ts +15 -0
  216. package/lib-es/bridge/transaction.d.ts.map +1 -0
  217. package/lib-es/bridge/transaction.js +55 -0
  218. package/lib-es/bridge/transaction.js.map +1 -0
  219. package/lib-es/config.d.ts +10 -0
  220. package/lib-es/config.d.ts.map +1 -0
  221. package/lib-es/config.js +12 -0
  222. package/lib-es/config.js.map +1 -0
  223. package/lib-es/index.d.ts +3 -0
  224. package/lib-es/index.d.ts.map +1 -0
  225. package/lib-es/index.js +3 -0
  226. package/lib-es/index.js.map +1 -0
  227. package/lib-es/logic/constants.d.ts +4 -0
  228. package/lib-es/logic/constants.d.ts.map +1 -0
  229. package/lib-es/logic/constants.js +4 -0
  230. package/lib-es/logic/constants.js.map +1 -0
  231. package/lib-es/logic/pagination.d.ts +3 -0
  232. package/lib-es/logic/pagination.d.ts.map +1 -0
  233. package/lib-es/logic/pagination.js +15 -0
  234. package/lib-es/logic/pagination.js.map +1 -0
  235. package/lib-es/logic/utils.d.ts +12 -0
  236. package/lib-es/logic/utils.d.ts.map +1 -0
  237. package/lib-es/logic/utils.js +167 -0
  238. package/lib-es/logic/utils.js.map +1 -0
  239. package/lib-es/network/format.d.ts +9 -0
  240. package/lib-es/network/format.d.ts.map +1 -0
  241. package/lib-es/network/format.js +117 -0
  242. package/lib-es/network/format.js.map +1 -0
  243. package/lib-es/network/index.d.ts +30 -0
  244. package/lib-es/network/index.d.ts.map +1 -0
  245. package/lib-es/network/index.integ.test.d.ts +2 -0
  246. package/lib-es/network/index.integ.test.d.ts.map +1 -0
  247. package/lib-es/network/index.integ.test.js +36 -0
  248. package/lib-es/network/index.integ.test.js.map +1 -0
  249. package/lib-es/network/index.js +524 -0
  250. package/lib-es/network/index.js.map +1 -0
  251. package/lib-es/network/index.test.d.ts +2 -0
  252. package/lib-es/network/index.test.d.ts.map +1 -0
  253. package/lib-es/network/index.test.js +63 -0
  254. package/lib-es/network/index.test.js.map +1 -0
  255. package/lib-es/network/superRepresentativesData.mock.d.ts +69 -0
  256. package/lib-es/network/superRepresentativesData.mock.d.ts.map +1 -0
  257. package/lib-es/network/superRepresentativesData.mock.js +2206 -0
  258. package/lib-es/network/superRepresentativesData.mock.js.map +1 -0
  259. package/lib-es/network/types.d.ts +122 -0
  260. package/lib-es/network/types.d.ts.map +1 -0
  261. package/lib-es/network/types.fixture.d.ts +498 -0
  262. package/lib-es/network/types.fixture.d.ts.map +1 -0
  263. package/lib-es/network/types.fixture.js +4371 -0
  264. package/lib-es/network/types.fixture.js.map +1 -0
  265. package/lib-es/network/types.js +8 -0
  266. package/lib-es/network/types.js.map +1 -0
  267. package/lib-es/signer/getAddress.d.ts +6 -0
  268. package/lib-es/signer/getAddress.d.ts.map +1 -0
  269. package/lib-es/signer/getAddress.js +17 -0
  270. package/lib-es/signer/getAddress.js.map +1 -0
  271. package/lib-es/signer/index.d.ts +6 -0
  272. package/lib-es/signer/index.d.ts.map +1 -0
  273. package/lib-es/signer/index.js +6 -0
  274. package/lib-es/signer/index.js.map +1 -0
  275. package/lib-es/test/bot-deviceActions.d.ts +4 -0
  276. package/lib-es/test/bot-deviceActions.d.ts.map +1 -0
  277. package/lib-es/test/bot-deviceActions.js +100 -0
  278. package/lib-es/test/bot-deviceActions.js.map +1 -0
  279. package/lib-es/test/bot-specs.d.ts +7 -0
  280. package/lib-es/test/bot-specs.d.ts.map +1 -0
  281. package/lib-es/test/bot-specs.js +428 -0
  282. package/lib-es/test/bot-specs.js.map +1 -0
  283. package/lib-es/test/bridgeDatasetTest.d.ts +4 -0
  284. package/lib-es/test/bridgeDatasetTest.d.ts.map +1 -0
  285. package/lib-es/test/bridgeDatasetTest.js +1278 -0
  286. package/lib-es/test/bridgeDatasetTest.js.map +1 -0
  287. package/lib-es/test/cli.d.ts +52 -0
  288. package/lib-es/test/cli.d.ts.map +1 -0
  289. package/lib-es/test/cli.js +149 -0
  290. package/lib-es/test/cli.js.map +1 -0
  291. package/lib-es/test/index.d.ts +6 -0
  292. package/lib-es/test/index.d.ts.map +1 -0
  293. package/lib-es/test/index.js +6 -0
  294. package/lib-es/test/index.js.map +1 -0
  295. package/lib-es/types/bridge.d.ts +245 -0
  296. package/lib-es/types/bridge.d.ts.map +1 -0
  297. package/lib-es/types/bridge.js +11 -0
  298. package/lib-es/types/bridge.js.map +1 -0
  299. package/lib-es/types/errors.d.ts +49 -0
  300. package/lib-es/types/errors.d.ts.map +1 -0
  301. package/lib-es/types/errors.js +18 -0
  302. package/lib-es/types/errors.js.map +1 -0
  303. package/lib-es/types/index.d.ts +4 -0
  304. package/lib-es/types/index.d.ts.map +1 -0
  305. package/lib-es/types/index.js +5 -0
  306. package/lib-es/types/index.js.map +1 -0
  307. package/lib-es/types/signer.d.ts +10 -0
  308. package/lib-es/types/signer.d.ts.map +1 -0
  309. package/lib-es/types/signer.js +2 -0
  310. package/lib-es/types/signer.js.map +1 -0
  311. package/package.json +133 -0
  312. package/src/bridge/broadcast.ts +24 -0
  313. package/src/bridge/buildOptimisticOperation.ts +133 -0
  314. package/src/bridge/createTransaction.ts +16 -0
  315. package/src/bridge/deviceTransactionConfig.ts +99 -0
  316. package/src/bridge/estimateMaxSpendable.ts +32 -0
  317. package/src/bridge/getEstimateFees.ts +52 -0
  318. package/src/bridge/getTransactionStatus.ts +263 -0
  319. package/src/bridge/index.ts +78 -0
  320. package/src/bridge/preload.ts +24 -0
  321. package/src/bridge/prepareTransaction.ts +12 -0
  322. package/src/bridge/serialization.ts +288 -0
  323. package/src/bridge/signOperation.ts +123 -0
  324. package/src/bridge/synchronization.integ.test.ts +105 -0
  325. package/src/bridge/synchronization.ts +284 -0
  326. package/src/bridge/transaction.ts +84 -0
  327. package/src/config.ts +24 -0
  328. package/src/index.ts +3 -0
  329. package/src/logic/constants.ts +4 -0
  330. package/src/logic/pagination.ts +21 -0
  331. package/src/logic/utils.ts +231 -0
  332. package/src/network/format.ts +148 -0
  333. package/src/network/index.integ.test.ts +33 -0
  334. package/src/network/index.test.ts +61 -0
  335. package/src/network/index.ts +747 -0
  336. package/src/network/superRepresentativesData.mock.ts +2205 -0
  337. package/src/network/types.fixture.ts +4476 -0
  338. package/src/network/types.ts +147 -0
  339. package/src/signer/getAddress.ts +13 -0
  340. package/src/signer/index.ts +7 -0
  341. package/src/test/bot-deviceActions.ts +121 -0
  342. package/src/test/bot-specs.ts +457 -0
  343. package/src/test/bridgeDatasetTest.ts +1343 -0
  344. package/src/test/cli.ts +219 -0
  345. package/src/test/index.ts +6 -0
  346. package/src/types/bridge.ts +304 -0
  347. package/src/types/errors.ts +22 -0
  348. package/src/types/index.ts +4 -0
  349. package/src/types/signer.ts +9 -0
  350. package/tsconfig.json +14 -0
  351. package/types/tronweb/index.d.ts +5 -0
@@ -0,0 +1,231 @@
1
+ import { encodeOperationId } from "@ledgerhq/coin-framework/operation";
2
+ import type { Account, OperationType } from "@ledgerhq/types-live";
3
+ import { BigNumber } from "bignumber.js";
4
+ import type {
5
+ BandwidthInfo,
6
+ NetworkInfo,
7
+ Transaction,
8
+ TrongridTxInfo,
9
+ TronOperation,
10
+ TronOperationMode,
11
+ TronResources,
12
+ } from "../types";
13
+
14
+ // see: https://solidity.readthedocs.io/en/v0.6.1/abi-spec.html#function-selector-and-argument-encoding
15
+ export const abiEncodeTrc20Transfer = (address: string, amount: BigNumber): string => {
16
+ const encodedAddress = address.padStart(64, "0");
17
+ const hexAmount = amount.toNumber().toString(16); // convert to hexadecimal
18
+
19
+ const encodedAmount = hexAmount.padStart(64, "0");
20
+ return encodedAddress.concat(encodedAmount);
21
+ };
22
+
23
+ export const hexToAscii = (hex: string): string => Buffer.from(hex, "hex").toString("ascii");
24
+
25
+ const parentTx = [
26
+ "TransferContract",
27
+ "VoteWitnessContract",
28
+ "WithdrawBalanceContract",
29
+ "ExchangeTransactionContract",
30
+ "FreezeBalanceV2Contract",
31
+ "UnfreezeBalanceV2Contract",
32
+ "WithdrawExpireUnfreezeContract",
33
+ "UnDelegateResourceContract",
34
+ "FreezeBalanceContract",
35
+ "UnfreezeBalanceContract",
36
+ ];
37
+
38
+ export const isParentTx = (tx: TrongridTxInfo): boolean => parentTx.includes(tx.type);
39
+
40
+ // This is an estimation, there is no endpoint to calculate the real size of a block before broadcasting it.
41
+ export const getEstimatedBlockSize = (a: Account, t: Transaction): BigNumber => {
42
+ switch (t.mode) {
43
+ case "send": {
44
+ const subAccount =
45
+ t.subAccountId && a.subAccounts ? a.subAccounts.find(sa => sa.id === t.subAccountId) : null;
46
+
47
+ if (subAccount && subAccount.type === "TokenAccount") {
48
+ if (subAccount.token.tokenType === "trc10") return new BigNumber(285);
49
+ if (subAccount.token.tokenType === "trc20") return new BigNumber(350);
50
+ }
51
+
52
+ return new BigNumber(270);
53
+ }
54
+
55
+ case "freeze":
56
+ case "unfreeze":
57
+ case "claimReward":
58
+ case "withdrawExpireUnfreeze":
59
+ case "unDelegateResource":
60
+ case "legacyUnfreeze":
61
+ return new BigNumber(260);
62
+
63
+ case "vote":
64
+ return new BigNumber(290 + t.votes.length * 19);
65
+
66
+ default:
67
+ return new BigNumber(0);
68
+ }
69
+ };
70
+
71
+ export const getOperationTypefromMode = (mode: TronOperationMode): OperationType => {
72
+ switch (mode) {
73
+ case "send":
74
+ return "OUT";
75
+
76
+ case "freeze":
77
+ return "FREEZE";
78
+
79
+ case "unfreeze":
80
+ return "UNFREEZE";
81
+
82
+ case "vote":
83
+ return "VOTE";
84
+
85
+ case "claimReward":
86
+ return "REWARD";
87
+
88
+ case "withdrawExpireUnfreeze":
89
+ return "WITHDRAW_EXPIRE_UNFREEZE";
90
+
91
+ case "unDelegateResource":
92
+ return "UNDELEGATE_RESOURCE";
93
+
94
+ case "legacyUnfreeze":
95
+ return "LEGACY_UNFREEZE";
96
+
97
+ default:
98
+ return "OUT";
99
+ }
100
+ };
101
+
102
+ const getOperationType = (
103
+ tx: TrongridTxInfo,
104
+ accountAddr: string,
105
+ ): OperationType | null | undefined => {
106
+ switch (tx.type) {
107
+ case "TransferContract":
108
+ case "TransferAssetContract":
109
+ case "TriggerSmartContract":
110
+ return tx.from === accountAddr ? "OUT" : "IN";
111
+
112
+ case "ExchangeTransactionContract":
113
+ return "OUT";
114
+
115
+ case "VoteWitnessContract":
116
+ return "VOTE";
117
+
118
+ case "WithdrawBalanceContract":
119
+ return "REWARD";
120
+
121
+ case "FreezeBalanceContract":
122
+ case "FreezeBalanceV2Contract":
123
+ return "FREEZE";
124
+
125
+ case "UnfreezeBalanceV2Contract":
126
+ return "UNFREEZE";
127
+
128
+ case "WithdrawExpireUnfreezeContract":
129
+ return "WITHDRAW_EXPIRE_UNFREEZE";
130
+
131
+ case "UnDelegateResourceContract":
132
+ return "UNDELEGATE_RESOURCE";
133
+
134
+ case "UnfreezeBalanceContract":
135
+ return "LEGACY_UNFREEZE";
136
+
137
+ default:
138
+ return undefined;
139
+ }
140
+ };
141
+
142
+ export const txInfoToOperation = (
143
+ id: string,
144
+ address: string,
145
+ tx: TrongridTxInfo,
146
+ ): TronOperation | null | undefined => {
147
+ const {
148
+ txID,
149
+ date,
150
+ from,
151
+ to,
152
+ type,
153
+ value = new BigNumber(0),
154
+ fee = new BigNumber(0),
155
+ blockHeight,
156
+ extra = {},
157
+ hasFailed,
158
+ } = tx;
159
+ const hash = txID;
160
+ const operationType = getOperationType(tx, address);
161
+
162
+ if (operationType) {
163
+ return {
164
+ id: encodeOperationId(id, hash, operationType),
165
+ hash,
166
+ type: operationType,
167
+ value: operationType === "OUT" && type === "TransferContract" ? value.plus(fee) : value,
168
+ // fee is not charged in TRC tokens
169
+ fee: fee,
170
+ blockHeight,
171
+ blockHash: null,
172
+ accountId: id,
173
+ senders: [from],
174
+ recipients: to ? [to] : [],
175
+ date,
176
+ extra,
177
+ hasFailed,
178
+ };
179
+ }
180
+
181
+ return undefined;
182
+ };
183
+
184
+ export const extractBandwidthInfo = (
185
+ networkInfo: NetworkInfo | null | undefined,
186
+ ): BandwidthInfo => {
187
+ // Calculate bandwidth info :
188
+ if (networkInfo) {
189
+ const { freeNetUsed, freeNetLimit, netUsed, netLimit } = networkInfo;
190
+ return {
191
+ freeUsed: freeNetUsed,
192
+ freeLimit: freeNetLimit,
193
+ gainedUsed: netUsed,
194
+ gainedLimit: netLimit,
195
+ };
196
+ }
197
+
198
+ return {
199
+ freeUsed: new BigNumber(0),
200
+ freeLimit: new BigNumber(0),
201
+ gainedUsed: new BigNumber(0),
202
+ gainedLimit: new BigNumber(0),
203
+ };
204
+ };
205
+
206
+ export const defaultTronResources: TronResources = {
207
+ frozen: {
208
+ bandwidth: undefined,
209
+ energy: undefined,
210
+ },
211
+ unFrozen: {
212
+ bandwidth: undefined,
213
+ energy: undefined,
214
+ },
215
+ delegatedFrozen: {
216
+ bandwidth: undefined,
217
+ energy: undefined,
218
+ },
219
+ legacyFrozen: {
220
+ bandwidth: undefined,
221
+ energy: undefined,
222
+ },
223
+ votes: [],
224
+ tronPower: 0,
225
+ energy: new BigNumber(0),
226
+ bandwidth: extractBandwidthInfo(null),
227
+ unwithdrawnReward: new BigNumber(0),
228
+ lastWithdrawnRewardDate: undefined,
229
+ lastVotedDate: undefined,
230
+ cacheTransactionInfoById: {},
231
+ };
@@ -0,0 +1,148 @@
1
+ import get from "lodash/get";
2
+ import { TrongridExtraTxInfo, TrongridTxInfo, TronTransactionInfo } from "../types";
3
+ import { TransactionTronAPI, Trc20API } from "./types";
4
+ import BigNumber from "bignumber.js";
5
+ import { log } from "@ledgerhq/logs";
6
+ import bs58check from "bs58check";
7
+
8
+ export const decode58Check = (base58: string): string =>
9
+ Buffer.from(bs58check.decode(base58)).toString("hex");
10
+
11
+ export const encode58Check = (hex: string): string => bs58check.encode(Buffer.from(hex, "hex"));
12
+
13
+ export const formatTrongridTrc20TxResponse = (tx: Trc20API): TrongridTxInfo | null | undefined => {
14
+ try {
15
+ const { from, to, block_timestamp, detail, value, transaction_id, token_info } = tx;
16
+ const type = "TriggerSmartContract";
17
+ const txID = transaction_id;
18
+ const date = new Date(block_timestamp);
19
+ const tokenId = token_info.address ?? undefined;
20
+ const formattedValue = value ? new BigNumber(value) : new BigNumber(0);
21
+ const fee = tx.detail.ret[0].fee || undefined;
22
+ const blockHeight = detail ? detail.blockNumber : undefined;
23
+ return {
24
+ txID,
25
+ date,
26
+ type,
27
+ tokenId,
28
+ from,
29
+ to,
30
+ blockHeight,
31
+ value: formattedValue,
32
+ fee: new BigNumber(fee || 0),
33
+ hasFailed: false, // trc20 txs are succeeded if returned by trongrid,
34
+ };
35
+ } catch (e) {
36
+ log("tron-error", "could not parse transaction", tx);
37
+ return undefined;
38
+ }
39
+ };
40
+
41
+ export const formatTrongridTxResponse = (
42
+ tx: TransactionTronAPI & { detail?: TronTransactionInfo },
43
+ ): TrongridTxInfo | null | undefined => {
44
+ try {
45
+ const { txID, block_timestamp, detail, blockNumber, unfreeze_amount, withdraw_amount } = tx;
46
+ const date = new Date(block_timestamp);
47
+ const type = tx.raw_data.contract[0].type;
48
+ const {
49
+ amount,
50
+ asset_name,
51
+ owner_address,
52
+ to_address,
53
+ contract_address,
54
+ quant,
55
+ frozen_balance,
56
+ votes,
57
+ unfreeze_balance,
58
+ balance,
59
+ receiver_address,
60
+ } = tx.raw_data.contract[0].parameter.value;
61
+ const hasFailed = get(tx, "ret[0].contractRet", "SUCCESS") !== "SUCCESS";
62
+ const tokenId =
63
+ type === "TransferAssetContract"
64
+ ? asset_name
65
+ : type === "TriggerSmartContract" && contract_address
66
+ ? encode58Check(contract_address)
67
+ : undefined;
68
+ const from = encode58Check(owner_address);
69
+ const to = to_address ? encode58Check(to_address) : undefined;
70
+
71
+ const getValue = (): BigNumber => {
72
+ switch (type) {
73
+ case "WithdrawBalanceContract":
74
+ return new BigNumber(withdraw_amount || detail?.withdraw_amount || 0);
75
+
76
+ case "ExchangeTransactionContract":
77
+ return new BigNumber(quant || 0);
78
+
79
+ default:
80
+ return amount ? new BigNumber(amount) : new BigNumber(0);
81
+ }
82
+ };
83
+
84
+ const value = getValue();
85
+ const fee = get(tx, "ret[0].fee", detail && detail.fee ? detail.fee : undefined);
86
+ const blockHeight = blockNumber || detail?.blockNumber;
87
+ const txInfo: TrongridTxInfo = {
88
+ txID,
89
+ date,
90
+ type,
91
+ tokenId,
92
+ from,
93
+ to,
94
+ value: !value.isNaN() ? value : new BigNumber(0),
95
+ fee: new BigNumber(fee || 0),
96
+ blockHeight,
97
+ hasFailed,
98
+ };
99
+
100
+ const getExtra = (): TrongridExtraTxInfo | null | undefined => {
101
+ switch (type) {
102
+ case "VoteWitnessContract":
103
+ return {
104
+ votes: votes?.map(v => ({
105
+ address: encode58Check(v.vote_address as string),
106
+ voteCount: v.vote_count,
107
+ })),
108
+ };
109
+
110
+ case "FreezeBalanceContract":
111
+ case "FreezeBalanceV2Contract":
112
+ return {
113
+ frozenAmount: new BigNumber(frozen_balance!),
114
+ };
115
+
116
+ case "UnfreezeBalanceV2Contract":
117
+ return {
118
+ unfreezeAmount: new BigNumber(unfreeze_balance!),
119
+ };
120
+
121
+ case "UnDelegateResourceContract":
122
+ return {
123
+ unDelegatedAmount: new BigNumber(balance!),
124
+ receiverAddress: encode58Check(receiver_address!),
125
+ };
126
+
127
+ case "UnfreezeBalanceContract":
128
+ return {
129
+ unfreezeAmount: new BigNumber(unfreeze_amount || detail!.unfreeze_amount!),
130
+ };
131
+
132
+ default:
133
+ return undefined;
134
+ }
135
+ };
136
+
137
+ const extra = getExtra();
138
+
139
+ if (extra) {
140
+ txInfo.extra = extra;
141
+ }
142
+
143
+ return txInfo;
144
+ } catch (e) {
145
+ log("tron-error", "could not parse transaction", tx);
146
+ return undefined;
147
+ }
148
+ };
@@ -0,0 +1,33 @@
1
+ import { fetchTronAccountTxs } from ".";
2
+ import { setCoinConfig } from "../config";
3
+
4
+ describe("fetchTronAccountTxs", () => {
5
+ beforeAll(() => {
6
+ setCoinConfig(() => ({
7
+ status: {
8
+ type: "active",
9
+ },
10
+ explorer: {
11
+ url: "https://tron.coin.ledger.com",
12
+ },
13
+ }));
14
+ });
15
+
16
+ it(
17
+ "maps all fields correctly",
18
+ async () => {
19
+ // WHEN
20
+ // const addr = "TL24LCps5FKwp3PoU1MvrYrwhi5LU1tHre";
21
+ // const addr = "TAVrrARNdnjHgCGMQYeQV7hv4PSu7mVsMj";
22
+ // const addr = "THAe4BNVxp293qgyQEqXEkHMpPcqtG73bi";
23
+ // const addr = "TRqkRnAj6ceJFYAn2p1eE7aWrgBBwtdhS9";
24
+ // const addr = "TUxd6v64YTWkfpFpNDdtgc5Ps4SfGxwizT";
25
+ const addr = "TY2ksFgpvb82TgGPwUSa7iseqPW5weYQyh";
26
+ const results = await fetchTronAccountTxs(addr, txs => txs.length < 100, {});
27
+
28
+ // THEN
29
+ expect(results).not.toHaveLength(0);
30
+ },
31
+ 10 * 1000,
32
+ );
33
+ });
@@ -0,0 +1,61 @@
1
+ import { HttpResponse, http } from "msw";
2
+ import { setupServer } from "msw/node";
3
+ import { TRANSACTION_DETAIL_FIXTURE, TRANSACTION_FIXTURE, TRC20_FIXTURE } from "./types.fixture";
4
+ import { setCoinConfig } from "../config";
5
+ import { fetchTronAccountTxs } from ".";
6
+
7
+ export const TRON_BASE_URL_TEST = "https://httpbin.org";
8
+
9
+ const handlers = [
10
+ http.get(`${TRON_BASE_URL_TEST}/v1/accounts/:addr/transactions`, () => {
11
+ // const url = new URL(request.url);
12
+ // const _ = url.searchParams.get("get_detail");
13
+ return HttpResponse.json(TRANSACTION_FIXTURE);
14
+ }),
15
+ http.get(`${TRON_BASE_URL_TEST}/v1/accounts/:addr/transactions/trc20`, () => {
16
+ // const url = new URL(request.url);
17
+ // const _ = url.searchParams.get("get_detail");
18
+ return HttpResponse.json(TRC20_FIXTURE);
19
+ }),
20
+ http.get(`${TRON_BASE_URL_TEST}/wallet/gettransactioninfobyid`, ({ request }) => {
21
+ const url = new URL(request.url);
22
+ const value = url.searchParams.get("value") ?? "UNKNOWN";
23
+ return HttpResponse.json(TRANSACTION_DETAIL_FIXTURE(value));
24
+ }),
25
+ ];
26
+
27
+ const mockServer = setupServer(...handlers);
28
+
29
+ describe("fetchTronAccountTxs", () => {
30
+ beforeAll(() => {
31
+ setCoinConfig(() => ({
32
+ status: {
33
+ type: "active",
34
+ },
35
+ explorer: {
36
+ url: TRON_BASE_URL_TEST,
37
+ },
38
+ }));
39
+
40
+ mockServer.listen();
41
+ });
42
+
43
+ beforeEach(() => {
44
+ mockServer.resetHandlers();
45
+ });
46
+
47
+ afterAll(() => {
48
+ mockServer.close();
49
+ });
50
+
51
+ it("convert correctly operations from the blockchain", async () => {
52
+ // WHEN
53
+ const results = await fetchTronAccountTxs("ADDRESS", txs => txs.length < 100, {});
54
+
55
+ // THEN
56
+ const tx = results.find(tx => tx.blockHeight === 62258698);
57
+ expect(tx).toBeDefined();
58
+ expect(tx!.from).toEqual("TQ7pF3NTDL2Tjz5rdJ6ECjQWjaWHpLZJMH");
59
+ expect(tx!.to).toEqual("TAVrrARNdnjHgCGMQYeQV7hv4PSu7mVsMj");
60
+ });
61
+ });