thirdweb 5.86.7-nightly-f5f8a40320d5d768d5866813f53ee4880bdf7851-20250129000319 → 5.87.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (197) hide show
  1. package/dist/cjs/exports/react.js +4 -3
  2. package/dist/cjs/exports/react.js.map +1 -1
  3. package/dist/cjs/exports/react.native.js +27 -1
  4. package/dist/cjs/exports/react.native.js.map +1 -1
  5. package/dist/cjs/react/{web/ui/prebuilt/Account → core/account}/provider.js +1 -1
  6. package/dist/cjs/react/core/account/provider.js.map +1 -0
  7. package/dist/cjs/react/core/utils/account.js +105 -0
  8. package/dist/cjs/react/core/utils/account.js.map +1 -0
  9. package/dist/cjs/react/core/utils/walletIcon.js +23 -0
  10. package/dist/cjs/react/core/utils/walletIcon.js.map +1 -1
  11. package/dist/cjs/react/core/utils/walletname.js +45 -0
  12. package/dist/cjs/react/core/utils/walletname.js.map +1 -0
  13. package/dist/cjs/react/core/wallet/provider.js.map +1 -0
  14. package/dist/cjs/react/native/ui/prebuilt/Account/address.js +50 -0
  15. package/dist/cjs/react/native/ui/prebuilt/Account/address.js.map +1 -0
  16. package/dist/cjs/react/native/ui/prebuilt/Account/avatar.js +151 -0
  17. package/dist/cjs/react/native/ui/prebuilt/Account/avatar.js.map +1 -0
  18. package/dist/cjs/react/native/ui/prebuilt/Account/balance.js +130 -0
  19. package/dist/cjs/react/native/ui/prebuilt/Account/balance.js.map +1 -0
  20. package/dist/cjs/react/native/ui/prebuilt/Account/blobbie.js +58 -0
  21. package/dist/cjs/react/native/ui/prebuilt/Account/blobbie.js.map +1 -0
  22. package/dist/cjs/react/native/ui/prebuilt/Account/name.js +113 -0
  23. package/dist/cjs/react/native/ui/prebuilt/Account/name.js.map +1 -0
  24. package/dist/cjs/react/native/ui/prebuilt/Wallet/icon.js +85 -0
  25. package/dist/cjs/react/native/ui/prebuilt/Wallet/icon.js.map +1 -0
  26. package/dist/cjs/react/native/ui/prebuilt/Wallet/name.js +66 -0
  27. package/dist/cjs/react/native/ui/prebuilt/Wallet/name.js.map +1 -0
  28. package/dist/cjs/react/web/ui/ConnectWallet/Blobbie.js +2 -18
  29. package/dist/cjs/react/web/ui/ConnectWallet/Blobbie.js.map +1 -1
  30. package/dist/cjs/react/web/ui/ConnectWallet/ConnectButton.js +1 -1
  31. package/dist/cjs/react/web/ui/ConnectWallet/ConnectButton.js.map +1 -1
  32. package/dist/cjs/react/web/ui/ConnectWallet/Details.js +5 -4
  33. package/dist/cjs/react/web/ui/ConnectWallet/Details.js.map +1 -1
  34. package/dist/cjs/react/web/ui/prebuilt/Account/address.js +1 -1
  35. package/dist/cjs/react/web/ui/prebuilt/Account/address.js.map +1 -1
  36. package/dist/cjs/react/web/ui/prebuilt/Account/avatar.js +1 -1
  37. package/dist/cjs/react/web/ui/prebuilt/Account/avatar.js.map +1 -1
  38. package/dist/cjs/react/web/ui/prebuilt/Account/balance.js +8 -90
  39. package/dist/cjs/react/web/ui/prebuilt/Account/balance.js.map +1 -1
  40. package/dist/cjs/react/web/ui/prebuilt/Account/blobbie.js +1 -1
  41. package/dist/cjs/react/web/ui/prebuilt/Account/blobbie.js.map +1 -1
  42. package/dist/cjs/react/web/ui/prebuilt/Account/name.js +1 -1
  43. package/dist/cjs/react/web/ui/prebuilt/Account/name.js.map +1 -1
  44. package/dist/cjs/react/web/ui/prebuilt/Wallet/icon.js +1 -23
  45. package/dist/cjs/react/web/ui/prebuilt/Wallet/icon.js.map +1 -1
  46. package/dist/cjs/react/web/ui/prebuilt/Wallet/name.js +2 -42
  47. package/dist/cjs/react/web/ui/prebuilt/Wallet/name.js.map +1 -1
  48. package/dist/cjs/utils/shortenLargeNumber.js +3 -0
  49. package/dist/cjs/utils/shortenLargeNumber.js.map +1 -1
  50. package/dist/cjs/version.js +1 -1
  51. package/dist/cjs/version.js.map +1 -1
  52. package/dist/cjs/wallets/manager/index.js +3 -1
  53. package/dist/cjs/wallets/manager/index.js.map +1 -1
  54. package/dist/esm/exports/react.js +3 -3
  55. package/dist/esm/exports/react.js.map +1 -1
  56. package/dist/esm/exports/react.native.js +15 -0
  57. package/dist/esm/exports/react.native.js.map +1 -1
  58. package/dist/esm/react/{web/ui/prebuilt/Account → core/account}/provider.js +1 -1
  59. package/dist/esm/react/core/account/provider.js.map +1 -0
  60. package/dist/esm/react/core/utils/account.js +99 -0
  61. package/dist/esm/react/core/utils/account.js.map +1 -0
  62. package/dist/esm/react/core/utils/walletIcon.js +21 -0
  63. package/dist/esm/react/core/utils/walletIcon.js.map +1 -1
  64. package/dist/esm/react/core/utils/walletname.js +40 -0
  65. package/dist/esm/react/core/utils/walletname.js.map +1 -0
  66. package/dist/esm/react/core/wallet/provider.js.map +1 -0
  67. package/dist/esm/react/native/ui/prebuilt/Account/address.js +47 -0
  68. package/dist/esm/react/native/ui/prebuilt/Account/address.js.map +1 -0
  69. package/dist/esm/react/native/ui/prebuilt/Account/avatar.js +148 -0
  70. package/dist/esm/react/native/ui/prebuilt/Account/avatar.js.map +1 -0
  71. package/dist/esm/react/native/ui/prebuilt/Account/balance.js +127 -0
  72. package/dist/esm/react/native/ui/prebuilt/Account/balance.js.map +1 -0
  73. package/dist/esm/react/native/ui/prebuilt/Account/blobbie.js +54 -0
  74. package/dist/esm/react/native/ui/prebuilt/Account/blobbie.js.map +1 -0
  75. package/dist/esm/react/native/ui/prebuilt/Account/name.js +110 -0
  76. package/dist/esm/react/native/ui/prebuilt/Account/name.js.map +1 -0
  77. package/dist/esm/react/native/ui/prebuilt/Wallet/icon.js +81 -0
  78. package/dist/esm/react/native/ui/prebuilt/Wallet/icon.js.map +1 -0
  79. package/dist/esm/react/native/ui/prebuilt/Wallet/name.js +63 -0
  80. package/dist/esm/react/native/ui/prebuilt/Wallet/name.js.map +1 -0
  81. package/dist/esm/react/web/ui/ConnectWallet/Blobbie.js +1 -17
  82. package/dist/esm/react/web/ui/ConnectWallet/Blobbie.js.map +1 -1
  83. package/dist/esm/react/web/ui/ConnectWallet/ConnectButton.js +1 -1
  84. package/dist/esm/react/web/ui/ConnectWallet/ConnectButton.js.map +1 -1
  85. package/dist/esm/react/web/ui/ConnectWallet/Details.js +3 -2
  86. package/dist/esm/react/web/ui/ConnectWallet/Details.js.map +1 -1
  87. package/dist/esm/react/web/ui/prebuilt/Account/address.js +1 -1
  88. package/dist/esm/react/web/ui/prebuilt/Account/address.js.map +1 -1
  89. package/dist/esm/react/web/ui/prebuilt/Account/avatar.js +1 -1
  90. package/dist/esm/react/web/ui/prebuilt/Account/avatar.js.map +1 -1
  91. package/dist/esm/react/web/ui/prebuilt/Account/balance.js +6 -85
  92. package/dist/esm/react/web/ui/prebuilt/Account/balance.js.map +1 -1
  93. package/dist/esm/react/web/ui/prebuilt/Account/blobbie.js +1 -1
  94. package/dist/esm/react/web/ui/prebuilt/Account/blobbie.js.map +1 -1
  95. package/dist/esm/react/web/ui/prebuilt/Account/name.js +1 -1
  96. package/dist/esm/react/web/ui/prebuilt/Account/name.js.map +1 -1
  97. package/dist/esm/react/web/ui/prebuilt/Wallet/icon.js +1 -22
  98. package/dist/esm/react/web/ui/prebuilt/Wallet/icon.js.map +1 -1
  99. package/dist/esm/react/web/ui/prebuilt/Wallet/name.js +1 -39
  100. package/dist/esm/react/web/ui/prebuilt/Wallet/name.js.map +1 -1
  101. package/dist/esm/utils/shortenLargeNumber.js +3 -0
  102. package/dist/esm/utils/shortenLargeNumber.js.map +1 -1
  103. package/dist/esm/version.js +1 -1
  104. package/dist/esm/version.js.map +1 -1
  105. package/dist/esm/wallets/manager/index.js +3 -1
  106. package/dist/esm/wallets/manager/index.js.map +1 -1
  107. package/dist/types/exports/react.d.ts +5 -4
  108. package/dist/types/exports/react.d.ts.map +1 -1
  109. package/dist/types/exports/react.native.d.ts +15 -0
  110. package/dist/types/exports/react.native.d.ts.map +1 -1
  111. package/dist/types/react/{web/ui/prebuilt/Account → core/account}/provider.d.ts +3 -4
  112. package/dist/types/react/core/account/provider.d.ts.map +1 -0
  113. package/dist/types/react/core/utils/account.d.ts +46 -0
  114. package/dist/types/react/core/utils/account.d.ts.map +1 -0
  115. package/dist/types/react/core/utils/walletIcon.d.ts +14 -0
  116. package/dist/types/react/core/utils/walletIcon.d.ts.map +1 -1
  117. package/dist/types/react/core/utils/walletname.d.ts +26 -0
  118. package/dist/types/react/core/utils/walletname.d.ts.map +1 -0
  119. package/dist/types/react/{web/ui/prebuilt/Wallet → core/wallet}/provider.d.ts +1 -1
  120. package/dist/types/react/core/wallet/provider.d.ts.map +1 -0
  121. package/dist/types/react/native/ui/prebuilt/Account/address.d.ts +54 -0
  122. package/dist/types/react/native/ui/prebuilt/Account/address.d.ts.map +1 -0
  123. package/dist/types/react/native/ui/prebuilt/Account/avatar.d.ts +141 -0
  124. package/dist/types/react/native/ui/prebuilt/Account/avatar.d.ts.map +1 -0
  125. package/dist/types/react/native/ui/prebuilt/Account/balance.d.ts +144 -0
  126. package/dist/types/react/native/ui/prebuilt/Account/balance.d.ts.map +1 -0
  127. package/dist/types/react/native/ui/prebuilt/Account/blobbie.d.ts +34 -0
  128. package/dist/types/react/native/ui/prebuilt/Account/blobbie.d.ts.map +1 -0
  129. package/dist/types/react/native/ui/prebuilt/Account/name.d.ts +118 -0
  130. package/dist/types/react/native/ui/prebuilt/Account/name.d.ts.map +1 -0
  131. package/dist/types/react/native/ui/prebuilt/Wallet/icon.d.ts +100 -0
  132. package/dist/types/react/native/ui/prebuilt/Wallet/icon.d.ts.map +1 -0
  133. package/dist/types/react/native/ui/prebuilt/Wallet/name.d.ts +94 -0
  134. package/dist/types/react/native/ui/prebuilt/Wallet/name.d.ts.map +1 -0
  135. package/dist/types/react/web/ui/ConnectWallet/Blobbie.d.ts +1 -1
  136. package/dist/types/react/web/ui/ConnectWallet/Blobbie.d.ts.map +1 -1
  137. package/dist/types/react/web/ui/ConnectWallet/ConnectButton.d.ts.map +1 -1
  138. package/dist/types/react/web/ui/ConnectWallet/Details.d.ts +1 -1
  139. package/dist/types/react/web/ui/ConnectWallet/Details.d.ts.map +1 -1
  140. package/dist/types/react/web/ui/prebuilt/Account/avatar.d.ts.map +1 -1
  141. package/dist/types/react/web/ui/prebuilt/Account/balance.d.ts +1 -42
  142. package/dist/types/react/web/ui/prebuilt/Account/balance.d.ts.map +1 -1
  143. package/dist/types/react/web/ui/prebuilt/Account/blobbie.d.ts.map +1 -1
  144. package/dist/types/react/web/ui/prebuilt/Wallet/icon.d.ts +2 -10
  145. package/dist/types/react/web/ui/prebuilt/Wallet/icon.d.ts.map +1 -1
  146. package/dist/types/react/web/ui/prebuilt/Wallet/name.d.ts +1 -18
  147. package/dist/types/react/web/ui/prebuilt/Wallet/name.d.ts.map +1 -1
  148. package/dist/types/utils/shortenLargeNumber.d.ts.map +1 -1
  149. package/dist/types/version.d.ts +1 -1
  150. package/dist/types/version.d.ts.map +1 -1
  151. package/dist/types/wallets/manager/index.d.ts +1 -0
  152. package/dist/types/wallets/manager/index.d.ts.map +1 -1
  153. package/package.json +4 -4
  154. package/src/exports/react.native.ts +47 -0
  155. package/src/exports/react.ts +5 -3
  156. package/src/react/{web/ui/prebuilt/Account → core/account}/provider.test.tsx +1 -1
  157. package/src/react/{web/ui/prebuilt/Account → core/account}/provider.tsx +3 -5
  158. package/src/react/core/utils/account.ts +146 -0
  159. package/src/react/core/utils/walletIcon.ts +28 -0
  160. package/src/react/core/utils/walletname.ts +53 -0
  161. package/src/react/{web/ui/prebuilt/Wallet → core/wallet}/provider.tsx +1 -1
  162. package/src/react/native/ui/prebuilt/Account/address.tsx +65 -0
  163. package/src/react/native/ui/prebuilt/Account/avatar.tsx +225 -0
  164. package/src/react/native/ui/prebuilt/Account/balance.tsx +221 -0
  165. package/src/react/native/ui/prebuilt/Account/blobbie.tsx +94 -0
  166. package/src/react/native/ui/prebuilt/Account/name.tsx +180 -0
  167. package/src/react/native/ui/prebuilt/Wallet/icon.tsx +124 -0
  168. package/src/react/native/ui/prebuilt/Wallet/name.tsx +112 -0
  169. package/src/react/web/ui/ConnectWallet/Blobbie.tsx +1 -18
  170. package/src/react/web/ui/ConnectWallet/ConnectButton.tsx +1 -1
  171. package/src/react/web/ui/ConnectWallet/Details.test.tsx +1 -1
  172. package/src/react/web/ui/ConnectWallet/Details.tsx +7 -7
  173. package/src/react/web/ui/prebuilt/Account/address.test.tsx +1 -1
  174. package/src/react/web/ui/prebuilt/Account/address.tsx +1 -1
  175. package/src/react/web/ui/prebuilt/Account/avatar.test.tsx +1 -1
  176. package/src/react/web/ui/prebuilt/Account/avatar.tsx +1 -2
  177. package/src/react/web/ui/prebuilt/Account/balance.test.tsx +7 -5
  178. package/src/react/web/ui/prebuilt/Account/balance.tsx +10 -130
  179. package/src/react/web/ui/prebuilt/Account/blobbie.tsx +1 -1
  180. package/src/react/web/ui/prebuilt/Account/name.test.tsx +1 -1
  181. package/src/react/web/ui/prebuilt/Account/name.tsx +1 -1
  182. package/src/react/web/ui/prebuilt/Wallet/icon.test.tsx +3 -2
  183. package/src/react/web/ui/prebuilt/Wallet/icon.tsx +6 -30
  184. package/src/react/web/ui/prebuilt/Wallet/name.test.tsx +6 -2
  185. package/src/react/web/ui/prebuilt/Wallet/name.tsx +2 -52
  186. package/src/utils/shortenLargeNumber.ts +3 -0
  187. package/src/version.ts +1 -1
  188. package/src/wallets/manager/index.ts +4 -1
  189. package/dist/cjs/react/web/ui/prebuilt/Account/provider.js.map +0 -1
  190. package/dist/cjs/react/web/ui/prebuilt/Wallet/provider.js.map +0 -1
  191. package/dist/esm/react/web/ui/prebuilt/Account/provider.js.map +0 -1
  192. package/dist/esm/react/web/ui/prebuilt/Wallet/provider.js.map +0 -1
  193. package/dist/types/react/web/ui/prebuilt/Account/provider.d.ts.map +0 -1
  194. package/dist/types/react/web/ui/prebuilt/Wallet/provider.d.ts.map +0 -1
  195. /package/dist/cjs/react/{web/ui/prebuilt/Wallet → core/wallet}/provider.js +0 -0
  196. /package/dist/esm/react/{web/ui/prebuilt/Wallet → core/wallet}/provider.js +0 -0
  197. /package/src/react/{web/ui/prebuilt/Wallet → core/wallet}/provider.test.tsx +0 -0
@@ -0,0 +1,221 @@
1
+ "use client";
2
+
3
+ import { type UseQueryOptions, useQuery } from "@tanstack/react-query";
4
+ import { Text, type TextProps } from "react-native";
5
+ import type { Chain } from "../../../../../chains/types.js";
6
+ import type { SupportedFiatCurrency } from "../../../../../pay/convert/type.js";
7
+ import { useActiveWalletChain } from "../../../../../react/core/hooks/wallets/useActiveWalletChain.js";
8
+ import { getAddress } from "../../../../../utils/address.js";
9
+ import { useAccountContext } from "../../../../core/account/provider.js";
10
+ import {
11
+ type AccountBalanceInfo,
12
+ formatAccountFiatBalance,
13
+ loadAccountBalance,
14
+ } from "../../../../core/utils/account.js";
15
+ import { formatAccountTokenBalance } from "../../../../core/utils/account.js";
16
+
17
+ /**
18
+ * Props for the AccountBalance component
19
+ * @component
20
+ * @wallet
21
+ */
22
+ export interface AccountBalanceProps extends Omit<TextProps, "children"> {
23
+ /**
24
+ * The network to fetch balance on
25
+ * If not passed, the component will use the current chain that the wallet is connected to (`useActiveWalletChain()`)
26
+ */
27
+ chain?: Chain;
28
+ /**
29
+ * By default this component will fetch the balance for the native token on a given chain
30
+ * If you want to fetch balance for an ERC20 token, use the `tokenAddress` props
31
+ */
32
+ tokenAddress?: string;
33
+ /**
34
+ * A function to format the balance's display value
35
+ * use this function to transform the balance display value like round up the number
36
+ * Particularly useful to avoid overflowing-UI issues
37
+ */
38
+ formatFn?: (props: AccountBalanceInfo) => string;
39
+ /**
40
+ * This component will be shown while the balance of the account is being fetched
41
+ * If not passed, the component will return `null`.
42
+ *
43
+ * You can/should pass a loading sign or spinner to this prop.
44
+ * @example
45
+ * ```tsx
46
+ * <AccountBalance
47
+ * chain={ethereum}
48
+ * loadingComponent={<Spinner />}
49
+ * />
50
+ * ```
51
+ */
52
+ loadingComponent?: React.ComponentType;
53
+ /**
54
+ * This component will be shown if the balance fails to be retreived
55
+ * If not passed, the component will return `null`.
56
+ *
57
+ * You can/should pass a descriptive text/component to this prop, indicating that the
58
+ * balance was not fetched succesfully
59
+ * @example
60
+ * ```tsx
61
+ * <AccountBalance
62
+ * chain={nonExistentChain}
63
+ * fallbackComponent={"Failed to load"}
64
+ * />
65
+ * ```
66
+ */
67
+ fallbackComponent?: React.ComponentType;
68
+ /**
69
+ * Optional `useQuery` params
70
+ */
71
+ queryOptions?: Omit<
72
+ UseQueryOptions<AccountBalanceInfo>,
73
+ "queryFn" | "queryKey"
74
+ >;
75
+
76
+ /**
77
+ * Show the token balance in a supported fiat currency (e.g "USD")
78
+ */
79
+ showBalanceInFiat?: SupportedFiatCurrency;
80
+ }
81
+
82
+ /**
83
+ * This component fetches and shows the balance of the wallet address on a given chain.
84
+ * It inherits all the attributes of a HTML <span> component, hence you can style it just like how you would style a normal <span>
85
+ *
86
+ *
87
+ * @example
88
+ * ### Basic usage
89
+ * ```tsx
90
+ * import { AccountProvider, AccountBalance } from "thirdweb/react";
91
+ * import { ethereum } from "thirdweb/chains";
92
+ *
93
+ * <AccountProvider address="0x...">
94
+ * <AccountBalance chain={ethereum} />
95
+ * </AccountProvider>
96
+ * ```
97
+ * Result:
98
+ * ```html
99
+ * <span>1.091435 ETH</span>
100
+ * ```
101
+ *
102
+ *
103
+ * ### Format the balance (round up, shorten etc.)
104
+ * The AccountBalance component accepts a `formatFn` which takes in an object of type `AccountBalanceInfo` and outputs a string
105
+ * The function is used to modify the display value of the wallet balance (either in crypto or fiat)
106
+ *
107
+ * ```tsx
108
+ * import type { AccountBalanceInfo } from "thirdweb/react";
109
+ * import { formatNumber } from "thirdweb/utils";
110
+ *
111
+ * const format = (props: AccountInfoBalance):string => `${formatNumber(props.balance, 1)} ${props.symbol.toLowerCase()}`
112
+ *
113
+ * <AccountBalance formatFn={format} />
114
+ * ```
115
+ *
116
+ * Result:
117
+ * ```html
118
+ * <span>1.1 eth</span> // the balance is rounded up to 1 decimal and the symbol is lowercased
119
+ * ```
120
+ *
121
+ * ### Show a loading sign when the balance is being fetched
122
+ * ```tsx
123
+ * import { AccountProvider, AccountBalance } from "thirdweb/react";
124
+ *
125
+ * <AccountProvider address="0x...">
126
+ * <AccountBalance
127
+ * chain={ethereum}
128
+ * loadingComponent={<Spinner />}
129
+ * />
130
+ * </AccountProvider>
131
+ * ```
132
+ *
133
+ * ### Fallback to something when the balance fails to resolve
134
+ * ```tsx
135
+ * <AccountProvider address="0x...">
136
+ * <AccountBalance
137
+ * chain={nonExistentChain}
138
+ * fallbackComponent={"Failed to load"}
139
+ * />
140
+ * </AccountProvider>
141
+ * ```
142
+ *
143
+ * ### Custom query options for useQuery
144
+ * This component uses `@tanstack-query`'s useQuery internally.
145
+ * You can use the `queryOptions` prop for more fine-grained control
146
+ * ```tsx
147
+ * <AccountBalance
148
+ * queryOptions={{
149
+ * enabled: isEnabled,
150
+ * retry: 4,
151
+ * }}
152
+ * />
153
+ * ```
154
+ *
155
+ * @component
156
+ * @wallet
157
+ * @beta
158
+ */
159
+ export function AccountBalance({
160
+ chain,
161
+ tokenAddress,
162
+ loadingComponent,
163
+ fallbackComponent,
164
+ queryOptions,
165
+ formatFn,
166
+ showBalanceInFiat,
167
+ ...restProps
168
+ }: AccountBalanceProps) {
169
+ const { address, client } = useAccountContext();
170
+ const walletChain = useActiveWalletChain();
171
+ const chainToLoad = chain || walletChain;
172
+ const balanceQuery = useQuery({
173
+ queryKey: [
174
+ "internal_account_balance",
175
+ chainToLoad?.id || -1,
176
+ address,
177
+ { tokenAddress },
178
+ showBalanceInFiat,
179
+ ] as const,
180
+ queryFn: async (): Promise<AccountBalanceInfo> =>
181
+ loadAccountBalance({
182
+ chain: chainToLoad,
183
+ client,
184
+ address: getAddress(address),
185
+ tokenAddress: tokenAddress ? getAddress(tokenAddress) : undefined,
186
+ showBalanceInFiat,
187
+ }),
188
+ retry: false,
189
+ ...queryOptions,
190
+ });
191
+
192
+ if (balanceQuery.isLoading) {
193
+ return loadingComponent || null;
194
+ }
195
+
196
+ if (balanceQuery.data === undefined) {
197
+ return fallbackComponent || null;
198
+ }
199
+
200
+ // Prioritize using the formatFn from users
201
+ if (formatFn) {
202
+ return <Text {...restProps}>{formatFn(balanceQuery.data)}</Text>;
203
+ }
204
+
205
+ if (showBalanceInFiat) {
206
+ return (
207
+ <Text {...restProps}>
208
+ {formatAccountFiatBalance({ ...balanceQuery.data, decimals: 2 })}
209
+ </Text>
210
+ );
211
+ }
212
+
213
+ return (
214
+ <Text {...restProps}>
215
+ {formatAccountTokenBalance({
216
+ ...balanceQuery.data,
217
+ decimals: balanceQuery.data.balance < 1 ? 3 : 2,
218
+ })}
219
+ </Text>
220
+ );
221
+ }
@@ -0,0 +1,94 @@
1
+ import { hexToNumber } from "@noble/curves/abstract/utils";
2
+ import { useMemo } from "react";
3
+ import { StyleSheet, View, type ViewStyle } from "react-native";
4
+ import { Defs, LinearGradient, Rect, Stop, Svg } from "react-native-svg";
5
+ import { useAccountContext } from "../../../../core/account/provider.js";
6
+ import { COLOR_OPTIONS } from "../../../../core/utils/account.js";
7
+ /**
8
+ * Props for the Blobbie component
9
+ * @component
10
+ */
11
+ export type BlobbieProps = {
12
+ address: string;
13
+ size: number;
14
+ style?: ViewStyle;
15
+ };
16
+
17
+ /**
18
+ * A wrapper for the Blobbie component
19
+ * @param props BlobbieProps
20
+ * @beta
21
+ * @wallet
22
+ */
23
+ export function AccountBlobbie(props: Omit<BlobbieProps, "address">) {
24
+ const { address } = useAccountContext();
25
+ return <Blobbie {...props} address={address} />;
26
+ }
27
+
28
+ /**
29
+ * A unique gradient avatar based on the provided address.
30
+ * @param props The component props.
31
+ * @param props.address The address to generate the gradient with.
32
+ * @param props.style The style for the component
33
+ * @param props.size The size of each side of the square avatar (in pixels)
34
+ * @component
35
+ * @wallet
36
+ * @example
37
+ * ```tsx
38
+ * import { Blobbie } from "thirdweb/react";
39
+ *
40
+ * <Blobbie address="0x...." style={{ width: 40, height: 40 }} />
41
+ * ```
42
+ */
43
+ export function Blobbie(props: BlobbieProps) {
44
+ const colors = useMemo(
45
+ () =>
46
+ COLOR_OPTIONS[
47
+ Number(hexToNumber(props.address.slice(2, 4))) % COLOR_OPTIONS.length
48
+ ] as [string, string],
49
+ [props.address],
50
+ );
51
+
52
+ const containerStyle = useMemo(() => {
53
+ const baseStyle = props.style || {};
54
+ if (props.size) {
55
+ return {
56
+ ...baseStyle,
57
+ width: props.size,
58
+ height: props.size,
59
+ };
60
+ }
61
+ return baseStyle;
62
+ }, [props.style, props.size]);
63
+
64
+ const gradientUniqueId = `grad${colors[0]}+${colors[1]}`.replace(
65
+ /[^a-zA-Z0-9 ]/g,
66
+ "",
67
+ );
68
+
69
+ return (
70
+ <View style={[styles.container, containerStyle]}>
71
+ <Svg height="100%" width="100%" style={StyleSheet.absoluteFillObject}>
72
+ <Defs>
73
+ <LinearGradient
74
+ id={gradientUniqueId}
75
+ x1="0%"
76
+ y1="100%"
77
+ x2="100%"
78
+ y2="0%"
79
+ >
80
+ <Stop offset="0" stopColor={colors[0]} />
81
+ <Stop offset="1" stopColor={colors[1]} />
82
+ </LinearGradient>
83
+ </Defs>
84
+ <Rect width="100%" height="100%" fill={`url(#${gradientUniqueId})`} />
85
+ </Svg>
86
+ </View>
87
+ );
88
+ }
89
+
90
+ const styles = StyleSheet.create({
91
+ container: {
92
+ overflow: "hidden",
93
+ },
94
+ });
@@ -0,0 +1,180 @@
1
+ "use client";
2
+
3
+ import { type UseQueryOptions, useQuery } from "@tanstack/react-query";
4
+ import { Text, type TextProps } from "react-native";
5
+ import {
6
+ type ResolveNameOptions,
7
+ resolveName,
8
+ } from "../../../../../extensions/ens/resolve-name.js";
9
+ import { getSocialProfiles } from "../../../../../social/profiles.js";
10
+ import type { SocialProfile } from "../../../../../social/types.js";
11
+ import { useAccountContext } from "../../../../core/account/provider.js";
12
+
13
+ /**
14
+ * Props for the AccountName component
15
+ * @component
16
+ * @wallet
17
+ */
18
+ export interface AccountNameProps
19
+ extends Omit<TextProps, "children">,
20
+ Omit<ResolveNameOptions, "client" | "address"> {
21
+ /**
22
+ * A function used to transform (format) the name of the account.
23
+ * it should take in a string and output a string.
24
+ *
25
+ * This function is particularly useful
26
+ */
27
+ formatFn?: (str: string) => string;
28
+ /**
29
+ * Use this prop to prioritize the social profile that you want to display
30
+ * This is useful for a wallet containing multiple social profiles
31
+ */
32
+ socialType?: SocialProfile["type"];
33
+ /**
34
+ * This component will be shown while the name of the account is being fetched
35
+ * If not passed, the component will return `null`.
36
+ *
37
+ * You can pass a loading sign or spinner to this prop.
38
+ * @example
39
+ * ```tsx
40
+ * <AccountName loadingComponent={<Spinner />} />
41
+ * ```
42
+ */
43
+ loadingComponent?: React.ComponentType;
44
+ /**
45
+ * This component will be shown if the request for fetching the name is done but could not retreive any result.
46
+ * You can pass the wallet address as the fallback option if that's the case.
47
+ *
48
+ * If not passed, the component will return `null`
49
+ *
50
+ * @example
51
+ * ```tsx
52
+ * <AccountName fallbackComponent={"0x1234....3f3f"} />
53
+ * ```
54
+ */
55
+ fallbackComponent?: React.ComponentType;
56
+ /**
57
+ * Optional `useQuery` params
58
+ */
59
+ queryOptions?: Omit<UseQueryOptions<string>, "queryFn" | "queryKey">;
60
+ }
61
+
62
+ /**
63
+ * This component is used to display the name of the account.
64
+ * A "name" in this context is the username, or account of the social profiles that the wallet may have.
65
+ * In case a name is not found or failed to resolve, you can always fallback to displaying the wallet address instead by using the `fallbackComponent` prop.
66
+ *
67
+ * This component inherits all attribute of a native HTML <span> element, so you can style it just like how you would style a <span>.
68
+ *
69
+ * @param props
70
+ * @returns A `<span>` containing the name of the account
71
+ * ```html
72
+ * <span>{name}</span>
73
+ * ```
74
+ *
75
+ * @example
76
+ * ### Basic usage
77
+ * ```tsx
78
+ * import { AccountProvider, AccountName } from "thirdweb/react";
79
+ *
80
+ * <AccountProvider address="0x1234...3f3f" client={client}>
81
+ * <AccountName />
82
+ * </AccountProvider>
83
+ * ```
84
+ *
85
+ * ### Show wallet address while social name is being loaded
86
+ * ```tsx
87
+ * <AccountName
88
+ * loadingComponent={<AccountAddress />}
89
+ * />
90
+ * ```
91
+ *
92
+ *
93
+ * ### Fallback to showing wallet address if fail to resolve social name
94
+ * ```tsx
95
+ * <AccountName
96
+ * fallbackComponent={<AccountAddress />}
97
+ * />
98
+ * ```
99
+ *
100
+ * ### Transform the account name using `formatFn` prop
101
+ * ```tsx
102
+ * import { isAddress, shortenAddress } from "thirdweb/utils";
103
+ * import { AccountProvider, AccountName } from "thirdweb/react";
104
+ *
105
+ * // Let's say we want the name to be capitalized without using CSS
106
+ * const formatName = (name: string) => name.toUpperCase();
107
+ *
108
+ * return <AccountName formatFn={formatName} />
109
+ * ```
110
+ *
111
+ *
112
+ * ### Custom query options for useQuery
113
+ * This component uses `@tanstack-query`'s useQuery internally.
114
+ * You can use the `queryOptions` prop for more fine-grained control
115
+ * ```tsx
116
+ * <AccountName
117
+ * queryOptions={{
118
+ * enabled: isEnabled,
119
+ * retry: 3
120
+ * }}
121
+ * />
122
+ * ```
123
+ *
124
+ * @component
125
+ * @wallet
126
+ * @beta
127
+ */
128
+ export function AccountName({
129
+ resolverAddress,
130
+ resolverChain,
131
+ socialType,
132
+ formatFn,
133
+ queryOptions,
134
+ loadingComponent,
135
+ fallbackComponent,
136
+ ...restProps
137
+ }: AccountNameProps) {
138
+ const { address, client } = useAccountContext();
139
+ const nameQuery = useQuery({
140
+ queryKey: [
141
+ "account-name",
142
+ address,
143
+ { socialType },
144
+ { resolverAddress, resolverChain },
145
+ ],
146
+ queryFn: async () => {
147
+ const [socialData, ensName] = await Promise.all([
148
+ getSocialProfiles({ address, client }),
149
+ resolveName({
150
+ client,
151
+ address,
152
+ resolverAddress,
153
+ resolverChain,
154
+ }),
155
+ ]);
156
+
157
+ const name =
158
+ socialData?.filter(
159
+ (p) => p.name && (socialType ? p.type === socialType : true),
160
+ )[0]?.name || ensName;
161
+
162
+ if (!name) {
163
+ throw new Error("Failed to resolve account name");
164
+ }
165
+ return formatFn ? formatFn(name) : name;
166
+ },
167
+ retry: false,
168
+ ...queryOptions,
169
+ });
170
+
171
+ if (nameQuery.isLoading) {
172
+ return loadingComponent || null;
173
+ }
174
+
175
+ if (!nameQuery.data) {
176
+ return fallbackComponent || null;
177
+ }
178
+
179
+ return <Text {...restProps}>{nameQuery.data}</Text>;
180
+ }
@@ -0,0 +1,124 @@
1
+ "use client";
2
+
3
+ import type { UseQueryOptions } from "@tanstack/react-query";
4
+ import { Image, type ImageProps } from "react-native";
5
+ import { SvgXml, type XmlProps } from "react-native-svg";
6
+ import type { AuthOption } from "../../../../../wallets/types.js";
7
+ import { useWalletIcon } from "../../../../core/utils/walletIcon.js";
8
+ import { getAuthProviderImage } from "../../components/WalletImage.js";
9
+
10
+ export interface WalletIconProps extends Omit<ImageProps, "uri"> {
11
+ /**
12
+ * This component will be shown while the icon of the wallet is being fetched
13
+ * If not passed, the component will return `null`.
14
+ *
15
+ * You can/should pass a loading sign or spinner to this prop.
16
+ * @example
17
+ * ```tsx
18
+ * <WalletIcon loadingComponent={<Spinner />} />
19
+ * ```
20
+ */
21
+ loadingComponent?: React.ComponentType;
22
+ /**
23
+ * This component will be shown if the icon fails to be retrieved
24
+ * If not passed, the component will return `null`.
25
+ *
26
+ * You can/should pass a descriptive text/component to this prop, indicating that the
27
+ * icon was not fetched successfully
28
+ * @example
29
+ * ```tsx
30
+ * <WalletIcon fallbackComponent={<span>Failed to load</span>}
31
+ * />
32
+ * ```
33
+ */
34
+ fallbackComponent?: React.ComponentType;
35
+ /**
36
+ * Optional `useQuery` params
37
+ */
38
+ queryOptions?: Omit<UseQueryOptions<string>, "queryFn" | "queryKey">;
39
+ }
40
+
41
+ /**
42
+ * This component tries to resolve the icon of a given wallet, then return an image.
43
+ * @returns an <img /> with the src of the wallet icon
44
+ *
45
+ * @example
46
+ * ### Basic usage
47
+ * ```tsx
48
+ * import { WalletProvider, WalletIcon } from "thirdweb/react";
49
+ *
50
+ * <WalletProvider id="io.metamask">
51
+ * <WalletIcon />
52
+ * </WalletProvider>
53
+ * ```
54
+ *
55
+ * Result: An <img /> component with the src of the icon
56
+ * ```html
57
+ * <img src="metamask-icon.png" />
58
+ * ```
59
+ *
60
+ * ### Show a loading sign while the icon is being loaded
61
+ * ```tsx
62
+ * <WalletIcon loadingComponent={<Spinner />} />
63
+ * ```
64
+ *
65
+ * ### Fallback to a dummy image if the wallet icon fails to resolve
66
+ * ```tsx
67
+ * <WalletIcon fallbackComponent={<img src="blank-image.png" />} />
68
+ * ```
69
+ *
70
+ * ### Usage with queryOptions
71
+ * WalletIcon uses useQuery() from tanstack query internally.
72
+ * It allows you to pass a custom queryOptions of your choice for more control of the internal fetching logic
73
+ * ```tsx
74
+ * <WalletIcon queryOptions={{ enabled: someLogic, retry: 3, }} />
75
+ * ```
76
+ *
77
+ * @component
78
+ * @wallet
79
+ * @beta
80
+ */
81
+ export function WalletIcon({
82
+ loadingComponent,
83
+ fallbackComponent,
84
+ queryOptions,
85
+ ...restProps
86
+ }: WalletIconProps) {
87
+ const imageQuery = useWalletIcon({ queryOptions });
88
+ if (imageQuery.isLoading) {
89
+ return loadingComponent || null;
90
+ }
91
+ if (!imageQuery.data) {
92
+ return fallbackComponent || null;
93
+ }
94
+ return <Image source={{ uri: imageQuery.data }} {...restProps} />;
95
+ }
96
+
97
+ export interface SocialIconProps extends Omit<XmlProps, "xml"> {
98
+ provider: AuthOption | (string & {});
99
+ }
100
+
101
+ /**
102
+ * Social auth provider icon
103
+ * @returns an <img /> component with the src set to the svg
104
+ *
105
+ * @example
106
+ * ```tsx
107
+ * import { SocialIcon } from "thirdweb/react";
108
+ *
109
+ * <SocialIcon provider="google" />
110
+ * ```
111
+ *
112
+ * Result: An <img /> component with the src of the icon
113
+ * ```html
114
+ * <img src="google-icon-svg" />
115
+ * ```
116
+ *
117
+ * @component
118
+ * @wallet
119
+ * @beta
120
+ */
121
+ export function SocialIcon({ provider, ...restProps }: SocialIconProps) {
122
+ const src = getAuthProviderImage(provider);
123
+ return <SvgXml xml={src} {...restProps} />;
124
+ }