@lifi/sdk 3.0.0-alpha.2 → 3.0.0-alpha.21

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 (446) hide show
  1. package/dist/_cjs/config.js +48 -0
  2. package/dist/_cjs/config.js.map +1 -0
  3. package/dist/{cjs → _cjs}/constants.js +2 -4
  4. package/dist/_cjs/constants.js.map +1 -0
  5. package/dist/_cjs/core/BaseStepExecutor.js +55 -0
  6. package/dist/_cjs/core/BaseStepExecutor.js.map +1 -0
  7. package/dist/_cjs/core/EVM/EVM.js +46 -0
  8. package/dist/_cjs/core/EVM/EVM.js.map +1 -0
  9. package/dist/_cjs/core/EVM/EVMStepExecutor.js +276 -0
  10. package/dist/_cjs/core/EVM/EVMStepExecutor.js.map +1 -0
  11. package/dist/{cjs/types → _cjs/core/EVM}/abi.js +1 -0
  12. package/dist/_cjs/core/EVM/abi.js.map +1 -0
  13. package/dist/{cjs/execution → _cjs/core/EVM}/checkAllowance.js +12 -12
  14. package/dist/_cjs/core/EVM/checkAllowance.js.map +1 -0
  15. package/dist/{cjs/allowance → _cjs/core/EVM}/getAllowance.js +15 -17
  16. package/dist/_cjs/core/EVM/getAllowance.js.map +1 -0
  17. package/dist/{cjs/balance/getBalance.js → _cjs/core/EVM/getEVMBalance.js} +22 -22
  18. package/dist/_cjs/core/EVM/getEVMBalance.js.map +1 -0
  19. package/dist/{cjs/execution → _cjs/core/EVM}/multisig.js +7 -11
  20. package/dist/_cjs/core/EVM/multisig.js.map +1 -0
  21. package/dist/_cjs/core/EVM/publicClient.js +35 -0
  22. package/dist/_cjs/core/EVM/publicClient.js.map +1 -0
  23. package/dist/_cjs/core/EVM/setAllowance.js +71 -0
  24. package/dist/_cjs/core/EVM/setAllowance.js.map +1 -0
  25. package/dist/_cjs/core/EVM/switchChain.js +37 -0
  26. package/dist/_cjs/core/EVM/switchChain.js.map +1 -0
  27. package/dist/_cjs/core/EVM/types.js +9 -0
  28. package/dist/_cjs/core/EVM/types.js.map +1 -0
  29. package/dist/{cjs/utils/getMaxPriorityFeePerGas.js → _cjs/core/EVM/utils.js} +11 -3
  30. package/dist/_cjs/core/EVM/utils.js.map +1 -0
  31. package/dist/_cjs/core/Solana/Solana.js +44 -0
  32. package/dist/_cjs/core/Solana/Solana.js.map +1 -0
  33. package/dist/_cjs/core/Solana/SolanaStepExecutor.js +157 -0
  34. package/dist/_cjs/core/Solana/SolanaStepExecutor.js.map +1 -0
  35. package/dist/_cjs/core/Solana/connection.js +18 -0
  36. package/dist/_cjs/core/Solana/connection.js.map +1 -0
  37. package/dist/_cjs/core/Solana/getSolanaBalance.js +50 -0
  38. package/dist/_cjs/core/Solana/getSolanaBalance.js.map +1 -0
  39. package/dist/_cjs/core/Solana/types.js +10 -0
  40. package/dist/_cjs/core/Solana/types.js.map +1 -0
  41. package/dist/_cjs/core/StatusManager.js +167 -0
  42. package/dist/_cjs/core/StatusManager.js.map +1 -0
  43. package/dist/{cjs/execution → _cjs/core}/checkBalance.js +5 -5
  44. package/dist/_cjs/core/checkBalance.js.map +1 -0
  45. package/dist/_cjs/core/execution.js +134 -0
  46. package/dist/_cjs/core/execution.js.map +1 -0
  47. package/dist/_cjs/core/executionState.js +23 -0
  48. package/dist/_cjs/core/executionState.js.map +1 -0
  49. package/dist/_cjs/core/index.js +7 -0
  50. package/dist/_cjs/core/index.js.map +1 -0
  51. package/dist/_cjs/core/prepareRestart.js +23 -0
  52. package/dist/_cjs/core/prepareRestart.js.map +1 -0
  53. package/dist/_cjs/core/provider.js +15 -0
  54. package/dist/_cjs/core/provider.js.map +1 -0
  55. package/dist/_cjs/core/stepComparison.js +25 -0
  56. package/dist/_cjs/core/stepComparison.js.map +1 -0
  57. package/dist/{cjs/allowance → _cjs/core}/types.js +1 -0
  58. package/dist/_cjs/core/types.js.map +1 -0
  59. package/dist/{cjs/execution → _cjs/core}/utils.js +16 -10
  60. package/dist/_cjs/core/utils.js.map +1 -0
  61. package/dist/{cjs/execution → _cjs/core}/waitForReceivingTransaction.js +10 -12
  62. package/dist/_cjs/core/waitForReceivingTransaction.js.map +1 -0
  63. package/dist/_cjs/createConfig.js +48 -0
  64. package/dist/_cjs/createConfig.js.map +1 -0
  65. package/dist/{cjs → _cjs}/helpers.js +11 -25
  66. package/dist/_cjs/helpers.js.map +1 -0
  67. package/dist/_cjs/index.js +20 -0
  68. package/dist/_cjs/index.js.map +1 -0
  69. package/dist/_cjs/package.json +1 -0
  70. package/dist/{cjs → _cjs}/request.js +22 -16
  71. package/dist/_cjs/request.js.map +1 -0
  72. package/dist/_cjs/services/api.js +284 -0
  73. package/dist/_cjs/services/api.js.map +1 -0
  74. package/dist/_cjs/services/balance.js +49 -0
  75. package/dist/_cjs/services/balance.js.map +1 -0
  76. package/dist/{cjs → _cjs}/typeguards.js +1 -0
  77. package/dist/_cjs/typeguards.js.map +1 -0
  78. package/dist/_cjs/types/index.js +5 -0
  79. package/dist/_cjs/types/index.js.map +1 -0
  80. package/dist/{cjs/types/internal.types.js → _cjs/types/internal.js} +1 -0
  81. package/dist/_cjs/types/internal.js.map +1 -0
  82. package/dist/{cjs → _cjs}/utils/errors.js +25 -3
  83. package/dist/_cjs/utils/errors.js.map +1 -0
  84. package/dist/_cjs/utils/getTransactionMessage.js +31 -0
  85. package/dist/_cjs/utils/getTransactionMessage.js.map +1 -0
  86. package/dist/_cjs/utils/index.js +9 -0
  87. package/dist/_cjs/utils/index.js.map +1 -0
  88. package/dist/{cjs → _cjs}/utils/median.js +1 -0
  89. package/dist/_cjs/utils/median.js.map +1 -0
  90. package/dist/_cjs/utils/parseBackendError.js +27 -0
  91. package/dist/_cjs/utils/parseBackendError.js.map +1 -0
  92. package/dist/_cjs/utils/parseError.js +67 -0
  93. package/dist/_cjs/utils/parseError.js.map +1 -0
  94. package/dist/{cjs → _cjs}/utils/utils.js +5 -11
  95. package/dist/_cjs/utils/utils.js.map +1 -0
  96. package/dist/{cjs → _cjs}/version.js +2 -1
  97. package/dist/_cjs/version.js.map +1 -0
  98. package/dist/_esm/config.js +45 -0
  99. package/dist/_esm/config.js.map +1 -0
  100. package/dist/{constants.js → _esm/constants.js} +1 -0
  101. package/dist/_esm/constants.js.map +1 -0
  102. package/dist/_esm/core/BaseStepExecutor.js +52 -0
  103. package/dist/_esm/core/BaseStepExecutor.js.map +1 -0
  104. package/dist/_esm/core/EVM/EVM.js +42 -0
  105. package/dist/_esm/core/EVM/EVM.js.map +1 -0
  106. package/dist/_esm/core/EVM/EVMStepExecutor.js +304 -0
  107. package/dist/_esm/core/EVM/EVMStepExecutor.js.map +1 -0
  108. package/dist/{types → _esm/core/EVM}/abi.js +1 -0
  109. package/dist/_esm/core/EVM/abi.js.map +1 -0
  110. package/dist/{execution → _esm/core/EVM}/checkAllowance.js +8 -6
  111. package/dist/_esm/core/EVM/checkAllowance.js.map +1 -0
  112. package/dist/{allowance → _esm/core/EVM}/getAllowance.js +20 -5
  113. package/dist/_esm/core/EVM/getAllowance.js.map +1 -0
  114. package/dist/{balance/getBalance.js → _esm/core/EVM/getEVMBalance.js} +11 -11
  115. package/dist/_esm/core/EVM/getEVMBalance.js.map +1 -0
  116. package/dist/{execution → _esm/core/EVM}/multisig.js +5 -6
  117. package/dist/_esm/core/EVM/multisig.js.map +1 -0
  118. package/dist/_esm/core/EVM/publicClient.js +37 -0
  119. package/dist/_esm/core/EVM/publicClient.js.map +1 -0
  120. package/dist/{allowance → _esm/core/EVM}/setAllowance.js +46 -11
  121. package/dist/_esm/core/EVM/setAllowance.js.map +1 -0
  122. package/dist/{execution → _esm/core/EVM}/switchChain.js +5 -3
  123. package/dist/_esm/core/EVM/switchChain.js.map +1 -0
  124. package/dist/_esm/core/EVM/types.js +6 -0
  125. package/dist/_esm/core/EVM/types.js.map +1 -0
  126. package/dist/{utils/getMaxPriorityFeePerGas.js → _esm/core/EVM/utils.js} +9 -1
  127. package/dist/_esm/core/EVM/utils.js.map +1 -0
  128. package/dist/_esm/core/Solana/Solana.js +40 -0
  129. package/dist/_esm/core/Solana/Solana.js.map +1 -0
  130. package/dist/_esm/core/Solana/SolanaStepExecutor.js +158 -0
  131. package/dist/_esm/core/Solana/SolanaStepExecutor.js.map +1 -0
  132. package/dist/_esm/core/Solana/connection.js +19 -0
  133. package/dist/_esm/core/Solana/connection.js.map +1 -0
  134. package/dist/_esm/core/Solana/getSolanaBalance.js +46 -0
  135. package/dist/_esm/core/Solana/getSolanaBalance.js.map +1 -0
  136. package/dist/_esm/core/Solana/types.js +7 -0
  137. package/dist/_esm/core/Solana/types.js.map +1 -0
  138. package/dist/_esm/core/StatusManager.js +203 -0
  139. package/dist/_esm/core/StatusManager.js.map +1 -0
  140. package/dist/{execution → _esm/core}/checkBalance.js +3 -2
  141. package/dist/_esm/core/checkBalance.js.map +1 -0
  142. package/dist/_esm/core/execution.js +173 -0
  143. package/dist/_esm/core/execution.js.map +1 -0
  144. package/dist/_esm/core/executionState.js +20 -0
  145. package/dist/_esm/core/executionState.js.map +1 -0
  146. package/dist/_esm/core/index.js +4 -0
  147. package/dist/_esm/core/index.js.map +1 -0
  148. package/dist/_esm/core/prepareRestart.js +64 -0
  149. package/dist/_esm/core/prepareRestart.js.map +1 -0
  150. package/dist/_esm/core/provider.js +11 -0
  151. package/dist/_esm/core/provider.js.map +1 -0
  152. package/dist/{execution → _esm/core}/stepComparison.js +7 -5
  153. package/dist/_esm/core/stepComparison.js.map +1 -0
  154. package/dist/_esm/core/types.js +2 -0
  155. package/dist/_esm/core/types.js.map +1 -0
  156. package/dist/{execution → _esm/core}/utils.js +14 -0
  157. package/dist/_esm/core/utils.js.map +1 -0
  158. package/dist/{execution → _esm/core}/waitForReceivingTransaction.js +8 -7
  159. package/dist/_esm/core/waitForReceivingTransaction.js.map +1 -0
  160. package/dist/_esm/createConfig.js +45 -0
  161. package/dist/_esm/createConfig.js.map +1 -0
  162. package/dist/{helpers.js → _esm/helpers.js} +4 -3
  163. package/dist/_esm/helpers.js.map +1 -0
  164. package/dist/_esm/index.js +12 -0
  165. package/dist/_esm/index.js.map +1 -0
  166. package/dist/_esm/package.json +1 -0
  167. package/dist/{request.js → _esm/request.js} +19 -9
  168. package/dist/_esm/request.js.map +1 -0
  169. package/dist/_esm/services/api.js +353 -0
  170. package/dist/_esm/services/api.js.map +1 -0
  171. package/dist/_esm/services/balance.js +65 -0
  172. package/dist/_esm/services/balance.js.map +1 -0
  173. package/dist/{typeguards.js → _esm/typeguards.js} +1 -0
  174. package/dist/_esm/typeguards.js.map +1 -0
  175. package/dist/_esm/types/index.js +2 -0
  176. package/dist/_esm/types/index.js.map +1 -0
  177. package/dist/_esm/types/internal.js +2 -0
  178. package/dist/_esm/types/internal.js.map +1 -0
  179. package/dist/{utils → _esm/utils}/errors.js +25 -0
  180. package/dist/_esm/utils/errors.js.map +1 -0
  181. package/dist/_esm/utils/getTransactionMessage.js +28 -0
  182. package/dist/_esm/utils/getTransactionMessage.js.map +1 -0
  183. package/dist/_esm/utils/index.js +6 -0
  184. package/dist/_esm/utils/index.js.map +1 -0
  185. package/dist/{utils → _esm/utils}/median.js +1 -0
  186. package/dist/_esm/utils/median.js.map +1 -0
  187. package/dist/_esm/utils/parseBackendError.js +24 -0
  188. package/dist/_esm/utils/parseBackendError.js.map +1 -0
  189. package/dist/{utils → _esm/utils}/parseError.js +4 -53
  190. package/dist/_esm/utils/parseError.js.map +1 -0
  191. package/dist/{utils → _esm/utils}/utils.js +2 -1
  192. package/dist/_esm/utils/utils.js.map +1 -0
  193. package/dist/_esm/version.js +3 -0
  194. package/dist/_esm/version.js.map +1 -0
  195. package/dist/_types/config.d.ts +11 -0
  196. package/dist/_types/config.d.ts.map +1 -0
  197. package/dist/{cjs → _types}/constants.d.ts +1 -0
  198. package/dist/_types/constants.d.ts.map +1 -0
  199. package/dist/_types/core/BaseStepExecutor.d.ts +13 -0
  200. package/dist/_types/core/BaseStepExecutor.d.ts.map +1 -0
  201. package/dist/_types/core/EVM/EVM.d.ts +3 -0
  202. package/dist/_types/core/EVM/EVM.d.ts.map +1 -0
  203. package/dist/_types/core/EVM/EVMStepExecutor.d.ts +17 -0
  204. package/dist/_types/core/EVM/EVMStepExecutor.d.ts.map +1 -0
  205. package/dist/{cjs/types → _types/core/EVM}/abi.d.ts +1 -0
  206. package/dist/_types/core/EVM/abi.d.ts.map +1 -0
  207. package/dist/_types/core/EVM/checkAllowance.d.ts +6 -0
  208. package/dist/_types/core/EVM/checkAllowance.d.ts.map +1 -0
  209. package/dist/{cjs/allowance → _types/core/EVM}/getAllowance.d.ts +15 -1
  210. package/dist/_types/core/EVM/getAllowance.d.ts.map +1 -0
  211. package/dist/_types/core/EVM/getEVMBalance.d.ts +3 -0
  212. package/dist/_types/core/EVM/getEVMBalance.d.ts.map +1 -0
  213. package/dist/_types/core/EVM/multisig.d.ts +6 -0
  214. package/dist/_types/core/EVM/multisig.d.ts.map +1 -0
  215. package/dist/_types/core/EVM/publicClient.d.ts +8 -0
  216. package/dist/_types/core/EVM/publicClient.d.ts.map +1 -0
  217. package/dist/_types/core/EVM/setAllowance.d.ts +25 -0
  218. package/dist/_types/core/EVM/setAllowance.d.ts.map +1 -0
  219. package/dist/{execution → _types/core/EVM}/switchChain.d.ts +6 -4
  220. package/dist/_types/core/EVM/switchChain.d.ts.map +1 -0
  221. package/dist/_types/core/EVM/types.d.ts +57 -0
  222. package/dist/_types/core/EVM/types.d.ts.map +1 -0
  223. package/dist/_types/core/EVM/utils.d.ts +5 -0
  224. package/dist/_types/core/EVM/utils.d.ts.map +1 -0
  225. package/dist/_types/core/Solana/Solana.d.ts +3 -0
  226. package/dist/_types/core/Solana/Solana.d.ts.map +1 -0
  227. package/dist/_types/core/Solana/SolanaStepExecutor.d.ts +13 -0
  228. package/dist/_types/core/Solana/SolanaStepExecutor.d.ts.map +1 -0
  229. package/dist/_types/core/Solana/connection.d.ts +8 -0
  230. package/dist/_types/core/Solana/connection.d.ts.map +1 -0
  231. package/dist/_types/core/Solana/getSolanaBalance.d.ts +3 -0
  232. package/dist/_types/core/Solana/getSolanaBalance.d.ts.map +1 -0
  233. package/dist/_types/core/Solana/types.d.ts +13 -0
  234. package/dist/_types/core/Solana/types.d.ts.map +1 -0
  235. package/dist/{cjs/execution → _types/core}/StatusManager.d.ts +12 -16
  236. package/dist/_types/core/StatusManager.d.ts.map +1 -0
  237. package/dist/_types/core/checkBalance.d.ts +3 -0
  238. package/dist/_types/core/checkBalance.d.ts.map +1 -0
  239. package/dist/_types/core/execution.d.ts +42 -0
  240. package/dist/_types/core/execution.d.ts.map +1 -0
  241. package/dist/_types/core/executionState.d.ts +16 -0
  242. package/dist/_types/core/executionState.d.ts.map +1 -0
  243. package/dist/_types/core/index.d.ts +4 -0
  244. package/dist/_types/core/index.d.ts.map +1 -0
  245. package/dist/_types/core/prepareRestart.d.ts +3 -0
  246. package/dist/_types/core/prepareRestart.d.ts.map +1 -0
  247. package/dist/_types/core/provider.d.ts +3 -0
  248. package/dist/_types/core/provider.d.ts.map +1 -0
  249. package/dist/{execution → _types/core}/stepComparison.d.ts +7 -4
  250. package/dist/_types/core/stepComparison.d.ts.map +1 -0
  251. package/dist/_types/core/types.d.ts +71 -0
  252. package/dist/_types/core/types.d.ts.map +1 -0
  253. package/dist/{execution → _types/core}/utils.d.ts +5 -2
  254. package/dist/_types/core/utils.d.ts.map +1 -0
  255. package/dist/_types/core/waitForReceivingTransaction.d.ts +4 -0
  256. package/dist/_types/core/waitForReceivingTransaction.d.ts.map +1 -0
  257. package/dist/_types/createConfig.d.ts +7 -0
  258. package/dist/_types/createConfig.d.ts.map +1 -0
  259. package/dist/{helpers.d.ts → _types/helpers.d.ts} +4 -3
  260. package/dist/_types/helpers.d.ts.map +1 -0
  261. package/dist/_types/index.d.ts +12 -0
  262. package/dist/_types/index.d.ts.map +1 -0
  263. package/dist/{request.d.ts → _types/request.d.ts} +1 -0
  264. package/dist/_types/request.d.ts.map +1 -0
  265. package/dist/_types/services/api.d.ts +87 -0
  266. package/dist/_types/services/api.d.ts.map +1 -0
  267. package/dist/_types/services/balance.d.ts +30 -0
  268. package/dist/_types/services/balance.d.ts.map +1 -0
  269. package/dist/{cjs → _types}/typeguards.d.ts +3 -2
  270. package/dist/_types/typeguards.d.ts.map +1 -0
  271. package/dist/_types/types/index.d.ts +2 -0
  272. package/dist/_types/types/index.d.ts.map +1 -0
  273. package/dist/_types/types/internal.d.ts +56 -0
  274. package/dist/_types/types/internal.d.ts.map +1 -0
  275. package/dist/{utils → _types/utils}/errors.d.ts +1 -0
  276. package/dist/_types/utils/errors.d.ts.map +1 -0
  277. package/dist/_types/utils/getTransactionMessage.d.ts +4 -0
  278. package/dist/_types/utils/getTransactionMessage.d.ts.map +1 -0
  279. package/dist/_types/utils/index.d.ts +6 -0
  280. package/dist/_types/utils/index.d.ts.map +1 -0
  281. package/dist/{cjs → _types}/utils/median.d.ts +1 -0
  282. package/dist/_types/utils/median.d.ts.map +1 -0
  283. package/dist/_types/utils/parseBackendError.d.ts +3 -0
  284. package/dist/_types/utils/parseBackendError.d.ts.map +1 -0
  285. package/dist/{utils → _types/utils}/parseError.d.ts +4 -6
  286. package/dist/_types/utils/parseError.d.ts.map +1 -0
  287. package/dist/{utils → _types/utils}/utils.d.ts +1 -0
  288. package/dist/_types/utils/utils.d.ts.map +1 -0
  289. package/dist/_types/version.d.ts +3 -0
  290. package/dist/_types/version.d.ts.map +1 -0
  291. package/dist/config.ts +47 -0
  292. package/dist/constants.ts +7 -0
  293. package/dist/core/BaseStepExecutor.ts +40 -0
  294. package/dist/core/EVM/EVM.ts +50 -0
  295. package/dist/core/EVM/EVMStepExecutor.ts +472 -0
  296. package/dist/core/EVM/abi.ts +47 -0
  297. package/dist/core/EVM/checkAllowance.ts +152 -0
  298. package/dist/core/EVM/getAllowance.ts +146 -0
  299. package/dist/core/EVM/getEVMBalance.ts +120 -0
  300. package/dist/core/EVM/multisig.ts +53 -0
  301. package/dist/core/EVM/publicClient.ts +46 -0
  302. package/dist/core/EVM/setAllowance.ts +144 -0
  303. package/dist/core/EVM/switchChain.ts +77 -0
  304. package/dist/core/EVM/types.ts +75 -0
  305. package/dist/core/EVM/utils.ts +43 -0
  306. package/dist/core/Solana/Solana.ts +46 -0
  307. package/dist/core/Solana/SolanaStepExecutor.ts +253 -0
  308. package/dist/core/Solana/connection.ts +19 -0
  309. package/dist/core/Solana/getSolanaBalance.ts +62 -0
  310. package/dist/core/Solana/types.ts +18 -0
  311. package/dist/core/StatusManager.ts +238 -0
  312. package/dist/core/checkBalance.ts +48 -0
  313. package/dist/core/execution.ts +218 -0
  314. package/dist/core/executionState.ts +40 -0
  315. package/dist/core/index.ts +3 -0
  316. package/dist/core/prepareRestart.ts +81 -0
  317. package/dist/core/provider.ts +12 -0
  318. package/dist/core/stepComparison.ts +51 -0
  319. package/dist/core/types.ts +103 -0
  320. package/dist/core/utils.ts +122 -0
  321. package/dist/core/waitForReceivingTransaction.ts +73 -0
  322. package/dist/createConfig.ts +54 -0
  323. package/dist/helpers.ts +97 -0
  324. package/dist/index.ts +11 -0
  325. package/dist/request.ts +79 -0
  326. package/dist/services/api.ts +472 -0
  327. package/dist/services/balance.ts +93 -0
  328. package/dist/typeguards.ts +89 -0
  329. package/dist/types/index.ts +1 -0
  330. package/dist/types/internal.ts +58 -0
  331. package/dist/utils/errors.ts +231 -0
  332. package/dist/utils/getTransactionMessage.ts +46 -0
  333. package/dist/utils/index.ts +5 -0
  334. package/dist/utils/median.ts +11 -0
  335. package/dist/utils/parseBackendError.ts +50 -0
  336. package/dist/utils/parseError.ts +204 -0
  337. package/dist/utils/utils.ts +48 -0
  338. package/dist/version.ts +2 -0
  339. package/package.json +27 -86
  340. package/dist/LiFi.d.ts +0 -171
  341. package/dist/LiFi.js +0 -246
  342. package/dist/allowance/getAllowance.d.ts +0 -6
  343. package/dist/allowance/index.d.ts +0 -3
  344. package/dist/allowance/index.js +0 -3
  345. package/dist/allowance/setAllowance.d.ts +0 -5
  346. package/dist/allowance/types.d.ts +0 -27
  347. package/dist/allowance/types.js +0 -1
  348. package/dist/balance/getBalance.d.ts +0 -2
  349. package/dist/balance/getTokenBalance.d.ts +0 -8
  350. package/dist/balance/getTokenBalance.js +0 -27
  351. package/dist/balance/index.d.ts +0 -2
  352. package/dist/balance/index.js +0 -2
  353. package/dist/cjs/LiFi.d.ts +0 -171
  354. package/dist/cjs/LiFi.js +0 -276
  355. package/dist/cjs/allowance/index.d.ts +0 -3
  356. package/dist/cjs/allowance/index.js +0 -19
  357. package/dist/cjs/allowance/setAllowance.d.ts +0 -5
  358. package/dist/cjs/allowance/setAllowance.js +0 -56
  359. package/dist/cjs/allowance/types.d.ts +0 -27
  360. package/dist/cjs/balance/getBalance.d.ts +0 -2
  361. package/dist/cjs/balance/getTokenBalance.d.ts +0 -8
  362. package/dist/cjs/balance/getTokenBalance.js +0 -33
  363. package/dist/cjs/balance/index.d.ts +0 -2
  364. package/dist/cjs/balance/index.js +0 -18
  365. package/dist/cjs/connectors.d.ts +0 -8
  366. package/dist/cjs/connectors.js +0 -75
  367. package/dist/cjs/execution/RouteExecutionManager.d.ts +0 -59
  368. package/dist/cjs/execution/RouteExecutionManager.js +0 -202
  369. package/dist/cjs/execution/StatusManager.js +0 -171
  370. package/dist/cjs/execution/StepExecutionManager.d.ts +0 -7
  371. package/dist/cjs/execution/StepExecutionManager.js +0 -267
  372. package/dist/cjs/execution/StepExecutor.d.ts +0 -15
  373. package/dist/cjs/execution/StepExecutor.js +0 -62
  374. package/dist/cjs/execution/checkAllowance.d.ts +0 -4
  375. package/dist/cjs/execution/checkBalance.d.ts +0 -2
  376. package/dist/cjs/execution/index.d.ts +0 -1
  377. package/dist/cjs/execution/index.js +0 -17
  378. package/dist/cjs/execution/multisig.d.ts +0 -4
  379. package/dist/cjs/execution/prepareRestart.d.ts +0 -3
  380. package/dist/cjs/execution/prepareRestart.js +0 -65
  381. package/dist/cjs/execution/stepComparison.d.ts +0 -14
  382. package/dist/cjs/execution/stepComparison.js +0 -37
  383. package/dist/cjs/execution/switchChain.d.ts +0 -21
  384. package/dist/cjs/execution/switchChain.js +0 -54
  385. package/dist/cjs/execution/utils.d.ts +0 -12
  386. package/dist/cjs/execution/waitForReceivingTransaction.d.ts +0 -3
  387. package/dist/cjs/helpers.d.ts +0 -18
  388. package/dist/cjs/index.d.ts +0 -5
  389. package/dist/cjs/index.js +0 -24
  390. package/dist/cjs/request.d.ts +0 -9
  391. package/dist/cjs/services/ApiService.d.ts +0 -17
  392. package/dist/cjs/services/ApiService.js +0 -326
  393. package/dist/cjs/services/ChainsService.d.ts +0 -11
  394. package/dist/cjs/services/ChainsService.js +0 -39
  395. package/dist/cjs/services/ConfigService.d.ts +0 -23
  396. package/dist/cjs/services/ConfigService.js +0 -101
  397. package/dist/cjs/types/index.d.ts +0 -4
  398. package/dist/cjs/types/index.js +0 -22
  399. package/dist/cjs/types/internal.types.d.ts +0 -142
  400. package/dist/cjs/utils/errors.d.ts +0 -107
  401. package/dist/cjs/utils/getMaxPriorityFeePerGas.d.ts +0 -2
  402. package/dist/cjs/utils/index.d.ts +0 -5
  403. package/dist/cjs/utils/index.js +0 -21
  404. package/dist/cjs/utils/parseError.d.ts +0 -37
  405. package/dist/cjs/utils/parseError.js +0 -157
  406. package/dist/cjs/utils/utils.d.ts +0 -10
  407. package/dist/cjs/version.d.ts +0 -2
  408. package/dist/connectors.d.ts +0 -8
  409. package/dist/connectors.js +0 -41
  410. package/dist/constants.d.ts +0 -6
  411. package/dist/execution/RouteExecutionManager.d.ts +0 -59
  412. package/dist/execution/RouteExecutionManager.js +0 -195
  413. package/dist/execution/StatusManager.d.ts +0 -67
  414. package/dist/execution/StatusManager.js +0 -167
  415. package/dist/execution/StepExecutionManager.d.ts +0 -7
  416. package/dist/execution/StepExecutionManager.js +0 -260
  417. package/dist/execution/StepExecutor.d.ts +0 -15
  418. package/dist/execution/StepExecutor.js +0 -58
  419. package/dist/execution/checkAllowance.d.ts +0 -4
  420. package/dist/execution/checkBalance.d.ts +0 -2
  421. package/dist/execution/index.d.ts +0 -1
  422. package/dist/execution/index.js +0 -1
  423. package/dist/execution/multisig.d.ts +0 -4
  424. package/dist/execution/prepareRestart.d.ts +0 -3
  425. package/dist/execution/prepareRestart.js +0 -61
  426. package/dist/execution/waitForReceivingTransaction.d.ts +0 -3
  427. package/dist/index.d.ts +0 -5
  428. package/dist/index.js +0 -6
  429. package/dist/services/ApiService.d.ts +0 -17
  430. package/dist/services/ApiService.js +0 -321
  431. package/dist/services/ChainsService.d.ts +0 -11
  432. package/dist/services/ChainsService.js +0 -33
  433. package/dist/services/ConfigService.d.ts +0 -23
  434. package/dist/services/ConfigService.js +0 -99
  435. package/dist/typeguards.d.ts +0 -4
  436. package/dist/types/abi.d.ts +0 -5
  437. package/dist/types/index.d.ts +0 -4
  438. package/dist/types/index.js +0 -4
  439. package/dist/types/internal.types.d.ts +0 -142
  440. package/dist/types/internal.types.js +0 -1
  441. package/dist/utils/getMaxPriorityFeePerGas.d.ts +0 -2
  442. package/dist/utils/index.d.ts +0 -5
  443. package/dist/utils/index.js +0 -5
  444. package/dist/utils/median.d.ts +0 -1
  445. package/dist/version.d.ts +0 -2
  446. package/dist/version.js +0 -2
@@ -0,0 +1,47 @@
1
+ import type { Abi } from 'viem'
2
+
3
+ export const approveAbi: Abi = [
4
+ {
5
+ name: 'approve',
6
+ inputs: [
7
+ { internalType: 'address', name: 'spender', type: 'address' },
8
+ { internalType: 'uint256', name: 'amount', type: 'uint256' },
9
+ ],
10
+ outputs: [{ internalType: 'bool', name: 'approved', type: 'bool' }],
11
+ stateMutability: 'nonpayable',
12
+ type: 'function',
13
+ },
14
+ ]
15
+
16
+ export const allowanceAbi: Abi = [
17
+ {
18
+ name: 'allowance',
19
+ inputs: [
20
+ { internalType: 'address', name: 'owner', type: 'address' },
21
+ { internalType: 'address', name: 'spender', type: 'address' },
22
+ ],
23
+ outputs: [{ internalType: 'uint256', name: 'allowance', type: 'uint256' }],
24
+ stateMutability: 'view',
25
+ type: 'function',
26
+ },
27
+ ]
28
+
29
+ export const getEthBalanceAbi: Abi = [
30
+ {
31
+ inputs: [{ name: '_owner', type: 'address' }],
32
+ name: 'getEthBalance',
33
+ outputs: [{ name: 'balance', type: 'uint256' }],
34
+ type: 'function',
35
+ stateMutability: 'view',
36
+ },
37
+ ]
38
+
39
+ export const balanceOfAbi: Abi = [
40
+ {
41
+ inputs: [{ name: '_owner', type: 'address' }],
42
+ name: 'balanceOf',
43
+ outputs: [{ name: 'balance', type: 'uint256' }],
44
+ type: 'function',
45
+ stateMutability: 'view',
46
+ },
47
+ ]
@@ -0,0 +1,152 @@
1
+ import type { Chain, LiFiStep, Process, ProcessType } from '@lifi/types'
2
+ import type { Address, Hash, ReplacementReason, WalletClient } from 'viem'
3
+ import { maxUint256, publicActions } from 'viem'
4
+ import { LiFiErrorCode, TransactionError } from '../../utils/index.js'
5
+ import { parseError } from '../../utils/parseError.js'
6
+ import type { StatusManager } from '../StatusManager.js'
7
+ import type { ExecutionOptions } from '../types.js'
8
+ import { getAllowance } from './getAllowance.js'
9
+ import { setAllowance } from './setAllowance.js'
10
+
11
+ export const checkAllowance = async (
12
+ chain: Chain,
13
+ step: LiFiStep,
14
+ walletClient: WalletClient,
15
+ statusManager: StatusManager,
16
+ settings?: ExecutionOptions,
17
+ allowUserInteraction = false,
18
+ shouldBatchTransactions = false
19
+ ): Promise<Hash | void> => {
20
+ // Ask the user to set an allowance
21
+ let allowanceProcess: Process = statusManager.findOrCreateProcess(
22
+ step,
23
+ 'TOKEN_ALLOWANCE'
24
+ )
25
+
26
+ // Check allowance
27
+ try {
28
+ if (allowanceProcess.txHash && allowanceProcess.status !== 'DONE') {
29
+ await waitForApprovalTransaction(
30
+ walletClient,
31
+ allowanceProcess.txHash! as Address,
32
+ allowanceProcess.type,
33
+ step,
34
+ chain,
35
+ statusManager
36
+ )
37
+ } else {
38
+ const approved = await getAllowance(
39
+ chain.id,
40
+ step.action.fromToken.address,
41
+ walletClient.account!.address,
42
+ step.estimate.approvalAddress
43
+ )
44
+
45
+ const fromAmount = BigInt(step.action.fromAmount)
46
+
47
+ if (fromAmount > approved) {
48
+ if (!allowUserInteraction) {
49
+ return
50
+ }
51
+ const approvalAmount = settings?.infiniteApproval
52
+ ? maxUint256
53
+ : fromAmount
54
+
55
+ if (shouldBatchTransactions) {
56
+ const approveTxHash = await setAllowance(
57
+ walletClient,
58
+ step.action.fromToken.address,
59
+ step.estimate.approvalAddress,
60
+ approvalAmount,
61
+ settings,
62
+ true
63
+ )
64
+
65
+ allowanceProcess = statusManager.updateProcess(
66
+ step,
67
+ allowanceProcess.type,
68
+ 'DONE'
69
+ )
70
+
71
+ return approveTxHash
72
+ }
73
+
74
+ const approveTxHash = await setAllowance(
75
+ walletClient,
76
+ step.action.fromToken.address,
77
+ step.estimate.approvalAddress,
78
+ approvalAmount
79
+ )
80
+ await waitForApprovalTransaction(
81
+ walletClient,
82
+ approveTxHash,
83
+ allowanceProcess.type,
84
+ step,
85
+ chain,
86
+ statusManager
87
+ )
88
+ } else {
89
+ allowanceProcess = statusManager.updateProcess(
90
+ step,
91
+ allowanceProcess.type,
92
+ 'DONE'
93
+ )
94
+ }
95
+ }
96
+ } catch (e: any) {
97
+ const error = await parseError(e, step, allowanceProcess)
98
+ allowanceProcess = statusManager.updateProcess(
99
+ step,
100
+ allowanceProcess.type,
101
+ 'FAILED',
102
+ {
103
+ error: {
104
+ message: error.message,
105
+ htmlMessage: error.htmlMessage,
106
+ code: error.code,
107
+ },
108
+ }
109
+ )
110
+ statusManager.updateExecution(step, 'FAILED')
111
+ throw error
112
+ }
113
+ }
114
+
115
+ const waitForApprovalTransaction = async (
116
+ walletClient: WalletClient,
117
+ txHash: Hash,
118
+ processType: ProcessType,
119
+ step: LiFiStep,
120
+ chain: Chain,
121
+ statusManager: StatusManager
122
+ ) => {
123
+ const client = walletClient.extend(publicActions)
124
+ statusManager.updateProcess(step, processType, 'PENDING', {
125
+ txHash,
126
+ txLink: `${chain.metamask.blockExplorerUrls[0]}tx/${txHash}`,
127
+ })
128
+
129
+ let replacementReason: ReplacementReason | undefined
130
+ const transactionReceipt = await client.waitForTransactionReceipt({
131
+ hash: txHash,
132
+ onReplaced(response) {
133
+ replacementReason = response.reason
134
+ statusManager.updateProcess(step, processType, 'PENDING', {
135
+ txHash: response.transaction.hash,
136
+ txLink: `${chain.metamask.blockExplorerUrls[0]}tx/${response.transaction.hash}`,
137
+ })
138
+ },
139
+ })
140
+
141
+ if (replacementReason === 'cancelled') {
142
+ throw new TransactionError(
143
+ LiFiErrorCode.TransactionCanceled,
144
+ 'User canceled transaction.'
145
+ )
146
+ }
147
+
148
+ statusManager.updateProcess(step, processType, 'DONE', {
149
+ txHash: transactionReceipt.transactionHash,
150
+ txLink: `${chain.metamask.blockExplorerUrls[0]}tx/${transactionReceipt.transactionHash}`,
151
+ })
152
+ }
@@ -0,0 +1,146 @@
1
+ import type { ChainId, Token } from '@lifi/types'
2
+ import type { Address } from 'viem'
3
+ import { getContract } from 'viem'
4
+ import { MulticallBatchSize } from '../../constants.js'
5
+ import { isNativeTokenAddress } from '../../utils/utils.js'
6
+ import { allowanceAbi } from './abi.js'
7
+ import { getPublicClient } from './publicClient.js'
8
+ import type {
9
+ TokenAllowance,
10
+ TokenSpender,
11
+ TokenSpenderAllowance,
12
+ } from './types.js'
13
+ import { getMulticallAddress } from './utils.js'
14
+
15
+ export const getAllowance = async (
16
+ chainId: ChainId,
17
+ tokenAddress: string,
18
+ ownerAddress: string,
19
+ spenderAddress: string
20
+ ): Promise<bigint> => {
21
+ const client = await getPublicClient(chainId)
22
+ const contract = getContract({
23
+ address: tokenAddress as Address,
24
+ abi: allowanceAbi,
25
+ client: client,
26
+ })
27
+
28
+ try {
29
+ const approved = (await contract.read.allowance([
30
+ ownerAddress,
31
+ spenderAddress,
32
+ ])) as bigint
33
+ return approved
34
+ } catch (e) {
35
+ return 0n
36
+ }
37
+ }
38
+
39
+ export const getAllowanceMulticall = async (
40
+ chainId: ChainId,
41
+ tokens: TokenSpender[],
42
+ ownerAddress: string
43
+ ): Promise<TokenSpenderAllowance[]> => {
44
+ if (!tokens.length) {
45
+ return []
46
+ }
47
+ const multicallAddress = await getMulticallAddress(chainId)
48
+ if (!multicallAddress) {
49
+ throw new Error(`No multicall address configured for chainId ${chainId}.`)
50
+ }
51
+
52
+ const client = await getPublicClient(chainId)
53
+
54
+ const contracts = tokens.map((token) => ({
55
+ address: token.token.address as Address,
56
+ abi: allowanceAbi,
57
+ functionName: 'allowance',
58
+ args: [ownerAddress, token.spenderAddress],
59
+ }))
60
+
61
+ const results = await client.multicall({
62
+ contracts,
63
+ multicallAddress: multicallAddress as Address,
64
+ batchSize: MulticallBatchSize,
65
+ })
66
+
67
+ if (!results.length) {
68
+ throw new Error(
69
+ `Couldn't load allowance from chainId ${chainId} using multicall.`
70
+ )
71
+ }
72
+
73
+ return tokens.map(({ token, spenderAddress }, i: number) => ({
74
+ token,
75
+ spenderAddress,
76
+ allowance: results[i].result as bigint,
77
+ }))
78
+ }
79
+
80
+ /**
81
+ * Get the current allowance for a certain token.
82
+ * @param token - The token that should be checked
83
+ * @param ownerAddress - The owner of the token
84
+ * @param spenderAddress - The spender address that has to be approved
85
+ * @returns Returns allowance
86
+ */
87
+ export const getTokenAllowance = async (
88
+ token: Token,
89
+ ownerAddress: string,
90
+ spenderAddress: string
91
+ ): Promise<bigint | undefined> => {
92
+ // native token don't need approval
93
+ if (isNativeTokenAddress(token.address)) {
94
+ return
95
+ }
96
+
97
+ const approved = await getAllowance(
98
+ token.chainId,
99
+ token.address,
100
+ ownerAddress,
101
+ spenderAddress
102
+ )
103
+ return approved
104
+ }
105
+
106
+ /**
107
+ * Get the current allowance for a list of token/spender address pairs.
108
+ * @param ownerAddress - The owner of the tokens
109
+ * @param tokens - A list of token and spender address pairs
110
+ * @returns Returns array of tokens and their allowance
111
+ */
112
+ export const getTokenAllowanceMulticall = async (
113
+ ownerAddress: string,
114
+ tokens: TokenSpender[]
115
+ ): Promise<TokenAllowance[]> => {
116
+ // filter out native tokens
117
+ const filteredTokens = tokens.filter(
118
+ ({ token }) => !isNativeTokenAddress(token.address)
119
+ )
120
+
121
+ // group by chain
122
+ const tokenDataByChain: { [chainId: number]: TokenSpender[] } = {}
123
+ filteredTokens.forEach((data) => {
124
+ if (!tokenDataByChain[data.token.chainId]) {
125
+ tokenDataByChain[data.token.chainId] = []
126
+ }
127
+ tokenDataByChain[data.token.chainId].push(data)
128
+ })
129
+
130
+ const chainKeys = Object.keys(tokenDataByChain).map(Number.parseInt)
131
+
132
+ const allowances = (
133
+ await Promise.all(
134
+ chainKeys.map(async (chainId) => {
135
+ // get allowances for current chain and token list
136
+ return getAllowanceMulticall(
137
+ chainId,
138
+ tokenDataByChain[chainId],
139
+ ownerAddress
140
+ )
141
+ })
142
+ )
143
+ ).flat()
144
+
145
+ return allowances
146
+ }
@@ -0,0 +1,120 @@
1
+ import type { ChainId, Token, TokenAmount } from '@lifi/types'
2
+ import type { Address } from 'viem'
3
+ import { MulticallBatchSize } from '../../constants.js'
4
+ import { isZeroAddress } from '../../utils/utils.js'
5
+ import { balanceOfAbi, getEthBalanceAbi } from './abi.js'
6
+ import { getPublicClient } from './publicClient.js'
7
+ import { getMulticallAddress } from './utils.js'
8
+
9
+ export const getEVMBalance = async (
10
+ walletAddress: string,
11
+ tokens: Token[]
12
+ ): Promise<TokenAmount[]> => {
13
+ if (tokens.length === 0) {
14
+ return []
15
+ }
16
+ const { chainId } = tokens[0]
17
+ tokens.forEach((token) => {
18
+ if (token.chainId !== chainId) {
19
+ console.warn(`Requested tokens have to be on the same chain.`)
20
+ }
21
+ })
22
+
23
+ const multicallAddress = await getMulticallAddress(chainId)
24
+
25
+ if (multicallAddress && tokens.length > 1) {
26
+ return getEVMBalanceMulticall(
27
+ chainId,
28
+ tokens,
29
+ walletAddress,
30
+ multicallAddress
31
+ )
32
+ } else {
33
+ return getEVMBalanceDefault(chainId, tokens, walletAddress)
34
+ }
35
+ }
36
+
37
+ const getEVMBalanceMulticall = async (
38
+ chainId: ChainId,
39
+ tokens: Token[],
40
+ walletAddress: string,
41
+ multicallAddress: string
42
+ ): Promise<TokenAmount[]> => {
43
+ const client = await getPublicClient(chainId)
44
+
45
+ const contracts = tokens.map((token) => {
46
+ if (isZeroAddress(token.address)) {
47
+ return {
48
+ address: multicallAddress as Address,
49
+ abi: getEthBalanceAbi,
50
+ functionName: 'getEthBalance',
51
+ args: [walletAddress],
52
+ }
53
+ }
54
+ return {
55
+ address: token.address as Address,
56
+ abi: balanceOfAbi,
57
+ functionName: 'balanceOf',
58
+ args: [walletAddress],
59
+ }
60
+ })
61
+ const blockNumber = await client.getBlockNumber()
62
+ const results = await client.multicall({
63
+ contracts,
64
+ multicallAddress: multicallAddress as Address,
65
+ blockNumber,
66
+ batchSize: MulticallBatchSize,
67
+ })
68
+
69
+ if (!results.length) {
70
+ return []
71
+ }
72
+
73
+ return tokens.map((token, i: number) => {
74
+ return {
75
+ ...token,
76
+ amount: results[i].result as bigint,
77
+ blockNumber,
78
+ }
79
+ })
80
+ }
81
+
82
+ const getEVMBalanceDefault = async (
83
+ chainId: ChainId,
84
+ tokens: Token[],
85
+ walletAddress: string
86
+ ): Promise<TokenAmount[]> => {
87
+ const client = await getPublicClient(chainId)
88
+ const blockNumber = await client.getBlockNumber()
89
+ const queue: Promise<bigint>[] = tokens.map((token) => {
90
+ if (isZeroAddress(token.address)) {
91
+ return client.getBalance({
92
+ address: walletAddress as Address,
93
+ })
94
+ }
95
+ return client.readContract({
96
+ address: token.address as Address,
97
+ abi: balanceOfAbi,
98
+ functionName: 'balanceOf',
99
+ args: [walletAddress],
100
+ }) as Promise<bigint>
101
+ })
102
+
103
+ const results = await Promise.allSettled(queue)
104
+
105
+ const tokenAmounts: TokenAmount[] = tokens.map((token, index) => {
106
+ const result = results[index]
107
+ if (result.status === 'rejected') {
108
+ return {
109
+ ...token,
110
+ blockNumber,
111
+ }
112
+ }
113
+ return {
114
+ ...token,
115
+ amount: result.value,
116
+ blockNumber,
117
+ }
118
+ })
119
+ return tokenAmounts
120
+ }
@@ -0,0 +1,53 @@
1
+ import type { ExtendedChain, LiFiStep, ProcessType } from '@lifi/types'
2
+ import type { Hash } from 'viem'
3
+ import { LiFiErrorCode, TransactionError } from '../../utils/errors.js'
4
+ import type { StatusManager } from '../StatusManager.js'
5
+ import type { MultisigConfig, MultisigTxDetails } from './types.js'
6
+
7
+ export const updateMultisigRouteProcess = async (
8
+ internalTxHash: Hash,
9
+ step: LiFiStep,
10
+ processType: ProcessType,
11
+ fromChain: ExtendedChain,
12
+ statusManager: StatusManager,
13
+ multisig?: MultisigConfig
14
+ ) => {
15
+ if (!multisig?.getMultisigTransactionDetails) {
16
+ throw new Error(
17
+ 'getMultisigTransactionDetails is missing in multisig config.'
18
+ )
19
+ }
20
+
21
+ const updateIntermediateMultisigStatus = () => {
22
+ statusManager.updateProcess(step, processType, 'PENDING')
23
+ }
24
+
25
+ const multisigStatusResponse: MultisigTxDetails =
26
+ await multisig?.getMultisigTransactionDetails(
27
+ internalTxHash,
28
+ fromChain.id,
29
+ updateIntermediateMultisigStatus
30
+ )
31
+
32
+ if (multisigStatusResponse.status === 'DONE') {
33
+ statusManager.updateProcess(step, processType, 'PENDING', {
34
+ txHash: multisigStatusResponse.txHash,
35
+ multisigTxHash: undefined,
36
+ txLink: `${fromChain.metamask.blockExplorerUrls[0]}tx/${multisigStatusResponse.txHash}`,
37
+ })
38
+ }
39
+
40
+ if (multisigStatusResponse.status === 'FAILED') {
41
+ throw new TransactionError(
42
+ LiFiErrorCode.TransactionFailed,
43
+ 'Multisig transaction failed.'
44
+ )
45
+ }
46
+
47
+ if (multisigStatusResponse.status === 'CANCELLED') {
48
+ throw new TransactionError(
49
+ LiFiErrorCode.SignatureRejected,
50
+ 'Transaction was rejected by user.'
51
+ )
52
+ }
53
+ }
@@ -0,0 +1,46 @@
1
+ import type { PublicClient } from 'viem'
2
+ import { createPublicClient, fallback, http } from 'viem'
3
+ import type { Chain } from 'viem/chains'
4
+ import { config } from '../../config.js'
5
+ import { getRpcUrls } from '../utils.js'
6
+
7
+ // cached providers
8
+ const publicClients: Record<number, PublicClient> = {}
9
+
10
+ /**
11
+ * Get an instance of a provider for a specific chain
12
+ * @param chainId - Id of the chain the provider is for
13
+ * @returns The public client for the given chain
14
+ */
15
+ export const getPublicClient = async (
16
+ chainId: number
17
+ ): Promise<PublicClient> => {
18
+ if (!publicClients[chainId]) {
19
+ const urls = await getRpcUrls(chainId)
20
+ const fallbackTransports = urls.map((url) =>
21
+ http(url, {
22
+ batch: true,
23
+ })
24
+ )
25
+ const _chain = await config.getChainById(chainId)
26
+ const chain: Chain = {
27
+ ..._chain,
28
+ ..._chain.metamask,
29
+ name: _chain.metamask.chainName,
30
+ rpcUrls: {
31
+ default: { http: _chain.metamask.rpcUrls },
32
+ public: { http: _chain.metamask.rpcUrls },
33
+ },
34
+ }
35
+ publicClients[chainId] = createPublicClient({
36
+ chain: chain,
37
+ transport: fallback(fallbackTransports),
38
+ })
39
+ }
40
+
41
+ if (!publicClients[chainId]) {
42
+ throw new Error(`Unable to configure provider for chain ${chainId}`)
43
+ }
44
+
45
+ return publicClients[chainId]
46
+ }
@@ -0,0 +1,144 @@
1
+ import type {
2
+ Address,
3
+ Hash,
4
+ PublicClient,
5
+ SendTransactionParameters,
6
+ WalletClient,
7
+ } from 'viem'
8
+ import { encodeFunctionData, maxUint256, publicActions } from 'viem'
9
+ import { isNativeTokenAddress } from '../../utils/utils.js'
10
+ import type { ExecutionOptions, TransactionParameters } from '../types.js'
11
+ import { approveAbi } from './abi.js'
12
+ import { getAllowance } from './getAllowance.js'
13
+ import type { ApproveTokenRequest, RevokeApprovalRequest } from './types.js'
14
+ import { getMaxPriorityFeePerGas } from './utils.js'
15
+
16
+ export const setAllowance = async (
17
+ walletClient: WalletClient,
18
+ tokenAddress: string,
19
+ contractAddress: string,
20
+ amount: bigint,
21
+ settings?: ExecutionOptions,
22
+ returnPopulatedTransaction?: boolean
23
+ ): Promise<Hash> => {
24
+ const data = encodeFunctionData({
25
+ abi: approveAbi,
26
+ functionName: 'approve',
27
+ args: [contractAddress, amount],
28
+ })
29
+
30
+ if (returnPopulatedTransaction) {
31
+ return data
32
+ }
33
+ const client = walletClient.extend(publicActions)
34
+
35
+ let transactionRequest: TransactionParameters = {
36
+ to: tokenAddress,
37
+ data,
38
+ maxPriorityFeePerGas:
39
+ walletClient.account?.type === 'local'
40
+ ? await getMaxPriorityFeePerGas(client as PublicClient)
41
+ : undefined,
42
+ }
43
+
44
+ if (settings?.updateTransactionRequestHook) {
45
+ const customizedTransactionRequest: TransactionParameters =
46
+ await settings.updateTransactionRequestHook({
47
+ requestType: 'approve',
48
+ ...transactionRequest,
49
+ })
50
+
51
+ transactionRequest = {
52
+ ...transactionRequest,
53
+ ...customizedTransactionRequest,
54
+ }
55
+ }
56
+
57
+ return client.sendTransaction({
58
+ to: transactionRequest.to as Address,
59
+ account: walletClient.account!,
60
+ data: transactionRequest.data,
61
+ gas: transactionRequest.gas,
62
+ gasPrice: transactionRequest.gasPrice,
63
+ maxFeePerGas: transactionRequest.maxFeePerGas,
64
+ maxPriorityFeePerGas: transactionRequest.maxPriorityFeePerGas,
65
+ chain: null,
66
+ } as SendTransactionParameters)
67
+ }
68
+
69
+ /**
70
+ * Set approval for a certain token and amount.
71
+ * @param request - The approval request
72
+ * @param request.walletClient
73
+ * @param request.token
74
+ * @param request.spenderAddress
75
+ * @param request.amount
76
+ * @param request.infiniteApproval
77
+ * @returns Returns Hash or nothing
78
+ */
79
+ export const setTokenAllowance = async ({
80
+ walletClient,
81
+ token,
82
+ spenderAddress,
83
+ amount,
84
+ infiniteApproval = false,
85
+ }: ApproveTokenRequest): Promise<Hash | void> => {
86
+ // native token don't need approval
87
+ if (isNativeTokenAddress(token.address)) {
88
+ return
89
+ }
90
+ const approvedAmount = await getAllowance(
91
+ token.chainId,
92
+ token.address,
93
+ walletClient.account!.address,
94
+ spenderAddress
95
+ )
96
+
97
+ if (amount > approvedAmount) {
98
+ const approvalAmount = infiniteApproval ? maxUint256 : amount
99
+
100
+ const approveTx = await setAllowance(
101
+ walletClient,
102
+ token.address,
103
+ spenderAddress,
104
+ approvalAmount
105
+ )
106
+
107
+ return approveTx
108
+ }
109
+ }
110
+
111
+ /**
112
+ * Revoke approval for a certain token.
113
+ * @param request - The revoke request
114
+ * @param request.walletClient
115
+ * @param request.token
116
+ * @param request.spenderAddress
117
+ * @returns Returns Hash or nothing
118
+ */
119
+ export const revokeTokenApproval = async ({
120
+ walletClient,
121
+ token,
122
+ spenderAddress,
123
+ }: RevokeApprovalRequest): Promise<Hash | void> => {
124
+ // native token don't need approval
125
+ if (isNativeTokenAddress(token.address)) {
126
+ return
127
+ }
128
+ const approvedAmount = await getAllowance(
129
+ token.chainId,
130
+ token.address,
131
+ walletClient.account!.address,
132
+ spenderAddress
133
+ )
134
+ if (approvedAmount > 0) {
135
+ const approveTx = await setAllowance(
136
+ walletClient,
137
+ token.address,
138
+ spenderAddress,
139
+ 0n
140
+ )
141
+
142
+ return approveTx
143
+ }
144
+ }