@berachain/berajs 0.1.0 → 0.1.2

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 (502) hide show
  1. package/dist/{dex-jD95agMv.d.cts → HoneyConfigProvider-6PXZJMSU.d.cts} +172 -172
  2. package/dist/{dex-CxzGqVLe.d.ts → HoneyConfigProvider-CwDFrt02.d.ts} +172 -172
  3. package/dist/abi/index.cjs +2 -1
  4. package/dist/abi/index.cjs.map +1 -0
  5. package/dist/abi/index.mjs +2 -1
  6. package/dist/abi/index.mjs.map +1 -0
  7. package/dist/actions/index.cjs +2 -1
  8. package/dist/actions/index.cjs.map +1 -0
  9. package/dist/actions/index.d.cts +8 -8
  10. package/dist/actions/index.d.ts +8 -8
  11. package/dist/actions/index.mjs +2 -1
  12. package/dist/actions/index.mjs.map +1 -0
  13. package/dist/chunk-22MQVFHP.cjs +2 -0
  14. package/dist/chunk-22MQVFHP.cjs.map +1 -0
  15. package/dist/chunk-3TKSVFZ3.mjs +2 -0
  16. package/dist/chunk-3TKSVFZ3.mjs.map +1 -0
  17. package/dist/chunk-6VF3RF53.cjs +3 -0
  18. package/dist/chunk-6VF3RF53.cjs.map +1 -0
  19. package/dist/{chunk-2422CKAT.mjs → chunk-6W22TFYA.mjs} +2 -1
  20. package/dist/chunk-6W22TFYA.mjs.map +1 -0
  21. package/dist/chunk-AMRJ4B36.mjs +2 -0
  22. package/dist/chunk-AMRJ4B36.mjs.map +1 -0
  23. package/dist/{chunk-4IBBLGNS.mjs → chunk-DS2ZDVKU.mjs} +1 -0
  24. package/dist/chunk-DS2ZDVKU.mjs.map +1 -0
  25. package/dist/{chunk-B47PDJZM.cjs → chunk-ED43NYSE.cjs} +2 -1
  26. package/dist/chunk-ED43NYSE.cjs.map +1 -0
  27. package/dist/chunk-EU4WFADK.cjs +2 -0
  28. package/dist/chunk-EU4WFADK.cjs.map +1 -0
  29. package/dist/{chunk-ASRHUDAH.mjs → chunk-HCTFDGXL.mjs} +1 -0
  30. package/dist/chunk-HCTFDGXL.mjs.map +1 -0
  31. package/dist/{chunk-K4NZIO2Z.mjs → chunk-IOSYBHKU.mjs} +1 -0
  32. package/dist/chunk-IOSYBHKU.mjs.map +1 -0
  33. package/dist/{chunk-4GY57XKG.cjs → chunk-JMKLDDKY.cjs} +1 -0
  34. package/dist/chunk-JMKLDDKY.cjs.map +1 -0
  35. package/dist/chunk-KMKUXP2S.mjs +3 -0
  36. package/dist/chunk-KMKUXP2S.mjs.map +1 -0
  37. package/dist/chunk-OJXJ6CZ4.mjs +4 -0
  38. package/dist/chunk-OJXJ6CZ4.mjs.map +1 -0
  39. package/dist/{chunk-VA37UBED.mjs → chunk-PHIOKO3A.mjs} +2 -1
  40. package/dist/chunk-PHIOKO3A.mjs.map +1 -0
  41. package/dist/{chunk-IKAE7CQU.cjs → chunk-QCK474XD.cjs} +1 -0
  42. package/dist/chunk-QCK474XD.cjs.map +1 -0
  43. package/dist/chunk-REDXXUK7.cjs +2 -0
  44. package/dist/chunk-REDXXUK7.cjs.map +1 -0
  45. package/dist/{chunk-K2FCHQHA.cjs → chunk-S74ZMG2G.cjs} +2 -1
  46. package/dist/chunk-S74ZMG2G.cjs.map +1 -0
  47. package/dist/{chunk-NOV7UCGA.cjs → chunk-TOAXF4YW.cjs} +1 -0
  48. package/dist/chunk-TOAXF4YW.cjs.map +1 -0
  49. package/dist/chunk-UZ5CB33M.cjs +2 -0
  50. package/dist/chunk-UZ5CB33M.cjs.map +1 -0
  51. package/dist/chunk-VYWYJR63.mjs +2 -0
  52. package/dist/chunk-VYWYJR63.mjs.map +1 -0
  53. package/dist/{chunk-WZLPSG5R.cjs → chunk-XFPTQMAK.cjs} +3 -2
  54. package/dist/chunk-XFPTQMAK.cjs.map +1 -0
  55. package/dist/chunk-YK4NINF5.mjs +2 -0
  56. package/dist/chunk-YK4NINF5.mjs.map +1 -0
  57. package/dist/contexts/index.cjs +2 -1
  58. package/dist/contexts/index.cjs.map +1 -0
  59. package/dist/contexts/index.d.cts +7 -5
  60. package/dist/contexts/index.d.ts +7 -5
  61. package/dist/contexts/index.mjs +2 -1
  62. package/dist/contexts/index.mjs.map +1 -0
  63. package/dist/enum/index.cjs +2 -1
  64. package/dist/enum/index.cjs.map +1 -0
  65. package/dist/enum/index.d.cts +1 -1
  66. package/dist/enum/index.d.ts +1 -1
  67. package/dist/enum/index.mjs +2 -1
  68. package/dist/enum/index.mjs.map +1 -0
  69. package/dist/errors/index.cjs +2 -1
  70. package/dist/errors/index.cjs.map +1 -0
  71. package/dist/errors/index.d.cts +1 -1
  72. package/dist/errors/index.d.ts +1 -1
  73. package/dist/errors/index.mjs +2 -1
  74. package/dist/errors/index.mjs.map +1 -0
  75. package/dist/{getValidatorQueuedOperatorAddress-9jSXlAse.d.ts → getValidatorQueuedOperatorAddress-9xY2RUGe.d.cts} +2 -2
  76. package/dist/{getValidatorQueuedOperatorAddress-BCYlvah3.d.cts → getValidatorQueuedOperatorAddress-Ne2zoiiV.d.ts} +2 -2
  77. package/dist/hooks/index.cjs +3 -2
  78. package/dist/hooks/index.cjs.map +1 -0
  79. package/dist/hooks/index.d.cts +9 -9
  80. package/dist/hooks/index.d.ts +9 -9
  81. package/dist/hooks/index.mjs +3 -2
  82. package/dist/hooks/index.mjs.map +1 -0
  83. package/dist/{staking-Bw46L2PT.d.ts → staking-F9Nx9TKD.d.ts} +1 -1
  84. package/dist/{staking-BxYDlMtj.d.cts → staking-f4BpYH7l.d.cts} +1 -1
  85. package/dist/{txnEnum-Diichv9Z.d.cts → txnEnum-BQKDfaeH.d.cts} +1 -0
  86. package/dist/{txnEnum-Diichv9Z.d.ts → txnEnum-BQKDfaeH.d.ts} +1 -0
  87. package/dist/types/index.cjs +2 -1
  88. package/dist/types/index.cjs.map +1 -0
  89. package/dist/types/index.d.cts +5 -5
  90. package/dist/types/index.d.ts +5 -5
  91. package/dist/types/index.mjs +2 -1
  92. package/dist/types/index.mjs.map +1 -0
  93. package/dist/{useHoneySwapState-YfMSTWY3.d.cts → useHoneySwapState-C-9BV4VB.d.cts} +1 -1
  94. package/dist/{useHoneySwapState-CD8v6y1k.d.ts → useHoneySwapState-CUY5Sm1b.d.ts} +1 -1
  95. package/dist/utils/index.cjs +2 -1
  96. package/dist/utils/index.cjs.map +1 -0
  97. package/dist/utils/index.d.cts +5 -5
  98. package/dist/utils/index.d.ts +5 -5
  99. package/dist/utils/index.mjs +2 -1
  100. package/dist/utils/index.mjs.map +1 -0
  101. package/package.json +4 -3
  102. package/src/abi/bend/bundler.ts +60 -0
  103. package/src/abi/bend/generalAdapter.ts +362 -0
  104. package/src/abi/bend/metaMorpho.ts +2584 -0
  105. package/src/abi/bend/morpho.ts +444 -0
  106. package/src/abi/bend/morphoVault.ts +1456 -0
  107. package/src/abi/bend/publicAllocator.ts +355 -0
  108. package/src/abi/bex/aggregatorsRouter.ts +492 -0
  109. package/src/abi/bex/balancerComposableStablePoolFactoryV6.ts +283 -0
  110. package/src/abi/bex/balancerPoolCreationHelper.ts +267 -0
  111. package/src/abi/bex/balancerVault.ts +757 -0
  112. package/src/abi/enso/ensoRouterAbi.ts +140 -0
  113. package/src/abi/governance/governance.ts +1016 -0
  114. package/src/abi/governance/governanceTimelock.ts +589 -0
  115. package/src/abi/honey/collateralVault.ts +1144 -0
  116. package/src/abi/honey/honey.ts +563 -0
  117. package/src/abi/honey/honeyFactory.ts +1270 -0
  118. package/src/abi/honey/honeyFactoryReader.ts +894 -0
  119. package/src/abi/honey/honeyRootOracle.ts +492 -0
  120. package/src/abi/honey/pythPriceOracle.ts +619 -0
  121. package/src/abi/honey/pythWrapper.ts +446 -0
  122. package/src/abi/index.ts +35 -0
  123. package/src/abi/pol/beaconDeposit.ts +595 -0
  124. package/src/abi/pol/beraChef.ts +1019 -0
  125. package/src/abi/pol/bgt.ts +1008 -0
  126. package/src/abi/pol/bgtIncentiveDistributor.ts +640 -0
  127. package/src/abi/pol/bgtStaker.ts +1034 -0
  128. package/src/abi/pol/rewardAllocationFactory.ts +408 -0
  129. package/src/abi/pol/rewardVault.ts +1928 -0
  130. package/src/abi/pol/rewardVaultFactory.ts +586 -0
  131. package/src/abi/pol/rewardVaultHelper.ts +882 -0
  132. package/src/abi/pol/stakeBeraVault.ts +1116 -0
  133. package/src/abi/pol/stakeBeraVaultWithdrawalRequest.ts +618 -0
  134. package/src/abi/pol/wbera.ts +366 -0
  135. package/src/abi/pyth/pyth.ts +566 -0
  136. package/src/abi/pyth/pythErrors.ts +72 -0
  137. package/src/abi/utils/EIP7702StatelessDelegator.ts +523 -0
  138. package/src/abi/utils/bundler3errors.ts +6 -0
  139. package/src/abi/utils/commonAbiErrors.ts +9 -0
  140. package/src/abi/utils/erc20errors.ts +74 -0
  141. package/src/abi/utils/merkleDistributor.ts +838 -0
  142. package/src/abi/utils/multicall3.ts +242 -0
  143. package/src/abi/utils/openChainErrors.ts +139 -0
  144. package/src/abi/utils/upgradableBeacon.ts +86 -0
  145. package/src/actions/bend/getConvertToAssets.ts +23 -0
  146. package/src/actions/bend/getMaxDeposit.ts +21 -0
  147. package/src/actions/clients/BeraApolloClient.ts +125 -0
  148. package/src/actions/clients/getApolloClient.ts +109 -0
  149. package/src/actions/clients/getEnsoClient.ts +24 -0
  150. package/src/actions/dex/__tests__/aggregators.unit.test.ts +112 -0
  151. package/src/actions/dex/__tests__/allPools.integration.test.ts +13 -0
  152. package/src/actions/dex/__tests__/getOnChainPool.unit.test.ts +15 -0
  153. package/src/actions/dex/__tests__/haiku.integration.test.ts +40 -0
  154. package/src/actions/dex/aggregators/base.ts +174 -0
  155. package/src/actions/dex/aggregators/bex.ts +255 -0
  156. package/src/actions/dex/aggregators/bgt.ts +50 -0
  157. package/src/actions/dex/aggregators/enso.ts +113 -0
  158. package/src/actions/dex/aggregators/fly.ts +136 -0
  159. package/src/actions/dex/aggregators/haiku.ts +171 -0
  160. package/src/actions/dex/aggregators/honey.ts +245 -0
  161. package/src/actions/dex/aggregators/kyberswap.ts +142 -0
  162. package/src/actions/dex/aggregators/oogabooga.ts +110 -0
  163. package/src/actions/dex/aggregators/openOcean.ts +142 -0
  164. package/src/actions/dex/aggregators/wbera.ts +72 -0
  165. package/src/actions/dex/b-sdk.ts +48 -0
  166. package/src/actions/dex/getAllPools.ts +60 -0
  167. package/src/actions/dex/getApiPool.ts +47 -0
  168. package/src/actions/dex/getGlobalLiquidityAndSwapVolume.ts +29 -0
  169. package/src/actions/dex/getIsTokenExploited.ts +63 -0
  170. package/src/actions/dex/getOnChainPool.ts +216 -0
  171. package/src/actions/dex/getPoolEvents.ts +35 -0
  172. package/src/actions/dex/getPoolHistoricalData.ts +51 -0
  173. package/src/actions/dex/getPoolPausedState.ts +40 -0
  174. package/src/actions/dex/getPoolWithState.ts +24 -0
  175. package/src/actions/dex/getSubgraphPool.ts +30 -0
  176. package/src/actions/dex/getTransactionCount.ts +29 -0
  177. package/src/actions/dex/getVaultPausedState.ts +21 -0
  178. package/src/actions/enso/bend.ts +143 -0
  179. package/src/actions/enso/getEnsoUserToken.debug.test.ts +57 -0
  180. package/src/actions/enso/getEnsoUserToken.ts +133 -0
  181. package/src/actions/enso/util.ts +60 -0
  182. package/src/actions/enso/zap.ts +78 -0
  183. package/src/actions/governance/__tests__/checkProposalField.unit.test.ts +108 -0
  184. package/src/actions/governance/__tests__/getDecodedFunctionData.integration.test.ts +127 -0
  185. package/src/actions/governance/__tests__/getQuorum.integration.test.ts +39 -0
  186. package/src/actions/governance/checkProposalField.ts +211 -0
  187. package/src/actions/governance/computeActualStatus.ts +154 -0
  188. package/src/actions/governance/constants.ts +7 -0
  189. package/src/actions/governance/getAllProposals.ts +75 -0
  190. package/src/actions/governance/getBodyErrors.ts +28 -0
  191. package/src/actions/governance/getDecodedFunctionData.ts +170 -0
  192. package/src/actions/governance/getProposalDetails.ts +40 -0
  193. package/src/actions/governance/getProposalFromTx.ts +101 -0
  194. package/src/actions/governance/getProposalParams.ts +247 -0
  195. package/src/actions/governance/getProposalVotes.ts +20 -0
  196. package/src/actions/governance/getQuorum.ts +38 -0
  197. package/src/actions/governance/parseProposalBody.ts +73 -0
  198. package/src/actions/honey/getChartData.ts +27 -0
  199. package/src/actions/honey/getCollateralWeights.ts +50 -0
  200. package/src/actions/honey/getGlobalCapLimit.ts +148 -0
  201. package/src/actions/honey/getHoneyCollaterals.ts +139 -0
  202. package/src/actions/honey/getHoneyFees.ts +100 -0
  203. package/src/actions/honey/getHoneyGlobalData.ts +21 -0
  204. package/src/actions/honey/getHoneyPausedState.ts +25 -0
  205. package/src/actions/honey/getHoneyPreview.ts +152 -0
  206. package/src/actions/honey/getHoneyPythPriceOracle.ts +21 -0
  207. package/src/actions/honey/getHoneyRootPriceOracle.ts +23 -0
  208. package/src/actions/honey/getHoneyVaultsBalance.ts +86 -0
  209. package/src/actions/honey/getPythLatestPrices.ts +54 -0
  210. package/src/actions/honey/getRelativeCapLimit.ts +83 -0
  211. package/src/actions/honey/getSharesWithoutFees.ts +65 -0
  212. package/src/actions/honey/getSwapPayload.ts +128 -0
  213. package/src/actions/honey/isBadCollateralAsset.ts +77 -0
  214. package/src/actions/honey/isBasketModeEnabled.ts +47 -0
  215. package/src/actions/honey/tests/pyth.integration.test.ts +63 -0
  216. package/src/actions/index.ts +105 -0
  217. package/src/actions/misc/getBlockTimestamp.ts +41 -0
  218. package/src/actions/pol/__tests__/getAllValidators.integration.test.ts +55 -0
  219. package/src/actions/pol/__tests__/getValidator.integration.test.ts +32 -0
  220. package/src/actions/pol/__tests__/rewardVaults.integration.test.ts +34 -0
  221. package/src/actions/pol/getBGTGlobalInfo.ts +43 -0
  222. package/src/actions/pol/getBgtAprSimulation.ts +207 -0
  223. package/src/actions/pol/getBgtAprSimultaion.unit.test.ts +297 -0
  224. package/src/actions/pol/getBgtTokenTotalBoosts.ts +30 -0
  225. package/src/actions/pol/getBgtTokenTotalSupply.ts +29 -0
  226. package/src/actions/pol/getBlockTime.ts +43 -0
  227. package/src/actions/pol/getGlobalCuttingBoard.ts +25 -0
  228. package/src/actions/pol/getGlobalData.ts +74 -0
  229. package/src/actions/pol/getIncentiveFeeClaimStats.ts +43 -0
  230. package/src/actions/pol/getMarkets.ts +36 -0
  231. package/src/actions/pol/getRewardProofsByValidator.ts +49 -0
  232. package/src/actions/pol/getRewardTokenToBeraRate.ts +30 -0
  233. package/src/actions/pol/getRewardVault.ts +54 -0
  234. package/src/actions/pol/getRewardVaultBeaconImplementation.integration.test.ts +18 -0
  235. package/src/actions/pol/getRewardVaultBeaconImplementation.ts +35 -0
  236. package/src/actions/pol/getRewardVaultDurationConstants.integration.test.ts +24 -0
  237. package/src/actions/pol/getRewardVaultDurationConstants.ts +45 -0
  238. package/src/actions/pol/getRewardVaultFromToken.ts +30 -0
  239. package/src/actions/pol/getRewardVaultIncentives.ts +122 -0
  240. package/src/actions/pol/getRewardVaultRewards.ts +109 -0
  241. package/src/actions/pol/getRewardVaultStakingToken.ts +19 -0
  242. package/src/actions/pol/getRewardVaults.ts +106 -0
  243. package/src/actions/pol/getSWBeraVaultMetadata.ts +166 -0
  244. package/src/actions/pol/getSWBeraWithdrawal.ts +165 -0
  245. package/src/actions/pol/getStakeWithdrawalCooldown.ts +21 -0
  246. package/src/actions/pol/getStakedBeraAPR.ts +56 -0
  247. package/src/actions/pol/getStakedBeraSnapshots.ts +48 -0
  248. package/src/actions/pol/getStakingTokenInformation.ts +60 -0
  249. package/src/actions/pol/getUserClaimableIncentives.ts +40 -0
  250. package/src/actions/pol/getUserVaultInfo.ts +52 -0
  251. package/src/actions/pol/getUserVaults.ts +185 -0
  252. package/src/actions/pol/getUserVaultsBalance.ts +54 -0
  253. package/src/actions/pol/getUserVaultsReward.ts +32 -0
  254. package/src/actions/pol/getVaultHistory.ts +43 -0
  255. package/src/actions/pol/getVaultValidators.ts +31 -0
  256. package/src/actions/pol/getVaultsSupply.ts +28 -0
  257. package/src/actions/prices/__tests__/prices.integration.test.ts +29 -0
  258. package/src/actions/prices/getTokenCurrentPrices.ts +79 -0
  259. package/src/actions/pyth/getHoneyPythFeeds.ts +46 -0
  260. package/src/actions/pyth/getUpdateFee.ts +27 -0
  261. package/src/actions/tokens/getAllowances.ts +59 -0
  262. package/src/actions/tokens/getTokenInformation.ts +57 -0
  263. package/src/actions/tokens/getTokens.ts +19 -0
  264. package/src/actions/tokens/getWalletBalances.integration.test.ts +40 -0
  265. package/src/actions/tokens/getWalletBalances.ts +123 -0
  266. package/src/actions/transactions/beraWriteContract.integration.test.ts +251 -0
  267. package/src/actions/transactions/beraWriteContract.ts +722 -0
  268. package/src/actions/validators/getActiveBoostAndCommissionChangeDelay.integration.test.ts +40 -0
  269. package/src/actions/validators/getActiveBoostAndCommissionChangeDelay.ts +109 -0
  270. package/src/actions/validators/getAllValidators.ts +33 -0
  271. package/src/actions/validators/getApiEnrichedAllocation.ts +33 -0
  272. package/src/actions/validators/getApiValidator.ts +38 -0
  273. package/src/actions/validators/getBaselineRewardAllocation.ts +26 -0
  274. package/src/actions/validators/getDailyValidatorBlockStats.ts +37 -0
  275. package/src/actions/validators/getDefaultRewardAllocation.ts +39 -0
  276. package/src/actions/validators/getManagedValidatorByAccount.ts +32 -0
  277. package/src/actions/validators/getStakingPoolBatch.ts +20 -0
  278. package/src/actions/validators/getUserActiveValidators.ts +101 -0
  279. package/src/actions/validators/getUserBoosts.ts +28 -0
  280. package/src/actions/validators/getUserBoostsOnValidator.ts +70 -0
  281. package/src/actions/validators/getUserStakingPositions.ts +225 -0
  282. package/src/actions/validators/getValidatorAnalytics.ts +34 -0
  283. package/src/actions/validators/getValidatorCommission.ts +24 -0
  284. package/src/actions/validators/getValidatorEstimatedBgtPerYear.ts +25 -0
  285. package/src/actions/validators/getValidatorOperatorAddress.ts +27 -0
  286. package/src/actions/validators/getValidatorQueuedCommission.ts +30 -0
  287. package/src/actions/validators/getValidatorQueuedOperatorAddress.ts +32 -0
  288. package/src/actions/validators/getValidatorQueuedRewardAllocation.ts +31 -0
  289. package/src/actions/validators/getValidatorRewardAllocation.ts +158 -0
  290. package/src/actions/validators/getValidatorRewardAllocatorAddress.ts +38 -0
  291. package/src/actions/validators/utils/__tests__/validatorUtils.integration.test.ts +36 -0
  292. package/src/actions/validators/utils/getValidatorBoostApy.ts +59 -0
  293. package/src/contexts/BeraFlags/BeraFlags.tsx +69 -0
  294. package/src/contexts/BeraFlags/defaultFlags.ts +42 -0
  295. package/src/contexts/BexStatusProvider.tsx +125 -0
  296. package/src/contexts/HoneyConfigProvider.tsx +73 -0
  297. package/src/contexts/SwrFallback.tsx +34 -0
  298. package/src/contexts/TokensProvider.tsx +66 -0
  299. package/src/contexts/berajsProvider/context.tsx +49 -0
  300. package/src/contexts/berajsProvider/index.ts +2 -0
  301. package/src/contexts/berajsProvider/useBeraJs.ts +18 -0
  302. package/src/contexts/block-time-provider.tsx +34 -0
  303. package/src/contexts/index.ts +14 -0
  304. package/src/data/contracts.ts +398 -0
  305. package/src/enum/dex.ts +10 -0
  306. package/src/enum/index.ts +4 -0
  307. package/src/enum/polling.ts +8 -0
  308. package/src/enum/slippage.ts +19 -0
  309. package/src/enum/txnEnum.ts +101 -0
  310. package/src/errors/BeraError.ts +202 -0
  311. package/src/errors/InvalidArgumentError.ts +81 -0
  312. package/src/errors/NotFoundError.ts +34 -0
  313. package/src/errors/RequestError.ts +207 -0
  314. package/src/errors/TransactionFailedError.ts +93 -0
  315. package/src/errors/assert.ts +144 -0
  316. package/src/errors/assert.unit.test.ts +110 -0
  317. package/src/errors/errorMap.ts +1157 -0
  318. package/src/errors/getErrorMessage.ts +40 -0
  319. package/src/errors/getRevertReason.integration.test.ts +449 -0
  320. package/src/errors/getRevertReason.ts +179 -0
  321. package/src/errors/index.ts +8 -0
  322. package/src/errors/initBeraError.ts +46 -0
  323. package/src/errors/parseViemError.ts +239 -0
  324. package/src/hooks/bend/useGetConvertToAssets.ts +42 -0
  325. package/src/hooks/dex/useAggregatorsQuotes.ts +502 -0
  326. package/src/hooks/dex/useAggregatorsRouterFeeBps.ts +34 -0
  327. package/src/hooks/dex/useAllUserPools.ts +38 -0
  328. package/src/hooks/dex/useApiPool.ts +53 -0
  329. package/src/hooks/dex/useBexStatus.ts +1 -0
  330. package/src/hooks/dex/useCreatePool.ts +437 -0
  331. package/src/hooks/dex/useExploitedTokens.ts +57 -0
  332. package/src/hooks/dex/useGlobalLiquidityAndSwapVolume.ts +22 -0
  333. package/src/hooks/dex/useLiquidityMismatch.ts +195 -0
  334. package/src/hooks/dex/useLpPosition.ts +115 -0
  335. package/src/hooks/dex/useMultipleTokenApprovalsWithSlippage.ts +92 -0
  336. package/src/hooks/dex/useOnChainPoolData.ts +126 -0
  337. package/src/hooks/dex/usePollPoolCreationRelayerApproval.ts +54 -0
  338. package/src/hooks/dex/usePollVaultIsPaused.ts +30 -0
  339. package/src/hooks/dex/usePool.ts +59 -0
  340. package/src/hooks/dex/usePoolEvents.ts +22 -0
  341. package/src/hooks/dex/usePoolHistoricalData.ts +44 -0
  342. package/src/hooks/dex/usePools.ts +83 -0
  343. package/src/hooks/enso/useBendTransactionRequests.ts +247 -0
  344. package/src/hooks/enso/useEnsoZap.ts +166 -0
  345. package/src/hooks/enso/useZapTransactionRequests.ts +81 -0
  346. package/src/hooks/governance/useCreateProposal.ts +88 -0
  347. package/src/hooks/governance/useGetPastVotes.ts +63 -0
  348. package/src/hooks/governance/useHasVoted.ts +38 -0
  349. package/src/hooks/governance/useIsCanceller.ts +43 -0
  350. package/src/hooks/governance/usePollAllProposals.ts +232 -0
  351. package/src/hooks/governance/usePollProposal.ts +96 -0
  352. package/src/hooks/governance/usePollProposalThreshold.ts +41 -0
  353. package/src/hooks/governance/usePollProposalVotes.ts +64 -0
  354. package/src/hooks/governance/usePollUserDelegates.ts +70 -0
  355. package/src/hooks/governance/useProposalFromTx.ts +37 -0
  356. package/src/hooks/governance/useProposalSnapshot.ts +44 -0
  357. package/src/hooks/governance/useProposalState.ts +48 -0
  358. package/src/hooks/governance/useProposalTimelockState.ts +61 -0
  359. package/src/hooks/governance/useQuorum.ts +42 -0
  360. package/src/hooks/honey/useCappedGlobally.ts +92 -0
  361. package/src/hooks/honey/useCappedRelatively.ts +88 -0
  362. package/src/hooks/honey/useCollateralWeights.ts +48 -0
  363. package/src/hooks/honey/useHoneyAlerts.ts +314 -0
  364. package/src/hooks/honey/useHoneyBalances.ts +79 -0
  365. package/src/hooks/honey/useHoneyChartData.ts +17 -0
  366. package/src/hooks/honey/useHoneyGlobalData.ts +22 -0
  367. package/src/hooks/honey/useHoneySwapState.ts +202 -0
  368. package/src/hooks/honey/useHoneyVaultsBalance.ts +41 -0
  369. package/src/hooks/honey/useIsBadCollateralAsset.ts +73 -0
  370. package/src/hooks/honey/useIsBasketModeEnabled.ts +70 -0
  371. package/src/hooks/honey/usePythLatestPrices.ts +41 -0
  372. package/src/hooks/index.ts +124 -0
  373. package/src/hooks/perps/usePythUpdateFee.ts +43 -0
  374. package/src/hooks/pol/useBgtAprSimulation.ts +22 -0
  375. package/src/hooks/pol/useBgtUnstakedBalance.ts +50 -0
  376. package/src/hooks/pol/useClaimableFees.ts +38 -0
  377. package/src/hooks/pol/useEarnedStakedBeraVault.ts +59 -0
  378. package/src/hooks/pol/useHighestVaultsAPR.ts +57 -0
  379. package/src/hooks/pol/useOnChainRewardVault.ts +114 -0
  380. package/src/hooks/pol/usePollGlobalData.ts +31 -0
  381. package/src/hooks/pol/usePollMarkets.ts +32 -0
  382. package/src/hooks/pol/useQueuedBeraUnlock.ts +70 -0
  383. package/src/hooks/pol/useRewardTokenToBeraRate.ts +45 -0
  384. package/src/hooks/pol/useRewardVault.ts +51 -0
  385. package/src/hooks/pol/useRewardVaultBalanceFromStakingToken.ts +82 -0
  386. package/src/hooks/pol/useRewardVaultFromToken.ts +58 -0
  387. package/src/hooks/pol/useRewardVaultIncentives.ts +37 -0
  388. package/src/hooks/pol/useRewardVaultRewards.ts +37 -0
  389. package/src/hooks/pol/useRewardVaults.ts +45 -0
  390. package/src/hooks/pol/useStakedBeraSnapshots.ts +27 -0
  391. package/src/hooks/pol/useUserVaultInfo.ts +46 -0
  392. package/src/hooks/pol/useUserVaults.ts +40 -0
  393. package/src/hooks/pol/useVaultAddress.ts +31 -0
  394. package/src/hooks/pol/useVaultHistory.ts +29 -0
  395. package/src/hooks/pol/useVaultValidators.ts +33 -0
  396. package/src/hooks/tokens/useBeraCurrentPrice.ts +12 -0
  397. package/src/hooks/tokens/useMultipleTokenInformation.ts +62 -0
  398. package/src/hooks/tokens/usePollAllowances.ts +60 -0
  399. package/src/hooks/tokens/usePollBalance.ts +113 -0
  400. package/src/hooks/tokens/usePollWalletBalances.ts +75 -0
  401. package/src/hooks/tokens/useStakingTokenInformation.ts +54 -0
  402. package/src/hooks/tokens/useTokenCurrentPrice.ts +19 -0
  403. package/src/hooks/tokens/useTokenCurrentPrices.ts +57 -0
  404. package/src/hooks/tokens/useTokenInformation.ts +59 -0
  405. package/src/hooks/tokens/useTokens.ts +26 -0
  406. package/src/hooks/tokens/useTotalSupply.ts +22 -0
  407. package/src/hooks/transactions/TransactionStoreContext.tsx +59 -0
  408. package/src/hooks/transactions/transactionStore.ts +275 -0
  409. package/src/hooks/transactions/transactionStore.unit.test.ts +508 -0
  410. package/src/hooks/transactions/useAddRecentTransaction.ts +29 -0
  411. package/src/hooks/transactions/useRecentTransactions.ts +27 -0
  412. package/src/hooks/useBlockToTimestamp.ts +51 -0
  413. package/src/hooks/useContractWrite/index.ts +2 -0
  414. package/src/hooks/useContractWrite/stateReducer.ts +60 -0
  415. package/src/hooks/useContractWrite/types.ts +88 -0
  416. package/src/hooks/useContractWrite/useBeraContractWrite.ts +137 -0
  417. package/src/hooks/useGetVerifiedAbi.ts +47 -0
  418. package/src/hooks/useHistoryState.ts +64 -0
  419. package/src/hooks/usePrevious.ts +13 -0
  420. package/src/hooks/validators/useAllValidators.ts +48 -0
  421. package/src/hooks/validators/useApiEnrichedAllocation.ts +17 -0
  422. package/src/hooks/validators/useApiValidator.ts +37 -0
  423. package/src/hooks/validators/useBaselineRewardAllocation.ts +16 -0
  424. package/src/hooks/validators/useDailyValidatorBlockStats.ts +35 -0
  425. package/src/hooks/validators/useDefaultRewardAllocation.ts +32 -0
  426. package/src/hooks/validators/useManagedValidatorByAccount.ts +40 -0
  427. package/src/hooks/validators/useManagedValidatorRole.ts +119 -0
  428. package/src/hooks/validators/useOnChainValidator.ts +66 -0
  429. package/src/hooks/validators/useStakingPoolBatch.ts +45 -0
  430. package/src/hooks/validators/useSubgraphUserValidators.ts +36 -0
  431. package/src/hooks/validators/useUserActiveValidators.ts +44 -0
  432. package/src/hooks/validators/useUserBoostsOnValidator.ts +45 -0
  433. package/src/hooks/validators/useUserClaimableIncentives.ts +149 -0
  434. package/src/hooks/validators/useUserStakingPositions.ts +62 -0
  435. package/src/hooks/validators/useValidator.ts +83 -0
  436. package/src/hooks/validators/useValidatorAnalytics.ts +36 -0
  437. package/src/hooks/validators/useValidatorCommission.ts +36 -0
  438. package/src/hooks/validators/useValidatorEstimatedBgtPerYear.ts +24 -0
  439. package/src/hooks/validators/useValidatorQueuedCommission.ts +41 -0
  440. package/src/hooks/validators/useValidatorQueuedOperatorAddress.ts +39 -0
  441. package/src/hooks/validators/useValidatorQueuedRewardAllocation.ts +37 -0
  442. package/src/hooks/validators/useValidatorRewardAllocation.ts +36 -0
  443. package/src/hooks/validators/validatorRoleManager.ts +0 -0
  444. package/src/types/bribe-boost.ts +50 -0
  445. package/src/types/dex.ts +196 -0
  446. package/src/types/enso.ts +42 -0
  447. package/src/types/global.ts +230 -0
  448. package/src/types/governance.ts +174 -0
  449. package/src/types/honeySwap.ts +35 -0
  450. package/src/types/index.ts +8 -0
  451. package/src/types/pol.ts +52 -0
  452. package/src/types/staking.ts +7 -0
  453. package/src/utils/beraFetch.ts +55 -0
  454. package/src/utils/constants.ts +35 -0
  455. package/src/utils/formatAggregatorsQuotes.ts +57 -0
  456. package/src/utils/formatAggregatorsQuotes.unit.test.ts +191 -0
  457. package/src/utils/formatIncentivesTokenMap.ts +19 -0
  458. package/src/utils/formatInputTokenValue.ts +12 -0
  459. package/src/utils/formatNumber.ts +225 -0
  460. package/src/utils/formatNumber.unit.test.ts +245 -0
  461. package/src/utils/formatTimestamps.ts +155 -0
  462. package/src/utils/formatTimestamps.unit.test.ts +395 -0
  463. package/src/utils/formatTokenList.ts +61 -0
  464. package/src/utils/formatUsd.ts +46 -0
  465. package/src/utils/getContractName.ts +27 -0
  466. package/src/utils/getContractName.unit.test.ts +64 -0
  467. package/src/utils/getErrorResponse.ts +20 -0
  468. package/src/utils/getHoneySwapMessage.test.ts +116 -0
  469. package/src/utils/getHoneySwapMessage.ts +70 -0
  470. package/src/utils/getServerSideClient.ts +49 -0
  471. package/src/utils/getTestClient.ts +19 -0
  472. package/src/utils/hasAnyTruthyValues.ts +17 -0
  473. package/src/utils/index.ts +27 -0
  474. package/src/utils/isDifferenceExceedingThreshold.ts +13 -0
  475. package/src/utils/isSubgraphStale.ts +10 -0
  476. package/src/utils/math.ts +28 -0
  477. package/src/utils/math.unit.test.ts +49 -0
  478. package/src/utils/parseBaseArgs.ts +23 -0
  479. package/src/utils/parseBaseArgs.unit.test.ts +55 -0
  480. package/src/utils/poolNamings.ts +54 -0
  481. package/src/utils/pythHoneyUtils.ts +3 -0
  482. package/src/utils/sentryBeforeSend.ts +52 -0
  483. package/src/utils/test-utils.ts +2 -0
  484. package/src/utils/time.ts +106 -0
  485. package/src/utils/time.unit.test.ts +76 -0
  486. package/src/utils/tokenWrapping.ts +16 -0
  487. package/src/utils/tokens.ts +106 -0
  488. package/src/utils/tokens.unit.test.ts +98 -0
  489. package/src/utils/truncateHash.ts +20 -0
  490. package/src/utils/truncateHash.unit.test.ts +19 -0
  491. package/src/utils/validatorUptimeStatus.ts +30 -0
  492. package/dist/chunk-3V2MGZRD.mjs +0 -2
  493. package/dist/chunk-4E5JGLUM.cjs +0 -1
  494. package/dist/chunk-AHAZC5K3.mjs +0 -1
  495. package/dist/chunk-ELUMRVNE.mjs +0 -1
  496. package/dist/chunk-JHCSZSJS.mjs +0 -1
  497. package/dist/chunk-KIA6GUIN.cjs +0 -1
  498. package/dist/chunk-LX5CM4Y6.cjs +0 -1
  499. package/dist/chunk-LZPBQLKP.mjs +0 -3
  500. package/dist/chunk-M5BKM52U.cjs +0 -1
  501. package/dist/chunk-MTBICGRP.mjs +0 -1
  502. package/dist/chunk-WDFIETJS.cjs +0 -2
@@ -0,0 +1,722 @@
1
+ import {
2
+ getPublicClient,
3
+ getWalletClient,
4
+ type SwitchChainErrorType,
5
+ switchChain,
6
+ } from "@wagmi/core";
7
+ import {
8
+ type Abi,
9
+ BaseError,
10
+ type ContractFunctionArgs,
11
+ type ContractFunctionName,
12
+ createWalletClient,
13
+ decodeErrorResult,
14
+ type GetTransactionCountReturnType,
15
+ type Hex,
16
+ HttpRequestError,
17
+ http,
18
+ type TransactionReceipt,
19
+ type WalletCallReceipt,
20
+ } from "viem";
21
+
22
+ import { ChainId, defaultChainId } from "@berachain/config/internal";
23
+
24
+ import { commonAbiErrors } from "~/abi/utils/commonAbiErrors";
25
+ import { assertPublicClient } from "~/errors/assert";
26
+ import { BeraError } from "~/errors/BeraError";
27
+ import { getRevertReason } from "~/errors/getRevertReason";
28
+ import { InvalidArgumentError } from "~/errors/InvalidArgumentError";
29
+ import { parseDecodedError, parseViemError } from "~/errors/parseViemError";
30
+ import { RequestError } from "~/errors/RequestError";
31
+ import { TransactionFailedError } from "~/errors/TransactionFailedError";
32
+ import type { IContractWrite } from "~/types";
33
+ import { DEFAULT_METAMASK_GAS_LIMIT } from "~/utils/constants";
34
+ import { getTestClient } from "~/utils/getTestClient";
35
+ import { seconds } from "~/utils/time";
36
+
37
+ /**
38
+ * Setting 1 seconds as refresh interval since block time is 2 seconds.
39
+ *
40
+ * Keeping this longer will increase the time to wait for the transaction to be confirmed.
41
+ * If tx is confirmed after 0.5 seconds, the user would have to wait for 1.5 seconds more before UI is updated.
42
+ * 1 seconds seems a good compromise. A shorter interval
43
+ */
44
+ const defaultPollingInterval = seconds(1);
45
+
46
+ const increaseByPercentage = (value: bigint, percentage: number) => {
47
+ return (value * (100n + BigInt(percentage))) / 100n;
48
+ };
49
+
50
+ /**
51
+ * This helps prevent IntegerOutOfRangeError by not providing a nonce if it's too large.
52
+ *
53
+ * Despite this should not be possible, some users have experienced this error. We're collecting
54
+ * the error and the nonce value to help debug the issue.
55
+ */
56
+ function parseNonce({
57
+ nonceResult,
58
+ onWarning,
59
+ }: {
60
+ nonceResult: PromiseSettledResult<GetTransactionCountReturnType | undefined>;
61
+ onWarning?: (e: BeraError) => void;
62
+ }): number | undefined {
63
+ if (nonceResult.status === "fulfilled") {
64
+ if (nonceResult.value && nonceResult.value > Number.MAX_SAFE_INTEGER) {
65
+ // This happened only to one user that manually set a very high nonce.
66
+ // This is very unlikely to happen if we get the nonce from the latest block instead of pending.
67
+ onWarning?.(
68
+ new BeraError({
69
+ message:
70
+ "Nonce is too large. No nonce will be provided to avoid IntegerOutOfRangeError.",
71
+ cause: nonceResult.value,
72
+ level: "debug",
73
+ }),
74
+ );
75
+ return undefined;
76
+ }
77
+
78
+ return nonceResult.value;
79
+ }
80
+ return undefined;
81
+ }
82
+
83
+ export function isImpersonateAccount(chainId: number = defaultChainId) {
84
+ return chainId === ChainId.BEPOLIA
85
+ ? process.env.NEXT_PUBLIC_80069_ANVIL_IMPERSONATE_ACCOUNT === "true"
86
+ : process.env.NEXT_PUBLIC_80094_ANVIL_IMPERSONATE_ACCOUNT === "true";
87
+ }
88
+
89
+ export async function beraWriteContract<
90
+ abi extends Abi | undefined = undefined,
91
+ functionName extends abi extends Abi
92
+ ? ContractFunctionName<abi>
93
+ : never = abi extends Abi ? ContractFunctionName<abi> : never,
94
+ params extends abi extends Abi
95
+ ? ContractFunctionArgs<abi, "payable" | "nonpayable", functionName>
96
+ : never = abi extends Abi
97
+ ? ContractFunctionArgs<abi, "payable" | "nonpayable", functionName>
98
+ : never,
99
+ >(
100
+ args: IContractWrite<abi, functionName, params> & {
101
+ enableSimulateCalls?: boolean;
102
+ chainId?: number;
103
+ impersonateAccount?: boolean;
104
+ /**
105
+ * The polling interval in milliseconds to fetch the on-chain transaction status
106
+ * @default 1000 (1 second)
107
+ */
108
+ pollingInterval?: number;
109
+ /**
110
+ * The timeout in milliseconds to wait for the transaction to be confirmed.
111
+ * @default 120_000 (2 minutes)
112
+ */
113
+ txConfirmationTimeout?: number;
114
+ },
115
+ ): Promise<
116
+ | TransactionReceipt
117
+ | WalletCallReceipt<bigint, "success" | "reverted">
118
+ | undefined
119
+ > {
120
+ if (
121
+ process.env.NODE_ENV === "development" &&
122
+ !args.contractName &&
123
+ // let's avoid this for eip5972 calls
124
+ args.address &&
125
+ !("calls" in args || args.calls)
126
+ ) {
127
+ console.warn(
128
+ "beraWriteContract: Contract name is required. Either update the contract mapping in devrel or provide a contract name in ContractName enum",
129
+ "This is a development error and will be thrown in development mode only.",
130
+ );
131
+
132
+ throw new InvalidArgumentError({
133
+ property: "contractName",
134
+ value: args.contractName,
135
+ expected: "ContractName",
136
+ displayMessage: `[DEV] Contract name is required as it's not mapped in the devrel's contract mapping ${args.address}`,
137
+ level: "error",
138
+ });
139
+ }
140
+
141
+ const {
142
+ address,
143
+ calls,
144
+ value,
145
+ gasLimit,
146
+ onLoading,
147
+ onSuccess,
148
+ onWarning,
149
+ onError,
150
+ onSubmission,
151
+ wagmiConfig,
152
+ walletClient: _walletClient,
153
+ account: argsAccount,
154
+ txConfirmationTimeout = 120_000,
155
+ chainId = defaultChainId,
156
+ impersonateAccount = isImpersonateAccount(chainId),
157
+ errorsAbi: _errorsAbi = [],
158
+ pollingInterval = defaultPollingInterval,
159
+ ...rest
160
+ } = args;
161
+
162
+ onLoading?.(args);
163
+
164
+ const data = "data" in rest ? (rest.data as Hex) : undefined;
165
+
166
+ const globalAbi = [
167
+ ...commonAbiErrors,
168
+ ..._errorsAbi,
169
+ ...("abi" in rest && rest.abi && Array.isArray(rest.abi)
170
+ ? (rest.abi as unknown as Abi)
171
+ : []),
172
+ ] as unknown as abi extends undefined ? Abi : abi;
173
+
174
+ let walletClient = _walletClient;
175
+
176
+ if (!wagmiConfig) {
177
+ onError?.(
178
+ new InvalidArgumentError({
179
+ property: "wagmiConfig",
180
+ value: wagmiConfig,
181
+ expected: "Config",
182
+ }),
183
+ args,
184
+ );
185
+ return;
186
+ }
187
+
188
+ const isLocalAccount = walletClient?.account?.type === "local";
189
+
190
+ if (!isLocalAccount) {
191
+ try {
192
+ /**
193
+ * This is needed because if we don't wrap the button into an ActionButton, or the connector doesn't warn us that the chain
194
+ * is not synced with the dapp, we'll get a ChainMismatchError.
195
+ */
196
+ await switchChain(wagmiConfig, { chainId });
197
+ } catch (e) {
198
+ const error = e as SwitchChainErrorType;
199
+ onError?.(
200
+ new BeraError({
201
+ cause: error,
202
+ displayMessage: "There was an error switching to the chain.",
203
+ }),
204
+ args,
205
+ );
206
+ return;
207
+ }
208
+ }
209
+
210
+ if (!walletClient) {
211
+ try {
212
+ walletClient = await getWalletClient(wagmiConfig, { chainId });
213
+ } catch (e) {
214
+ onError?.(
215
+ new BeraError({
216
+ cause: e,
217
+ displayMessage: "There was an error connecting to the wallet.",
218
+ }),
219
+ args,
220
+ );
221
+ return;
222
+ }
223
+ }
224
+
225
+ if (!walletClient) {
226
+ onError?.(
227
+ new InvalidArgumentError({
228
+ displayMessage: "There was an error connecting to the wallet.",
229
+ property: "walletClient",
230
+ value: walletClient,
231
+ expected: "WalletClient",
232
+ }),
233
+ args,
234
+ );
235
+ return;
236
+ }
237
+
238
+ const account = argsAccount || walletClient.account;
239
+
240
+ if (!account) {
241
+ onError?.(
242
+ new InvalidArgumentError({
243
+ property: "account",
244
+ value: account,
245
+ expected: "Account",
246
+ }),
247
+ args,
248
+ );
249
+ return;
250
+ }
251
+
252
+ const accountAddress =
253
+ typeof account === "string" ? account : account.address;
254
+
255
+ const publicClient = getPublicClient(wagmiConfig, {
256
+ chainId,
257
+ });
258
+
259
+ try {
260
+ assertPublicClient(publicClient);
261
+ } catch (error) {
262
+ onError?.(error as InvalidArgumentError, args);
263
+ return;
264
+ }
265
+
266
+ if (impersonateAccount) {
267
+ const testClient = getTestClient(chainId);
268
+ await testClient.impersonateAccount({
269
+ address: accountAddress,
270
+ });
271
+
272
+ walletClient = createWalletClient({
273
+ transport: http(testClient.transport.url),
274
+ account,
275
+ chain: walletClient.chain,
276
+ });
277
+ }
278
+ /**
279
+ * If tx is submitted, this will be assigned the tx hash
280
+ */
281
+ let txHash: Hex | undefined;
282
+ /**
283
+ * This is just needed for the error object
284
+ */
285
+ const input = {
286
+ calls: "calls" in rest ? rest.calls : undefined,
287
+ params: "params" in rest ? rest.params : undefined,
288
+ value,
289
+ data: "data" in rest ? (rest.data as Hex) : undefined,
290
+ } as const;
291
+
292
+ try {
293
+ /**
294
+ * Nonces are security-critical for preventing replay attacks.
295
+ */
296
+ const noncePromise = publicClient
297
+ .getTransactionCount({
298
+ address: accountAddress,
299
+ /**
300
+ * We won't use pending any more because if someone previously send a txn with a higher nonce, this won't be executed
301
+ * unless all the previous nonces are executed.
302
+ *
303
+ * This will lead to a UX issue where the user thinks the txn was not sent because it's pending forever.
304
+ *
305
+ * The only case where pending is needed is if chain is halted or the user is sending multiple transactions at the same time.
306
+ * However, it's less likely to happen.
307
+ *
308
+ * A lot of waitForTransactionReceipt were timing out, and maybe it was related to that.
309
+ */
310
+ blockTag: "latest",
311
+ })
312
+ // This must be caught to prevent the promise from rejecting before being awaited
313
+ .catch(() => undefined);
314
+
315
+ /**
316
+ * These are properties that are shared between the simulation and gas estimation whether we have
317
+ */
318
+ const sharedTxProperties = {
319
+ account: accountAddress,
320
+ to: address,
321
+ address,
322
+ chainId,
323
+ value,
324
+ } as const;
325
+
326
+ if (calls) {
327
+ const parsedCalls = calls.map((call) => {
328
+ if ("abi" in call && Array.isArray(globalAbi)) {
329
+ globalAbi.push(...(call.abi as Abi));
330
+ }
331
+
332
+ if ("data" in call) {
333
+ return {
334
+ to: call.address,
335
+ data: call.data as Hex,
336
+ value: call.value ?? 0n,
337
+ };
338
+ }
339
+ if ("abi" in call && "functionName" in call) {
340
+ const params = "params" in call ? call.params : undefined;
341
+
342
+ return {
343
+ to: call.address,
344
+ abi: call.abi as Abi,
345
+ functionName: call.functionName as ContractFunctionName<Abi>,
346
+ args: params as readonly unknown[],
347
+ value: call.value ?? 0n,
348
+ };
349
+ }
350
+ return {
351
+ to: call.address,
352
+ value: call.value ?? 0n,
353
+ };
354
+ });
355
+
356
+ if (args.enableSimulateCalls) {
357
+ const res = await publicClient.simulateCalls({
358
+ calls: parsedCalls,
359
+ account,
360
+ });
361
+
362
+ const failedCallIndex = res.results.findIndex(
363
+ (result) => result.status === "failure",
364
+ );
365
+ if (failedCallIndex !== -1) {
366
+ if (res.block.number === null) {
367
+ throw new InvalidArgumentError({
368
+ property: "blockNumber",
369
+ value: res.block.number,
370
+ expected: "bigint",
371
+ message: "Block number is null",
372
+ });
373
+ }
374
+
375
+ const failedCall = res.results[failedCallIndex];
376
+
377
+ const { reason: decodedReason } =
378
+ failedCall.data && failedCall.data !== "0x"
379
+ ? {
380
+ reason: parseDecodedError(
381
+ decodeErrorResult({
382
+ data: failedCall.data,
383
+ abi: globalAbi,
384
+ }),
385
+ ),
386
+ }
387
+ : parseViemError({
388
+ error: failedCall.error as Error,
389
+ revertIfUnknown: false,
390
+ abi: globalAbi,
391
+ });
392
+
393
+ onError?.(
394
+ new TransactionFailedError({
395
+ cause: failedCall.error,
396
+ txHash,
397
+ chainId,
398
+ reason: decodedReason,
399
+ abi: globalAbi,
400
+ input,
401
+ blockNumber: res.block.number,
402
+ }),
403
+ args,
404
+ );
405
+ return;
406
+ }
407
+ }
408
+
409
+ if (process.env.NODE_ENV === "test") {
410
+ // sendCalls uses EIP-5972 so that the wallet provider can send batch calls with EIP-7702
411
+ // @todo We can simulate it using the EIP-7702StatelessDelegator ABI, assuming the wallet has sent authorized metamask's contract
412
+ throw new InvalidArgumentError({
413
+ displayMessage: "Batch calls are not supported in test environment",
414
+ expected: "not test",
415
+ property: "NODE_ENV",
416
+ value: process.env.NODE_ENV,
417
+ level: "fatal",
418
+ });
419
+ }
420
+
421
+ const request = await walletClient.sendCalls({
422
+ calls: parsedCalls,
423
+ account,
424
+ // This is needed to ensure that the transaction is atomic
425
+ // If this was not set, we might have a list of receipts instead of a single one
426
+ forceAtomic: true,
427
+ });
428
+
429
+ /**
430
+ * This is different from the other onSubmission callbacks
431
+ * however `request` does not have the txHash, but just the request id to the wallet provider
432
+ * Wallet provider should support the showCallStatus method. MM does not atm.
433
+ */
434
+ onSubmission?.({ id: request.id }, args);
435
+
436
+ // If we get the call status right after the call is submitted, it will be pending
437
+ // Receipts will be empty
438
+ // Here's an example of the response:
439
+ // {
440
+ // "id": "0x939c0c8b9fd546878b042c8232802450",
441
+ // "status": "pending",
442
+ // "atomic": true,
443
+ // "chainId": 80069,
444
+ // "receipts": [],
445
+ // "statusCode": 100,
446
+ // "version": "2.0.0"
447
+ // }
448
+ // const callStatusPending = await walletClient.getCallsStatus({
449
+ // id: request.id,
450
+ // });
451
+
452
+ // This will wait for the transaction to be submitted to the chain
453
+ const callStatus = await walletClient.waitForCallsStatus({
454
+ id: request.id,
455
+ pollingInterval,
456
+ timeout: txConfirmationTimeout,
457
+ });
458
+
459
+ if (callStatus.status === "success") {
460
+ const receipt = callStatus.receipts?.[0];
461
+
462
+ if (!receipt) {
463
+ // This should never happen
464
+ throw new BeraError({
465
+ message: "Call status is success but no receipt was found",
466
+ cause: callStatus,
467
+ level: "error",
468
+ reason: "CALL_STATUS_SUCCESS_BUT_NO_RECEIPT",
469
+ });
470
+ }
471
+
472
+ onSuccess?.(receipt, args);
473
+ return receipt;
474
+ }
475
+
476
+ console.error("wallet_sendCalls: Call failed", callStatus);
477
+
478
+ throw new TransactionFailedError({
479
+ cause: callStatus,
480
+ level: "error",
481
+ input,
482
+ txHash,
483
+ abi: globalAbi,
484
+ chainId,
485
+ });
486
+ }
487
+
488
+ if (data) {
489
+ // In this scenario data was already encoded by external library
490
+ // and we can use it directly. If ABI was provided, errors will be decoded by parseViemError
491
+ // This might also be just a value transaction
492
+
493
+ // Add gas estimation for direct transactions
494
+ const [simulationResult, estimatedGas, nonceResult] =
495
+ await Promise.allSettled([
496
+ publicClient.call({
497
+ ...sharedTxProperties,
498
+ data,
499
+ }),
500
+ gasLimit ??
501
+ publicClient.estimateGas({
502
+ ...sharedTxProperties,
503
+ data,
504
+ }),
505
+ noncePromise,
506
+ ]);
507
+
508
+ if (simulationResult.status === "rejected") {
509
+ throw simulationResult.reason;
510
+ }
511
+
512
+ const gas =
513
+ estimatedGas.status === "fulfilled"
514
+ ? increaseByPercentage(estimatedGas.value, 10)
515
+ : DEFAULT_METAMASK_GAS_LIMIT;
516
+
517
+ txHash = await walletClient.sendTransaction({
518
+ data,
519
+ to: address,
520
+ account,
521
+ gas,
522
+ chainId,
523
+ nonce: parseNonce({
524
+ nonceResult,
525
+ onWarning(e) {
526
+ onWarning?.(e, args);
527
+ },
528
+ }),
529
+ value,
530
+ });
531
+ onSubmission?.({ txHash }, args);
532
+ } else if ("abi" in rest && "functionName" in rest) {
533
+ const { abi, functionName } = rest as {
534
+ abi: NonNullable<abi>;
535
+ functionName: NonNullable<functionName>;
536
+ };
537
+
538
+ const params = "params" in rest ? rest.params : undefined;
539
+ // Run simulation and gas estimation in parallel
540
+ // TODO: figure out clean way to early detect errors and effectively show them on the UI
541
+ const [simulationResult, gasEstimateResult, nonceResult] =
542
+ await Promise.allSettled([
543
+ // @ts-expect-error nested type issues. Be careful when changing this.
544
+ publicClient.simulateContract({
545
+ ...sharedTxProperties,
546
+ abi: globalAbi as typeof abi,
547
+ functionName,
548
+ args: params,
549
+ account,
550
+ }),
551
+ // Only estimate gas if no gasLimit is provided
552
+ gasLimit ??
553
+ // @ts-expect-error nested type issues. Be careful when changing this.
554
+ publicClient.estimateContractGas({
555
+ ...sharedTxProperties,
556
+ abi: globalAbi,
557
+ functionName,
558
+ args: params,
559
+ }),
560
+ noncePromise,
561
+ ]);
562
+
563
+ if (simulationResult.status === "rejected") {
564
+ throw simulationResult.reason;
565
+ }
566
+
567
+ const estimatedGas =
568
+ gasLimit ??
569
+ (gasEstimateResult.status === "fulfilled"
570
+ ? increaseByPercentage(gasEstimateResult.value, 10)
571
+ : DEFAULT_METAMASK_GAS_LIMIT);
572
+
573
+ // @ts-expect-error nested type issues. Be careful when changing this.
574
+ txHash = await walletClient.writeContract({
575
+ ...simulationResult.value.request,
576
+ chainId,
577
+ gas: estimatedGas,
578
+ nonce: parseNonce({
579
+ nonceResult,
580
+ onWarning(e) {
581
+ onWarning?.(e, args);
582
+ },
583
+ }),
584
+ });
585
+ onSubmission?.({ txHash }, args);
586
+ } else {
587
+ const [nonceResult] = await Promise.allSettled([noncePromise]);
588
+ txHash = await walletClient.sendTransaction({
589
+ chainId,
590
+ to: address,
591
+ value,
592
+ account,
593
+ nonce: parseNonce({
594
+ nonceResult,
595
+ onWarning(e) {
596
+ onWarning?.(e, args);
597
+ },
598
+ }),
599
+ });
600
+ onSubmission?.({ txHash }, args);
601
+ }
602
+
603
+ if (process.env.NODE_ENV === "test") {
604
+ const testClient = getTestClient(chainId);
605
+ await testClient.mine({ blocks: 1 });
606
+ }
607
+
608
+ const confirmationReceipt = await publicClient.waitForTransactionReceipt({
609
+ hash: txHash,
610
+ pollingInterval,
611
+ timeout: txConfirmationTimeout,
612
+ confirmations: 1,
613
+ });
614
+
615
+ if (confirmationReceipt.status === "success") {
616
+ onSuccess?.(confirmationReceipt, args);
617
+ } else {
618
+ const revertReason = await getRevertReason({
619
+ publicClient,
620
+ blockNumber: confirmationReceipt.blockNumber,
621
+ to: address,
622
+ account: accountAddress,
623
+ abi: globalAbi,
624
+ args:
625
+ "params" in rest ? (rest.params as readonly unknown[]) : undefined,
626
+ functionName:
627
+ "functionName" in rest
628
+ ? (rest.functionName as functionName)
629
+ : undefined,
630
+ value,
631
+ data,
632
+ });
633
+
634
+ if (process.env.NODE_ENV === "test") {
635
+ console.error("beraWriteContract: Revert reason", revertReason);
636
+ }
637
+
638
+ onError?.(
639
+ new TransactionFailedError({
640
+ reason: revertReason.reason,
641
+ txHash,
642
+ receipt: confirmationReceipt,
643
+ to: address,
644
+ functionName:
645
+ "functionName" in rest
646
+ ? (rest.functionName as functionName)
647
+ : undefined,
648
+ input,
649
+ chainId,
650
+ abi: globalAbi,
651
+ blockNumber: confirmationReceipt.blockNumber,
652
+ }),
653
+ args,
654
+ );
655
+ }
656
+ return confirmationReceipt;
657
+ } catch (e) {
658
+ if (e instanceof BeraError) {
659
+ onError?.(e, args);
660
+ } else if (e instanceof Error) {
661
+ const viemError = parseViemError({
662
+ error: e,
663
+ revertIfUnknown: false,
664
+ abi: globalAbi,
665
+ });
666
+
667
+ if (
668
+ viemError &&
669
+ viemError.rootCause instanceof BaseError &&
670
+ "url" in viemError.rootCause
671
+ ) {
672
+ onError?.(
673
+ new RequestError({
674
+ cause: viemError.rootCause,
675
+ response: undefined,
676
+ endpoint: {
677
+ url: viemError.rootCause.url as string,
678
+ type: "rpc",
679
+ },
680
+ statusCode:
681
+ viemError.rootCause instanceof HttpRequestError
682
+ ? viemError.rootCause.status
683
+ : undefined,
684
+ }),
685
+ args,
686
+ );
687
+ return;
688
+ }
689
+ onError?.(
690
+ new TransactionFailedError({
691
+ cause: e,
692
+ to: address,
693
+ txHash,
694
+ chainId,
695
+ abi: globalAbi,
696
+ functionName:
697
+ "functionName" in rest
698
+ ? (rest.functionName as functionName)
699
+ : undefined,
700
+ input,
701
+ }),
702
+ args,
703
+ );
704
+ } else {
705
+ onError?.(
706
+ new TransactionFailedError({
707
+ cause: e,
708
+ txHash,
709
+ to: address,
710
+ chainId,
711
+ functionName:
712
+ "functionName" in rest
713
+ ? (rest.functionName as functionName)
714
+ : undefined,
715
+ input,
716
+ abi: globalAbi,
717
+ }),
718
+ args,
719
+ );
720
+ }
721
+ }
722
+ }