@b3dotfun/sdk 0.0.40 → 0.0.41-test.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (151) hide show
  1. package/dist/cjs/anyspend/react/components/AnySpend.d.ts +10 -1
  2. package/dist/cjs/anyspend/react/components/AnySpend.js +7 -3
  3. package/dist/cjs/anyspend/react/components/AnyspendDepositHype.d.ts +8 -0
  4. package/dist/cjs/anyspend/react/components/AnyspendDepositHype.js +6 -3
  5. package/dist/cjs/anyspend/react/components/common/ConnectWalletPayment.js +5 -3
  6. package/dist/cjs/anyspend/react/components/common/CryptoPaySection.d.ts +4 -1
  7. package/dist/cjs/anyspend/react/components/common/CryptoPaySection.js +9 -9
  8. package/dist/cjs/anyspend/react/components/common/CryptoPaymentMethod.js +80 -10
  9. package/dist/cjs/anyspend/react/components/common/CryptoReceiveSection.d.ts +2 -1
  10. package/dist/cjs/anyspend/react/components/common/CryptoReceiveSection.js +39 -37
  11. package/dist/cjs/anyspend/react/components/common/OrderDetails.js +8 -11
  12. package/dist/cjs/anyspend/react/components/common/OrderTokenAmount.d.ts +4 -1
  13. package/dist/cjs/anyspend/react/components/common/OrderTokenAmount.js +23 -9
  14. package/dist/cjs/anyspend/react/components/common/PanelOnramp.d.ts +4 -1
  15. package/dist/cjs/anyspend/react/components/common/PanelOnramp.js +7 -6
  16. package/dist/cjs/anyspend/react/components/common/PointsDetailPanel.d.ts +6 -0
  17. package/dist/cjs/anyspend/react/components/common/PointsDetailPanel.js +14 -0
  18. package/dist/cjs/anyspend/react/contexts/FeatureFlagsContext.d.ts +11 -0
  19. package/dist/cjs/anyspend/react/contexts/FeatureFlagsContext.js +21 -0
  20. package/dist/cjs/anyspend/react/hooks/useAnyspendFlow.d.ts +4 -1
  21. package/dist/cjs/anyspend/react/hooks/useAnyspendFlow.js +13 -5
  22. package/dist/cjs/anyspend/react/providers/AnyspendProvider.d.ts +5 -2
  23. package/dist/cjs/anyspend/react/providers/AnyspendProvider.js +5 -3
  24. package/dist/cjs/anyspend/react/providers/index.d.ts +1 -0
  25. package/dist/cjs/anyspend/react/providers/index.js +3 -0
  26. package/dist/cjs/anyspend/types/api.d.ts +10 -0
  27. package/dist/cjs/anyspend/utils/chain.js +1 -1
  28. package/dist/cjs/bondkit/bondkitToken.d.ts +37 -2
  29. package/dist/cjs/bondkit/bondkitToken.js +268 -2
  30. package/dist/cjs/bondkit/bondkitTokenFactory.d.ts +1 -1
  31. package/dist/cjs/bondkit/bondkitTokenFactory.js +2 -2
  32. package/dist/cjs/bondkit/config.d.ts +1 -1
  33. package/dist/cjs/bondkit/config.js +5 -2
  34. package/dist/cjs/bondkit/constants.d.ts +4 -0
  35. package/dist/cjs/bondkit/constants.js +6 -1
  36. package/dist/cjs/bondkit/index.d.ts +1 -0
  37. package/dist/cjs/bondkit/index.js +4 -1
  38. package/dist/cjs/bondkit/swapService.d.ts +43 -0
  39. package/dist/cjs/bondkit/swapService.js +373 -0
  40. package/dist/cjs/bondkit/types.d.ts +10 -4
  41. package/dist/cjs/bondkit/types.js +4 -5
  42. package/dist/cjs/global-account/react/components/B3Provider/B3Provider.d.ts +4 -3
  43. package/dist/cjs/global-account/react/components/B3Provider/B3Provider.js +37 -9
  44. package/dist/cjs/global-account/react/components/LinkAccount/LinkAccount.js +63 -3
  45. package/dist/cjs/global-account/react/components/ManageAccount/ManageAccount.js +35 -2
  46. package/dist/cjs/global-account/react/components/SignInWithB3/steps/LoginStepCustom.js +4 -3
  47. package/dist/cjs/global-account/react/hooks/useAuthentication.js +1 -2
  48. package/dist/cjs/global-account/react/hooks/useSimBalance.js +2 -2
  49. package/dist/cjs/global-account/react/hooks/useUnifiedChainSwitchAndExecute.js +27 -22
  50. package/dist/esm/anyspend/react/components/AnySpend.d.ts +10 -1
  51. package/dist/esm/anyspend/react/components/AnySpend.js +7 -3
  52. package/dist/esm/anyspend/react/components/AnyspendDepositHype.d.ts +8 -0
  53. package/dist/esm/anyspend/react/components/AnyspendDepositHype.js +6 -3
  54. package/dist/esm/anyspend/react/components/common/ConnectWalletPayment.js +6 -4
  55. package/dist/esm/anyspend/react/components/common/CryptoPaySection.d.ts +4 -1
  56. package/dist/esm/anyspend/react/components/common/CryptoPaySection.js +9 -9
  57. package/dist/esm/anyspend/react/components/common/CryptoPaymentMethod.js +80 -10
  58. package/dist/esm/anyspend/react/components/common/CryptoReceiveSection.d.ts +2 -1
  59. package/dist/esm/anyspend/react/components/common/CryptoReceiveSection.js +39 -37
  60. package/dist/esm/anyspend/react/components/common/OrderDetails.js +8 -11
  61. package/dist/esm/anyspend/react/components/common/OrderTokenAmount.d.ts +4 -1
  62. package/dist/esm/anyspend/react/components/common/OrderTokenAmount.js +23 -9
  63. package/dist/esm/anyspend/react/components/common/PanelOnramp.d.ts +4 -1
  64. package/dist/esm/anyspend/react/components/common/PanelOnramp.js +7 -6
  65. package/dist/esm/anyspend/react/components/common/PointsDetailPanel.d.ts +6 -0
  66. package/dist/esm/anyspend/react/components/common/PointsDetailPanel.js +8 -0
  67. package/dist/esm/anyspend/react/contexts/FeatureFlagsContext.d.ts +11 -0
  68. package/dist/esm/anyspend/react/contexts/FeatureFlagsContext.js +17 -0
  69. package/dist/esm/anyspend/react/hooks/useAnyspendFlow.d.ts +4 -1
  70. package/dist/esm/anyspend/react/hooks/useAnyspendFlow.js +13 -5
  71. package/dist/esm/anyspend/react/providers/AnyspendProvider.d.ts +5 -2
  72. package/dist/esm/anyspend/react/providers/AnyspendProvider.js +5 -3
  73. package/dist/esm/anyspend/react/providers/index.d.ts +1 -0
  74. package/dist/esm/anyspend/react/providers/index.js +1 -0
  75. package/dist/esm/anyspend/types/api.d.ts +10 -0
  76. package/dist/esm/anyspend/utils/chain.js +1 -1
  77. package/dist/esm/bondkit/bondkitToken.d.ts +37 -2
  78. package/dist/esm/bondkit/bondkitToken.js +268 -2
  79. package/dist/esm/bondkit/bondkitTokenFactory.d.ts +1 -1
  80. package/dist/esm/bondkit/bondkitTokenFactory.js +2 -2
  81. package/dist/esm/bondkit/config.d.ts +1 -1
  82. package/dist/esm/bondkit/config.js +5 -2
  83. package/dist/esm/bondkit/constants.d.ts +4 -0
  84. package/dist/esm/bondkit/constants.js +5 -0
  85. package/dist/esm/bondkit/index.d.ts +1 -0
  86. package/dist/esm/bondkit/index.js +2 -0
  87. package/dist/esm/bondkit/swapService.d.ts +43 -0
  88. package/dist/esm/bondkit/swapService.js +369 -0
  89. package/dist/esm/bondkit/types.d.ts +10 -4
  90. package/dist/esm/bondkit/types.js +4 -5
  91. package/dist/esm/global-account/react/components/B3Provider/B3Provider.d.ts +4 -3
  92. package/dist/esm/global-account/react/components/B3Provider/B3Provider.js +38 -9
  93. package/dist/esm/global-account/react/components/LinkAccount/LinkAccount.js +65 -5
  94. package/dist/esm/global-account/react/components/ManageAccount/ManageAccount.js +35 -2
  95. package/dist/esm/global-account/react/components/SignInWithB3/steps/LoginStepCustom.js +3 -2
  96. package/dist/esm/global-account/react/hooks/useAuthentication.js +1 -2
  97. package/dist/esm/global-account/react/hooks/useSimBalance.js +2 -2
  98. package/dist/esm/global-account/react/hooks/useUnifiedChainSwitchAndExecute.js +27 -22
  99. package/dist/styles/index.css +1 -1
  100. package/dist/types/anyspend/react/components/AnySpend.d.ts +10 -1
  101. package/dist/types/anyspend/react/components/AnyspendDepositHype.d.ts +8 -0
  102. package/dist/types/anyspend/react/components/common/CryptoPaySection.d.ts +4 -1
  103. package/dist/types/anyspend/react/components/common/CryptoReceiveSection.d.ts +2 -1
  104. package/dist/types/anyspend/react/components/common/OrderTokenAmount.d.ts +4 -1
  105. package/dist/types/anyspend/react/components/common/PanelOnramp.d.ts +4 -1
  106. package/dist/types/anyspend/react/components/common/PointsDetailPanel.d.ts +6 -0
  107. package/dist/types/anyspend/react/contexts/FeatureFlagsContext.d.ts +11 -0
  108. package/dist/types/anyspend/react/hooks/useAnyspendFlow.d.ts +4 -1
  109. package/dist/types/anyspend/react/providers/AnyspendProvider.d.ts +5 -2
  110. package/dist/types/anyspend/react/providers/index.d.ts +1 -0
  111. package/dist/types/anyspend/types/api.d.ts +10 -0
  112. package/dist/types/bondkit/bondkitToken.d.ts +37 -2
  113. package/dist/types/bondkit/bondkitTokenFactory.d.ts +1 -1
  114. package/dist/types/bondkit/config.d.ts +1 -1
  115. package/dist/types/bondkit/constants.d.ts +4 -0
  116. package/dist/types/bondkit/index.d.ts +1 -0
  117. package/dist/types/bondkit/swapService.d.ts +43 -0
  118. package/dist/types/bondkit/types.d.ts +10 -4
  119. package/dist/types/global-account/react/components/B3Provider/B3Provider.d.ts +4 -3
  120. package/package.json +7 -5
  121. package/src/anyspend/react/components/AnySpend.tsx +23 -0
  122. package/src/anyspend/react/components/AnyspendDepositHype.tsx +22 -0
  123. package/src/anyspend/react/components/common/ConnectWalletPayment.tsx +7 -4
  124. package/src/anyspend/react/components/common/CryptoPaySection.tsx +13 -8
  125. package/src/anyspend/react/components/common/CryptoPaymentMethod.tsx +170 -44
  126. package/src/anyspend/react/components/common/CryptoReceiveSection.tsx +63 -45
  127. package/src/anyspend/react/components/common/OrderDetails.tsx +10 -14
  128. package/src/anyspend/react/components/common/OrderTokenAmount.tsx +28 -8
  129. package/src/anyspend/react/components/common/PanelOnramp.tsx +28 -15
  130. package/src/anyspend/react/components/common/PointsDetailPanel.tsx +55 -0
  131. package/src/anyspend/react/contexts/FeatureFlagsContext.tsx +34 -0
  132. package/src/anyspend/react/hooks/useAnyspendFlow.ts +13 -5
  133. package/src/anyspend/react/providers/AnyspendProvider.tsx +11 -6
  134. package/src/anyspend/react/providers/index.ts +1 -0
  135. package/src/anyspend/types/api.ts +10 -0
  136. package/src/anyspend/types/api_req_res.ts +6 -10
  137. package/src/anyspend/utils/chain.ts +1 -1
  138. package/src/bondkit/bondkitToken.ts +323 -3
  139. package/src/bondkit/bondkitTokenFactory.ts +2 -2
  140. package/src/bondkit/config.ts +5 -2
  141. package/src/bondkit/constants.ts +7 -0
  142. package/src/bondkit/index.ts +3 -0
  143. package/src/bondkit/swapService.ts +461 -0
  144. package/src/bondkit/types.ts +12 -5
  145. package/src/global-account/react/components/B3Provider/B3Provider.tsx +54 -15
  146. package/src/global-account/react/components/LinkAccount/LinkAccount.tsx +106 -32
  147. package/src/global-account/react/components/ManageAccount/ManageAccount.tsx +60 -5
  148. package/src/global-account/react/components/SignInWithB3/steps/LoginStepCustom.tsx +4 -2
  149. package/src/global-account/react/hooks/useAuthentication.ts +1 -2
  150. package/src/global-account/react/hooks/useSimBalance.ts +2 -2
  151. package/src/global-account/react/hooks/useUnifiedChainSwitchAndExecute.ts +28 -23
@@ -0,0 +1,461 @@
1
+ import type { Address, Hex, WalletClient, PublicClient } from "viem";
2
+ import {
3
+ parseUnits,
4
+ formatUnits,
5
+ encodeAbiParameters,
6
+ parseAbiParameters,
7
+ getContract,
8
+ createPublicClient,
9
+ http,
10
+ } from "viem";
11
+ import { base } from "viem/chains";
12
+ import { UniversalRouterAddress, QuoterAddress, Permit2Address, BaseMainnetRpcUrl } from "./constants";
13
+ import type { SwapQuote } from "./types";
14
+
15
+ // Minimal ABIs needed for swap functionality
16
+ const UNIVERSAL_ROUTER_ABI = [
17
+ {
18
+ inputs: [
19
+ { name: "commands", type: "bytes" },
20
+ { name: "inputs", type: "bytes[]" },
21
+ { name: "deadline", type: "uint256" },
22
+ ],
23
+ name: "execute",
24
+ outputs: [],
25
+ stateMutability: "payable",
26
+ type: "function",
27
+ },
28
+ ] as const;
29
+
30
+ const QUOTER_ABI = [
31
+ {
32
+ inputs: [
33
+ {
34
+ components: [
35
+ {
36
+ components: [
37
+ { internalType: "Currency", name: "currency0", type: "address" },
38
+ { internalType: "Currency", name: "currency1", type: "address" },
39
+ { internalType: "uint24", name: "fee", type: "uint24" },
40
+ { internalType: "int24", name: "tickSpacing", type: "int24" },
41
+ { internalType: "contract IHooks", name: "hooks", type: "address" },
42
+ ],
43
+ internalType: "struct PoolKey",
44
+ name: "poolKey",
45
+ type: "tuple",
46
+ },
47
+ { internalType: "bool", name: "zeroForOne", type: "bool" },
48
+ { internalType: "uint128", name: "exactAmount", type: "uint128" },
49
+ { internalType: "bytes", name: "hookData", type: "bytes" },
50
+ ],
51
+ internalType: "struct IV4Quoter.QuoteExactSingleParams",
52
+ name: "params",
53
+ type: "tuple",
54
+ },
55
+ ],
56
+ name: "quoteExactInputSingle",
57
+ outputs: [
58
+ { internalType: "uint256", name: "amountOut", type: "uint256" },
59
+ { internalType: "uint256", name: "gasEstimate", type: "uint256" },
60
+ ],
61
+ stateMutability: "nonpayable",
62
+ type: "function",
63
+ },
64
+ ] as const;
65
+
66
+ const ERC20_ABI = [
67
+ {
68
+ inputs: [
69
+ { name: "spender", type: "address" },
70
+ { name: "amount", type: "uint256" },
71
+ ],
72
+ name: "approve",
73
+ outputs: [{ name: "", type: "bool" }],
74
+ stateMutability: "nonpayable",
75
+ type: "function",
76
+ },
77
+ {
78
+ inputs: [
79
+ { name: "owner", type: "address" },
80
+ { name: "spender", type: "address" },
81
+ ],
82
+ name: "allowance",
83
+ outputs: [{ name: "", type: "uint256" }],
84
+ stateMutability: "view",
85
+ type: "function",
86
+ },
87
+ ] as const;
88
+
89
+ const PERMIT2_ABI = [
90
+ {
91
+ inputs: [
92
+ { name: "token", type: "address" },
93
+ { name: "spender", type: "address" },
94
+ { name: "amount", type: "uint160" },
95
+ { name: "expiration", type: "uint48" },
96
+ ],
97
+ name: "approve",
98
+ outputs: [],
99
+ stateMutability: "nonpayable",
100
+ type: "function",
101
+ },
102
+ {
103
+ inputs: [
104
+ { name: "owner", type: "address" },
105
+ { name: "token", type: "address" },
106
+ { name: "spender", type: "address" },
107
+ ],
108
+ name: "allowance",
109
+ outputs: [
110
+ { name: "amount", type: "uint160" },
111
+ { name: "expiration", type: "uint48" },
112
+ { name: "nonce", type: "uint48" },
113
+ ],
114
+ stateMutability: "view",
115
+ type: "function",
116
+ },
117
+ ] as const;
118
+
119
+ const TOKEN_V4_CONFIG_ABI = [
120
+ {
121
+ inputs: [],
122
+ name: "v4Hook",
123
+ outputs: [{ internalType: "address", name: "", type: "address" }],
124
+ stateMutability: "view",
125
+ type: "function",
126
+ },
127
+ {
128
+ inputs: [],
129
+ name: "v4PoolFee",
130
+ outputs: [{ internalType: "uint24", name: "", type: "uint24" }],
131
+ stateMutability: "view",
132
+ type: "function",
133
+ },
134
+ {
135
+ inputs: [],
136
+ name: "v4TickSpacing",
137
+ outputs: [{ internalType: "int24", name: "", type: "int24" }],
138
+ stateMutability: "view",
139
+ type: "function",
140
+ },
141
+ ] as const;
142
+
143
+ // Command and action constants
144
+ const COMMANDS = {
145
+ V4_SWAP: "0x10",
146
+ } as const;
147
+
148
+ const V4_ACTIONS = {
149
+ SWAP_EXACT_IN_SINGLE: 6,
150
+ TAKE_ALL: 15,
151
+ SETTLE_ALL: 12,
152
+ } as const;
153
+
154
+ interface V4PoolConfig {
155
+ hook: string;
156
+ fee: number;
157
+ tickSpacing: number;
158
+ }
159
+
160
+ interface SwapParams {
161
+ tokenIn: Address;
162
+ tokenOut: Address;
163
+ amountIn: string;
164
+ tokenInDecimals: number;
165
+ tokenOutDecimals: number;
166
+ slippageTolerance: number;
167
+ recipient: Address;
168
+ deadline?: number;
169
+ }
170
+
171
+ /**
172
+ * Internal swap service for handling Uniswap V4 swaps between trading token and bondkit token
173
+ */
174
+ export class BondkitSwapService {
175
+ private v4Config: V4PoolConfig | null = null;
176
+ private configInitialized = false;
177
+ private readonly bondkitTokenAddress: Address;
178
+ private readonly publicClient: PublicClient;
179
+
180
+ constructor(bondkitTokenAddress: Address) {
181
+ this.bondkitTokenAddress = bondkitTokenAddress;
182
+ this.publicClient = createPublicClient({
183
+ chain: base,
184
+ transport: http(BaseMainnetRpcUrl),
185
+ }) as PublicClient;
186
+ }
187
+
188
+ /**
189
+ * Initialize V4 pool configuration from bondkit token contract
190
+ */
191
+ private async initializeV4Config(): Promise<void> {
192
+ if (this.configInitialized) {
193
+ return;
194
+ }
195
+
196
+ try {
197
+ const tokenContract = getContract({
198
+ address: this.bondkitTokenAddress,
199
+ abi: TOKEN_V4_CONFIG_ABI,
200
+ client: this.publicClient,
201
+ });
202
+
203
+ const [hook, fee, tickSpacing] = await Promise.all([
204
+ tokenContract.read.v4Hook(),
205
+ tokenContract.read.v4PoolFee(),
206
+ tokenContract.read.v4TickSpacing(),
207
+ ]);
208
+
209
+ this.v4Config = {
210
+ hook: hook as string,
211
+ fee: Number(fee),
212
+ tickSpacing: Number(tickSpacing),
213
+ };
214
+
215
+ this.configInitialized = true;
216
+ } catch (error) {
217
+ console.warn("Failed to initialize V4 configuration:", error);
218
+ // Use fallback configuration
219
+ this.v4Config = {
220
+ hook: "0xB36f4A2FB18b745ef8eD31452781a463d2B3f0cC",
221
+ fee: 30000,
222
+ tickSpacing: 60,
223
+ };
224
+ this.configInitialized = true;
225
+ }
226
+ }
227
+
228
+ /**
229
+ * Get V4 pool configuration
230
+ */
231
+ private async getV4Config(): Promise<V4PoolConfig> {
232
+ await this.initializeV4Config();
233
+ return this.v4Config!;
234
+ }
235
+
236
+ /**
237
+ * Handle token approvals for swap
238
+ */
239
+ private async handleTokenApprovals(
240
+ tokenAddress: Address,
241
+ amountIn: string,
242
+ walletClient: WalletClient,
243
+ deadline: number,
244
+ ): Promise<void> {
245
+ // Skip approvals for ETH
246
+ if (tokenAddress === "0x0000000000000000000000000000000000000000") {
247
+ return;
248
+ }
249
+
250
+ const userAddress = walletClient.account?.address;
251
+ if (!userAddress) {
252
+ throw new Error("No user address found");
253
+ }
254
+
255
+ const erc20Contract = getContract({
256
+ address: tokenAddress,
257
+ abi: ERC20_ABI,
258
+ client: walletClient,
259
+ });
260
+
261
+ const permit2Contract = getContract({
262
+ address: Permit2Address,
263
+ abi: PERMIT2_ABI,
264
+ client: walletClient,
265
+ });
266
+
267
+ // Check ERC20 allowance to Permit2
268
+ const currentAllowance = (await erc20Contract.read.allowance([userAddress, Permit2Address])) as bigint;
269
+
270
+ const requiredAmount = BigInt(amountIn);
271
+
272
+ if (currentAllowance < requiredAmount) {
273
+ await erc20Contract.write.approve(
274
+ [Permit2Address, BigInt("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")],
275
+ {
276
+ account: userAddress,
277
+ chain: base,
278
+ },
279
+ );
280
+ }
281
+
282
+ // Check Permit2 allowance for Universal Router
283
+ const permit2Allowance = (await permit2Contract.read.allowance([
284
+ userAddress,
285
+ tokenAddress,
286
+ UniversalRouterAddress,
287
+ ])) as [bigint, number, number];
288
+
289
+ const [currentPermit2Amount, expiration] = permit2Allowance;
290
+ const currentTime = Math.floor(Date.now() / 1000);
291
+ const isExpired = expiration <= currentTime;
292
+
293
+ if (currentPermit2Amount < requiredAmount || isExpired) {
294
+ await permit2Contract.write.approve(
295
+ [tokenAddress, UniversalRouterAddress, BigInt("0xffffffffffffffffffffffffffffffffffffff"), Number(deadline)],
296
+ {
297
+ account: userAddress,
298
+ chain: base,
299
+ },
300
+ );
301
+ }
302
+ }
303
+
304
+ /**
305
+ * Get swap quote
306
+ */
307
+ async getSwapQuote(params: SwapParams): Promise<SwapQuote | null> {
308
+ try {
309
+ const { tokenIn, tokenOut, amountIn, tokenInDecimals, tokenOutDecimals, slippageTolerance } = params;
310
+
311
+ const v4Config = await this.getV4Config();
312
+ const amountInWei = parseUnits(amountIn, tokenInDecimals);
313
+
314
+ // Determine token order for pool
315
+ const currency0 = tokenIn.toLowerCase() < tokenOut.toLowerCase() ? tokenIn : tokenOut;
316
+ const currency1 = tokenIn.toLowerCase() < tokenOut.toLowerCase() ? tokenOut : tokenIn;
317
+ const zeroForOne = tokenIn.toLowerCase() === currency0.toLowerCase();
318
+
319
+ const poolKey = {
320
+ currency0: currency0 as Address,
321
+ currency1: currency1 as Address,
322
+ fee: v4Config.fee,
323
+ tickSpacing: v4Config.tickSpacing,
324
+ hooks: v4Config.hook as Address,
325
+ };
326
+
327
+ const quoteParams = {
328
+ poolKey,
329
+ zeroForOne,
330
+ exactAmount: BigInt(amountInWei.toString()),
331
+ hookData: "0x" as Hex,
332
+ };
333
+
334
+ const { result } = await this.publicClient.simulateContract({
335
+ address: QuoterAddress,
336
+ abi: QUOTER_ABI,
337
+ functionName: "quoteExactInputSingle",
338
+ args: [quoteParams],
339
+ });
340
+
341
+ const [amountOut] = result;
342
+ const amountOutRaw = formatUnits(amountOut, tokenOutDecimals);
343
+ const amountOutFormatted = parseFloat(amountOutRaw).toFixed(Math.min(6, tokenOutDecimals));
344
+
345
+ // Calculate minimum amount out with slippage
346
+ const slippageMultiplier = (100 - slippageTolerance) / 100;
347
+ const amountOutMinRaw = parseFloat(amountOutFormatted) * slippageMultiplier;
348
+ const amountOutMin = amountOutMinRaw.toFixed(tokenOutDecimals);
349
+
350
+ // Simple execution price calculation
351
+ const rate = parseFloat(amountOutFormatted) / parseFloat(amountIn);
352
+ const executionPrice = `1 = ${rate.toFixed(6)}`;
353
+
354
+ return {
355
+ amountOut: amountOutFormatted,
356
+ amountOutMin,
357
+ priceImpact: "0.0", // Simplified
358
+ executionPrice,
359
+ fee: (v4Config.fee / 10000).toString(),
360
+ };
361
+ } catch (error) {
362
+ console.warn("Error getting swap quote:", error);
363
+ return null;
364
+ }
365
+ }
366
+
367
+ /**
368
+ * Execute swap transaction
369
+ */
370
+ async executeSwap(params: SwapParams, walletClient: WalletClient): Promise<string | null> {
371
+ try {
372
+ const { tokenIn, tokenOut, amountIn, tokenInDecimals, tokenOutDecimals, deadline } = params;
373
+
374
+ const swapDeadline = deadline || Math.floor(Date.now() / 1000) + 3600;
375
+
376
+ if (!walletClient.account) {
377
+ throw new Error("Wallet client must have an account");
378
+ }
379
+
380
+ const amountInWei = parseUnits(amountIn, tokenInDecimals);
381
+
382
+ // Handle token approvals
383
+ await this.handleTokenApprovals(tokenIn, amountInWei.toString(), walletClient, swapDeadline);
384
+
385
+ // Get quote for minimum amount out
386
+ const quote = await this.getSwapQuote(params);
387
+ if (!quote) {
388
+ throw new Error("Unable to get swap quote");
389
+ }
390
+
391
+ const amountOutMinimum = parseUnits(quote.amountOutMin, tokenOutDecimals);
392
+ const v4Config = await this.getV4Config();
393
+
394
+ // Determine token order
395
+ const currency0 = tokenIn.toLowerCase() < tokenOut.toLowerCase() ? tokenIn : tokenOut;
396
+ const currency1 = tokenIn.toLowerCase() < tokenOut.toLowerCase() ? tokenOut : tokenIn;
397
+ const zeroForOne = tokenIn.toLowerCase() === currency0.toLowerCase();
398
+
399
+ const poolKey = [currency0, currency1, v4Config.fee, v4Config.tickSpacing, v4Config.hook];
400
+
401
+ // Encode V4 actions
402
+ const actions = [
403
+ {
404
+ type: V4_ACTIONS.SWAP_EXACT_IN_SINGLE,
405
+ params: [poolKey, zeroForOne, amountInWei, amountOutMinimum, "0x" as `0x${string}`],
406
+ },
407
+ {
408
+ type: V4_ACTIONS.TAKE_ALL,
409
+ params: [(zeroForOne ? currency1 : currency0) as Address, BigInt(0)],
410
+ },
411
+ {
412
+ type: V4_ACTIONS.SETTLE_ALL,
413
+ params: [(zeroForOne ? currency0 : currency1) as Address, amountInWei],
414
+ },
415
+ ];
416
+
417
+ // Encode actions
418
+ const actionTypes = actions.map(action => action.type);
419
+ const actionsBytes = ("0x" + actionTypes.map(type => type.toString(16).padStart(2, "0")).join("")) as Hex;
420
+
421
+ const actionParams = actions.map(action => {
422
+ switch (action.type) {
423
+ case V4_ACTIONS.SWAP_EXACT_IN_SINGLE:
424
+ return encodeAbiParameters(
425
+ parseAbiParameters("((address,address,uint24,int24,address),bool,uint128,uint128,bytes)"),
426
+ [action.params as any],
427
+ );
428
+ case V4_ACTIONS.TAKE_ALL:
429
+ return encodeAbiParameters(parseAbiParameters("address,uint256"), action.params as [Address, bigint]);
430
+ case V4_ACTIONS.SETTLE_ALL:
431
+ return encodeAbiParameters(parseAbiParameters("address,uint256"), action.params as [Address, bigint]);
432
+ default:
433
+ return "0x00" as Hex;
434
+ }
435
+ });
436
+
437
+ const v4SwapInput = encodeAbiParameters(parseAbiParameters("bytes,bytes[]"), [actionsBytes, actionParams]);
438
+
439
+ const commands = COMMANDS.V4_SWAP;
440
+ const inputs = [v4SwapInput];
441
+
442
+ // Execute swap
443
+ const universalRouter = getContract({
444
+ address: UniversalRouterAddress,
445
+ abi: UNIVERSAL_ROUTER_ABI,
446
+ client: walletClient,
447
+ });
448
+
449
+ const txHash = await universalRouter.write.execute([commands, inputs, BigInt(swapDeadline)], {
450
+ account: walletClient.account,
451
+ chain: base,
452
+ value: tokenIn === "0x0000000000000000000000000000000000000000" ? amountInWei : BigInt(0),
453
+ });
454
+
455
+ return txHash;
456
+ } catch (error) {
457
+ console.warn("Error executing swap:", error);
458
+ return null;
459
+ }
460
+ }
461
+ }
@@ -64,12 +64,11 @@ export type DexMigrationEventArgs = {
64
64
  ethForFeeRecipient: bigint;
65
65
  };
66
66
 
67
- // Enum for Status (used in BondkitToken ABI)
67
+ // Enum for Status (matches contract Status enum exactly)
68
68
  export enum TokenStatus {
69
- Inactive = 0, // Assuming mapping from ABI, verify actual enum values if specified elsewhere
70
- BondingPhase = 1,
71
- DexPhase = 2,
72
- Migrated = 3,
69
+ Uninitialized = 0,
70
+ Bonding = 1,
71
+ Dex = 2,
73
72
  }
74
73
 
75
74
  export interface GetTransactionHistoryOptions {
@@ -101,3 +100,11 @@ export interface TransactionResponse {
101
100
  skip: number;
102
101
  data: Transaction[];
103
102
  }
103
+
104
+ export interface SwapQuote {
105
+ amountOut: string;
106
+ amountOutMin: string;
107
+ priceImpact: string;
108
+ executionPrice: string;
109
+ fee: string;
110
+ }
@@ -1,9 +1,17 @@
1
+ import { Users } from "@b3dotfun/b3-api";
1
2
  import { RelayKitProviderWrapper, TooltipProvider, useAuthStore } from "@b3dotfun/sdk/global-account/react";
2
3
  import { PermissionsConfig } from "@b3dotfun/sdk/global-account/types/permissions";
3
4
  import { loadGA4Script } from "@b3dotfun/sdk/global-account/utils/analytics";
5
+ import { ecosystemWalletId } from "@b3dotfun/sdk/shared/constants";
4
6
  import { supportedChains } from "@b3dotfun/sdk/shared/constants/chains/supported";
7
+ import { debugB3React } from "@b3dotfun/sdk/shared/utils/debug";
8
+ import { client } from "@b3dotfun/sdk/shared/utils/thirdweb";
9
+ import { farcasterMiniApp as miniAppConnector } from "@farcaster/miniapp-wagmi-connector";
10
+ import "@reservoir0x/relay-kit-ui/styles.css";
5
11
  import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
6
- import { useCallback, useEffect, useState } from "react";
12
+ import { inAppWalletConnector } from "@thirdweb-dev/wagmi-adapter";
13
+
14
+ import { useCallback, useEffect, useMemo, useState } from "react";
7
15
  import { Toaster } from "sonner";
8
16
  import {
9
17
  getLastAuthProvider,
@@ -18,8 +26,7 @@ import { ClientType, setClientType } from "../../../client-manager";
18
26
  import { StyleRoot } from "../StyleRoot";
19
27
  import { B3Context, B3ContextType } from "./types";
20
28
 
21
- import { Users } from "@b3dotfun/b3-api";
22
- import "@reservoir0x/relay-kit-ui/styles.css";
29
+ const debug = debugB3React("B3Provider");
23
30
 
24
31
  /**
25
32
  * Default permissions configuration for B3 provider
@@ -31,11 +38,6 @@ const DEFAULT_PERMISSIONS = {
31
38
  endDate: new Date(Date.now() + 1000 * 60 * 60 * 24 * 365), // 1 year from now
32
39
  };
33
40
 
34
- export const wagmiConfig = createConfig({
35
- chains: [supportedChains[0], ...supportedChains.slice(1)],
36
- transports: Object.fromEntries(supportedChains.map(chain => [chain.id, http()])) as any,
37
- });
38
-
39
41
  // Create queryClient instance
40
42
  const queryClient = new QueryClient();
41
43
 
@@ -51,6 +53,8 @@ export function B3Provider({
51
53
  simDuneApiKey,
52
54
  toaster,
53
55
  clientType = "rest",
56
+ rpcUrls,
57
+ partnerId,
54
58
  }: {
55
59
  theme: "light" | "dark";
56
60
  children: React.ReactNode;
@@ -63,6 +67,8 @@ export function B3Provider({
63
67
  style?: React.CSSProperties;
64
68
  };
65
69
  clientType?: ClientType;
70
+ rpcUrls?: Record<number, string>;
71
+ partnerId?: string;
66
72
  }) {
67
73
  // Initialize Google Analytics on mount
68
74
  useEffect(() => {
@@ -74,10 +80,42 @@ export function B3Provider({
74
80
  setClientType(clientType);
75
81
  }, [clientType]);
76
82
 
83
+ const ecocystemConfig = useMemo(() => {
84
+ if (!partnerId) return undefined;
85
+
86
+ return {
87
+ ecosystemId: ecosystemWalletId,
88
+ partnerId: partnerId,
89
+ client,
90
+ };
91
+ }, [partnerId]);
92
+
93
+ /**
94
+ * Creates wagmi config with optional custom RPC URLs
95
+ * @param rpcUrls - Optional mapping of chain IDs to RPC URLs
96
+ */
97
+ const wagmiConfig = useMemo(
98
+ () =>
99
+ createConfig({
100
+ chains: [supportedChains[0], ...supportedChains.slice(1)],
101
+ transports: Object.fromEntries(supportedChains.map(chain => [chain.id, http(rpcUrls?.[chain.id])])) as any,
102
+ connectors: [
103
+ inAppWalletConnector({
104
+ ...(ecocystemConfig || {}),
105
+ client,
106
+ }),
107
+ miniAppConnector(),
108
+ // injected(),
109
+ // coinbaseWallet({ appName: "HypeDuel" }),
110
+ ],
111
+ }),
112
+ [partnerId],
113
+ );
114
+
77
115
  return (
78
- <WagmiProvider config={wagmiConfig}>
79
- <QueryClientProvider client={queryClient}>
80
- <ThirdwebProvider>
116
+ <ThirdwebProvider>
117
+ <WagmiProvider config={wagmiConfig} reconnectOnMount={false}>
118
+ <QueryClientProvider client={queryClient}>
81
119
  <TooltipProvider>
82
120
  <InnerProvider
83
121
  accountOverride={accountOverride}
@@ -94,9 +132,9 @@ export function B3Provider({
94
132
  </RelayKitProviderWrapper>
95
133
  </InnerProvider>
96
134
  </TooltipProvider>
97
- </ThirdwebProvider>
98
- </QueryClientProvider>
99
- </WagmiProvider>
135
+ </QueryClientProvider>
136
+ </WagmiProvider>
137
+ </ThirdwebProvider>
100
138
  );
101
139
  }
102
140
 
@@ -125,6 +163,7 @@ export function InnerProvider({
125
163
  const wallets = useConnectedWallets();
126
164
  const setActiveWallet = useSetActiveWallet();
127
165
  const isAuthenticated = useAuthStore(state => state.isAuthenticated);
166
+ debug("@@wallets", wallets);
128
167
 
129
168
  const [user, setUser] = useState<Users | undefined>(() => {
130
169
  // Try to restore user from localStorage on initialization
@@ -158,7 +197,7 @@ export function InnerProvider({
158
197
  (wallet: Wallet) => {
159
198
  setManuallySelectedWallet(wallet);
160
199
  const account = wallet.getAccount();
161
- console.log("@@gio:setWallet", wallet.id, account?.address);
200
+ debug("@@setWallet", wallet.id, account?.address);
162
201
  setActiveWallet(wallet);
163
202
  },
164
203
  [setManuallySelectedWallet, setActiveWallet],