@mezo-org/passport 0.4.0-dev.7 → 0.4.0-dev.71

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 (255) hide show
  1. package/README.md +18 -22
  2. package/dist/src/api/auth.d.ts +7 -3
  3. package/dist/src/api/auth.d.ts.map +1 -1
  4. package/dist/src/api/auth.js +3 -1
  5. package/dist/src/api/auth.js.map +1 -1
  6. package/dist/src/api/client.d.ts +4 -1
  7. package/dist/src/api/client.d.ts.map +1 -1
  8. package/dist/src/api/client.js +9 -2
  9. package/dist/src/api/client.js.map +1 -1
  10. package/dist/src/api/portal.d.ts +3 -9
  11. package/dist/src/api/portal.d.ts.map +1 -1
  12. package/dist/src/api/portal.js +8 -5
  13. package/dist/src/api/portal.js.map +1 -1
  14. package/dist/src/components/Dropdown/ConnectedTrigger.d.ts +2 -1
  15. package/dist/src/components/Dropdown/ConnectedTrigger.d.ts.map +1 -1
  16. package/dist/src/components/Dropdown/ConnectedTrigger.js +29 -20
  17. package/dist/src/components/Dropdown/ConnectedTrigger.js.map +1 -1
  18. package/dist/src/components/Dropdown/Content.d.ts +19 -5
  19. package/dist/src/components/Dropdown/Content.d.ts.map +1 -1
  20. package/dist/src/components/Dropdown/Content.js +19 -61
  21. package/dist/src/components/Dropdown/Content.js.map +1 -1
  22. package/dist/src/components/Dropdown/DisconnectedTrigger.d.ts.map +1 -1
  23. package/dist/src/components/Dropdown/DisconnectedTrigger.js +2 -2
  24. package/dist/src/components/Dropdown/DisconnectedTrigger.js.map +1 -1
  25. package/dist/src/components/Dropdown/Dropdown.d.ts +13 -6
  26. package/dist/src/components/Dropdown/Dropdown.d.ts.map +1 -1
  27. package/dist/src/components/Dropdown/Dropdown.js +32 -9
  28. package/dist/src/components/Dropdown/Dropdown.js.map +1 -1
  29. package/dist/src/components/Dropdown/ListingItem.d.ts +13 -0
  30. package/dist/src/components/Dropdown/ListingItem.d.ts.map +1 -0
  31. package/dist/src/components/Dropdown/ListingItem.js +34 -0
  32. package/dist/src/components/Dropdown/ListingItem.js.map +1 -0
  33. package/dist/src/components/Dropdown/NestedViewLayout.d.ts +7 -0
  34. package/dist/src/components/Dropdown/NestedViewLayout.d.ts.map +1 -0
  35. package/dist/src/components/Dropdown/NestedViewLayout.js +34 -0
  36. package/dist/src/components/Dropdown/NestedViewLayout.js.map +1 -0
  37. package/dist/src/components/Dropdown/Receive/Receive.d.ts +7 -0
  38. package/dist/src/components/Dropdown/Receive/Receive.d.ts.map +1 -0
  39. package/dist/src/components/Dropdown/Receive/Receive.js +53 -0
  40. package/dist/src/components/Dropdown/Receive/Receive.js.map +1 -0
  41. package/dist/src/components/Dropdown/Root/AccountAddressActions.d.ts +7 -0
  42. package/dist/src/components/Dropdown/Root/AccountAddressActions.d.ts.map +1 -0
  43. package/dist/src/components/Dropdown/Root/AccountAddressActions.js +48 -0
  44. package/dist/src/components/Dropdown/Root/AccountAddressActions.js.map +1 -0
  45. package/dist/src/components/Dropdown/Root/AccountAssetItem.d.ts +11 -0
  46. package/dist/src/components/Dropdown/Root/AccountAssetItem.d.ts.map +1 -0
  47. package/dist/src/components/Dropdown/Root/AccountAssetItem.js +9 -0
  48. package/dist/src/components/Dropdown/Root/AccountAssetItem.js.map +1 -0
  49. package/dist/src/components/Dropdown/{AccountBalance.d.ts → Root/AccountBalance.d.ts} +2 -1
  50. package/dist/src/components/Dropdown/Root/AccountBalance.d.ts.map +1 -0
  51. package/dist/src/components/Dropdown/Root/AccountBalance.js +15 -0
  52. package/dist/src/components/Dropdown/Root/AccountBalance.js.map +1 -0
  53. package/dist/src/components/Dropdown/Root/AccountOtherAssets.d.ts +9 -0
  54. package/dist/src/components/Dropdown/Root/AccountOtherAssets.d.ts.map +1 -0
  55. package/dist/src/components/Dropdown/Root/AccountOtherAssets.js +48 -0
  56. package/dist/src/components/Dropdown/Root/AccountOtherAssets.js.map +1 -0
  57. package/dist/src/components/Dropdown/Root/Root.d.ts +23 -0
  58. package/dist/src/components/Dropdown/Root/Root.d.ts.map +1 -0
  59. package/dist/src/components/Dropdown/Root/Root.js +57 -0
  60. package/dist/src/components/Dropdown/Root/Root.js.map +1 -0
  61. package/dist/src/components/Dropdown/Root/WalletAddress.d.ts +8 -0
  62. package/dist/src/components/Dropdown/Root/WalletAddress.d.ts.map +1 -0
  63. package/dist/src/components/Dropdown/{AccountAddress.js → Root/WalletAddress.js} +21 -15
  64. package/dist/src/components/Dropdown/Root/WalletAddress.js.map +1 -0
  65. package/dist/src/components/Dropdown/{WelcomeBlock.d.ts → Root/WelcomeBlock.d.ts} +1 -1
  66. package/dist/src/components/Dropdown/Root/WelcomeBlock.d.ts.map +1 -0
  67. package/dist/src/components/Dropdown/{WelcomeBlock.js → Root/WelcomeBlock.js} +5 -5
  68. package/dist/src/components/Dropdown/Root/WelcomeBlock.js.map +1 -0
  69. package/dist/src/components/Dropdown/TestnetTopBanner.d.ts +3 -0
  70. package/dist/src/components/Dropdown/TestnetTopBanner.d.ts.map +1 -0
  71. package/dist/src/components/Dropdown/TestnetTopBanner.js +14 -0
  72. package/dist/src/components/Dropdown/TestnetTopBanner.js.map +1 -0
  73. package/dist/src/config.d.ts +18 -5
  74. package/dist/src/config.d.ts.map +1 -1
  75. package/dist/src/config.js +45 -14
  76. package/dist/src/config.js.map +1 -1
  77. package/dist/src/constants.d.ts +11 -3
  78. package/dist/src/constants.d.ts.map +1 -1
  79. package/dist/src/constants.js +12 -4
  80. package/dist/src/constants.js.map +1 -1
  81. package/dist/src/hooks/index.d.ts +4 -0
  82. package/dist/src/hooks/index.d.ts.map +1 -1
  83. package/dist/src/hooks/index.js +4 -0
  84. package/dist/src/hooks/index.js.map +1 -1
  85. package/dist/src/hooks/useAssetsConversionRates.d.ts +23 -0
  86. package/dist/src/hooks/useAssetsConversionRates.d.ts.map +1 -0
  87. package/dist/src/hooks/useAssetsConversionRates.js +90 -0
  88. package/dist/src/hooks/useAssetsConversionRates.js.map +1 -0
  89. package/dist/src/hooks/useAuthenticateWithWallet.d.ts.map +1 -1
  90. package/dist/src/hooks/useAuthenticateWithWallet.js +9 -9
  91. package/dist/src/hooks/useAuthenticateWithWallet.js.map +1 -1
  92. package/dist/src/hooks/useBorrowData.d.ts +82 -0
  93. package/dist/src/hooks/useBorrowData.d.ts.map +1 -0
  94. package/dist/src/hooks/useBorrowData.js +159 -0
  95. package/dist/src/hooks/useBorrowData.js.map +1 -0
  96. package/dist/src/hooks/useCreateAccount.d.ts.map +1 -1
  97. package/dist/src/hooks/useCreateAccount.js +22 -1
  98. package/dist/src/hooks/useCreateAccount.js.map +1 -1
  99. package/dist/src/hooks/useDropdownData.d.ts +37 -34
  100. package/dist/src/hooks/useDropdownData.d.ts.map +1 -1
  101. package/dist/src/hooks/useDropdownData.js +85 -61
  102. package/dist/src/hooks/useDropdownData.js.map +1 -1
  103. package/dist/src/hooks/useLinkAccount.d.ts +19 -13
  104. package/dist/src/hooks/useLinkAccount.d.ts.map +1 -1
  105. package/dist/src/hooks/useLinkAccount.js +20 -1
  106. package/dist/src/hooks/useLinkAccount.js.map +1 -1
  107. package/dist/src/hooks/useRefreshPassport.d.ts +19 -0
  108. package/dist/src/hooks/useRefreshPassport.d.ts.map +1 -0
  109. package/dist/src/hooks/useRefreshPassport.js +44 -0
  110. package/dist/src/hooks/useRefreshPassport.js.map +1 -0
  111. package/dist/src/hooks/useTokensBalances.d.ts +74 -0
  112. package/dist/src/hooks/useTokensBalances.d.ts.map +1 -0
  113. package/dist/src/hooks/useTokensBalances.js +140 -0
  114. package/dist/src/hooks/useTokensBalances.js.map +1 -0
  115. package/dist/src/hooks/useValidateMezoId.d.ts +8 -0
  116. package/dist/src/hooks/useValidateMezoId.d.ts.map +1 -0
  117. package/dist/src/hooks/useValidateMezoId.js +24 -0
  118. package/dist/src/hooks/useValidateMezoId.js.map +1 -0
  119. package/dist/src/hooks/useWalletAccount.d.ts +2 -1
  120. package/dist/src/hooks/useWalletAccount.d.ts.map +1 -1
  121. package/dist/src/hooks/useWalletAccount.js +19 -15
  122. package/dist/src/hooks/useWalletAccount.js.map +1 -1
  123. package/dist/src/hooks/useWatchTransferEvents.d.ts +5 -0
  124. package/dist/src/hooks/useWatchTransferEvents.d.ts.map +1 -0
  125. package/dist/src/hooks/useWatchTransferEvents.js +63 -0
  126. package/dist/src/hooks/useWatchTransferEvents.js.map +1 -0
  127. package/dist/src/index.d.ts +2 -1
  128. package/dist/src/index.d.ts.map +1 -1
  129. package/dist/src/index.js +2 -1
  130. package/dist/src/index.js.map +1 -1
  131. package/dist/src/lib/contracts/index.d.ts +13 -0
  132. package/dist/src/lib/contracts/index.d.ts.map +1 -0
  133. package/dist/src/lib/contracts/index.js +62 -0
  134. package/dist/src/lib/contracts/index.js.map +1 -0
  135. package/dist/src/lib/contracts/priceOracle.d.ts +43 -0
  136. package/dist/src/lib/contracts/priceOracle.d.ts.map +1 -0
  137. package/dist/src/lib/contracts/priceOracle.js +52 -0
  138. package/dist/src/lib/contracts/priceOracle.js.map +1 -0
  139. package/dist/src/provider.js +1 -1
  140. package/dist/src/provider.js.map +1 -1
  141. package/dist/src/stores/dropdownStore.d.ts +12 -0
  142. package/dist/src/stores/dropdownStore.d.ts.map +1 -0
  143. package/dist/src/stores/dropdownStore.js +13 -0
  144. package/dist/src/stores/dropdownStore.js.map +1 -0
  145. package/dist/src/utils/address.d.ts +1 -1
  146. package/dist/src/utils/address.d.ts.map +1 -1
  147. package/dist/src/utils/address.js +2 -0
  148. package/dist/src/utils/address.js.map +1 -1
  149. package/dist/src/utils/address.test.js +8 -5
  150. package/dist/src/utils/address.test.js.map +1 -1
  151. package/dist/src/utils/cryptoAssets.d.ts +17 -1
  152. package/dist/src/utils/cryptoAssets.d.ts.map +1 -1
  153. package/dist/src/utils/cryptoAssets.js +87 -31
  154. package/dist/src/utils/cryptoAssets.js.map +1 -1
  155. package/dist/src/utils/cryptoAssets.test.js +51 -33
  156. package/dist/src/utils/cryptoAssets.test.js.map +1 -1
  157. package/dist/src/utils/currency.d.ts +8 -0
  158. package/dist/src/utils/currency.d.ts.map +1 -1
  159. package/dist/src/utils/currency.js +11 -0
  160. package/dist/src/utils/currency.js.map +1 -1
  161. package/dist/src/utils/currency.test.js +1 -0
  162. package/dist/src/utils/currency.test.js.map +1 -1
  163. package/dist/src/utils/mezoId.d.ts +7 -0
  164. package/dist/src/utils/mezoId.d.ts.map +1 -0
  165. package/dist/src/utils/mezoId.js +41 -0
  166. package/dist/src/utils/mezoId.js.map +1 -0
  167. package/dist/src/utils/numbers.d.ts +39 -17
  168. package/dist/src/utils/numbers.d.ts.map +1 -1
  169. package/dist/src/utils/numbers.js +91 -33
  170. package/dist/src/utils/numbers.js.map +1 -1
  171. package/dist/src/utils/numbers.test.js +144 -22
  172. package/dist/src/utils/numbers.test.js.map +1 -1
  173. package/dist/src/utils/siww.d.ts +1 -0
  174. package/dist/src/utils/siww.d.ts.map +1 -1
  175. package/dist/src/utils/siww.js +50 -12
  176. package/dist/src/utils/siww.js.map +1 -1
  177. package/dist/src/utils/wagmi.d.ts +3 -0
  178. package/dist/src/utils/wagmi.d.ts.map +1 -0
  179. package/dist/src/utils/wagmi.js +7 -0
  180. package/dist/src/utils/wagmi.js.map +1 -0
  181. package/dist/src/wallet/index.d.ts +1 -1
  182. package/dist/src/wallet/index.d.ts.map +1 -1
  183. package/dist/src/wallet/index.js +1 -1
  184. package/dist/src/wallet/index.js.map +1 -1
  185. package/package.json +10 -8
  186. package/src/api/auth.ts +8 -4
  187. package/src/api/client.ts +11 -2
  188. package/src/api/portal.ts +11 -14
  189. package/src/components/Dropdown/ConnectedTrigger.tsx +52 -32
  190. package/src/components/Dropdown/Content.tsx +67 -117
  191. package/src/components/Dropdown/DisconnectedTrigger.tsx +2 -1
  192. package/src/components/Dropdown/Dropdown.tsx +75 -15
  193. package/src/components/Dropdown/ListingItem.tsx +80 -0
  194. package/src/components/Dropdown/NestedViewLayout.tsx +75 -0
  195. package/src/components/Dropdown/README.md +10 -18
  196. package/src/components/Dropdown/Receive/Receive.tsx +119 -0
  197. package/src/components/Dropdown/Root/AccountAddressActions.tsx +101 -0
  198. package/src/components/Dropdown/Root/AccountAssetItem.tsx +26 -0
  199. package/src/components/Dropdown/Root/AccountBalance.tsx +30 -0
  200. package/src/components/Dropdown/Root/AccountOtherAssets.tsx +68 -0
  201. package/src/components/Dropdown/Root/Root.tsx +147 -0
  202. package/src/components/Dropdown/Root/WalletAddress.tsx +128 -0
  203. package/src/components/Dropdown/{WelcomeBlock.tsx → Root/WelcomeBlock.tsx} +5 -6
  204. package/src/components/Dropdown/TestnetTopBanner.tsx +32 -0
  205. package/src/config.ts +73 -20
  206. package/src/constants.ts +12 -4
  207. package/src/hooks/index.ts +7 -0
  208. package/src/hooks/useAssetsConversionRates.ts +97 -0
  209. package/src/hooks/useAuthenticateWithWallet.ts +18 -12
  210. package/src/hooks/useBorrowData.ts +187 -0
  211. package/src/hooks/useCreateAccount.ts +28 -2
  212. package/src/hooks/useDropdownData.ts +119 -97
  213. package/src/hooks/useLinkAccount.ts +36 -4
  214. package/src/hooks/useRefreshPassport.ts +56 -0
  215. package/src/hooks/useTokensBalances.ts +187 -0
  216. package/src/hooks/useValidateMezoId.ts +31 -0
  217. package/src/hooks/useWalletAccount.ts +29 -19
  218. package/src/hooks/useWatchTransferEvents.ts +74 -0
  219. package/src/index.ts +12 -1
  220. package/src/lib/contracts/index.ts +92 -0
  221. package/src/lib/contracts/priceOracle.ts +53 -0
  222. package/src/provider.ts +1 -1
  223. package/src/stores/dropdownStore.ts +20 -0
  224. package/src/utils/address.test.ts +10 -6
  225. package/src/utils/address.ts +5 -3
  226. package/src/utils/cryptoAssets.test.ts +57 -37
  227. package/src/utils/cryptoAssets.ts +113 -35
  228. package/src/utils/currency.test.ts +2 -0
  229. package/src/utils/currency.ts +21 -0
  230. package/src/utils/mezoId.ts +51 -0
  231. package/src/utils/numbers.test.ts +181 -23
  232. package/src/utils/numbers.ts +121 -34
  233. package/src/utils/siww.ts +71 -16
  234. package/src/utils/wagmi.ts +12 -0
  235. package/src/wallet/index.ts +3 -2
  236. package/dist/src/components/Dropdown/AccountAddress.d.ts +0 -8
  237. package/dist/src/components/Dropdown/AccountAddress.d.ts.map +0 -1
  238. package/dist/src/components/Dropdown/AccountAddress.js.map +0 -1
  239. package/dist/src/components/Dropdown/AccountAssets.d.ts +0 -14
  240. package/dist/src/components/Dropdown/AccountAssets.d.ts.map +0 -1
  241. package/dist/src/components/Dropdown/AccountAssets.js +0 -44
  242. package/dist/src/components/Dropdown/AccountAssets.js.map +0 -1
  243. package/dist/src/components/Dropdown/AccountBalance.d.ts.map +0 -1
  244. package/dist/src/components/Dropdown/AccountBalance.js +0 -18
  245. package/dist/src/components/Dropdown/AccountBalance.js.map +0 -1
  246. package/dist/src/components/Dropdown/WelcomeBlock.d.ts.map +0 -1
  247. package/dist/src/components/Dropdown/WelcomeBlock.js.map +0 -1
  248. package/dist/src/hooks/useAssetsUSDConversion.d.ts +0 -8
  249. package/dist/src/hooks/useAssetsUSDConversion.d.ts.map +0 -1
  250. package/dist/src/hooks/useAssetsUSDConversion.js +0 -21
  251. package/dist/src/hooks/useAssetsUSDConversion.js.map +0 -1
  252. package/src/components/Dropdown/AccountAddress.tsx +0 -111
  253. package/src/components/Dropdown/AccountAssets.tsx +0 -110
  254. package/src/components/Dropdown/AccountBalance.tsx +0 -38
  255. package/src/hooks/useAssetsUSDConversion.ts +0 -31
package/src/constants.ts CHANGED
@@ -1,11 +1,19 @@
1
- import { mezoMatsnetTestnet } from "@mezo-org/orangekit"
1
+ import { mezoMainnet, mezoTestnet } from "@mezo-org/orangekit"
2
2
 
3
- export { mezoMatsnetTestnet } from "@mezo-org/orangekit"
3
+ export { mezoMainnet, mezoTestnet } from "@mezo-org/orangekit"
4
4
 
5
5
  export const CHAIN_ID = {
6
- testnet: mezoMatsnetTestnet.id,
6
+ testnet: mezoTestnet.id,
7
+ mainnet: mezoMainnet.id,
7
8
  }
8
9
 
9
10
  export const RPC_BY_NETWORK = {
10
- testnet: mezoMatsnetTestnet.rpcUrls.default.http[0],
11
+ mainnet: {
12
+ http: mezoMainnet.rpcUrls.default.http[0],
13
+ webSocket: mezoMainnet.rpcUrls.default.webSocket?.[0],
14
+ },
15
+ testnet: {
16
+ http: mezoTestnet.rpcUrls.default.http[0],
17
+ webSocket: mezoTestnet.rpcUrls.default.webSocket?.[0],
18
+ },
11
19
  }
@@ -6,13 +6,20 @@ export {
6
6
  useSubscribeToConnectorEvent,
7
7
  useSubscribeToWalletNetworkDoesNotMatchProviderChain,
8
8
  } from "@mezo-org/orangekit"
9
+ export { useInvalidateBorrowData, useResetBorrowData } from "./useBorrowData"
9
10
  export * from "./useGetAccountByAddress"
10
11
  export * from "./useGetAccountByMezoId"
11
12
  export * from "./useGetCurrentAccount"
12
13
  export * from "./useGetSession"
13
14
  export * from "./useLinkAccount"
15
+ export * from "./useRefreshPassport"
14
16
  export * from "./useSignInWithDiscord"
15
17
  export * from "./useSignInWithWallet"
16
18
  export * from "./useSignOut"
17
19
  export * from "./useSignUpWithWallet"
20
+ export {
21
+ useInvalidateTokensBalances,
22
+ useResetTokensBalances,
23
+ } from "./useTokensBalances"
18
24
  export * from "./useUpdateMezoId"
25
+ export * from "./useValidateMezoId"
@@ -0,0 +1,97 @@
1
+ import { useQuery } from "@tanstack/react-query"
2
+ import { useReadContract } from "wagmi"
3
+ import { ONE_SECOND_MS } from "../utils/time"
4
+ import { QUERY_KEYS } from "./constants"
5
+ import { usePortalApiClient } from "./usePortalApiClient"
6
+ import { priceOracleContract } from "../lib/contracts"
7
+ import { fromFloatToBigInt } from "../utils/numbers"
8
+ import { getCryptoAsset } from "../utils/cryptoAssets"
9
+ import { CHAIN_ID } from "../constants"
10
+ import { usePassportContext } from "./usePassportContext"
11
+
12
+ const BTC_TARGET_DIGITS = getCryptoAsset("BTC").decimals
13
+ const T_TARGET_DIGITS = getCryptoAsset("mT").decimals
14
+ // If the oracle has not been updated in at least 60 seconds, it is stale.
15
+ const MAX_PRICE_DELAY = 60 * ONE_SECOND_MS
16
+
17
+ /**
18
+ * Scales the price by the number of digits.
19
+ * @dev Oracles returns prices with variable number of digits. This function
20
+ * scales the price to a fixed number of digits ensuring consistency.
21
+ * @param price - The price to scale.
22
+ * @param priceDigits - The number of digits in the price.
23
+ * @returns The scaled price.
24
+ */
25
+ function scalePriceByDigits(price: bigint, priceDigits: number): bigint {
26
+ if (priceDigits > BTC_TARGET_DIGITS) {
27
+ return price / 10n ** BigInt(priceDigits - BTC_TARGET_DIGITS)
28
+ }
29
+ if (priceDigits < BTC_TARGET_DIGITS) {
30
+ return price * 10n ** BigInt(BTC_TARGET_DIGITS - priceDigits)
31
+ }
32
+ return price
33
+ }
34
+
35
+ /**
36
+ * Hook to fetch assets conversion rates.
37
+ * @see https://github.com/mezo-org/musd/blob/f0b2030315f0d8b0fc11a9fc778856fe4673051f/solidity/contracts/PriceFeed.sol
38
+ * @returns {}.data.{}.price - The latest asset price.
39
+ * @returns {}.data.{}.digits - The latest asset price digits.
40
+ * @returns {}.isPending - Whether the request is pending.
41
+ * @returns {}.isError - Whether there was an error fetching the price.
42
+ */
43
+ export function useAssetsConversionRates() {
44
+ const portalApiClient = usePortalApiClient()
45
+ const { environment = "mainnet" } = usePassportContext()
46
+
47
+ const btcPrice = useReadContract({
48
+ ...priceOracleContract,
49
+ functionName: "latestRoundData",
50
+ query: {
51
+ refetchInterval: ({ state }) => {
52
+ if (!state.data) return MAX_PRICE_DELAY
53
+
54
+ const [, , , updatedAt] = state.data ?? []
55
+ if (!updatedAt) return MAX_PRICE_DELAY
56
+
57
+ const age = Date.now() - Number(updatedAt) * 1000
58
+
59
+ return age > MAX_PRICE_DELAY
60
+ ? // If the data is stale, we want to refetch as soon as possible, but
61
+ // not faster than once per second, to avoid excessive requests.
62
+ // Math.max ensures the interval is at least 1 second.
63
+ Math.max(ONE_SECOND_MS, MAX_PRICE_DELAY - age)
64
+ : MAX_PRICE_DELAY - age
65
+ },
66
+ select: (data) => {
67
+ const [, answer] = data
68
+ return scalePriceByDigits(answer, BTC_TARGET_DIGITS)
69
+ },
70
+ },
71
+ chainId: CHAIN_ID[environment],
72
+ })
73
+
74
+ const tPrice = useQuery({
75
+ queryKey: [QUERY_KEYS.ASSETS_USD_CONVERSION],
76
+ queryFn: () => portalApiClient.getPortalStatistics(),
77
+ select: (data) =>
78
+ fromFloatToBigInt(data.currentUsdPerTToken, T_TARGET_DIGITS),
79
+ refetchInterval: MAX_PRICE_DELAY,
80
+ throwOnError: true,
81
+ })
82
+
83
+ return {
84
+ data: {
85
+ t: {
86
+ price: tPrice.data ?? 0n,
87
+ decimals: T_TARGET_DIGITS,
88
+ },
89
+ btc: {
90
+ price: btcPrice.data ?? 0n,
91
+ decimals: BTC_TARGET_DIGITS,
92
+ },
93
+ },
94
+ isPending: tPrice.isPending || btcPrice.isPending,
95
+ isError: tPrice.isError || btcPrice.isError,
96
+ }
97
+ }
@@ -1,11 +1,12 @@
1
1
  import { useSignMessage } from "wagmi"
2
- import { useBitcoinAccount } from "@mezo-org/orangekit"
3
2
  import { MutationOptions, useMutation } from "@tanstack/react-query"
4
- import { useMemo } from "react"
5
3
  import { useCreateAccount } from "./useCreateAccount"
6
4
  import { useCreateSession } from "./useCreateSession"
7
5
  import { useEnsureNoSessionAndFetchNonce } from "./useEnsureNoSessionAndFetchNonce"
8
- import { createSignInWithWalletMessage } from "../utils/siww"
6
+ import {
7
+ createSignInWithWalletMessage,
8
+ verifyDomainInSignInWithWalletMessage,
9
+ } from "../utils/siww"
9
10
  import { useGetAccountByAddress } from "./useGetAccountByAddress"
10
11
  import type { Session } from "../api"
11
12
  import useWalletAccount from "./useWalletAccount"
@@ -17,32 +18,31 @@ function useAuthenticateWithWallet(
17
18
  "mutationFn" | "mutationKey"
18
19
  > = {},
19
20
  ) {
20
- const { address, chainId, connector, networkFamily } = useWalletAccount()
21
- const { btcAddress } = useBitcoinAccount()
21
+ const { walletAddress, chainId, connector, networkFamily } =
22
+ useWalletAccount()
22
23
  const { ensureNoSessionAndFetchNonce } = useEnsureNoSessionAndFetchNonce()
23
24
  const { createSessionAsync } = useCreateSession()
24
25
  const { createAccountAsync } = useCreateAccount()
25
26
  const { signMessageAsync } = useSignMessage()
26
- const walletAddress = useMemo(
27
- () => (connector?.type === "orangekit" ? btcAddress : address),
28
- [btcAddress, address, connector],
29
- )
30
27
 
31
28
  const { refetch: getAccountByAddress } = useGetAccountByAddress(
32
29
  walletAddress,
33
- { enabled: false, retry: false },
30
+ {
31
+ enabled: false,
32
+ retry: false,
33
+ },
34
34
  )
35
35
 
36
36
  const { mutate, mutateAsync, ...signInMutationRestParameters } = useMutation({
37
37
  mutationFn: async () => {
38
- if (!address) {
38
+ if (!walletAddress) {
39
39
  throw new Error("Sign in error: Wallet not connected!")
40
40
  }
41
41
 
42
42
  const nonce = await ensureNoSessionAndFetchNonce()
43
43
 
44
44
  const messageResult = createSignInWithWalletMessage(
45
- btcAddress || address,
45
+ walletAddress,
46
46
  nonce,
47
47
  networkFamily,
48
48
  chainId,
@@ -52,6 +52,12 @@ function useAuthenticateWithWallet(
52
52
  connector,
53
53
  })
54
54
 
55
+ await verifyDomainInSignInWithWalletMessage(
56
+ messageResult,
57
+ signatureResult,
58
+ nonce,
59
+ )
60
+
55
61
  if (shouldCreateAccount) {
56
62
  const getAccountByAddressResult = await getAccountByAddress()
57
63
 
@@ -0,0 +1,187 @@
1
+ import { useAccount, useReadContracts } from "wagmi"
2
+ import { useCallback, useMemo } from "react"
3
+ import { useQueryClient } from "@tanstack/react-query"
4
+ import { ONE_MINUTE_MS } from "../utils/time"
5
+ import {
6
+ mainnetBorrowContracts,
7
+ testnetBorrowContracts,
8
+ } from "../lib/contracts"
9
+ import { usePassportContext } from "./usePassportContext"
10
+ import { CHAIN_ID } from "../constants"
11
+ import { bigIntMax } from "../utils/numbers"
12
+
13
+ // Wagmi handles typesafety with ABI const assertions. TypeScript doesn't
14
+ // support importing JSON as const yet so types cannot be inferred from the
15
+ // imported contract. As a workaround there is minimal ABI definition that can
16
+ // be asserted types from.
17
+ // Ref: https://wagmi.sh/core/typescript#const-assert-abis-typed-data
18
+
19
+ const TROVE_MANAGER_ABI = [
20
+ {
21
+ inputs: [
22
+ {
23
+ internalType: "address",
24
+ name: "_borrower",
25
+ type: "address",
26
+ },
27
+ ],
28
+ name: "getEntireDebtAndColl",
29
+ outputs: [
30
+ {
31
+ internalType: "uint256",
32
+ name: "coll",
33
+ type: "uint256",
34
+ },
35
+ {
36
+ internalType: "uint256",
37
+ name: "principal",
38
+ type: "uint256",
39
+ },
40
+ {
41
+ internalType: "uint256",
42
+ name: "interest",
43
+ type: "uint256",
44
+ },
45
+ {
46
+ internalType: "uint256",
47
+ name: "pendingCollateral",
48
+ type: "uint256",
49
+ },
50
+ {
51
+ internalType: "uint256",
52
+ name: "pendingPrincipal",
53
+ type: "uint256",
54
+ },
55
+ {
56
+ internalType: "uint256",
57
+ name: "pendingInterest",
58
+ type: "uint256",
59
+ },
60
+ ],
61
+ stateMutability: "view",
62
+ type: "function",
63
+ },
64
+ ] as const
65
+
66
+ const BORROWER_OPERATIONS_ABI = [
67
+ {
68
+ inputs: [],
69
+ name: "MUSD_GAS_COMPENSATION",
70
+ outputs: [
71
+ {
72
+ internalType: "uint256",
73
+ name: "",
74
+ type: "uint256",
75
+ },
76
+ ],
77
+ stateMutability: "view",
78
+ type: "function",
79
+ },
80
+ ] as const
81
+
82
+ /**
83
+ * Query hook for getting borrow data. Returns collateral and trove debt for the
84
+ * connected account, based on it's evm address.
85
+ * @param queryOptions Query options passed to the underlying `useQuery` hook.
86
+ */
87
+ export function useBorrowData(queryOptions = {}) {
88
+ const { environment = "mainnet" } = usePassportContext()
89
+ const { address } = useAccount()
90
+
91
+ const contractAddress = useMemo(() => {
92
+ if (environment === "mainnet") {
93
+ return {
94
+ troveManager: mainnetBorrowContracts.TroveManager.address,
95
+ borrowerOperations: mainnetBorrowContracts.BorrowerOperations.address,
96
+ }
97
+ }
98
+ return {
99
+ troveManager: testnetBorrowContracts.TroveManager.address,
100
+ borrowerOperations: testnetBorrowContracts.BorrowerOperations.address,
101
+ }
102
+ }, [environment])
103
+
104
+ const chainId = CHAIN_ID[environment]
105
+
106
+ return useReadContracts({
107
+ contracts: [
108
+ {
109
+ abi: TROVE_MANAGER_ABI,
110
+ address: contractAddress.troveManager,
111
+ functionName: "getEntireDebtAndColl",
112
+ args: [address || "0x"],
113
+ chainId,
114
+ },
115
+ {
116
+ abi: BORROWER_OPERATIONS_ABI,
117
+ address: contractAddress.borrowerOperations,
118
+ functionName: "MUSD_GAS_COMPENSATION",
119
+ args: [],
120
+ chainId,
121
+ },
122
+ ],
123
+ query: {
124
+ enabled: !!address,
125
+ staleTime: 5 * ONE_MINUTE_MS,
126
+ retry: 1,
127
+ select: (data) => {
128
+ if (!data || data.some((item) => !item.result)) return undefined
129
+
130
+ const [collateral, principal, interest] = data[0].result!
131
+ const gasCompensation = data[1].result!
132
+
133
+ const troveDebt = bigIntMax(
134
+ principal + interest - gasCompensation,
135
+ 0n, // Ensure that debt is not negative
136
+ )
137
+
138
+ return {
139
+ collateral,
140
+ // Principal includes gas compensation value which is not
141
+ troveDebt,
142
+ }
143
+ },
144
+ ...queryOptions,
145
+ },
146
+ })
147
+ }
148
+
149
+ /**
150
+ * Hook for for invalidating current user's borrow data. Can be used to
151
+ * invalidate borrow data manually, which forces the data to be re-fetched.
152
+ * @returns Function `invalidateBorrowData` for invalidating the borrow data
153
+ */
154
+ export function useInvalidateBorrowData() {
155
+ const queryClient = useQueryClient()
156
+
157
+ const { queryKey } = useBorrowData()
158
+
159
+ const invalidateBorrowDataHandler = useCallback(
160
+ () => queryClient.invalidateQueries({ queryKey }),
161
+ [queryClient, queryKey],
162
+ )
163
+
164
+ return {
165
+ invalidateBorrowData: invalidateBorrowDataHandler,
166
+ }
167
+ }
168
+
169
+ /**
170
+ * Hook for for resetting current user's borrow data. Can be used to reset
171
+ * borrow data manually, which forces the data to be re-fetched.
172
+ * @returns Function `resetBorrowData` for resetting the borrow data
173
+ */
174
+ export function useResetBorrowData() {
175
+ const queryClient = useQueryClient()
176
+
177
+ const { queryKey } = useBorrowData()
178
+
179
+ const resetBorrowDataHandler = useCallback(
180
+ () => queryClient.resetQueries({ queryKey }),
181
+ [queryClient, queryKey],
182
+ )
183
+
184
+ return {
185
+ resetBorrowData: resetBorrowDataHandler,
186
+ }
187
+ }
@@ -7,6 +7,8 @@ import {
7
7
  import { useAuthApiClient } from "./useAuthApiClient"
8
8
  import { QUERY_KEYS } from "./constants"
9
9
  import type { CreateAccountRequest, CreateAccountResponse } from "../api"
10
+ import useWalletAccount from "./useWalletAccount"
11
+ import { getBitcoinPublicKeyFromConnector } from "../utils/wagmi"
10
12
 
11
13
  export function useCreateAccount(
12
14
  mutationOptions: Omit<
@@ -16,12 +18,36 @@ export function useCreateAccount(
16
18
  ) {
17
19
  const queryClient = useQueryClient()
18
20
  const authApiClient = useAuthApiClient()
21
+ const { connector } = useWalletAccount()
19
22
 
20
23
  const { onSuccess: customOnSuccess, ...restMutationOptions } = mutationOptions
21
24
 
22
25
  const { mutate, mutateAsync, ...rest } = useMutation({
23
- mutationFn: (createAccountRequest: CreateAccountRequest) =>
24
- authApiClient.createAccount(createAccountRequest),
26
+ mutationFn: async (
27
+ createAccountMutationFnParameters: CreateAccountRequest,
28
+ ) => {
29
+ if (createAccountMutationFnParameters.type === "wallet") {
30
+ if (!connector) {
31
+ throw new Error("Connector not defined.")
32
+ }
33
+
34
+ const bitcoinPublicKey =
35
+ await getBitcoinPublicKeyFromConnector(connector)
36
+
37
+ // Creating bitcoin mezo account
38
+ if (bitcoinPublicKey) {
39
+ return authApiClient.createAccount({
40
+ ...createAccountMutationFnParameters,
41
+ bitcoinPublicKey,
42
+ })
43
+ }
44
+ }
45
+
46
+ // Creating evm mezo account
47
+ return authApiClient.createAccount({
48
+ ...createAccountMutationFnParameters,
49
+ })
50
+ },
25
51
  onSuccess: (data, variables, context) => {
26
52
  queryClient.resetQueries({
27
53
  queryKey: [QUERY_KEYS.ACCOUNT],