four-flap-meme-sdk 1.9.49 → 2.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 (530) hide show
  1. package/README.en.md +41 -6
  2. package/README.md +31 -0
  3. package/README.zh-CN.md +41 -6
  4. package/dist/abis/common.js +74 -68
  5. package/dist/abis/flap/vault.js +9 -7
  6. package/dist/abis/index.d.ts +3 -4
  7. package/dist/abis/index.js +3 -4
  8. package/dist/bundle-core/__tests__/config-helpers.test.js +28 -0
  9. package/dist/bundle-core/__tests__/facade-parity.test.js +33 -0
  10. package/dist/bundle-core/__tests__/sign-context-helpers.test.d.ts +1 -0
  11. package/dist/bundle-core/__tests__/sign-context-helpers.test.js +60 -0
  12. package/dist/bundle-core/__tests__/sign-fixture.test.d.ts +1 -0
  13. package/dist/bundle-core/__tests__/sign-fixture.test.js +220 -0
  14. package/dist/bundle-core/__tests__/sign-fixtures.d.ts +10 -0
  15. package/dist/bundle-core/__tests__/sign-fixtures.js +16 -0
  16. package/dist/bundle-core/config-helpers.d.ts +36 -0
  17. package/dist/bundle-core/config-helpers.js +57 -0
  18. package/dist/bundle-core/errors.d.ts +50 -0
  19. package/dist/bundle-core/errors.js +34 -0
  20. package/dist/bundle-core/four-meme/approve-tokenmanager.d.ts +7 -0
  21. package/dist/bundle-core/four-meme/approve-tokenmanager.js +99 -0
  22. package/dist/bundle-core/four-meme/core-helpers.d.ts +8 -0
  23. package/dist/bundle-core/four-meme/core-helpers.js +40 -0
  24. package/dist/bundle-core/four-meme/core.d.ts +4 -0
  25. package/dist/bundle-core/four-meme/core.js +515 -0
  26. package/dist/bundle-core/four-meme/pancake-proxy.d.ts +28 -0
  27. package/dist/bundle-core/four-meme/pancake-proxy.js +679 -0
  28. package/dist/bundle-core/four-meme/private.d.ts +27 -0
  29. package/dist/bundle-core/four-meme/private.js +465 -0
  30. package/dist/bundle-core/four-meme/sign-context-helpers.d.ts +2 -0
  31. package/dist/bundle-core/four-meme/sign-context-helpers.js +2 -0
  32. package/dist/bundle-core/four-meme/swap-buy-first.d.ts +8 -0
  33. package/dist/bundle-core/four-meme/swap-buy-first.js +493 -0
  34. package/dist/bundle-core/four-meme/swap-hop-helpers.d.ts +6 -0
  35. package/dist/bundle-core/four-meme/swap-hop-helpers.js +63 -0
  36. package/dist/bundle-core/four-meme/swap-internal.d.ts +3 -0
  37. package/dist/bundle-core/four-meme/swap-internal.js +18 -0
  38. package/dist/bundle-core/four-meme/swap-sign-helpers.d.ts +27 -0
  39. package/dist/bundle-core/four-meme/swap-sign-helpers.js +105 -0
  40. package/dist/bundle-core/four-meme/swap.d.ts +17 -0
  41. package/dist/bundle-core/four-meme/swap.js +505 -0
  42. package/dist/bundle-core/four-meme/types/buy-first.d.ts +50 -0
  43. package/dist/bundle-core/four-meme/types/buy-first.js +1 -0
  44. package/dist/bundle-core/four-meme/types/core-flow.d.ts +63 -0
  45. package/dist/bundle-core/four-meme/types/core-flow.js +1 -0
  46. package/dist/bundle-core/four-meme/types/index.d.ts +600 -0
  47. package/dist/bundle-core/four-meme/types/index.js +1 -0
  48. package/dist/bundle-core/four-meme/types/swap-internal.d.ts +19 -0
  49. package/dist/bundle-core/four-meme/types/swap-internal.js +1 -0
  50. package/dist/bundle-core/four-meme/types.d.ts +1 -0
  51. package/dist/bundle-core/four-meme/types.js +1 -0
  52. package/dist/bundle-core/four-meme/utils-disperse.d.ts +7 -0
  53. package/dist/bundle-core/four-meme/utils-disperse.js +396 -0
  54. package/dist/bundle-core/four-meme/utils-pairwise.d.ts +8 -0
  55. package/dist/bundle-core/four-meme/utils-pairwise.js +328 -0
  56. package/dist/bundle-core/four-meme/utils-sweep.d.ts +8 -0
  57. package/dist/bundle-core/four-meme/utils-sweep.js +744 -0
  58. package/dist/bundle-core/index.d.ts +8 -0
  59. package/dist/bundle-core/index.js +8 -0
  60. package/dist/bundle-core/internal.d.ts +21 -0
  61. package/dist/bundle-core/internal.js +182 -0
  62. package/dist/bundle-core/sign-context-helpers.d.ts +25 -0
  63. package/dist/bundle-core/sign-context-helpers.js +67 -0
  64. package/dist/bundle-core/submit.d.ts +293 -0
  65. package/dist/bundle-core/submit.js +727 -0
  66. package/dist/bundle-core/types/index.d.ts +8 -0
  67. package/dist/bundle-core/types/index.js +1 -0
  68. package/dist/bundle-core/types.d.ts +1 -0
  69. package/dist/bundle-core/types.js +1 -0
  70. package/dist/chains/bsc/four/approve-tokenmanager.d.ts +1 -26
  71. package/dist/chains/bsc/four/approve-tokenmanager.js +1 -116
  72. package/dist/chains/bsc/four/config.d.ts +5 -70
  73. package/dist/chains/bsc/four/config.js +2 -115
  74. package/dist/chains/bsc/four/core.d.ts +1 -4
  75. package/dist/chains/bsc/four/core.js +1 -589
  76. package/dist/chains/bsc/four/disperse.d.ts +12 -0
  77. package/dist/chains/bsc/four/disperse.js +470 -0
  78. package/dist/chains/bsc/four/index.d.ts +6 -6
  79. package/dist/chains/bsc/four/index.js +6 -6
  80. package/dist/chains/bsc/four/internal.d.ts +2 -50
  81. package/dist/chains/bsc/four/internal.js +2 -237
  82. package/dist/chains/bsc/four/pairwise.d.ts +7 -0
  83. package/dist/chains/bsc/four/pairwise.js +308 -0
  84. package/dist/chains/bsc/four/pancake-proxy.d.ts +1 -28
  85. package/dist/chains/bsc/four/pancake-proxy.js +1 -688
  86. package/dist/chains/bsc/four/private.d.ts +1 -27
  87. package/dist/chains/bsc/four/private.js +1 -477
  88. package/dist/chains/bsc/four/submit.d.ts +2 -315
  89. package/dist/chains/bsc/four/submit.js +2 -746
  90. package/dist/chains/bsc/four/swap-buy-first.d.ts +2 -55
  91. package/dist/chains/bsc/four/swap-buy-first.js +2 -506
  92. package/dist/chains/bsc/four/swap-internal.d.ts +1 -3
  93. package/dist/chains/bsc/four/swap-internal.js +1 -18
  94. package/dist/chains/bsc/four/swap.d.ts +2 -144
  95. package/dist/chains/bsc/four/swap.js +2 -760
  96. package/dist/chains/bsc/four/sweep.d.ts +13 -0
  97. package/dist/chains/bsc/four/sweep.js +788 -0
  98. package/dist/chains/bsc/four/types.d.ts +1 -476
  99. package/dist/chains/bsc/four/utils-disperse.d.ts +1 -0
  100. package/dist/chains/bsc/four/utils-disperse.js +1 -0
  101. package/dist/chains/bsc/four/utils-pairwise.d.ts +1 -0
  102. package/dist/chains/bsc/four/utils-pairwise.js +1 -0
  103. package/dist/chains/bsc/four/utils-sweep.d.ts +1 -0
  104. package/dist/chains/bsc/four/utils-sweep.js +1 -0
  105. package/dist/chains/bsc/four/utils.d.ts +5 -18
  106. package/dist/chains/bsc/four/utils.js +5 -1534
  107. package/dist/chains/bsc/pancake/bundle-buy-first-helpers.d.ts +159 -0
  108. package/dist/chains/bsc/pancake/bundle-buy-first-helpers.js +117 -0
  109. package/dist/chains/bsc/pancake/bundle-buy-first.d.ts +1 -91
  110. package/dist/chains/bsc/pancake/bundle-buy-first.js +97 -207
  111. package/dist/chains/bsc/pancake/bundle-swap-helpers.d.ts +241 -0
  112. package/dist/chains/bsc/pancake/bundle-swap-helpers.js +565 -0
  113. package/dist/chains/bsc/pancake/bundle-swap.d.ts +1 -79
  114. package/dist/chains/bsc/pancake/bundle-swap.js +111 -686
  115. package/dist/chains/bsc/pancake/index.d.ts +4 -2
  116. package/dist/chains/bsc/pancake/index.js +1 -3
  117. package/dist/chains/bsc/platforms/iro/factory.js +3 -1
  118. package/dist/chains/bsc/platforms/iro/index.d.ts +5 -5
  119. package/dist/chains/bsc/platforms/iro/index.js +3 -3
  120. package/dist/chains/bsc/platforms/iro/pool.js +31 -10
  121. package/dist/chains/bsc/platforms/iro/token.js +4 -1
  122. package/dist/chains/eni/batch-router/bundle-approve.js +4 -3
  123. package/dist/chains/eni/batch-router/transfer.js +54 -23
  124. package/dist/chains/eni/batch-router/utils.js +32 -6
  125. package/dist/chains/eni/bundler/sign.js +4 -4
  126. package/dist/chains/eni/bundler/submit.js +1 -4
  127. package/dist/chains/eni/constants.js +1 -1
  128. package/dist/chains/eni/index.d.ts +1 -1
  129. package/dist/chains/eni/platforms/daoaas/create.js +2 -2
  130. package/dist/chains/eni/platforms/daoaas/index.d.ts +3 -3
  131. package/dist/chains/eni/platforms/daoaas/index.js +3 -3
  132. package/dist/chains/eni/platforms/daoaas/meta.js +8 -4
  133. package/dist/chains/eni/platforms/daoaas/portal-direct.js +28 -43
  134. package/dist/chains/eni/platforms/daoaas/portal.js +10 -6
  135. package/dist/chains/eni/platforms/dswap/liquidity.js +58 -26
  136. package/dist/chains/eni/platforms/fair-launch/index.d.ts +2 -2
  137. package/dist/chains/eni/platforms/fair-launch/index.js +1 -1
  138. package/dist/chains/eni/platforms/fair-launch/launcher.js +87 -47
  139. package/dist/chains/eni/platforms/fair-launch/pool.js +4 -1
  140. package/dist/chains/eni/platforms/fair-launch/presets.js +2 -2
  141. package/dist/chains/eni/platforms/iro/factory.js +3 -1
  142. package/dist/chains/eni/platforms/iro/index.d.ts +6 -6
  143. package/dist/chains/eni/platforms/iro/index.js +4 -4
  144. package/dist/chains/eni/platforms/iro/pool.js +90 -26
  145. package/dist/chains/eni/platforms/iro/token.js +107 -31
  146. package/dist/chains/eni/platforms/iro/whitelist.js +6 -18
  147. package/dist/chains/eni/submit.js +5 -13
  148. package/dist/chains/xlayer/eip7702/bundle-approve.d.ts +2 -26
  149. package/dist/chains/xlayer/eip7702/bundle-approve.js +11 -20
  150. package/dist/chains/xlayer/eip7702/bundle-buy.d.ts +2 -6
  151. package/dist/chains/xlayer/eip7702/bundle-buy.js +13 -51
  152. package/dist/chains/xlayer/eip7702/bundle-create.js +93 -56
  153. package/dist/chains/xlayer/eip7702/bundle-sell.d.ts +2 -6
  154. package/dist/chains/xlayer/eip7702/bundle-sell.js +26 -74
  155. package/dist/chains/xlayer/eip7702/bundle-swap.d.ts +3 -65
  156. package/dist/chains/xlayer/eip7702/bundle-swap.js +51 -68
  157. package/dist/chains/xlayer/eip7702/constants.d.ts +1 -16
  158. package/dist/chains/xlayer/eip7702/constants.js +3 -21
  159. package/dist/chains/xlayer/eip7702/index.d.ts +27 -46
  160. package/dist/chains/xlayer/eip7702/index.js +27 -81
  161. package/dist/chains/xlayer/eip7702/multi-hop-transfer-helpers.d.ts +79 -0
  162. package/dist/chains/xlayer/eip7702/multi-hop-transfer-helpers.js +1 -0
  163. package/dist/chains/xlayer/eip7702/multi-hop-transfer.d.ts +2 -203
  164. package/dist/chains/xlayer/eip7702/multi-hop-transfer.js +58 -291
  165. package/dist/chains/xlayer/eip7702/transfer-context-helpers.d.ts +26 -0
  166. package/dist/chains/xlayer/eip7702/transfer-context-helpers.js +57 -0
  167. package/dist/chains/xlayer/eip7702/types.d.ts +88 -0
  168. package/dist/chains/xlayer/eip7702/utils.d.ts +0 -3
  169. package/dist/chains/xlayer/eip7702/utils.js +23 -28
  170. package/dist/chains/xlayer/eip7702/volume-helpers.d.ts +148 -0
  171. package/dist/chains/xlayer/eip7702/volume-helpers.js +48 -0
  172. package/dist/chains/xlayer/eip7702/volume.d.ts +6 -184
  173. package/dist/chains/xlayer/eip7702/volume.js +83 -158
  174. package/dist/chains/xlayer/eoa/constants.js +1 -1
  175. package/dist/chains/xlayer/eoa/dex-helpers.js +5 -5
  176. package/dist/chains/xlayer/eoa/eoa-bundle-swap-helpers.d.ts +126 -0
  177. package/dist/chains/xlayer/eoa/eoa-bundle-swap-helpers.js +228 -0
  178. package/dist/chains/xlayer/eoa/eoa-bundle-swap.d.ts +1 -95
  179. package/dist/chains/xlayer/eoa/eoa-bundle-swap.js +64 -276
  180. package/dist/chains/xlayer/eoa/eoa-wash-volume.d.ts +1 -1
  181. package/dist/chains/xlayer/eoa/eoa-wash-volume.js +14 -14
  182. package/dist/chains/xlayer/eoa/index.d.ts +10 -6
  183. package/dist/chains/xlayer/eoa/index.js +8 -23
  184. package/dist/chains/xlayer/eoa/portal-ops.js +7 -2
  185. package/dist/chains/xlayer/eoa/router-manager.js +3 -3
  186. package/dist/chains/xlayer/eoa/types-core.d.ts +363 -0
  187. package/dist/chains/xlayer/eoa/types-core.js +53 -0
  188. package/dist/chains/xlayer/eoa/types-create.d.ts +413 -0
  189. package/dist/chains/xlayer/eoa/types-create.js +9 -0
  190. package/dist/chains/xlayer/eoa/types-volume.d.ts +209 -0
  191. package/dist/chains/xlayer/eoa/types-volume.js +13 -0
  192. package/dist/chains/xlayer/eoa/types.d.ts +2 -2
  193. package/dist/chains/xlayer/eoa/types.js +1 -3
  194. package/dist/contracts/helper3.d.ts +20 -17
  195. package/dist/contracts/helper3.js +56 -20
  196. package/dist/contracts/tm-bundle-helpers.d.ts +88 -0
  197. package/dist/contracts/tm-bundle-helpers.js +72 -0
  198. package/dist/contracts/tm-bundle-merkle/approve-tokenmanager.d.ts +1 -26
  199. package/dist/contracts/tm-bundle-merkle/approve-tokenmanager.js +1 -117
  200. package/dist/contracts/tm-bundle-merkle/config.d.ts +5 -70
  201. package/dist/contracts/tm-bundle-merkle/config.js +2 -115
  202. package/dist/contracts/tm-bundle-merkle/core.d.ts +1 -4
  203. package/dist/contracts/tm-bundle-merkle/core.js +1 -588
  204. package/dist/contracts/tm-bundle-merkle/index.d.ts +5 -5
  205. package/dist/contracts/tm-bundle-merkle/index.js +5 -5
  206. package/dist/contracts/tm-bundle-merkle/internal.d.ts +2 -50
  207. package/dist/contracts/tm-bundle-merkle/internal.js +2 -236
  208. package/dist/contracts/tm-bundle-merkle/pancake-proxy.d.ts +1 -28
  209. package/dist/contracts/tm-bundle-merkle/pancake-proxy.js +1 -687
  210. package/dist/contracts/tm-bundle-merkle/private.d.ts +1 -27
  211. package/dist/contracts/tm-bundle-merkle/private.js +1 -476
  212. package/dist/contracts/tm-bundle-merkle/submit.d.ts +3 -314
  213. package/dist/contracts/tm-bundle-merkle/submit.js +3 -919
  214. package/dist/contracts/tm-bundle-merkle/swap-buy-first.d.ts +2 -55
  215. package/dist/contracts/tm-bundle-merkle/swap-buy-first.js +2 -505
  216. package/dist/contracts/tm-bundle-merkle/swap-internal.d.ts +1 -3
  217. package/dist/contracts/tm-bundle-merkle/swap-internal.js +1 -18
  218. package/dist/contracts/tm-bundle-merkle/swap.d.ts +2 -144
  219. package/dist/contracts/tm-bundle-merkle/swap.js +2 -757
  220. package/dist/contracts/tm-bundle-merkle/types.d.ts +1 -476
  221. package/dist/contracts/tm-bundle-merkle/utils-disperse.d.ts +1 -0
  222. package/dist/contracts/tm-bundle-merkle/utils-disperse.js +1 -0
  223. package/dist/contracts/tm-bundle-merkle/utils-pairwise.d.ts +1 -0
  224. package/dist/contracts/tm-bundle-merkle/utils-pairwise.js +1 -0
  225. package/dist/contracts/tm-bundle-merkle/utils-sweep.d.ts +1 -0
  226. package/dist/contracts/tm-bundle-merkle/utils-sweep.js +1 -0
  227. package/dist/contracts/tm-bundle-merkle/utils.d.ts +5 -6
  228. package/dist/contracts/tm-bundle-merkle/utils.js +5 -6
  229. package/dist/contracts/tm-bundle.d.ts +3 -51
  230. package/dist/contracts/tm-bundle.js +102 -142
  231. package/dist/contracts/tm.d.ts +3 -2
  232. package/dist/contracts/tm.js +36 -32
  233. package/dist/contracts/tm1.js +9 -4
  234. package/dist/contracts/tm2.js +9 -4
  235. package/dist/dex/direct-router-helpers.d.ts +264 -0
  236. package/dist/dex/direct-router-helpers.js +539 -0
  237. package/dist/dex/direct-router.d.ts +3 -125
  238. package/dist/dex/direct-router.js +237 -652
  239. package/dist/dex/types.d.ts +81 -0
  240. package/dist/dex/types.js +1 -0
  241. package/dist/exports/root-bundle-and-tooling.d.ts +27 -0
  242. package/dist/exports/root-bundle-and-tooling.js +30 -0
  243. package/dist/exports/root-eni-and-bsc-iro.d.ts +26 -0
  244. package/dist/exports/root-eni-and-bsc-iro.js +66 -0
  245. package/dist/exports/root-foundations.d.ts +35 -0
  246. package/dist/exports/root-foundations.js +70 -0
  247. package/dist/exports/root-swap-dex-and-xlayer.d.ts +30 -0
  248. package/dist/exports/root-swap-dex-and-xlayer.js +78 -0
  249. package/dist/flows/create.d.ts +2 -2
  250. package/dist/flows/create.js +6 -6
  251. package/dist/flows/index.d.ts +1 -0
  252. package/dist/flows/index.js +1 -0
  253. package/dist/index.d.ts +20 -91
  254. package/dist/index.js +20 -215
  255. package/dist/shared/abis/common.d.ts +2 -83
  256. package/dist/shared/abis/common.js +2 -252
  257. package/dist/shared/abis/index.d.ts +5 -6
  258. package/dist/shared/abis/index.js +5 -7
  259. package/dist/shared/clients/blockrazor.js +41 -24
  260. package/dist/shared/clients/club48.js +30 -26
  261. package/dist/shared/clients/emitservice.js +2 -0
  262. package/dist/shared/clients/four.js +23 -18
  263. package/dist/shared/clients/index.d.ts +8 -0
  264. package/dist/shared/clients/index.js +8 -0
  265. package/dist/shared/clients/merkle.js +26 -30
  266. package/dist/shared/constants/addresses.d.ts +1 -1
  267. package/dist/shared/constants/addresses.js +11 -2
  268. package/dist/shared/constants/chains.d.ts +1 -1
  269. package/dist/shared/constants/chains.js +1 -1
  270. package/dist/shared/constants/gas.d.ts +1 -1
  271. package/dist/shared/constants/gas.js +2 -6
  272. package/dist/shared/constants/index.d.ts +1 -0
  273. package/dist/shared/constants/index.js +1 -0
  274. package/dist/shared/constants/quote.d.ts +30 -0
  275. package/dist/shared/constants/quote.js +37 -0
  276. package/dist/shared/flap/__tests__/curve.test.js +3 -3
  277. package/dist/shared/flap/abi.js +1 -1
  278. package/dist/shared/flap/constants.d.ts +1 -1
  279. package/dist/shared/flap/constants.js +2 -2
  280. package/dist/shared/flap/curve.js +3 -0
  281. package/dist/shared/flap/errors.d.ts +1 -4
  282. package/dist/shared/flap/errors.js +21 -3
  283. package/dist/shared/flap/index.d.ts +2 -2
  284. package/dist/shared/flap/index.js +2 -2
  285. package/dist/shared/flap/meta.d.ts +16 -18
  286. package/dist/shared/flap/meta.js +14 -26
  287. package/dist/shared/flap/permit.js +6 -5
  288. package/dist/shared/flap/pinata.d.ts +9 -2
  289. package/dist/shared/flap/pinata.js +20 -18
  290. package/dist/shared/flap/portal-bundle-merkle/config.d.ts +3 -72
  291. package/dist/shared/flap/portal-bundle-merkle/config.js +4 -124
  292. package/dist/shared/flap/portal-bundle-merkle/core-helpers.d.ts +32 -0
  293. package/dist/shared/flap/portal-bundle-merkle/core-helpers.js +83 -0
  294. package/dist/shared/flap/portal-bundle-merkle/core.d.ts +0 -4
  295. package/dist/shared/flap/portal-bundle-merkle/core.js +66 -156
  296. package/dist/shared/flap/portal-bundle-merkle/create-to-dex.js +81 -86
  297. package/dist/shared/flap/portal-bundle-merkle/curve-to-dex.js +100 -92
  298. package/dist/shared/flap/portal-bundle-merkle/index.d.ts +11 -7
  299. package/dist/shared/flap/portal-bundle-merkle/index.js +4 -7
  300. package/dist/shared/flap/portal-bundle-merkle/pancake-proxy.js +71 -68
  301. package/dist/shared/flap/portal-bundle-merkle/private.js +61 -114
  302. package/dist/shared/flap/portal-bundle-merkle/swap-buy-first-helpers.d.ts +125 -0
  303. package/dist/shared/flap/portal-bundle-merkle/swap-buy-first-helpers.js +113 -0
  304. package/dist/shared/flap/portal-bundle-merkle/swap-buy-first.d.ts +1 -64
  305. package/dist/shared/flap/portal-bundle-merkle/swap-buy-first.js +66 -247
  306. package/dist/shared/flap/portal-bundle-merkle/swap-helpers.d.ts +149 -0
  307. package/dist/shared/flap/portal-bundle-merkle/swap-helpers.js +259 -0
  308. package/dist/shared/flap/portal-bundle-merkle/swap.d.ts +2 -71
  309. package/dist/shared/flap/portal-bundle-merkle/swap.js +103 -410
  310. package/dist/shared/flap/portal-bundle-merkle/types.d.ts +73 -3
  311. package/dist/shared/flap/portal-bundle-merkle/utils-helpers.d.ts +100 -0
  312. package/dist/shared/flap/portal-bundle-merkle/utils-helpers.js +133 -0
  313. package/dist/shared/flap/portal-bundle-merkle/utils.d.ts +1 -80
  314. package/dist/shared/flap/portal-bundle-merkle/utils.js +145 -265
  315. package/dist/shared/flap/portal-bundle.js +54 -42
  316. package/dist/shared/flap/portal.js +32 -10
  317. package/dist/shared/flap/vanity.js +4 -7
  318. package/dist/shared/flap/vault.d.ts +1 -1
  319. package/dist/shared/flap/vault.js +54 -48
  320. package/dist/shared/flap 2/__tests__/curve.test.d.ts +1 -0
  321. package/dist/shared/flap 2/portal-bundle-merkle/types.js +1 -0
  322. package/dist/shared/foundation/dex/v3-path.d.ts +6 -0
  323. package/dist/shared/foundation/dex/v3-path.js +35 -0
  324. package/dist/shared/foundation/gas/bundle-gas.d.ts +39 -0
  325. package/dist/shared/foundation/gas/bundle-gas.js +93 -0
  326. package/dist/shared/foundation/gas/profit-hop.d.ts +20 -0
  327. package/dist/shared/foundation/gas/profit-hop.js +72 -0
  328. package/dist/shared/foundation/index.d.ts +13 -0
  329. package/dist/shared/foundation/index.js +12 -0
  330. package/dist/shared/foundation/nonce/nonce-manager.d.ts +17 -0
  331. package/dist/shared/foundation/nonce/nonce-manager.js +180 -0
  332. package/dist/shared/foundation/normalize-unknown.d.ts +9 -0
  333. package/dist/shared/foundation/normalize-unknown.js +29 -0
  334. package/dist/shared/foundation/sdk-logger.d.ts +13 -0
  335. package/dist/shared/foundation/sdk-logger.js +12 -0
  336. package/dist/shared/foundation/tx/build-request.d.ts +17 -0
  337. package/dist/shared/foundation/tx/build-request.js +25 -0
  338. package/dist/shared/foundation/tx/sign-batch.d.ts +5 -0
  339. package/dist/shared/foundation/tx/sign-batch.js +26 -0
  340. package/dist/shared/foundation/tx/wallet-sign-patch.d.ts +1 -0
  341. package/dist/shared/foundation/tx/wallet-sign-patch.js +18 -0
  342. package/dist/shared/foundation/types/airdrop-sweep.d.ts +79 -0
  343. package/dist/shared/foundation/types/airdrop-sweep.js +1 -0
  344. package/dist/shared/foundation/types/erc20.d.ts +65 -0
  345. package/dist/shared/foundation/types/erc20.js +1 -0
  346. package/dist/shared/foundation/types/holders-maker.d.ts +64 -0
  347. package/dist/shared/foundation/types/holders-maker.js +1 -0
  348. package/dist/shared/foundation/types/index.d.ts +7 -0
  349. package/dist/shared/foundation/types/index.js +1 -0
  350. package/dist/shared/foundation/types/lp-inspect.d.ts +102 -0
  351. package/dist/shared/foundation/types/lp-inspect.js +1 -0
  352. package/dist/shared/foundation/types/multicall.d.ts +5 -0
  353. package/dist/shared/foundation/types/multicall.js +1 -0
  354. package/dist/shared/foundation/types/private-sale.d.ts +35 -0
  355. package/dist/shared/foundation/types/private-sale.js +1 -0
  356. package/dist/shared/foundation/types/quote-helpers.d.ts +17 -0
  357. package/dist/shared/foundation/types/quote-helpers.js +1 -0
  358. package/dist/shared/four/tax-token.d.ts +1 -1
  359. package/dist/shared/four/tax-token.js +27 -7
  360. package/dist/shared/index.d.ts +4 -0
  361. package/dist/shared/index.js +4 -0
  362. package/dist/types/distribute.d.ts +72 -0
  363. package/dist/types/distribute.js +1 -0
  364. package/dist/types 2/errors.d.ts +27 -0
  365. package/dist/types 2/errors.js +34 -0
  366. package/dist/utils/airdrop-sweep-types.d.ts +1 -0
  367. package/dist/utils/airdrop-sweep-types.js +1 -0
  368. package/dist/utils/airdrop-sweep.d.ts +4 -76
  369. package/dist/utils/airdrop-sweep.js +38 -52
  370. package/dist/utils/bundle-helpers.d.ts +9 -243
  371. package/dist/utils/bundle-helpers.js +10 -584
  372. package/dist/utils/constants.d.ts +5 -61
  373. package/dist/utils/constants.js +5 -80
  374. package/dist/utils/contract-factory.d.ts +2 -4
  375. package/dist/utils/contract-factory.js +25 -18
  376. package/dist/utils/erc20-types.d.ts +1 -0
  377. package/dist/utils/erc20-types.js +1 -0
  378. package/dist/utils/erc20.d.ts +8 -90
  379. package/dist/utils/erc20.js +94 -153
  380. package/dist/utils/errors.d.ts +10 -22
  381. package/dist/utils/errors.js +61 -79
  382. package/dist/utils/holders-maker/addresses.d.ts +12 -0
  383. package/dist/utils/holders-maker/addresses.js +15 -0
  384. package/dist/utils/holders-maker/buy-tx.d.ts +44 -0
  385. package/dist/utils/holders-maker/buy-tx.js +278 -0
  386. package/dist/utils/holders-maker/constants.d.ts +6 -0
  387. package/dist/utils/holders-maker/constants.js +7 -0
  388. package/dist/utils/holders-maker/disperse.d.ts +18 -0
  389. package/dist/utils/holders-maker/disperse.js +90 -0
  390. package/dist/utils/holders-maker/routing.d.ts +4 -0
  391. package/dist/utils/holders-maker/routing.js +45 -0
  392. package/dist/utils/holders-maker/transfer-tx.d.ts +4 -0
  393. package/dist/utils/holders-maker/transfer-tx.js +67 -0
  394. package/dist/utils/holders-maker-helpers.d.ts +9 -0
  395. package/dist/utils/holders-maker-helpers.js +9 -0
  396. package/dist/utils/holders-maker-types.d.ts +1 -0
  397. package/dist/utils/holders-maker-types.js +1 -0
  398. package/dist/utils/holders-maker.d.ts +2 -138
  399. package/dist/utils/holders-maker.js +23 -651
  400. package/dist/utils/hop-chains.d.ts +35 -0
  401. package/dist/utils/hop-chains.js +215 -0
  402. package/dist/utils/lp-inspect-helpers.d.ts +9 -0
  403. package/dist/utils/lp-inspect-helpers.js +109 -0
  404. package/dist/utils/lp-inspect-types.d.ts +1 -0
  405. package/dist/utils/lp-inspect-types.js +1 -0
  406. package/dist/utils/lp-inspect.d.ts +2 -112
  407. package/dist/utils/lp-inspect.js +63 -195
  408. package/dist/utils/mpcExclusive.d.ts +2 -5
  409. package/dist/utils/mpcExclusive.js +4 -3
  410. package/dist/utils/private-sale-types.d.ts +1 -0
  411. package/dist/utils/private-sale-types.js +1 -0
  412. package/dist/utils/private-sale.d.ts +2 -58
  413. package/dist/utils/private-sale.js +2 -7
  414. package/dist/utils/provider-factory.d.ts +4 -0
  415. package/dist/utils/provider-factory.js +10 -0
  416. package/dist/utils/quote-helpers-types.d.ts +1 -0
  417. package/dist/utils/quote-helpers-types.js +1 -0
  418. package/dist/utils/quote-helpers.d.ts +4 -45
  419. package/dist/utils/quote-helpers.js +15 -65
  420. package/dist/utils/stealth-transfer.d.ts +2 -28
  421. package/dist/utils/stealth-transfer.js +30 -14
  422. package/dist/utils/swap-helpers.d.ts +0 -3
  423. package/dist/utils/swap-helpers.js +6 -9
  424. package/dist/utils/types/airdrop-sweep.d.ts +1 -0
  425. package/dist/utils/types/airdrop-sweep.js +1 -0
  426. package/dist/utils/types/contract-factory.d.ts +1 -0
  427. package/dist/utils/types/contract-factory.js +1 -0
  428. package/dist/utils/types/erc20.d.ts +1 -0
  429. package/dist/utils/types/erc20.js +1 -0
  430. package/dist/utils/types/errors.d.ts +1 -0
  431. package/dist/utils/types/errors.js +1 -0
  432. package/dist/utils/types/holders-maker.d.ts +1 -0
  433. package/dist/utils/types/holders-maker.js +1 -0
  434. package/dist/utils/types/hop-chains.d.ts +8 -0
  435. package/dist/utils/types/hop-chains.js +1 -0
  436. package/dist/utils/types/index.d.ts +13 -0
  437. package/dist/utils/types/index.js +1 -0
  438. package/dist/utils/types/lp-inspect.d.ts +1 -0
  439. package/dist/utils/types/lp-inspect.js +1 -0
  440. package/dist/utils/types/mpc-exclusive.d.ts +5 -0
  441. package/dist/utils/types/mpc-exclusive.js +1 -0
  442. package/dist/utils/types/private-sale.d.ts +1 -0
  443. package/dist/utils/types/private-sale.js +1 -0
  444. package/dist/utils/types/quote-helpers.d.ts +1 -0
  445. package/dist/utils/types/quote-helpers.js +1 -0
  446. package/dist/utils/types/stealth-transfer.d.ts +44 -0
  447. package/dist/utils/types/stealth-transfer.js +1 -0
  448. package/dist/utils/types/wallet.d.ts +25 -0
  449. package/dist/utils/types/wallet.js +1 -0
  450. package/dist/utils/wallet.d.ts +2 -25
  451. package/dist/utils/wallet.js +9 -10
  452. package/package.json +55 -2
  453. package/src/abis/contracts/TaxToken.json +969 -0
  454. package/src/abis/contracts/TokenManager.json +836 -0
  455. package/src/abis/contracts/TokenManager2.json +136 -0
  456. package/src/abis/contracts/TokenManagerHelper3.json +993 -0
  457. /package/dist/{shared 2/flap/__tests__/curve.test.d.ts → bundle-core/__tests__/config-helpers.test.d.ts} +0 -0
  458. /package/dist/{shared 2/flap/portal-bundle-merkle/types.js → bundle-core/__tests__/facade-parity.test.d.ts} +0 -0
  459. /package/dist/{shared 2/abis → shared/abis 2}/TaxToken.json +0 -0
  460. /package/dist/{shared 2/abis → shared/abis 2}/TokenManager.json +0 -0
  461. /package/dist/{shared 2/abis → shared/abis 2}/TokenManager2.json +0 -0
  462. /package/dist/{shared 2/abis → shared/abis 2}/TokenManagerHelper3.json +0 -0
  463. /package/dist/{shared 2/abis → shared/abis 2}/common.d.ts +0 -0
  464. /package/dist/{shared 2/abis → shared/abis 2}/common.js +0 -0
  465. /package/dist/{shared 2/abis → shared/abis 2}/index.d.ts +0 -0
  466. /package/dist/{shared 2/abis → shared/abis 2}/index.js +0 -0
  467. /package/dist/{shared 2/clients → shared/clients 2}/blockrazor.d.ts +0 -0
  468. /package/dist/{shared 2/clients → shared/clients 2}/blockrazor.js +0 -0
  469. /package/dist/{shared 2/clients → shared/clients 2}/club48.d.ts +0 -0
  470. /package/dist/{shared 2/clients → shared/clients 2}/club48.js +0 -0
  471. /package/dist/{shared 2/clients → shared/clients 2}/emitservice.d.ts +0 -0
  472. /package/dist/{shared 2/clients → shared/clients 2}/emitservice.js +0 -0
  473. /package/dist/{shared 2/clients → shared/clients 2}/four.d.ts +0 -0
  474. /package/dist/{shared 2/clients → shared/clients 2}/four.js +0 -0
  475. /package/dist/{shared 2/clients → shared/clients 2}/merkle.d.ts +0 -0
  476. /package/dist/{shared 2/clients → shared/clients 2}/merkle.js +0 -0
  477. /package/dist/{shared 2/flap → shared/flap 2}/__tests__/curve.test.js +0 -0
  478. /package/dist/{shared 2/flap → shared/flap 2}/abi.d.ts +0 -0
  479. /package/dist/{shared 2/flap → shared/flap 2}/abi.js +0 -0
  480. /package/dist/{shared 2/flap → shared/flap 2}/constants.d.ts +0 -0
  481. /package/dist/{shared 2/flap → shared/flap 2}/constants.js +0 -0
  482. /package/dist/{shared 2/flap → shared/flap 2}/curve.d.ts +0 -0
  483. /package/dist/{shared 2/flap → shared/flap 2}/curve.js +0 -0
  484. /package/dist/{shared 2/flap → shared/flap 2}/errors.d.ts +0 -0
  485. /package/dist/{shared 2/flap → shared/flap 2}/errors.js +0 -0
  486. /package/dist/{shared 2/flap → shared/flap 2}/index.d.ts +0 -0
  487. /package/dist/{shared 2/flap → shared/flap 2}/index.js +0 -0
  488. /package/dist/{shared 2/flap → shared/flap 2}/ipfs.d.ts +0 -0
  489. /package/dist/{shared 2/flap → shared/flap 2}/ipfs.js +0 -0
  490. /package/dist/{shared 2/flap → shared/flap 2}/meta.d.ts +0 -0
  491. /package/dist/{shared 2/flap → shared/flap 2}/meta.js +0 -0
  492. /package/dist/{shared 2/flap → shared/flap 2}/permit.d.ts +0 -0
  493. /package/dist/{shared 2/flap → shared/flap 2}/permit.js +0 -0
  494. /package/dist/{shared 2/flap → shared/flap 2}/pinata.d.ts +0 -0
  495. /package/dist/{shared 2/flap → shared/flap 2}/pinata.js +0 -0
  496. /package/dist/{shared 2/flap → shared/flap 2}/portal-bundle-merkle/config.d.ts +0 -0
  497. /package/dist/{shared 2/flap → shared/flap 2}/portal-bundle-merkle/config.js +0 -0
  498. /package/dist/{shared 2/flap → shared/flap 2}/portal-bundle-merkle/core.d.ts +0 -0
  499. /package/dist/{shared 2/flap → shared/flap 2}/portal-bundle-merkle/core.js +0 -0
  500. /package/dist/{shared 2/flap → shared/flap 2}/portal-bundle-merkle/create-to-dex.d.ts +0 -0
  501. /package/dist/{shared 2/flap → shared/flap 2}/portal-bundle-merkle/create-to-dex.js +0 -0
  502. /package/dist/{shared 2/flap → shared/flap 2}/portal-bundle-merkle/curve-to-dex.d.ts +0 -0
  503. /package/dist/{shared 2/flap → shared/flap 2}/portal-bundle-merkle/curve-to-dex.js +0 -0
  504. /package/dist/{shared 2/flap → shared/flap 2}/portal-bundle-merkle/index.d.ts +0 -0
  505. /package/dist/{shared 2/flap → shared/flap 2}/portal-bundle-merkle/index.js +0 -0
  506. /package/dist/{shared 2/flap → shared/flap 2}/portal-bundle-merkle/pancake-proxy.d.ts +0 -0
  507. /package/dist/{shared 2/flap → shared/flap 2}/portal-bundle-merkle/pancake-proxy.js +0 -0
  508. /package/dist/{shared 2/flap → shared/flap 2}/portal-bundle-merkle/private.d.ts +0 -0
  509. /package/dist/{shared 2/flap → shared/flap 2}/portal-bundle-merkle/private.js +0 -0
  510. /package/dist/{shared 2/flap → shared/flap 2}/portal-bundle-merkle/swap-buy-first.d.ts +0 -0
  511. /package/dist/{shared 2/flap → shared/flap 2}/portal-bundle-merkle/swap-buy-first.js +0 -0
  512. /package/dist/{shared 2/flap → shared/flap 2}/portal-bundle-merkle/swap.d.ts +0 -0
  513. /package/dist/{shared 2/flap → shared/flap 2}/portal-bundle-merkle/swap.js +0 -0
  514. /package/dist/{shared 2/flap → shared/flap 2}/portal-bundle-merkle/types.d.ts +0 -0
  515. /package/dist/{shared 2/flap → shared/flap 2}/portal-bundle-merkle/utils.d.ts +0 -0
  516. /package/dist/{shared 2/flap → shared/flap 2}/portal-bundle-merkle/utils.js +0 -0
  517. /package/dist/{shared 2/flap → shared/flap 2}/portal-bundle.d.ts +0 -0
  518. /package/dist/{shared 2/flap → shared/flap 2}/portal-bundle.js +0 -0
  519. /package/dist/{shared 2/flap → shared/flap 2}/portal.d.ts +0 -0
  520. /package/dist/{shared 2/flap → shared/flap 2}/portal.js +0 -0
  521. /package/dist/{shared 2/flap → shared/flap 2}/vanity.d.ts +0 -0
  522. /package/dist/{shared 2/flap → shared/flap 2}/vanity.js +0 -0
  523. /package/dist/{shared 2/flap → shared/flap 2}/vault.d.ts +0 -0
  524. /package/dist/{shared 2/flap → shared/flap 2}/vault.js +0 -0
  525. /package/dist/{shared 2/four → shared/four 2}/index.d.ts +0 -0
  526. /package/dist/{shared 2/four → shared/four 2}/index.js +0 -0
  527. /package/dist/{shared 2/four → shared/four 2}/tax-token.d.ts +0 -0
  528. /package/dist/{shared 2/four → shared/four 2}/tax-token.js +0 -0
  529. /package/dist/{shared 2/index.d.ts → shared/index 2.js} +0 -0
  530. /package/dist/{shared 2/index.js → shared/index.d 2.ts} +0 -0
@@ -1,1536 +1,7 @@
1
- import { ethers, Wallet } from 'ethers';
2
- import { getOptimizedGasPrice, NonceManager, buildProfitHopTransactions, PROFIT_HOP_COUNT, buildGasFields } from '../../../utils/bundle-helpers.js';
3
- import { BLOCKRAZOR_BUILDER_EOA, calculateTransferFee } from '../../../utils/constants.js';
4
- import { GAS_LIMITS, CHAINS } from '../../../shared/constants/index.js';
5
- import { getTxType, getGasPriceConfig, shouldExtractProfit, getProfitRecipient, getBribeAmount } from './config.js';
6
- // ==================== Gas 常量(使用统一常量) ====================
7
- const NATIVE_TRANSFER_GAS_LIMIT = GAS_LIMITS.NATIVE_TRANSFER;
8
- const BRIBE_GAS_LIMIT = GAS_LIMITS.BRIBE;
9
- const PROFIT_HOP_GAS_LIMIT = GAS_LIMITS.NATIVE_TRANSFER; // 利润多跳使用 NATIVE_TRANSFER
10
- import { getErc20DecimalsMerkle as _getErc20DecimalsMerkle, generateHopWallets as _generateHopWallets, normalizeAmounts as _normalizeAmounts, batchGetBalances as _batchGetBalances, calculateGasLimit as _calculateGasLimit, isNativeTokenAddress as _isNativeTokenAddress } from './internal.js';
11
- // ==================== 钱包转账按地址收费 ====================
12
1
  /**
13
- * 分发(仅签名版本 - 不依赖 Merkle)
14
- * 精简版:只负责签名交易,不提交到 Merkle
15
- * ✅ 优化版:支持 startNonce 参数避免并行调用时的 nonce 冲突
2
+ * Four.meme Merkle 转账类工具(分发 / 归集 / 多对多)。
3
+ * 实现已拆分为子模块以降低单文件体量(参见审计文档 P0)。
16
4
  */
17
- export async function disperseWithBundleMerkle(params) {
18
- const { fromPrivateKey, recipients, amount, amounts, tokenAddress, tokenDecimals, hopCount = 0, hopPrivateKeys, items, config, startNonce } = params;
19
- // 快速返回空结果
20
- if (!recipients || recipients.length === 0) {
21
- return {
22
- signedTransactions: [],
23
- hopWallets: undefined
24
- };
25
- }
26
- // 初始化
27
- const chainIdNum = config.chainId ?? CHAINS.BSC.chainId;
28
- const chainName = chainIdNum === CHAINS.MONAD.chainId ? 'Monad' : chainIdNum === CHAINS.BSC.chainId ? 'BSC' : `Chain-${chainIdNum}`;
29
- const provider = new ethers.JsonRpcProvider(config.rpcUrl, {
30
- chainId: chainIdNum,
31
- name: chainName
32
- });
33
- const mainWallet = new Wallet(fromPrivateKey, provider);
34
- const isNative = _isNativeTokenAddress(tokenAddress);
35
- const txType = getTxType(config);
36
- // ✅ 优化:预处理数据(不需要 RPC)
37
- const normalizedAmounts = items && items.length > 0
38
- ? items.map(it => (typeof it.amount === 'bigint' ? it.amount.toString() : String(it.amount)))
39
- : _normalizeAmounts(recipients, amount, amounts);
40
- const providedHops = (() => {
41
- if (hopPrivateKeys && hopPrivateKeys.length > 0) {
42
- if (hopPrivateKeys.length !== recipients.length) {
43
- throw new Error(`hopPrivateKeys length (${hopPrivateKeys.length}) must match recipients length (${recipients.length})`);
44
- }
45
- return hopPrivateKeys.every(h => h.length === 0) ? null : hopPrivateKeys;
46
- }
47
- if (items && items.length > 0) {
48
- const hops = items.map(it => it.hopPrivateKeys ?? []);
49
- return hops.every(h => h.length === 0) ? null : hops;
50
- }
51
- return null;
52
- })();
53
- const hopCountInput = (() => {
54
- if (items && items.length > 0) {
55
- const baseArray = Array.isArray(hopCount) ? hopCount : new Array(recipients.length).fill(hopCount);
56
- return items.map((it, i) => (typeof it.hopCount === 'number' ? it.hopCount : (baseArray[i] ?? 0)));
57
- }
58
- return hopCount;
59
- })();
60
- const preparedHops = providedHops ?? _generateHopWallets(recipients.length, hopCountInput);
61
- const hasHops = preparedHops !== null;
62
- const maxHopCount = hasHops ? Math.max(...preparedHops.map(h => h.length)) : 0;
63
- const finalGasLimit = _calculateGasLimit(config, isNative, hasHops, maxHopCount);
64
- const nativeGasLimit = (config.prefer21000ForNative ?? false) ? BRIBE_GAS_LIMIT : finalGasLimit;
65
- const signedTxs = [];
66
- const extractProfit = shouldExtractProfit(config);
67
- const profitAddr = getProfitRecipient();
68
- let totalProfit = 0n;
69
- let totalAmountBeforeProfit = 0n;
70
- let profitHopWallets; // ✅ 收集利润多跳钱包
71
- // ✅ 优化:并行获取 gasPrice 和 nonces(或使用传入的 startNonce)
72
- const nonceManager = new NonceManager(provider);
73
- if (!preparedHops) {
74
- // ========== 无多跳:直接批量转账 ==========
75
- // ✅ BSC 链贿赂支持
76
- const bribeAmount = chainIdNum === CHAINS.BSC.chainId ? getBribeAmount(config) : 0n;
77
- const needBribeTx = bribeAmount > 0n;
78
- const extraTxCount = (extractProfit ? 1 : 0) + (needBribeTx ? 1 : 0);
79
- const totalTxCount = recipients.length + extraTxCount;
80
- // ✅ 优化:并行获取 gasPrice 和 nonces
81
- const [gasPrice, nonces] = await Promise.all([
82
- getOptimizedGasPrice(provider, getGasPriceConfig(config)),
83
- startNonce !== undefined
84
- ? Promise.resolve(Array.from({ length: totalTxCount }, (_, i) => startNonce + i))
85
- : nonceManager.getNextNonceBatch(mainWallet, totalTxCount)
86
- ]);
87
- // ✅ 贿赂交易放在最前面
88
- let nonceOffset = 0;
89
- if (needBribeTx) {
90
- const bribeTx = await mainWallet.signTransaction({
91
- to: BLOCKRAZOR_BUILDER_EOA,
92
- value: bribeAmount,
93
- nonce: nonces[nonceOffset++],
94
- gasPrice,
95
- gasLimit: BRIBE_GAS_LIMIT,
96
- chainId: chainIdNum,
97
- type: txType
98
- });
99
- signedTxs.push(bribeTx);
100
- }
101
- if (isNative) {
102
- // ✅ 原生币:按地址数量收取固定费用,全额转账
103
- if (extractProfit) {
104
- totalProfit = calculateTransferFee(chainIdNum, recipients.length);
105
- }
106
- const txDataList = recipients.map((to, i) => {
107
- const originalAmount = ethers.parseEther(normalizedAmounts[i]);
108
- totalAmountBeforeProfit += originalAmount;
109
- return { to, value: originalAmount, nonce: nonces[nonceOffset + i] };
110
- });
111
- // ✅ 并行签名所有交易
112
- const txPromises = txDataList.map(({ to, value, nonce }) => mainWallet.signTransaction({
113
- to,
114
- value,
115
- nonce,
116
- gasPrice,
117
- gasLimit: nativeGasLimit,
118
- chainId: chainIdNum,
119
- type: txType
120
- }));
121
- signedTxs.push(...(await Promise.all(txPromises)));
122
- // ✅ 利润多跳转账(强制 2 跳中转)
123
- if (extractProfit && totalProfit > 0n) {
124
- const profitHopResult = await buildProfitHopTransactions({
125
- provider,
126
- payerWallet: mainWallet,
127
- profitAmount: totalProfit,
128
- profitRecipient: profitAddr,
129
- hopCount: PROFIT_HOP_COUNT,
130
- gasPrice,
131
- chainId: chainIdNum,
132
- txType,
133
- startNonce: nonces[nonceOffset + recipients.length]
134
- });
135
- signedTxs.push(...profitHopResult.signedTransactions);
136
- profitHopWallets = profitHopResult.hopWallets; // ✅ 收集利润多跳钱包
137
- }
138
- }
139
- else {
140
- // ✅ ERC20:并行获取 decimals(传入 chainIdNum 避免 NETWORK_ERROR)
141
- const decimals = tokenDecimals ?? (await _getErc20DecimalsMerkle(provider, tokenAddress, chainIdNum));
142
- const iface = new ethers.Interface(['function transfer(address,uint256) returns (bool)']);
143
- // ✅ 先计算所有原始金额
144
- const originalAmounts = [];
145
- for (let i = 0; i < recipients.length; i++) {
146
- const originalAmount = ethers.parseUnits(normalizedAmounts[i], decimals);
147
- originalAmounts.push(originalAmount);
148
- totalAmountBeforeProfit += originalAmount;
149
- }
150
- // ✅ 按地址数量收取固定费用(原生代币)
151
- let nativeProfitAmount = 0n;
152
- if (extractProfit) {
153
- nativeProfitAmount = calculateTransferFee(chainIdNum, recipients.length);
154
- totalProfit = nativeProfitAmount;
155
- }
156
- // ✅ ERC20 全额分发,费用以原生币单独支付
157
- const txDataList = recipients.map((to, i) => {
158
- const originalAmount = originalAmounts[i];
159
- const data = iface.encodeFunctionData('transfer', [to, originalAmount]);
160
- return { data, nonce: nonces[nonceOffset + i] };
161
- });
162
- // ✅ 并行签名所有交易
163
- const txPromises = txDataList.map(({ data, nonce }) => mainWallet.signTransaction({
164
- to: tokenAddress,
165
- data,
166
- value: 0n,
167
- nonce,
168
- gasPrice,
169
- gasLimit: finalGasLimit,
170
- chainId: chainIdNum,
171
- type: txType
172
- }));
173
- signedTxs.push(...(await Promise.all(txPromises)));
174
- // ✅ 费用多跳转账(强制 2 跳中转)
175
- if (extractProfit && nativeProfitAmount > 0n) {
176
- const profitHopResult = await buildProfitHopTransactions({
177
- provider,
178
- payerWallet: mainWallet,
179
- profitAmount: nativeProfitAmount,
180
- profitRecipient: profitAddr,
181
- hopCount: PROFIT_HOP_COUNT,
182
- gasPrice,
183
- chainId: chainIdNum,
184
- txType,
185
- startNonce: nonces[nonceOffset + recipients.length]
186
- });
187
- signedTxs.push(...profitHopResult.signedTransactions);
188
- profitHopWallets = profitHopResult.hopWallets; // ✅ 收集利润多跳钱包
189
- }
190
- }
191
- }
192
- else {
193
- // ========== 有多跳:构建跳转链 ==========
194
- // ✅ BSC 链贿赂支持
195
- const bribeAmountHop = chainIdNum === CHAINS.BSC.chainId ? getBribeAmount(config) : 0n;
196
- const needBribeTxHop = bribeAmountHop > 0n;
197
- // ✅ 优化:并行获取 gasPrice 和 decimals
198
- const [gasPrice, decimals] = await Promise.all([
199
- getOptimizedGasPrice(provider, getGasPriceConfig(config)),
200
- isNative ? Promise.resolve(18) : Promise.resolve(tokenDecimals ?? await _getErc20DecimalsMerkle(provider, tokenAddress, chainIdNum))
201
- ]);
202
- const iface = isNative ? null : new ethers.Interface(['function transfer(address,uint256) returns (bool)']);
203
- // ✅ Gas limit 设置(与 swap.ts 保持一致)
204
- // - 原生代币转账:使用 GAS_LIMITS.NATIVE_TRANSFER(固定)
205
- // - ERC20 转账:使用 GAS_LIMITS.ERC20_TRANSFER(固定,因为 gas 波动较大)
206
- const nativeTransferGasLimit = NATIVE_TRANSFER_GAS_LIMIT;
207
- const erc20TransferGasLimit = GAS_LIMITS.ERC20_TRANSFER;
208
- // ✅ 原生代币多跳的 gas 费(每跳需要 21055)
209
- const nativeHopGasFee = nativeTransferGasLimit * gasPrice;
210
- // ✅ ERC20 多跳时,中转钱包需要执行 2 笔交易(转 gas + 转 ERC20)
211
- const erc20HopGasFee = erc20TransferGasLimit * gasPrice;
212
- const nativeHopGasFeeForErc20 = nativeTransferGasLimit * gasPrice;
213
- // ✅ 优化:预先计算主钱包需要的总 nonce 数量
214
- // - 原生代币多跳:主钱包只需要 1 个 nonce(一笔转账包含后续所有 gas)
215
- // - ERC20 多跳:主钱包需要 2 个 nonce(转 gas + 转 ERC20)
216
- let mainWalletNonceCount = 0;
217
- // 贿赂交易需要 1 个额外的 nonce
218
- if (needBribeTxHop)
219
- mainWalletNonceCount += 1;
220
- for (let i = 0; i < recipients.length; i++) {
221
- const hopChain = preparedHops[i];
222
- if (hopChain.length === 0) {
223
- // 无跳转:1 个 nonce
224
- mainWalletNonceCount += 1;
225
- }
226
- else {
227
- // 有跳转:
228
- // - Native: 1(一笔转账)
229
- // - ERC20: 2(转 gas + 转 ERC20)
230
- mainWalletNonceCount += isNative ? 1 : 2;
231
- }
232
- }
233
- // 利润交易需要额外 1 个 nonce
234
- if (extractProfit)
235
- mainWalletNonceCount += 1;
236
- // ✅ 一次性获取所有主钱包的 nonces
237
- const allMainNonces = startNonce !== undefined
238
- ? Array.from({ length: mainWalletNonceCount }, (_, i) => startNonce + i)
239
- : await nonceManager.getNextNonceBatch(mainWallet, mainWalletNonceCount);
240
- let mainNonceIdx = 0;
241
- // ✅ 贿赂交易放在最前面(多跳场景)
242
- if (needBribeTxHop) {
243
- const bribeTx = await mainWallet.signTransaction({
244
- to: BLOCKRAZOR_BUILDER_EOA,
245
- value: bribeAmountHop,
246
- nonce: allMainNonces[mainNonceIdx++],
247
- gasPrice,
248
- gasLimit: BRIBE_GAS_LIMIT,
249
- chainId: chainIdNum,
250
- type: txType
251
- });
252
- signedTxs.push(bribeTx);
253
- }
254
- const txsToSign = [];
255
- // ✅ 按地址数量收取固定费用(原生代币),不再扣除转账金额
256
- if (extractProfit) {
257
- totalProfit = calculateTransferFee(chainIdNum, recipients.length);
258
- }
259
- for (let i = 0; i < recipients.length; i++) {
260
- const finalRecipient = recipients[i];
261
- const originalAmountWei = isNative
262
- ? ethers.parseEther(normalizedAmounts[i])
263
- : ethers.parseUnits(normalizedAmounts[i], decimals);
264
- totalAmountBeforeProfit += originalAmountWei;
265
- const amountWei = originalAmountWei; // 全额转账
266
- const hopChain = preparedHops[i];
267
- if (hopChain.length === 0) {
268
- // 无跳转:直接转账
269
- const nonce = allMainNonces[mainNonceIdx++];
270
- if (isNative) {
271
- txsToSign.push({
272
- wallet: mainWallet,
273
- tx: {
274
- to: finalRecipient,
275
- value: amountWei,
276
- nonce,
277
- gasPrice,
278
- gasLimit: nativeGasLimit,
279
- chainId: chainIdNum,
280
- type: txType
281
- }
282
- });
283
- }
284
- else {
285
- const data = iface.encodeFunctionData('transfer', [finalRecipient, amountWei]);
286
- txsToSign.push({
287
- wallet: mainWallet,
288
- tx: {
289
- to: tokenAddress,
290
- data,
291
- value: 0n,
292
- nonce,
293
- gasPrice,
294
- gasLimit: finalGasLimit,
295
- chainId: chainIdNum,
296
- type: txType
297
- }
298
- });
299
- }
300
- continue;
301
- }
302
- // 有跳转
303
- // ✅ 支持 string 和 GeneratedWallet 两种类型
304
- const fullChain = [mainWallet, ...hopChain.map(w => new Wallet(typeof w === 'string' ? w : w.privateKey, provider))];
305
- const addresses = [...fullChain.map(w => w.address), finalRecipient];
306
- if (isNative) {
307
- // ========== 原生代币多跳:gas 包含在转账金额中逐层传递 ==========
308
- for (let j = 0; j < addresses.length - 1; j++) {
309
- const fromWallet = fullChain[j];
310
- const toAddress = addresses[j + 1];
311
- const nonce = j === 0 ? allMainNonces[mainNonceIdx++] : 0;
312
- const remainingHops = addresses.length - 2 - j;
313
- const additionalGas = nativeHopGasFee * BigInt(remainingHops);
314
- const transferValue = amountWei + additionalGas;
315
- txsToSign.push({
316
- wallet: fromWallet,
317
- tx: {
318
- to: toAddress,
319
- value: transferValue,
320
- nonce,
321
- gasPrice,
322
- gasLimit: nativeTransferGasLimit,
323
- chainId: chainIdNum,
324
- type: txType
325
- }
326
- });
327
- }
328
- }
329
- else {
330
- // ========== ERC20 多跳:gas 也逐层传递(保护隐私)==========
331
- // 计算每个中转钱包需要的 gas(从后往前)
332
- // - 最后一个中转钱包:只需要 ERC20 转账的 gas
333
- // - 其他中转钱包:需要 转 gas 的 gas + ERC20 转账的 gas + 后续所有的 gas
334
- const hopGasNeeds = [];
335
- for (let j = hopChain.length - 1; j >= 0; j--) {
336
- if (j === hopChain.length - 1) {
337
- // 最后一个中转钱包:只需要 ERC20 转账的 gas
338
- hopGasNeeds.unshift(erc20HopGasFee);
339
- }
340
- else {
341
- // 其他中转钱包:转 gas(21000) + 转 ERC20 + 后续的 gas
342
- const nextHopGas = hopGasNeeds[0];
343
- hopGasNeeds.unshift(nativeHopGasFeeForErc20 + erc20HopGasFee + nextHopGas);
344
- }
345
- }
346
- // 第一步:主钱包给第一个中转钱包转 gas(包含所有后续的 gas)
347
- const totalGasForFirstHop = hopGasNeeds[0];
348
- txsToSign.push({
349
- wallet: mainWallet,
350
- tx: {
351
- to: fullChain[1].address,
352
- value: totalGasForFirstHop,
353
- nonce: allMainNonces[mainNonceIdx++],
354
- gasPrice,
355
- gasLimit: nativeTransferGasLimit,
356
- chainId: chainIdNum,
357
- type: txType
358
- }
359
- });
360
- // 第二步:主钱包给第一个中转钱包转 ERC20
361
- const mainToFirstHopData = iface.encodeFunctionData('transfer', [fullChain[1].address, amountWei]);
362
- txsToSign.push({
363
- wallet: mainWallet,
364
- tx: {
365
- to: tokenAddress,
366
- data: mainToFirstHopData,
367
- value: 0n,
368
- nonce: allMainNonces[mainNonceIdx++],
369
- gasPrice,
370
- gasLimit: erc20TransferGasLimit,
371
- chainId: chainIdNum,
372
- type: txType
373
- }
374
- });
375
- // 第三步:中转钱包逐层传递(gas 和 ERC20)
376
- for (let j = 1; j < fullChain.length; j++) {
377
- const fromWallet = fullChain[j];
378
- const toAddress = addresses[j + 1]; // 下一个地址(可能是中转钱包或最终接收者)
379
- const isLastHop = j === fullChain.length - 1;
380
- if (!isLastHop) {
381
- // 非最后一个中转钱包:先转 gas 给下一个中转钱包
382
- const gasToTransfer = hopGasNeeds[j]; // 下一个中转钱包需要的 gas
383
- txsToSign.push({
384
- wallet: fromWallet,
385
- tx: {
386
- to: toAddress,
387
- value: gasToTransfer,
388
- nonce: 0, // 中转钱包的第一笔交易
389
- gasPrice,
390
- gasLimit: nativeTransferGasLimit,
391
- chainId: chainIdNum,
392
- type: txType
393
- }
394
- });
395
- // 再转 ERC20
396
- const erc20Data = iface.encodeFunctionData('transfer', [toAddress, amountWei]);
397
- txsToSign.push({
398
- wallet: fromWallet,
399
- tx: {
400
- to: tokenAddress,
401
- data: erc20Data,
402
- value: 0n,
403
- nonce: 1, // 中转钱包的第二笔交易
404
- gasPrice,
405
- gasLimit: erc20TransferGasLimit,
406
- chainId: chainIdNum,
407
- type: txType
408
- }
409
- });
410
- }
411
- else {
412
- // 最后一个中转钱包:只转 ERC20 给最终接收者
413
- const erc20Data = iface.encodeFunctionData('transfer', [toAddress, amountWei]);
414
- txsToSign.push({
415
- wallet: fromWallet,
416
- tx: {
417
- to: tokenAddress,
418
- data: erc20Data,
419
- value: 0n,
420
- nonce: 0, // 最后一个中转钱包只有一笔交易
421
- gasPrice,
422
- gasLimit: erc20TransferGasLimit,
423
- chainId: chainIdNum,
424
- type: txType
425
- }
426
- });
427
- }
428
- }
429
- }
430
- }
431
- // ✅ 并行签名所有交易
432
- const signedTxsResult = await Promise.all(txsToSign.map(({ wallet, tx }) => wallet.signTransaction(tx)));
433
- signedTxs.push(...signedTxsResult);
434
- // ✅ 利润多跳转账(强制 2 跳中转)
435
- if (extractProfit && totalProfit > 0n) {
436
- const profitNonce = allMainNonces[mainNonceIdx];
437
- const profitHopResult = await buildProfitHopTransactions({
438
- provider,
439
- payerWallet: mainWallet,
440
- profitAmount: totalProfit,
441
- profitRecipient: profitAddr,
442
- hopCount: PROFIT_HOP_COUNT,
443
- gasPrice,
444
- chainId: chainIdNum,
445
- txType,
446
- startNonce: profitNonce
447
- });
448
- signedTxs.push(...profitHopResult.signedTransactions);
449
- profitHopWallets = profitHopResult.hopWallets; // ✅ 收集利润多跳钱包
450
- }
451
- }
452
- // ✅ 简化返回:只返回签名交易、多跳钱包和元数据
453
- return {
454
- signedTransactions: signedTxs,
455
- hopWallets: preparedHops || undefined,
456
- profitHopWallets, // ✅ 返回利润多跳钱包
457
- metadata: extractProfit ? {
458
- totalAmount: ethers.formatEther(totalAmountBeforeProfit),
459
- profitAmount: ethers.formatEther(totalProfit),
460
- profitRecipient: profitAddr,
461
- recipientCount: recipients.length,
462
- isNative,
463
- tokenAddress: isNative ? undefined : tokenAddress
464
- } : undefined
465
- };
466
- }
467
- /**
468
- * 归集(仅签名版本 - 不依赖 Merkle)
469
- * ✅ 精简版:只负责签名交易,不提交到 Merkle
470
- * ✅ 优化版:支持 startNonce 参数避免并行调用时的 nonce 冲突
471
- */
472
- export async function sweepWithBundleMerkle(params) {
473
- const { sourcePrivateKeys, target, targetPrivateKey, ratioPct, ratios, amount, amounts, tokenAddress, tokenDecimals, skipIfInsufficient = true, hopCount = 0, hopPrivateKeys, sources, config } = params;
474
- // 快速返回空结果
475
- if (!sourcePrivateKeys || sourcePrivateKeys.length === 0) {
476
- return {
477
- signedTransactions: [],
478
- hopWallets: undefined
479
- };
480
- }
481
- // 初始化
482
- const chainIdNum = config.chainId ?? CHAINS.BSC.chainId;
483
- const chainName = chainIdNum === CHAINS.MONAD.chainId ? 'Monad' : chainIdNum === CHAINS.BSC.chainId ? 'BSC' : `Chain-${chainIdNum}`;
484
- const provider = new ethers.JsonRpcProvider(config.rpcUrl, {
485
- chainId: chainIdNum,
486
- name: chainName
487
- });
488
- const isNative = _isNativeTokenAddress(tokenAddress);
489
- const txType = getTxType(config);
490
- // ✅ 如果提供了 targetPrivateKey,创建 targetWallet 用于支付利润
491
- const targetWallet = targetPrivateKey ? new Wallet(targetPrivateKey, provider) : null;
492
- // 归集比例处理
493
- const clamp = (n) => (typeof n === 'number' && Number.isFinite(n)
494
- ? Math.max(0, Math.min(100, Math.floor(n)))
495
- : undefined);
496
- const ratio = clamp(ratioPct);
497
- // 生成跳转钱包
498
- // 统一 sources 输入
499
- const actualKeys = (() => {
500
- if (sources && sources.length > 0)
501
- return sources.map(s => s.privateKey);
502
- return sourcePrivateKeys;
503
- })();
504
- // hop 私钥链优先使用传入
505
- const providedHops = (() => {
506
- if (hopPrivateKeys && hopPrivateKeys.length > 0) {
507
- if (hopPrivateKeys.length !== actualKeys.length) {
508
- throw new Error(`hopPrivateKeys length (${hopPrivateKeys.length}) must match sourcePrivateKeys length (${actualKeys.length})`);
509
- }
510
- return hopPrivateKeys.every(h => h.length === 0) ? null : hopPrivateKeys;
511
- }
512
- if (sources && sources.length > 0) {
513
- const hops = sources.map(s => s.hopPrivateKeys ?? []);
514
- return hops.every(h => h.length === 0) ? null : hops;
515
- }
516
- return null;
517
- })();
518
- // hop 数量数组
519
- const hopCountInput = (() => {
520
- if (sources && sources.length > 0) {
521
- const baseArray = Array.isArray(hopCount) ? hopCount : new Array(actualKeys.length).fill(hopCount);
522
- return sources.map((s, i) => (typeof s.hopCount === 'number' ? s.hopCount : (baseArray[i] ?? 0)));
523
- }
524
- return hopCount;
525
- })();
526
- const preparedHops = providedHops ?? _generateHopWallets(actualKeys.length, hopCountInput);
527
- const hasHops = preparedHops !== null;
528
- const maxHopCount = hasHops ? Math.max(...preparedHops.map(h => h.length)) : 0;
529
- // 智能计算 Gas Limit
530
- const finalGasLimit = _calculateGasLimit(config, isNative, hasHops, maxHopCount);
531
- const nativeGasLimit = (config.prefer21000ForNative ?? false) ? BRIBE_GAS_LIMIT : finalGasLimit;
532
- const signedTxs = [];
533
- // ✅ 利润提取配置
534
- const extractProfit = shouldExtractProfit(config);
535
- const profitAddr = getProfitRecipient();
536
- let totalProfit = 0n;
537
- let totalAmountBeforeProfit = 0n;
538
- let profitHopWallets; // ✅ 收集利润多跳钱包
539
- // ✅ BSC 链贿赂支持(由 targetWallet 支付,如果提供了 targetPrivateKey)
540
- const bribeAmount = chainIdNum === CHAINS.BSC.chainId ? getBribeAmount(config) : 0n;
541
- const needBribeTx = bribeAmount > 0n && targetWallet !== null;
542
- const bribeSignedTx = [];
543
- // ✅ 修复 nonce 冲突:统一管理 targetWallet 的 nonce
544
- // 贿赂交易和利润多跳交易都由 targetWallet 签名,需要一次性获取所有 nonce
545
- let targetWalletNonceStart;
546
- let targetWalletNonceUsed = 0;
547
- if (targetWallet && (needBribeTx || extractProfit)) {
548
- // 预先获取 targetWallet 的 pending nonce,后续按顺序使用
549
- targetWalletNonceStart = await provider.getTransactionCount(targetWallet.address, 'pending');
550
- }
551
- // ✅ 贿赂交易放在最前面(由 targetWallet 签名)
552
- if (needBribeTx && targetWallet && targetWalletNonceStart !== undefined) {
553
- const bribeGasPrice = await getOptimizedGasPrice(provider, getGasPriceConfig(config));
554
- const bribeNonce = targetWalletNonceStart + targetWalletNonceUsed;
555
- targetWalletNonceUsed += 1; // ✅ 递增 nonce 计数
556
- const bribeTx = await targetWallet.signTransaction({
557
- to: BLOCKRAZOR_BUILDER_EOA,
558
- value: bribeAmount,
559
- nonce: bribeNonce,
560
- gasPrice: bribeGasPrice,
561
- gasLimit: BRIBE_GAS_LIMIT,
562
- chainId: chainIdNum,
563
- type: txType
564
- });
565
- bribeSignedTx.push(bribeTx);
566
- }
567
- if (!preparedHops) {
568
- // ========== 无多跳:直接批量归集 ==========
569
- const wallets = actualKeys.map(pk => new Wallet(pk, provider));
570
- const addresses = wallets.map(w => w.address);
571
- const nonceManager = new NonceManager(provider);
572
- // ✅ 优化:并行获取 gasPrice 和余额
573
- if (isNative) {
574
- // ✅ 原生币:并行获取 gasPrice 和余额
575
- const [gasPrice, balances] = await Promise.all([
576
- getOptimizedGasPrice(provider, getGasPriceConfig(config)),
577
- _batchGetBalances(provider, addresses)
578
- ]);
579
- const gasCostBase = nativeGasLimit * gasPrice;
580
- // ✅ 利润多跳需要 PROFIT_HOP_COUNT + 1 笔交易的 gas(payer → hop1 → hop2 → 利润地址)
581
- const profitTxGas = PROFIT_HOP_GAS_LIMIT * gasPrice * BigInt(PROFIT_HOP_COUNT + 1);
582
- // ✅ 第一步:计算所有钱包的归集金额和利润,找出归集金额最大的钱包
583
- const sweepAmounts = [];
584
- let maxSweepIndex = -1;
585
- let maxSweepAmount = 0n;
586
- for (let i = 0; i < wallets.length; i++) {
587
- const bal = balances[i];
588
- let toSend = 0n;
589
- const ratioForI = (() => {
590
- if (sources && sources[i] && sources[i].ratioPct !== undefined)
591
- return clamp(sources[i].ratioPct);
592
- if (ratios && ratios[i] !== undefined)
593
- return clamp(ratios[i]);
594
- return ratio;
595
- })();
596
- const amountStrForI = (() => {
597
- if (sources && sources[i] && sources[i].amount !== undefined)
598
- return String(sources[i].amount);
599
- if (amounts && amounts[i] !== undefined)
600
- return String(amounts[i]);
601
- return amount !== undefined ? String(amount) : undefined;
602
- })();
603
- // ✅ 只有支付者需要 2 笔交易的 gas,其他钱包只需要 1 笔
604
- const gasCost = gasCostBase; // 所有钱包都只计算主转账的 gas
605
- if (ratioForI !== undefined) {
606
- const want = (bal * BigInt(ratioForI)) / 100n;
607
- const maxSendable = bal > gasCost ? (bal - gasCost) : 0n;
608
- toSend = want > maxSendable ? maxSendable : want;
609
- }
610
- else if (amountStrForI && amountStrForI.trim().length > 0) {
611
- const amt = ethers.parseEther(amountStrForI);
612
- const need = amt + gasCost;
613
- if (!skipIfInsufficient || bal >= need)
614
- toSend = amt;
615
- }
616
- sweepAmounts.push(toSend);
617
- totalAmountBeforeProfit += toSend;
618
- // 找出归集金额最大的钱包
619
- if (toSend > maxSweepAmount) {
620
- maxSweepAmount = toSend;
621
- maxSweepIndex = i;
622
- }
623
- }
624
- // ✅ 按地址数量收取固定费用(原生代币)
625
- const activeNativeAddressCount = sweepAmounts.filter(a => a > 0n).length;
626
- if (extractProfit && activeNativeAddressCount > 0) {
627
- totalProfit = calculateTransferFee(chainIdNum, activeNativeAddressCount);
628
- }
629
- // ✅ 如果需要提取费用,检查支付者是否有足够余额
630
- if (extractProfit && totalProfit > 0n && maxSweepIndex >= 0) {
631
- const payerBalance = balances[maxSweepIndex];
632
- const payerSweepAmount = sweepAmounts[maxSweepIndex];
633
- const payerNeedGas = gasCostBase + profitTxGas + totalProfit;
634
- if (payerBalance < payerSweepAmount + payerNeedGas) {
635
- const maxPayerSweep = payerBalance > payerNeedGas ? payerBalance - payerNeedGas : 0n;
636
- sweepAmounts[maxSweepIndex] = maxPayerSweep;
637
- totalAmountBeforeProfit = sweepAmounts.reduce((sum, amt) => sum + amt, 0n);
638
- }
639
- }
640
- // ✅ 第二步:生成归集交易
641
- // ✅ 确定利润支付者:优先使用 targetWallet,否则使用归集金额最大的源钱包
642
- const useTargetAsPayer = targetWallet !== null;
643
- const profitPayerWallet = useTargetAsPayer ? targetWallet : (maxSweepIndex >= 0 ? wallets[maxSweepIndex] : null);
644
- // ✅ 修复 nonce 冲突:统一管理 targetWallet 的 nonce
645
- // 如果 targetWallet 作为利润支付者,使用预先获取的 nonce 起点 + 偏移量
646
- let payerProfitNonce;
647
- if (extractProfit && totalProfit > 0n && profitPayerWallet) {
648
- if (useTargetAsPayer && targetWalletNonceStart !== undefined) {
649
- // ✅ targetWallet:使用统一管理的 nonce(贿赂交易已使用 targetWalletNonceUsed 个)
650
- payerProfitNonce = targetWalletNonceStart + targetWalletNonceUsed;
651
- }
652
- else {
653
- // ✅ 源钱包支付利润:使用 NonceManager 获取 2 个 nonce(归集 + 利润)
654
- const nonces = await nonceManager.getNextNonceBatch(profitPayerWallet, 2);
655
- payerProfitNonce = nonces[1];
656
- }
657
- }
658
- // ✅ 优化:批量获取所有钱包的 nonce(JSON-RPC 批量请求)
659
- // 过滤出需要归集的钱包(如果用 targetWallet 支付利润,则不排除 maxSweepIndex)
660
- const walletsToSweep = wallets.filter((_, i) => sweepAmounts[i] > 0n && (useTargetAsPayer || i !== maxSweepIndex));
661
- const nonces = walletsToSweep.length > 0
662
- ? await nonceManager.getNextNoncesForWallets(walletsToSweep)
663
- : [];
664
- let nonceIdx = 0;
665
- const txPromises = wallets.map(async (w, i) => {
666
- const toSend = sweepAmounts[i];
667
- if (toSend <= 0n)
668
- return null;
669
- // ✅ 全额归集,费用由支付者额外支付原生代币
670
- const actualToSend = toSend;
671
- // ✅ 支付者使用预留的第一个 nonce,其他钱包使用批量获取的 nonce
672
- let nonce;
673
- if (!useTargetAsPayer && i === maxSweepIndex && payerProfitNonce !== undefined) {
674
- nonce = payerProfitNonce - 1; // 使用预留的第一个 nonce
675
- }
676
- else {
677
- nonce = nonces[nonceIdx++];
678
- }
679
- const mainTx = await w.signTransaction({
680
- to: target,
681
- value: actualToSend,
682
- nonce,
683
- gasPrice,
684
- gasLimit: nativeGasLimit,
685
- chainId: chainIdNum,
686
- type: txType
687
- });
688
- return mainTx;
689
- });
690
- const allTxs = (await Promise.all(txPromises)).filter(tx => tx !== null);
691
- signedTxs.push(...allTxs);
692
- // ✅ 第三步:利润多跳转账(强制 2 跳中转)
693
- if (extractProfit && totalProfit > 0n && profitPayerWallet && payerProfitNonce !== undefined) {
694
- const profitHopResult = await buildProfitHopTransactions({
695
- provider,
696
- payerWallet: profitPayerWallet,
697
- profitAmount: totalProfit,
698
- profitRecipient: profitAddr,
699
- hopCount: PROFIT_HOP_COUNT,
700
- gasPrice,
701
- chainId: chainIdNum,
702
- txType,
703
- startNonce: payerProfitNonce
704
- });
705
- signedTxs.push(...profitHopResult.signedTransactions);
706
- profitHopWallets = profitHopResult.hopWallets; // ✅ 收集利润多跳钱包
707
- }
708
- }
709
- else {
710
- // ✅ ERC20:并行获取 gasPrice、decimals、余额(传入 chainIdNum 避免 NETWORK_ERROR)
711
- const [gasPrice, decimals, balances, bnbBalances] = await Promise.all([
712
- getOptimizedGasPrice(provider, getGasPriceConfig(config)),
713
- Promise.resolve(tokenDecimals ?? await _getErc20DecimalsMerkle(provider, tokenAddress, chainIdNum)),
714
- _batchGetBalances(provider, addresses, tokenAddress),
715
- config.checkBnbForErc20NoHop ? _batchGetBalances(provider, addresses) : Promise.resolve([])
716
- ]);
717
- const iface = new ethers.Interface(['function transfer(address,uint256) returns (bool)']);
718
- // ✅ 利润多跳需要 PROFIT_HOP_COUNT + 1 笔交易的 gas
719
- const profitTxGas = PROFIT_HOP_GAS_LIMIT * gasPrice * BigInt(PROFIT_HOP_COUNT + 1);
720
- // ✅ 第一步:计算所有钱包的归集金额和利润,找出归集金额最大的钱包
721
- const sweepAmounts = [];
722
- let maxSweepIndex = -1;
723
- let maxSweepAmount = 0n;
724
- for (let i = 0; i < wallets.length; i++) {
725
- const bal = balances[i];
726
- let toSend = 0n;
727
- const ratioForI = (() => {
728
- if (sources && sources[i] && sources[i].ratioPct !== undefined)
729
- return clamp(sources[i].ratioPct);
730
- if (ratios && ratios[i] !== undefined)
731
- return clamp(ratios[i]);
732
- return ratio;
733
- })();
734
- const amountStrForI = (() => {
735
- if (sources && sources[i] && sources[i].amount !== undefined)
736
- return String(sources[i].amount);
737
- if (amounts && amounts[i] !== undefined)
738
- return String(amounts[i]);
739
- return amount !== undefined ? String(amount) : undefined;
740
- })();
741
- if (ratioForI !== undefined) {
742
- toSend = (bal * BigInt(ratioForI)) / 100n;
743
- }
744
- else if (amountStrForI && amountStrForI.trim().length > 0) {
745
- toSend = ethers.parseUnits(amountStrForI, decimals);
746
- }
747
- if (toSend <= 0n || (skipIfInsufficient && bal < toSend)) {
748
- sweepAmounts.push(0n);
749
- continue;
750
- }
751
- // ✅ 检查 BNB 余额(只需主转账的 gas,支付者后面单独检查)
752
- const totalGasNeeded = finalGasLimit * gasPrice;
753
- if (config.checkBnbForErc20NoHop) {
754
- const bnb = bnbBalances[i] ?? 0n;
755
- if (skipIfInsufficient && bnb < totalGasNeeded) {
756
- sweepAmounts.push(0n);
757
- continue;
758
- }
759
- }
760
- sweepAmounts.push(toSend);
761
- totalAmountBeforeProfit += toSend;
762
- // 找出归集金额最大的钱包
763
- if (toSend > maxSweepAmount) {
764
- maxSweepAmount = toSend;
765
- maxSweepIndex = i;
766
- }
767
- }
768
- // ✅ 按地址数量收取固定费用(原生代币支付,不受转账金额影响)
769
- const activeAddressCountErc20 = sweepAmounts.filter(a => a > 0n).length;
770
- let nativeProfitAmount = 0n;
771
- if (extractProfit && activeAddressCountErc20 > 0) {
772
- nativeProfitAmount = calculateTransferFee(chainIdNum, activeAddressCountErc20);
773
- totalProfit = nativeProfitAmount;
774
- }
775
- // ✅ 确定费用支付者:优先使用 targetWallet,否则使用归集金额最大的源钱包
776
- const useTargetAsPayerErc20 = targetWallet !== null;
777
- const profitPayerWalletErc20 = useTargetAsPayerErc20 ? targetWallet : (maxSweepIndex >= 0 ? wallets[maxSweepIndex] : null);
778
- // ✅ 如果需要提取费用,检查支付者是否有足够原生代币支付
779
- if (extractProfit && nativeProfitAmount > 0n && profitPayerWalletErc20 && config.checkBnbForErc20NoHop) {
780
- const payerBnbBalance = useTargetAsPayerErc20
781
- ? await provider.getBalance(target)
782
- : (bnbBalances[maxSweepIndex] ?? 0n);
783
- const payerNeedGas = (useTargetAsPayerErc20 ? 0n : finalGasLimit * gasPrice) + profitTxGas + nativeProfitAmount;
784
- if (payerBnbBalance < payerNeedGas) {
785
- nativeProfitAmount = 0n;
786
- totalProfit = 0n;
787
- }
788
- }
789
- // ✅ 第二步:生成归集交易(ERC20 全额归集,费用以原生币单独支付)
790
- let payerProfitNonce;
791
- if (extractProfit && nativeProfitAmount > 0n && profitPayerWalletErc20) {
792
- if (useTargetAsPayerErc20 && targetWalletNonceStart !== undefined) {
793
- payerProfitNonce = targetWalletNonceStart + targetWalletNonceUsed;
794
- }
795
- else {
796
- const nonces = await nonceManager.getNextNonceBatch(profitPayerWalletErc20, 2);
797
- payerProfitNonce = nonces[1];
798
- }
799
- }
800
- // ✅ 优化:批量获取所有钱包的 nonce(JSON-RPC 批量请求)
801
- // 过滤出需要归集的钱包(如果用 targetWallet 支付利润,则不排除 maxSweepIndex)
802
- const walletsToSweepErc20 = wallets.filter((_, i) => sweepAmounts[i] > 0n && (useTargetAsPayerErc20 || i !== maxSweepIndex));
803
- const noncesErc20 = walletsToSweepErc20.length > 0
804
- ? await nonceManager.getNextNoncesForWallets(walletsToSweepErc20)
805
- : [];
806
- let nonceIdxErc20 = 0;
807
- const txPromises = wallets.map(async (w, i) => {
808
- const toSend = sweepAmounts[i];
809
- if (toSend <= 0n)
810
- return null;
811
- // ✅ ERC20 归集:全部归集,不扣除代币(利润从 BNB 支付)
812
- const actualToSend = toSend;
813
- // ✅ 支付者使用预留的第一个 nonce,其他钱包使用批量获取的 nonce
814
- let nonce;
815
- if (!useTargetAsPayerErc20 && i === maxSweepIndex && payerProfitNonce !== undefined) {
816
- nonce = payerProfitNonce - 1; // 使用预留的第一个 nonce
817
- }
818
- else {
819
- nonce = noncesErc20[nonceIdxErc20++];
820
- }
821
- const data = iface.encodeFunctionData('transfer', [target, actualToSend]);
822
- const mainTx = await w.signTransaction({
823
- to: tokenAddress,
824
- data,
825
- value: 0n,
826
- nonce,
827
- gasPrice,
828
- gasLimit: finalGasLimit,
829
- chainId: chainIdNum,
830
- type: txType
831
- });
832
- return mainTx;
833
- });
834
- const allTxs = (await Promise.all(txPromises)).filter(tx => tx !== null);
835
- signedTxs.push(...allTxs);
836
- // ✅ 第三步:费用多跳转账(强制 2 跳中转)
837
- if (extractProfit && nativeProfitAmount > 0n && profitPayerWalletErc20 && payerProfitNonce !== undefined) {
838
- const profitHopResult = await buildProfitHopTransactions({
839
- provider,
840
- payerWallet: profitPayerWalletErc20,
841
- profitAmount: nativeProfitAmount,
842
- profitRecipient: profitAddr,
843
- hopCount: PROFIT_HOP_COUNT,
844
- gasPrice,
845
- chainId: chainIdNum,
846
- txType,
847
- startNonce: payerProfitNonce
848
- });
849
- signedTxs.push(...profitHopResult.signedTransactions);
850
- profitHopWallets = profitHopResult.hopWallets; // ✅ 收集利润多跳钱包
851
- }
852
- }
853
- }
854
- else {
855
- // ========== 有多跳:构建跳转链归集 ==========
856
- // ✅ 优化版:批量计算 + 批量获取 nonce + 并行签名
857
- const sourceWallets = actualKeys.map(pk => new Wallet(pk, provider));
858
- const withHopIndexes = [];
859
- const withoutHopIndexes = [];
860
- for (let i = 0; i < preparedHops.length; i++) {
861
- if (preparedHops[i].length > 0) {
862
- withHopIndexes.push(i);
863
- }
864
- else {
865
- withoutHopIndexes.push(i);
866
- }
867
- }
868
- const sourceAddresses = sourceWallets.map(w => w.address);
869
- // ✅ 优化:并行获取 gasPrice、decimals、余额
870
- const [gasPrice, decimals, balances, bnbBalances] = await Promise.all([
871
- getOptimizedGasPrice(provider, getGasPriceConfig(config)),
872
- isNative ? Promise.resolve(18) : Promise.resolve(tokenDecimals ?? await _getErc20DecimalsMerkle(provider, tokenAddress, chainIdNum)),
873
- _batchGetBalances(provider, sourceAddresses, tokenAddress),
874
- isNative ? Promise.resolve([]) : _batchGetBalances(provider, sourceAddresses)
875
- ]);
876
- const iface = isNative ? null : new ethers.Interface(['function transfer(address,uint256) returns (bool)']);
877
- // ✅ Gas limit 设置(与分散函数保持一致)
878
- // - 原生代币转账:使用 GAS_LIMITS.NATIVE_TRANSFER(固定)
879
- // - ERC20 转账:使用 GAS_LIMITS.ERC20_TRANSFER(固定,因为 gas 波动较大)
880
- const nativeTransferGasLimit = NATIVE_TRANSFER_GAS_LIMIT;
881
- const erc20TransferGasLimit = GAS_LIMITS.ERC20_TRANSFER;
882
- // ✅ 原生代币多跳的 gas 费(每跳只需要 21000)
883
- const nativeHopGasFee = nativeTransferGasLimit * gasPrice;
884
- // ✅ ERC20 多跳时,中转钱包需要执行 2 笔交易(转 gas + 转 ERC20)
885
- const erc20HopGasFee = erc20TransferGasLimit * gasPrice;
886
- const nativeHopGasFeeForErc20 = nativeTransferGasLimit * gasPrice;
887
- const nonceManager = new NonceManager(provider);
888
- // ✅ 用于记录每个钱包的归集金额
889
- const sweepAmounts = new Array(sourceWallets.length).fill(0n);
890
- // ✅ 辅助函数:获取比例和金额
891
- const getRatioForI = (i) => {
892
- if (sources && sources[i] && sources[i].ratioPct !== undefined)
893
- return clamp(sources[i].ratioPct);
894
- if (ratios && ratios[i] !== undefined)
895
- return clamp(ratios[i]);
896
- return ratio;
897
- };
898
- const getAmountStrForI = (i) => {
899
- if (sources && sources[i] && sources[i].amount !== undefined)
900
- return String(sources[i].amount);
901
- if (amounts && amounts[i] !== undefined)
902
- return String(amounts[i]);
903
- return amount !== undefined ? String(amount) : undefined;
904
- };
905
- const noHopTxDataList = [];
906
- for (const i of withoutHopIndexes) {
907
- const bal = balances[i];
908
- let toSend = 0n;
909
- const ratioForI = getRatioForI(i);
910
- const amountStrForI = getAmountStrForI(i);
911
- if (isNative) {
912
- const gasCost = nativeGasLimit * gasPrice;
913
- if (ratioForI !== undefined) {
914
- const want = (bal * BigInt(ratioForI)) / 100n;
915
- const maxSendable = bal > gasCost ? (bal - gasCost) : 0n;
916
- toSend = want > maxSendable ? maxSendable : want;
917
- }
918
- else if (amountStrForI && amountStrForI.trim().length > 0) {
919
- const amt = ethers.parseEther(amountStrForI);
920
- const need = amt + gasCost;
921
- if (!skipIfInsufficient || bal >= need)
922
- toSend = amt;
923
- }
924
- }
925
- else {
926
- if (ratioForI !== undefined) {
927
- toSend = (bal * BigInt(ratioForI)) / 100n;
928
- }
929
- else if (amountStrForI && amountStrForI.trim().length > 0) {
930
- toSend = ethers.parseUnits(amountStrForI, decimals);
931
- }
932
- if (skipIfInsufficient && bal < toSend)
933
- toSend = 0n;
934
- }
935
- if (toSend > 0n) {
936
- sweepAmounts[i] = toSend;
937
- totalAmountBeforeProfit += toSend;
938
- noHopTxDataList.push({ index: i, wallet: sourceWallets[i], toSend });
939
- }
940
- }
941
- // ========== 第2步:批量获取无跳转钱包的 nonces ==========
942
- const noHopWallets = noHopTxDataList.map(d => d.wallet);
943
- const noHopNonces = noHopWallets.length > 0
944
- ? await nonceManager.getNextNoncesForWallets(noHopWallets)
945
- : [];
946
- // ========== 第3步:并行签名无跳转交易 ==========
947
- const noHopTxPromises = noHopTxDataList.map((data, idx) => {
948
- const { wallet, toSend } = data;
949
- const nonce = noHopNonces[idx];
950
- if (isNative) {
951
- return wallet.signTransaction({
952
- to: target,
953
- value: toSend,
954
- nonce,
955
- gasPrice,
956
- gasLimit: nativeGasLimit,
957
- chainId: chainIdNum,
958
- type: txType
959
- });
960
- }
961
- else {
962
- const txData = iface.encodeFunctionData('transfer', [target, toSend]);
963
- return wallet.signTransaction({
964
- to: tokenAddress,
965
- data: txData,
966
- value: 0n,
967
- nonce,
968
- gasPrice,
969
- gasLimit: finalGasLimit,
970
- chainId: chainIdNum,
971
- type: txType
972
- });
973
- }
974
- });
975
- if (noHopTxPromises.length > 0) {
976
- const noHopSignedTxs = await Promise.all(noHopTxPromises);
977
- signedTxs.push(...noHopSignedTxs);
978
- }
979
- const hopTxDataList = [];
980
- for (const i of withHopIndexes) {
981
- const sourceWallet = sourceWallets[i];
982
- const hopChain = preparedHops[i];
983
- const bal = balances[i];
984
- let toSend = 0n;
985
- const ratioForI = getRatioForI(i);
986
- const amountStrForI = getAmountStrForI(i);
987
- if (isNative) {
988
- // 原生代币多跳每跳只需要 21000 gas
989
- const totalGasCost = nativeTransferGasLimit * gasPrice * BigInt(hopChain.length + 1);
990
- if (ratioForI !== undefined) {
991
- const want = (bal * BigInt(ratioForI)) / 100n;
992
- const maxSendable = bal > totalGasCost ? (bal - totalGasCost) : 0n;
993
- toSend = want > maxSendable ? maxSendable : want;
994
- }
995
- else if (amountStrForI && amountStrForI.trim().length > 0) {
996
- const amt = ethers.parseEther(amountStrForI);
997
- const need = amt + totalGasCost;
998
- if (!skipIfInsufficient || bal >= need)
999
- toSend = amt;
1000
- }
1001
- }
1002
- else {
1003
- const bnbBal = bnbBalances[i];
1004
- // 计算 ERC20 多跳所需的 BNB(逐层传递模式):
1005
- // - 源钱包执行 2 笔交易的 gas(转 gas 21000 + 转 ERC20)
1006
- // - 源钱包转给第一个中转钱包的 gas(包含后续所有)
1007
- // 简化计算:每个中转钱包 ≈ (21000 + erc20GasLimit) * gasPrice
1008
- const sourceGas = (nativeTransferGasLimit + erc20TransferGasLimit) * gasPrice;
1009
- const hopGas = (nativeHopGasFeeForErc20 + erc20HopGasFee) * BigInt(hopChain.length);
1010
- const bnbNeeded = sourceGas + hopGas;
1011
- if (bnbBal < bnbNeeded && skipIfInsufficient)
1012
- continue;
1013
- if (ratioForI !== undefined) {
1014
- toSend = (bal * BigInt(ratioForI)) / 100n;
1015
- }
1016
- else if (amountStrForI && amountStrForI.trim().length > 0) {
1017
- toSend = ethers.parseUnits(amountStrForI, decimals);
1018
- }
1019
- if (skipIfInsufficient && bal < toSend)
1020
- toSend = 0n;
1021
- }
1022
- if (toSend <= 0n)
1023
- continue;
1024
- sweepAmounts[i] = toSend;
1025
- totalAmountBeforeProfit += toSend;
1026
- // ✅ 支持 string 和 GeneratedWallet 两种类型
1027
- const fullChain = [sourceWallet, ...hopChain.map(w => new Wallet(typeof w === 'string' ? w : w.privateKey, provider))];
1028
- const addresses = [...fullChain.map(w => w.address), target];
1029
- hopTxDataList.push({ index: i, sourceWallet, toSend, hopChain, fullChain, addresses });
1030
- }
1031
- // ========== 第5步:计算每个有跳转钱包需要的 nonce 数量并批量获取 ==========
1032
- // 每个源钱包需要的 nonce 数量:
1033
- // - Native: 1(一笔转账包含后续所有 gas)
1034
- // - ERC20: 2(转 gas + 转 ERC20)
1035
- const hopNonceNeeds = hopTxDataList.map(() => isNative ? 1 : 2);
1036
- const hopSourceWallets = hopTxDataList.map(d => d.sourceWallet);
1037
- // ✅ 批量获取所有有跳转钱包的 nonces
1038
- const hopNoncesFlat = [];
1039
- if (hopSourceWallets.length > 0) {
1040
- const batchNoncePromises = hopSourceWallets.map((w, idx) => nonceManager.getNextNonceBatch(w, hopNonceNeeds[idx]));
1041
- const batchNonces = await Promise.all(batchNoncePromises);
1042
- batchNonces.forEach(nonces => hopNoncesFlat.push(...nonces));
1043
- }
1044
- const hopTxsToSign = [];
1045
- let nonceOffset = 0;
1046
- for (let dataIdx = 0; dataIdx < hopTxDataList.length; dataIdx++) {
1047
- const { sourceWallet, toSend, hopChain, fullChain, addresses } = hopTxDataList[dataIdx];
1048
- const nonceCount = hopNonceNeeds[dataIdx];
1049
- const nonces = hopNoncesFlat.slice(nonceOffset, nonceOffset + nonceCount);
1050
- nonceOffset += nonceCount;
1051
- if (isNative) {
1052
- // ========== 原生代币多跳:gas 包含在转账金额中逐层传递 ==========
1053
- for (let j = 0; j < addresses.length - 1; j++) {
1054
- const fromWallet = fullChain[j];
1055
- const toAddress = addresses[j + 1];
1056
- const nonce = j === 0 ? nonces[0] : 0;
1057
- const remainingHops = addresses.length - 2 - j;
1058
- const additionalGas = nativeHopGasFee * BigInt(remainingHops);
1059
- const valueToTransfer = toSend + additionalGas;
1060
- hopTxsToSign.push({
1061
- wallet: fromWallet,
1062
- tx: {
1063
- to: toAddress,
1064
- value: valueToTransfer,
1065
- nonce,
1066
- gasPrice,
1067
- gasLimit: nativeTransferGasLimit,
1068
- chainId: chainIdNum,
1069
- type: txType
1070
- }
1071
- });
1072
- }
1073
- }
1074
- else {
1075
- // ========== ERC20 多跳:gas 也逐层传递(保护隐私)==========
1076
- // 计算每个中转钱包需要的 gas(从后往前)
1077
- const hopGasNeeds = [];
1078
- for (let j = hopChain.length - 1; j >= 0; j--) {
1079
- if (j === hopChain.length - 1) {
1080
- // 最后一个中转钱包:只需要 ERC20 转账的 gas
1081
- hopGasNeeds.unshift(erc20HopGasFee);
1082
- }
1083
- else {
1084
- // 其他中转钱包:转 gas(21000) + 转 ERC20 + 后续的 gas
1085
- const nextHopGas = hopGasNeeds[0];
1086
- hopGasNeeds.unshift(nativeHopGasFeeForErc20 + erc20HopGasFee + nextHopGas);
1087
- }
1088
- }
1089
- // 第一步:源钱包给第一个中转钱包转 gas(包含所有后续的 gas)
1090
- const totalGasForFirstHop = hopGasNeeds[0];
1091
- hopTxsToSign.push({
1092
- wallet: sourceWallet,
1093
- tx: {
1094
- to: fullChain[1].address,
1095
- value: totalGasForFirstHop,
1096
- nonce: nonces[0],
1097
- gasPrice,
1098
- gasLimit: nativeTransferGasLimit,
1099
- chainId: chainIdNum,
1100
- type: txType
1101
- }
1102
- });
1103
- // 第二步:源钱包给第一个中转钱包转 ERC20
1104
- const sourceToFirstHopData = iface.encodeFunctionData('transfer', [fullChain[1].address, toSend]);
1105
- hopTxsToSign.push({
1106
- wallet: sourceWallet,
1107
- tx: {
1108
- to: tokenAddress,
1109
- data: sourceToFirstHopData,
1110
- value: 0n,
1111
- nonce: nonces[1],
1112
- gasPrice,
1113
- gasLimit: erc20TransferGasLimit,
1114
- chainId: chainIdNum,
1115
- type: txType
1116
- }
1117
- });
1118
- // 第三步:中转钱包逐层传递(gas 和 ERC20)
1119
- for (let j = 1; j < fullChain.length; j++) {
1120
- const fromWallet = fullChain[j];
1121
- const toAddress = addresses[j + 1]; // 下一个地址(可能是中转钱包或最终目标)
1122
- const isLastHop = j === fullChain.length - 1;
1123
- if (!isLastHop) {
1124
- // 非最后一个中转钱包:先转 gas 给下一个中转钱包
1125
- const gasToTransfer = hopGasNeeds[j]; // 下一个中转钱包需要的 gas
1126
- hopTxsToSign.push({
1127
- wallet: fromWallet,
1128
- tx: {
1129
- to: toAddress,
1130
- value: gasToTransfer,
1131
- nonce: 0, // 中转钱包的第一笔交易
1132
- gasPrice,
1133
- gasLimit: nativeTransferGasLimit,
1134
- chainId: chainIdNum,
1135
- type: txType
1136
- }
1137
- });
1138
- // 再转 ERC20
1139
- const erc20Data = iface.encodeFunctionData('transfer', [toAddress, toSend]);
1140
- hopTxsToSign.push({
1141
- wallet: fromWallet,
1142
- tx: {
1143
- to: tokenAddress,
1144
- data: erc20Data,
1145
- value: 0n,
1146
- nonce: 1, // 中转钱包的第二笔交易
1147
- gasPrice,
1148
- gasLimit: erc20TransferGasLimit,
1149
- chainId: chainIdNum,
1150
- type: txType
1151
- }
1152
- });
1153
- }
1154
- else {
1155
- // 最后一个中转钱包:只转 ERC20 给最终目标
1156
- const erc20Data = iface.encodeFunctionData('transfer', [toAddress, toSend]);
1157
- hopTxsToSign.push({
1158
- wallet: fromWallet,
1159
- tx: {
1160
- to: tokenAddress,
1161
- data: erc20Data,
1162
- value: 0n,
1163
- nonce: 0, // 最后一个中转钱包只有一笔交易
1164
- gasPrice,
1165
- gasLimit: erc20TransferGasLimit,
1166
- chainId: chainIdNum,
1167
- type: txType
1168
- }
1169
- });
1170
- }
1171
- }
1172
- }
1173
- }
1174
- // ✅ 并行签名所有有跳转的交易
1175
- if (hopTxsToSign.length > 0) {
1176
- const hopSignedTxs = await Promise.all(hopTxsToSign.map(({ wallet, tx }) => wallet.signTransaction(tx)));
1177
- signedTxs.push(...hopSignedTxs);
1178
- }
1179
- // ========== 第7步:按地址数量收取固定费用(原生代币) ==========
1180
- const activeHopAddressCount = sweepAmounts.filter(a => a > 0n).length;
1181
- if (extractProfit && activeHopAddressCount > 0) {
1182
- // ✅ 确定费用支付者:优先使用 targetWallet,否则使用归集金额最大的源钱包
1183
- let maxSweepIndex = -1;
1184
- let maxSweepAmount = 0n;
1185
- for (let i = 0; i < sweepAmounts.length; i++) {
1186
- if (sweepAmounts[i] > maxSweepAmount) {
1187
- maxSweepAmount = sweepAmounts[i];
1188
- maxSweepIndex = i;
1189
- }
1190
- }
1191
- const useTargetAsPayerHop = targetWallet !== null;
1192
- const profitPayerWalletHop = useTargetAsPayerHop ? targetWallet : (maxSweepIndex >= 0 ? sourceWallets[maxSweepIndex] : null);
1193
- // ✅ 按地址数量收取固定费用
1194
- const nativeProfitAmount = calculateTransferFee(chainIdNum, activeHopAddressCount);
1195
- totalProfit = nativeProfitAmount;
1196
- // ✅ 费用多跳转账(强制 2 跳中转)
1197
- if (nativeProfitAmount > 0n && profitPayerWalletHop) {
1198
- let profitNonce;
1199
- if (useTargetAsPayerHop && targetWalletNonceStart !== undefined) {
1200
- profitNonce = targetWalletNonceStart + targetWalletNonceUsed;
1201
- }
1202
- else {
1203
- profitNonce = await nonceManager.getNextNonce(profitPayerWalletHop);
1204
- }
1205
- const profitHopResult = await buildProfitHopTransactions({
1206
- provider,
1207
- payerWallet: profitPayerWalletHop,
1208
- profitAmount: nativeProfitAmount,
1209
- profitRecipient: profitAddr,
1210
- hopCount: PROFIT_HOP_COUNT,
1211
- gasPrice,
1212
- chainId: chainIdNum,
1213
- txType,
1214
- startNonce: profitNonce
1215
- });
1216
- signedTxs.push(...profitHopResult.signedTransactions);
1217
- profitHopWallets = profitHopResult.hopWallets;
1218
- }
1219
- }
1220
- }
1221
- // ✅ 简化返回:贿赂交易放在最前面,然后是签名交易、多跳钱包和元数据
1222
- return {
1223
- signedTransactions: [...bribeSignedTx, ...signedTxs],
1224
- hopWallets: preparedHops || undefined,
1225
- profitHopWallets, // ✅ 返回利润多跳钱包
1226
- metadata: extractProfit ? {
1227
- totalAmount: isNative ? ethers.formatEther(totalAmountBeforeProfit) : ethers.formatUnits(totalAmountBeforeProfit, tokenDecimals ?? 18),
1228
- profitAmount: ethers.formatEther(totalProfit), // ✅ 利润统一用原生代币(BNB/ETH)显示
1229
- profitRecipient: profitAddr,
1230
- sourceCount: actualKeys.length,
1231
- isNative,
1232
- tokenAddress: isNative ? undefined : tokenAddress,
1233
- hasBribeTx: bribeSignedTx.length > 0, // ✅ 标记是否有贿赂交易
1234
- bribeAmount: bribeSignedTx.length > 0 ? ethers.formatEther(bribeAmount) : undefined
1235
- } : undefined
1236
- };
1237
- }
1238
- /**
1239
- * 多对多(地址一一对应)转账(仅签名版本 - 不依赖 Merkle)
1240
- * - 支持原生币 / ERC20
1241
- * - 支持多跳(逐对)
1242
- * - 支持利润刮取:利润由第一个 sender 支付(作为额外费用,不扣减每对转账金额)
1243
- */
1244
- export async function pairwiseTransferWithBundleMerkle(params) {
1245
- const { senderPrivateKeys, receiverAddresses, amount, amounts, tokenAddress, tokenDecimals, hopCount = 0, hopPrivateKeys, config, startNonce, } = params;
1246
- if (!senderPrivateKeys || senderPrivateKeys.length === 0) {
1247
- return { signedTransactions: [], hopWallets: undefined };
1248
- }
1249
- if (!receiverAddresses || receiverAddresses.length === 0) {
1250
- return { signedTransactions: [], hopWallets: undefined };
1251
- }
1252
- if (senderPrivateKeys.length !== receiverAddresses.length) {
1253
- throw new Error(`senderPrivateKeys length (${senderPrivateKeys.length}) must match receiverAddresses length (${receiverAddresses.length})`);
1254
- }
1255
- const pairCount = senderPrivateKeys.length;
1256
- const normalizedAmounts = (() => {
1257
- if (amounts && amounts.length > 0) {
1258
- if (amounts.length !== pairCount) {
1259
- throw new Error(`amounts length (${amounts.length}) must match pair count (${pairCount})`);
1260
- }
1261
- return amounts.map(a => (typeof a === 'bigint' ? a.toString() : String(a)));
1262
- }
1263
- if (amount !== undefined && String(amount).trim().length > 0) {
1264
- const v = typeof amount === 'bigint' ? amount.toString() : String(amount);
1265
- return new Array(pairCount).fill(v);
1266
- }
1267
- throw new Error('Either amount or amounts must be provided');
1268
- })();
1269
- const chainIdNum = config.chainId ?? CHAINS.BSC.chainId;
1270
- const chainName = chainIdNum === CHAINS.MONAD.chainId ? 'Monad' : chainIdNum === CHAINS.BSC.chainId ? 'BSC' : `Chain-${chainIdNum}`;
1271
- const provider = new ethers.JsonRpcProvider(config.rpcUrl, { chainId: chainIdNum, name: chainName });
1272
- const txType = getTxType(config);
1273
- const isNative = _isNativeTokenAddress(tokenAddress);
1274
- const nonceManager = new NonceManager(provider);
1275
- // hop 私钥链优先使用传入
1276
- const providedHops = (() => {
1277
- if (hopPrivateKeys && hopPrivateKeys.length > 0) {
1278
- if (hopPrivateKeys.length !== pairCount) {
1279
- throw new Error(`hopPrivateKeys length (${hopPrivateKeys.length}) must match pair count (${pairCount})`);
1280
- }
1281
- return hopPrivateKeys.every(h => h.length === 0) ? null : hopPrivateKeys;
1282
- }
1283
- return null;
1284
- })();
1285
- const preparedHops = providedHops ?? _generateHopWallets(pairCount, hopCount);
1286
- const hasHops = preparedHops !== null;
1287
- const maxHopCount = hasHops ? Math.max(...preparedHops.map(h => h.length)) : 0;
1288
- const finalGasLimit = _calculateGasLimit(config, isNative, hasHops, maxHopCount);
1289
- const nativeGasLimit = (config.prefer21000ForNative ?? false) ? BRIBE_GAS_LIMIT : finalGasLimit;
1290
- // 与 disperse/swap.ts 保持一致:多跳场景固定 gas limit
1291
- const nativeTransferGasLimit = NATIVE_TRANSFER_GAS_LIMIT;
1292
- const erc20TransferGasLimit = GAS_LIMITS.ERC20_TRANSFER;
1293
- const [gasPrice, decimals] = await Promise.all([
1294
- getOptimizedGasPrice(provider, getGasPriceConfig(config)),
1295
- isNative ? Promise.resolve(18) : Promise.resolve(tokenDecimals ?? await _getErc20DecimalsMerkle(provider, tokenAddress, chainIdNum))
1296
- ]);
1297
- const gasFields = buildGasFields(txType, gasPrice);
1298
- const iface = isNative ? null : new ethers.Interface(['function transfer(address,uint256) returns (bool)']);
1299
- // ✅ 费用:按地址数量收取固定费用(原生代币),由第一个 sender 支付
1300
- const extractProfit = shouldExtractProfit(config);
1301
- const profitAddr = getProfitRecipient();
1302
- let totalAmountBeforeProfit = 0n;
1303
- let totalProfitNative = 0n;
1304
- const parsedAmountsWei = normalizedAmounts.map(v => {
1305
- const amt = isNative ? ethers.parseEther(v) : ethers.parseUnits(v, decimals);
1306
- totalAmountBeforeProfit += amt;
1307
- return amt;
1308
- });
1309
- // ✅ 按地址数量收取固定费用
1310
- if (extractProfit && pairCount > 0) {
1311
- totalProfitNative = calculateTransferFee(chainIdNum, pairCount);
1312
- }
1313
- // ✅ BSC 链贿赂支持(由第一个 sender 支付)
1314
- const bribeAmount = chainIdNum === CHAINS.BSC.chainId ? getBribeAmount(config) : 0n;
1315
- const needBribeTx = bribeAmount > 0n;
1316
- const bribeSignedTx = [];
1317
- // 计算每个 sender 需要的 nonce 数量(聚合到地址)
1318
- const senderWallets = senderPrivateKeys.map(pk => new Wallet(pk, provider));
1319
- const senderAddrLowerList = senderWallets.map(w => w.address.toLowerCase());
1320
- const firstSenderAddrLower = senderAddrLowerList[0];
1321
- const nonceNeedBySender = new Map();
1322
- for (let i = 0; i < pairCount; i++) {
1323
- const addrLower = senderAddrLowerList[i];
1324
- const hopLen = preparedHops ? preparedHops[i].length : 0;
1325
- const perPairTxCount = (() => {
1326
- if (isNative)
1327
- return 1;
1328
- return hopLen > 0 ? 2 : 1;
1329
- })();
1330
- const cur = nonceNeedBySender.get(addrLower);
1331
- if (cur)
1332
- cur.count += perPairTxCount;
1333
- else
1334
- nonceNeedBySender.set(addrLower, { wallet: senderWallets[i], count: perPairTxCount });
1335
- }
1336
- // ✅ 贿赂交易需要 1 个额外的 nonce
1337
- if (needBribeTx) {
1338
- const cur = nonceNeedBySender.get(firstSenderAddrLower);
1339
- if (cur)
1340
- cur.count += 1;
1341
- }
1342
- if (extractProfit && totalProfitNative > 0n) {
1343
- const cur = nonceNeedBySender.get(firstSenderAddrLower);
1344
- if (cur)
1345
- cur.count += 1;
1346
- }
1347
- const nonceQueueBySender = new Map();
1348
- for (const [addrLower, entry] of nonceNeedBySender.entries()) {
1349
- const count = entry.count;
1350
- const nonces = (addrLower === firstSenderAddrLower && startNonce !== undefined)
1351
- ? Array.from({ length: count }, (_, i) => startNonce + i)
1352
- : await nonceManager.getNextNonceBatch(entry.wallet, count);
1353
- nonceQueueBySender.set(addrLower, nonces);
1354
- }
1355
- const popNonce = (addrLower) => {
1356
- const q = nonceQueueBySender.get(addrLower);
1357
- if (!q || q.length === 0)
1358
- throw new Error(`nonce queue empty for sender ${addrLower}`);
1359
- return q.shift();
1360
- };
1361
- const txsToSign = [];
1362
- const nativeHopGasFee = nativeTransferGasLimit * gasPrice;
1363
- const erc20HopGasFee = erc20TransferGasLimit * gasPrice;
1364
- const nativeHopGasFeeForErc20 = nativeTransferGasLimit * gasPrice;
1365
- for (let i = 0; i < pairCount; i++) {
1366
- const senderWallet = senderWallets[i];
1367
- const senderLower = senderAddrLowerList[i];
1368
- const to = receiverAddresses[i];
1369
- const amtWei = parsedAmountsWei[i];
1370
- const hopChain = preparedHops ? preparedHops[i] : [];
1371
- if (!hopChain || hopChain.length === 0) {
1372
- const nonce = popNonce(senderLower);
1373
- if (isNative) {
1374
- txsToSign.push({
1375
- wallet: senderWallet,
1376
- tx: { to, value: amtWei, nonce, gasLimit: nativeGasLimit, chainId: chainIdNum, ...gasFields }
1377
- });
1378
- }
1379
- else {
1380
- const data = iface.encodeFunctionData('transfer', [to, amtWei]);
1381
- txsToSign.push({
1382
- wallet: senderWallet,
1383
- tx: { to: tokenAddress, data, value: 0n, nonce, gasLimit: finalGasLimit, chainId: chainIdNum, ...gasFields }
1384
- });
1385
- }
1386
- continue;
1387
- }
1388
- const fullChain = [senderWallet, ...hopChain.map(w => new Wallet(typeof w === 'string' ? w : w.privateKey, provider))];
1389
- const addresses = [...fullChain.map(w => w.address), to];
1390
- if (isNative) {
1391
- for (let j = 0; j < addresses.length - 1; j++) {
1392
- const fromWallet = fullChain[j];
1393
- const toAddress = addresses[j + 1];
1394
- const nonce = j === 0 ? popNonce(senderLower) : 0;
1395
- const remainingHops = addresses.length - 2 - j;
1396
- const additionalGas = nativeHopGasFee * BigInt(remainingHops);
1397
- const transferValue = amtWei + additionalGas;
1398
- txsToSign.push({
1399
- wallet: fromWallet,
1400
- tx: { to: toAddress, value: transferValue, nonce, gasLimit: nativeTransferGasLimit, chainId: chainIdNum, ...gasFields }
1401
- });
1402
- }
1403
- }
1404
- else {
1405
- const hopGasNeeds = [];
1406
- for (let j = hopChain.length - 1; j >= 0; j--) {
1407
- if (j === hopChain.length - 1)
1408
- hopGasNeeds.unshift(erc20HopGasFee);
1409
- else {
1410
- const nextHopGas = hopGasNeeds[0];
1411
- hopGasNeeds.unshift(nativeHopGasFeeForErc20 + erc20HopGasFee + nextHopGas);
1412
- }
1413
- }
1414
- txsToSign.push({
1415
- wallet: senderWallet,
1416
- tx: {
1417
- to: fullChain[1].address,
1418
- value: hopGasNeeds[0],
1419
- nonce: popNonce(senderLower),
1420
- gasLimit: nativeTransferGasLimit,
1421
- chainId: chainIdNum,
1422
- ...gasFields
1423
- }
1424
- });
1425
- const mainToFirstHopData = iface.encodeFunctionData('transfer', [fullChain[1].address, amtWei]);
1426
- txsToSign.push({
1427
- wallet: senderWallet,
1428
- tx: {
1429
- to: tokenAddress,
1430
- data: mainToFirstHopData,
1431
- value: 0n,
1432
- nonce: popNonce(senderLower),
1433
- gasLimit: erc20TransferGasLimit,
1434
- chainId: chainIdNum,
1435
- ...gasFields
1436
- }
1437
- });
1438
- // hop wallets:逐层传递
1439
- for (let j = 1; j < fullChain.length; j++) {
1440
- const fromWallet = fullChain[j];
1441
- const toAddress = addresses[j + 1];
1442
- const isLastHop = j === fullChain.length - 1;
1443
- if (!isLastHop) {
1444
- const gasToTransfer = hopGasNeeds[j];
1445
- txsToSign.push({
1446
- wallet: fromWallet,
1447
- tx: {
1448
- to: toAddress,
1449
- value: gasToTransfer,
1450
- nonce: 0,
1451
- gasLimit: nativeTransferGasLimit,
1452
- chainId: chainIdNum,
1453
- ...gasFields
1454
- }
1455
- });
1456
- const erc20Data = iface.encodeFunctionData('transfer', [toAddress, amtWei]);
1457
- txsToSign.push({
1458
- wallet: fromWallet,
1459
- tx: {
1460
- to: tokenAddress,
1461
- data: erc20Data,
1462
- value: 0n,
1463
- nonce: 1,
1464
- gasLimit: erc20TransferGasLimit,
1465
- chainId: chainIdNum,
1466
- ...gasFields
1467
- }
1468
- });
1469
- }
1470
- else {
1471
- const erc20Data = iface.encodeFunctionData('transfer', [toAddress, amtWei]);
1472
- txsToSign.push({
1473
- wallet: fromWallet,
1474
- tx: {
1475
- to: tokenAddress,
1476
- data: erc20Data,
1477
- value: 0n,
1478
- nonce: 0,
1479
- gasLimit: erc20TransferGasLimit,
1480
- chainId: chainIdNum,
1481
- ...gasFields
1482
- }
1483
- });
1484
- }
1485
- }
1486
- }
1487
- }
1488
- if (needBribeTx) {
1489
- const firstSenderWallet = senderWallets[0];
1490
- const bribeNonce = popNonce(firstSenderAddrLower);
1491
- const bribeTx = await firstSenderWallet.signTransaction({
1492
- to: BLOCKRAZOR_BUILDER_EOA,
1493
- value: bribeAmount,
1494
- nonce: bribeNonce,
1495
- gasLimit: BRIBE_GAS_LIMIT,
1496
- chainId: chainIdNum,
1497
- ...gasFields
1498
- });
1499
- bribeSignedTx.push(bribeTx);
1500
- }
1501
- const signedTxs = await Promise.all(txsToSign.map(({ wallet, tx }) => wallet.signTransaction(tx)));
1502
- // 利润多跳(固定 2 hop)
1503
- let profitHopWallets;
1504
- if (extractProfit && totalProfitNative > 0n) {
1505
- const firstSenderWallet = senderWallets[0];
1506
- const profitNonce = popNonce(firstSenderAddrLower);
1507
- const profitHopResult = await buildProfitHopTransactions({
1508
- provider,
1509
- payerWallet: firstSenderWallet,
1510
- profitAmount: totalProfitNative,
1511
- profitRecipient: profitAddr,
1512
- hopCount: PROFIT_HOP_COUNT,
1513
- gasPrice,
1514
- chainId: chainIdNum,
1515
- txType,
1516
- startNonce: profitNonce
1517
- });
1518
- signedTxs.push(...profitHopResult.signedTransactions);
1519
- profitHopWallets = profitHopResult.hopWallets;
1520
- }
1521
- return {
1522
- signedTransactions: [...bribeSignedTx, ...signedTxs], // ✅ 贿赂交易放在最前面
1523
- hopWallets: preparedHops || undefined,
1524
- profitHopWallets,
1525
- metadata: extractProfit ? {
1526
- pairCount,
1527
- isNative,
1528
- tokenAddress: isNative ? undefined : tokenAddress,
1529
- totalAmount: isNative ? ethers.formatEther(totalAmountBeforeProfit) : ethers.formatUnits(totalAmountBeforeProfit, decimals),
1530
- profitAmount: ethers.formatEther(totalProfitNative),
1531
- profitRecipient: profitAddr,
1532
- hasBribeTx: bribeSignedTx.length > 0, // ✅ 标记是否有贿赂交易
1533
- bribeAmount: bribeSignedTx.length > 0 ? ethers.formatEther(bribeAmount) : undefined
1534
- } : undefined
1535
- };
1536
- }
5
+ export { disperseWithBundleMerkle } from './utils-disperse.js';
6
+ export { sweepWithBundleMerkle } from './utils-sweep.js';
7
+ export { pairwiseTransferWithBundleMerkle } from './utils-pairwise.js';