@glowlabs-org/utils 0.2.113 → 0.2.115
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.
- package/dist/cjs/browser.js +1 -1
- package/dist/cjs/{farms-router-djet_3Cn.js → farms-router-CuZE5YJ_.js} +414 -252
- package/dist/cjs/farms-router-CuZE5YJ_.js.map +1 -0
- package/dist/cjs/index.js +1 -1
- package/dist/cjs/lib/hooks/use-offchain-fractions.d.ts +2 -2
- package/dist/esm/browser.js +2 -2
- package/dist/esm/{farms-router-CDZkFsAs.js → farms-router-BUi6rxeX.js} +414 -252
- package/dist/esm/farms-router-BUi6rxeX.js.map +1 -0
- package/dist/esm/index.js +2 -2
- package/dist/esm/lib/hooks/use-offchain-fractions.d.ts +2 -2
- package/package.json +1 -1
- package/src/constants/addresses.ts +1 -1
- package/src/lib/hooks/use-offchain-fractions.ts +390 -319
- package/dist/cjs/farms-router-djet_3Cn.js.map +0 -1
- package/dist/esm/farms-router-CDZkFsAs.js.map +0 -1
|
@@ -1,8 +1,12 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
type WalletClient,
|
|
3
|
+
type PublicClient,
|
|
4
|
+
type Address,
|
|
5
|
+
formatEther,
|
|
6
|
+
} from "viem";
|
|
2
7
|
import { OFFCHAIN_FRACTIONS_ABI } from "../abis/offchainFractions";
|
|
3
8
|
import { ERC20_ABI } from "../abis/erc20.abi";
|
|
4
9
|
import { getAddresses } from "../../constants/addresses";
|
|
5
|
-
import { formatEther } from "viem";
|
|
6
10
|
|
|
7
11
|
export enum OffchainFractionsError {
|
|
8
12
|
CONTRACT_NOT_AVAILABLE = "Contract not available",
|
|
@@ -59,43 +63,46 @@ export interface RefundDetails {
|
|
|
59
63
|
useCounterfactualAddress: boolean;
|
|
60
64
|
}
|
|
61
65
|
|
|
62
|
-
// Utility to extract the most useful revert reason from
|
|
63
|
-
function
|
|
66
|
+
// Utility to extract the most useful revert reason from a viem error object
|
|
67
|
+
function parseViemError(error: unknown): string {
|
|
64
68
|
if (!error) return "Unknown error";
|
|
65
|
-
const possibleError: any = error;
|
|
66
69
|
|
|
67
|
-
//
|
|
68
|
-
if (
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
} catch {}
|
|
74
|
-
}
|
|
70
|
+
// Check if it's a viem BaseError
|
|
71
|
+
if (error instanceof Error) {
|
|
72
|
+
// For contract revert errors
|
|
73
|
+
if ((error as any).cause?.reason) {
|
|
74
|
+
return (error as any).cause.reason;
|
|
75
|
+
}
|
|
75
76
|
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
77
|
+
// For viem's shortMessage
|
|
78
|
+
if ((error as any).shortMessage) {
|
|
79
|
+
return (error as any).shortMessage;
|
|
80
|
+
}
|
|
80
81
|
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
82
|
+
// Fallback to regular message
|
|
83
|
+
if (error.message) {
|
|
84
|
+
return error.message;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
84
87
|
|
|
85
88
|
return OffchainFractionsError.UNKNOWN_ERROR;
|
|
86
89
|
}
|
|
87
90
|
|
|
88
|
-
// Type-guard style helper to ensure a
|
|
89
|
-
function
|
|
90
|
-
|
|
91
|
-
): asserts
|
|
92
|
-
if (!
|
|
91
|
+
// Type-guard style helper to ensure a wallet client exists throughout the rest of the function.
|
|
92
|
+
function assertWalletClient(
|
|
93
|
+
maybeWalletClient: WalletClient | undefined
|
|
94
|
+
): asserts maybeWalletClient is WalletClient {
|
|
95
|
+
if (!maybeWalletClient) {
|
|
93
96
|
throw new Error(OffchainFractionsError.SIGNER_NOT_AVAILABLE);
|
|
94
97
|
}
|
|
98
|
+
if (!maybeWalletClient.account) {
|
|
99
|
+
throw new Error("Wallet client must have an account");
|
|
100
|
+
}
|
|
95
101
|
}
|
|
96
102
|
|
|
97
103
|
export function useOffchainFractions(
|
|
98
|
-
|
|
104
|
+
walletClient: WalletClient | undefined,
|
|
105
|
+
publicClient: PublicClient | undefined,
|
|
99
106
|
CHAIN_ID: number
|
|
100
107
|
) {
|
|
101
108
|
// Use dynamic addresses based on chain configuration
|
|
@@ -107,22 +114,13 @@ export function useOffchainFractions(
|
|
|
107
114
|
isProcessing = value;
|
|
108
115
|
};
|
|
109
116
|
|
|
110
|
-
//
|
|
111
|
-
function
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
);
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
/**
|
|
121
|
-
* Get the appropriate token contract
|
|
122
|
-
*/
|
|
123
|
-
function getTokenContract(tokenAddress: string) {
|
|
124
|
-
assertSigner(signer);
|
|
125
|
-
return new Contract(tokenAddress, ERC20_ABI, signer);
|
|
117
|
+
// Helper to assert public client is available
|
|
118
|
+
function assertPublicClient(
|
|
119
|
+
maybePublicClient: PublicClient | undefined
|
|
120
|
+
): asserts maybePublicClient is PublicClient {
|
|
121
|
+
if (!maybePublicClient) {
|
|
122
|
+
throw new Error("Public client not available");
|
|
123
|
+
}
|
|
126
124
|
}
|
|
127
125
|
|
|
128
126
|
/**
|
|
@@ -134,20 +132,18 @@ export function useOffchainFractions(
|
|
|
134
132
|
owner: string,
|
|
135
133
|
tokenAddress: string
|
|
136
134
|
): Promise<bigint> {
|
|
137
|
-
|
|
135
|
+
assertPublicClient(publicClient);
|
|
138
136
|
|
|
139
137
|
try {
|
|
140
|
-
const
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
);
|
|
148
|
-
return allowance;
|
|
138
|
+
const allowance = await publicClient.readContract({
|
|
139
|
+
address: tokenAddress as Address,
|
|
140
|
+
abi: ERC20_ABI,
|
|
141
|
+
functionName: "allowance",
|
|
142
|
+
args: [owner as Address, ADDRESSES.OFFCHAIN_FRACTIONS as Address],
|
|
143
|
+
});
|
|
144
|
+
return allowance as bigint;
|
|
149
145
|
} catch (error) {
|
|
150
|
-
throw new Error(
|
|
146
|
+
throw new Error(parseViemError(error));
|
|
151
147
|
}
|
|
152
148
|
}
|
|
153
149
|
|
|
@@ -160,17 +156,18 @@ export function useOffchainFractions(
|
|
|
160
156
|
owner: string,
|
|
161
157
|
tokenAddress: string
|
|
162
158
|
): Promise<bigint> {
|
|
163
|
-
|
|
159
|
+
assertPublicClient(publicClient);
|
|
164
160
|
|
|
165
161
|
try {
|
|
166
|
-
const
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
162
|
+
const balance = await publicClient.readContract({
|
|
163
|
+
address: tokenAddress as Address,
|
|
164
|
+
abi: ERC20_ABI,
|
|
165
|
+
functionName: "balanceOf",
|
|
166
|
+
args: [owner as Address],
|
|
167
|
+
});
|
|
168
|
+
return balance as bigint;
|
|
172
169
|
} catch (error) {
|
|
173
|
-
throw new Error(
|
|
170
|
+
throw new Error(parseViemError(error));
|
|
174
171
|
}
|
|
175
172
|
}
|
|
176
173
|
|
|
@@ -183,24 +180,28 @@ export function useOffchainFractions(
|
|
|
183
180
|
tokenAddress: string,
|
|
184
181
|
amount: bigint
|
|
185
182
|
): Promise<boolean> {
|
|
186
|
-
|
|
183
|
+
assertWalletClient(walletClient);
|
|
184
|
+
assertPublicClient(publicClient);
|
|
187
185
|
|
|
188
186
|
try {
|
|
189
|
-
const tokenContract = getTokenContract(tokenAddress);
|
|
190
|
-
if (!tokenContract)
|
|
191
|
-
throw new Error(OffchainFractionsError.CONTRACT_NOT_AVAILABLE);
|
|
192
|
-
|
|
193
187
|
setIsProcessing(true);
|
|
194
188
|
|
|
195
|
-
const
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
189
|
+
const hash = await walletClient.writeContract({
|
|
190
|
+
address: tokenAddress as Address,
|
|
191
|
+
abi: ERC20_ABI,
|
|
192
|
+
functionName: "approve",
|
|
193
|
+
args: [ADDRESSES.OFFCHAIN_FRACTIONS as Address, amount],
|
|
194
|
+
chain: walletClient.chain,
|
|
195
|
+
account: walletClient.account!,
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
await publicClient.waitForTransactionReceipt({
|
|
199
|
+
hash,
|
|
200
|
+
});
|
|
200
201
|
|
|
201
202
|
return true;
|
|
202
203
|
} catch (error) {
|
|
203
|
-
throw new Error(
|
|
204
|
+
throw new Error(parseViemError(error));
|
|
204
205
|
} finally {
|
|
205
206
|
setIsProcessing(false);
|
|
206
207
|
}
|
|
@@ -211,13 +212,10 @@ export function useOffchainFractions(
|
|
|
211
212
|
* @param params Parameters for creating the fraction
|
|
212
213
|
*/
|
|
213
214
|
async function createFraction(params: CreateFractionParams): Promise<string> {
|
|
214
|
-
|
|
215
|
+
assertWalletClient(walletClient);
|
|
216
|
+
assertPublicClient(publicClient);
|
|
215
217
|
|
|
216
218
|
try {
|
|
217
|
-
const contract = getOffchainFractionsContract();
|
|
218
|
-
if (!contract)
|
|
219
|
-
throw new Error(OffchainFractionsError.CONTRACT_NOT_AVAILABLE);
|
|
220
|
-
|
|
221
219
|
setIsProcessing(true);
|
|
222
220
|
|
|
223
221
|
const {
|
|
@@ -245,42 +243,56 @@ export function useOffchainFractions(
|
|
|
245
243
|
throw new Error("minSharesToRaise cannot be greater than totalSteps");
|
|
246
244
|
}
|
|
247
245
|
|
|
248
|
-
// Run a
|
|
246
|
+
// Run a simulation first to surface any revert reason
|
|
249
247
|
try {
|
|
250
|
-
await
|
|
251
|
-
.
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
248
|
+
await publicClient.simulateContract({
|
|
249
|
+
address: ADDRESSES.OFFCHAIN_FRACTIONS as Address,
|
|
250
|
+
abi: OFFCHAIN_FRACTIONS_ABI,
|
|
251
|
+
functionName: "createFraction",
|
|
252
|
+
args: [
|
|
253
|
+
id as `0x${string}`,
|
|
254
|
+
token as Address,
|
|
255
255
|
step,
|
|
256
256
|
totalSteps,
|
|
257
257
|
expiration,
|
|
258
|
-
to,
|
|
258
|
+
to as Address,
|
|
259
259
|
useCounterfactualAddress,
|
|
260
260
|
minSharesToRaise,
|
|
261
|
-
closer
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
261
|
+
closer as Address,
|
|
262
|
+
],
|
|
263
|
+
account: walletClient.account!,
|
|
264
|
+
});
|
|
265
|
+
} catch (simulationError) {
|
|
266
|
+
throw new Error(parseViemError(simulationError));
|
|
265
267
|
}
|
|
266
268
|
|
|
267
269
|
// Execute the transaction
|
|
268
|
-
const
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
270
|
+
const hash = await walletClient.writeContract({
|
|
271
|
+
address: ADDRESSES.OFFCHAIN_FRACTIONS as Address,
|
|
272
|
+
abi: OFFCHAIN_FRACTIONS_ABI,
|
|
273
|
+
functionName: "createFraction",
|
|
274
|
+
args: [
|
|
275
|
+
id as `0x${string}`,
|
|
276
|
+
token as Address,
|
|
277
|
+
step,
|
|
278
|
+
totalSteps,
|
|
279
|
+
expiration,
|
|
280
|
+
to as Address,
|
|
281
|
+
useCounterfactualAddress,
|
|
282
|
+
minSharesToRaise,
|
|
283
|
+
closer as Address,
|
|
284
|
+
],
|
|
285
|
+
chain: walletClient.chain,
|
|
286
|
+
account: walletClient.account!,
|
|
287
|
+
});
|
|
288
|
+
|
|
289
|
+
await publicClient.waitForTransactionReceipt({
|
|
290
|
+
hash,
|
|
291
|
+
});
|
|
280
292
|
|
|
281
|
-
return
|
|
293
|
+
return hash;
|
|
282
294
|
} catch (error) {
|
|
283
|
-
throw new Error(
|
|
295
|
+
throw new Error(parseViemError(error));
|
|
284
296
|
} finally {
|
|
285
297
|
setIsProcessing(false);
|
|
286
298
|
}
|
|
@@ -291,13 +303,10 @@ export function useOffchainFractions(
|
|
|
291
303
|
* @param params Parameters for buying fractions
|
|
292
304
|
*/
|
|
293
305
|
async function buyFractions(params: BuyFractionsParams): Promise<string> {
|
|
294
|
-
|
|
306
|
+
assertWalletClient(walletClient);
|
|
307
|
+
assertPublicClient(publicClient);
|
|
295
308
|
|
|
296
309
|
try {
|
|
297
|
-
const contract = getOffchainFractionsContract();
|
|
298
|
-
if (!contract)
|
|
299
|
-
throw new Error(OffchainFractionsError.CONTRACT_NOT_AVAILABLE);
|
|
300
|
-
|
|
301
310
|
setIsProcessing(true);
|
|
302
311
|
|
|
303
312
|
const {
|
|
@@ -327,7 +336,10 @@ export function useOffchainFractions(
|
|
|
327
336
|
const fractionData = await getFraction(creator, id);
|
|
328
337
|
const requiredAmount = stepsToBuy * fractionData.step;
|
|
329
338
|
|
|
330
|
-
const owner =
|
|
339
|
+
const owner = walletClient.account?.address;
|
|
340
|
+
if (!owner) {
|
|
341
|
+
throw new Error("No account found in wallet client");
|
|
342
|
+
}
|
|
331
343
|
|
|
332
344
|
// Check token balance
|
|
333
345
|
const balance = await checkTokenBalance(owner, fractionData.token);
|
|
@@ -338,49 +350,65 @@ export function useOffchainFractions(
|
|
|
338
350
|
// Check and approve tokens if necessary
|
|
339
351
|
const allowance = await checkTokenAllowance(owner, fractionData.token);
|
|
340
352
|
if (allowance < requiredAmount) {
|
|
341
|
-
const
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
353
|
+
const approveHash = await walletClient.writeContract({
|
|
354
|
+
address: fractionData.token as Address,
|
|
355
|
+
abi: ERC20_ABI,
|
|
356
|
+
functionName: "approve",
|
|
357
|
+
args: [ADDRESSES.OFFCHAIN_FRACTIONS as Address, requiredAmount],
|
|
358
|
+
chain: walletClient.chain,
|
|
359
|
+
account: walletClient.account!,
|
|
360
|
+
});
|
|
361
|
+
await publicClient.waitForTransactionReceipt({
|
|
362
|
+
hash: approveHash,
|
|
363
|
+
});
|
|
347
364
|
}
|
|
348
365
|
|
|
349
|
-
// Run a
|
|
366
|
+
// Run a simulation first to surface any revert reason
|
|
350
367
|
try {
|
|
351
|
-
await
|
|
352
|
-
.
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
368
|
+
await publicClient.simulateContract({
|
|
369
|
+
address: ADDRESSES.OFFCHAIN_FRACTIONS as Address,
|
|
370
|
+
abi: OFFCHAIN_FRACTIONS_ABI,
|
|
371
|
+
functionName: "buyFractions",
|
|
372
|
+
args: [
|
|
373
|
+
creator as Address,
|
|
374
|
+
id as `0x${string}`,
|
|
356
375
|
stepsToBuy,
|
|
357
376
|
minStepsToBuy,
|
|
358
|
-
refundTo,
|
|
359
|
-
creditTo,
|
|
377
|
+
refundTo as Address,
|
|
378
|
+
creditTo as Address,
|
|
360
379
|
useCounterfactualAddressForRefund,
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
throw new Error(parseEthersError(staticError));
|
|
380
|
+
],
|
|
381
|
+
account: walletClient.account!,
|
|
382
|
+
});
|
|
383
|
+
} catch (simulationError) {
|
|
384
|
+
throw new Error(parseViemError(simulationError));
|
|
367
385
|
}
|
|
368
386
|
|
|
369
387
|
// Execute the transaction
|
|
370
|
-
const
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
388
|
+
const hash = await walletClient.writeContract({
|
|
389
|
+
address: ADDRESSES.OFFCHAIN_FRACTIONS as Address,
|
|
390
|
+
abi: OFFCHAIN_FRACTIONS_ABI,
|
|
391
|
+
functionName: "buyFractions",
|
|
392
|
+
args: [
|
|
393
|
+
creator as Address,
|
|
394
|
+
id as `0x${string}`,
|
|
395
|
+
stepsToBuy,
|
|
396
|
+
minStepsToBuy,
|
|
397
|
+
refundTo as Address,
|
|
398
|
+
creditTo as Address,
|
|
399
|
+
useCounterfactualAddressForRefund,
|
|
400
|
+
],
|
|
401
|
+
chain: walletClient.chain,
|
|
402
|
+
account: walletClient.account!,
|
|
403
|
+
});
|
|
380
404
|
|
|
381
|
-
|
|
405
|
+
await publicClient.waitForTransactionReceipt({
|
|
406
|
+
hash,
|
|
407
|
+
});
|
|
408
|
+
|
|
409
|
+
return hash;
|
|
382
410
|
} catch (error) {
|
|
383
|
-
throw new Error(
|
|
411
|
+
throw new Error(parseViemError(error));
|
|
384
412
|
} finally {
|
|
385
413
|
setIsProcessing(false);
|
|
386
414
|
}
|
|
@@ -397,13 +425,10 @@ export function useOffchainFractions(
|
|
|
397
425
|
creator: string,
|
|
398
426
|
id: string
|
|
399
427
|
): Promise<string> {
|
|
400
|
-
|
|
428
|
+
assertWalletClient(walletClient);
|
|
429
|
+
assertPublicClient(publicClient);
|
|
401
430
|
|
|
402
431
|
try {
|
|
403
|
-
const contract = getOffchainFractionsContract();
|
|
404
|
-
if (!contract)
|
|
405
|
-
throw new Error(OffchainFractionsError.CONTRACT_NOT_AVAILABLE);
|
|
406
|
-
|
|
407
432
|
setIsProcessing(true);
|
|
408
433
|
|
|
409
434
|
// Normalize addresses to lowercase for consistency
|
|
@@ -427,7 +452,10 @@ export function useOffchainFractions(
|
|
|
427
452
|
throw new Error(OffchainFractionsError.INVALID_PARAMETERS);
|
|
428
453
|
}
|
|
429
454
|
|
|
430
|
-
const owner =
|
|
455
|
+
const owner = walletClient.account?.address;
|
|
456
|
+
if (!owner) {
|
|
457
|
+
throw new Error("No account found in wallet client");
|
|
458
|
+
}
|
|
431
459
|
|
|
432
460
|
// Check if user has steps purchased
|
|
433
461
|
const userSteps = await getStepsPurchased(user, creator, id);
|
|
@@ -435,9 +463,9 @@ export function useOffchainFractions(
|
|
|
435
463
|
throw new Error("No steps purchased for this fraction");
|
|
436
464
|
}
|
|
437
465
|
|
|
438
|
-
// Run a
|
|
466
|
+
// Run a simulation first to surface any revert reason
|
|
439
467
|
try {
|
|
440
|
-
console.log("Calling claimRefund
|
|
468
|
+
console.log("Calling claimRefund simulation with:", {
|
|
441
469
|
user,
|
|
442
470
|
creator,
|
|
443
471
|
id,
|
|
@@ -445,22 +473,35 @@ export function useOffchainFractions(
|
|
|
445
473
|
contractMethod: "claimRefund",
|
|
446
474
|
});
|
|
447
475
|
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
476
|
+
await publicClient.simulateContract({
|
|
477
|
+
address: ADDRESSES.OFFCHAIN_FRACTIONS as Address,
|
|
478
|
+
abi: OFFCHAIN_FRACTIONS_ABI,
|
|
479
|
+
functionName: "claimRefund",
|
|
480
|
+
args: [user as Address, creator as Address, id as `0x${string}`],
|
|
481
|
+
account: walletClient.account!,
|
|
451
482
|
});
|
|
452
|
-
} catch (
|
|
453
|
-
console.error("
|
|
454
|
-
throw new Error(
|
|
483
|
+
} catch (simulationError) {
|
|
484
|
+
console.error("Simulation failed:", simulationError);
|
|
485
|
+
throw new Error(parseViemError(simulationError));
|
|
455
486
|
}
|
|
456
487
|
|
|
457
|
-
// Execute the transaction
|
|
458
|
-
const
|
|
459
|
-
|
|
488
|
+
// Execute the transaction
|
|
489
|
+
const hash = await walletClient.writeContract({
|
|
490
|
+
address: ADDRESSES.OFFCHAIN_FRACTIONS as Address,
|
|
491
|
+
abi: OFFCHAIN_FRACTIONS_ABI,
|
|
492
|
+
functionName: "claimRefund",
|
|
493
|
+
args: [user as Address, creator as Address, id as `0x${string}`],
|
|
494
|
+
chain: walletClient.chain,
|
|
495
|
+
account: walletClient.account!,
|
|
496
|
+
});
|
|
460
497
|
|
|
461
|
-
|
|
498
|
+
await publicClient.waitForTransactionReceipt({
|
|
499
|
+
hash,
|
|
500
|
+
});
|
|
501
|
+
|
|
502
|
+
return hash;
|
|
462
503
|
} catch (error) {
|
|
463
|
-
throw new Error(
|
|
504
|
+
throw new Error(parseViemError(error));
|
|
464
505
|
} finally {
|
|
465
506
|
setIsProcessing(false);
|
|
466
507
|
}
|
|
@@ -472,13 +513,10 @@ export function useOffchainFractions(
|
|
|
472
513
|
* @param id The unique identifier of the fraction sale to close
|
|
473
514
|
*/
|
|
474
515
|
async function closeFraction(creator: string, id: string): Promise<string> {
|
|
475
|
-
|
|
516
|
+
assertWalletClient(walletClient);
|
|
517
|
+
assertPublicClient(publicClient);
|
|
476
518
|
|
|
477
519
|
try {
|
|
478
|
-
const contract = getOffchainFractionsContract();
|
|
479
|
-
if (!contract)
|
|
480
|
-
throw new Error(OffchainFractionsError.CONTRACT_NOT_AVAILABLE);
|
|
481
|
-
|
|
482
520
|
setIsProcessing(true);
|
|
483
521
|
|
|
484
522
|
// Validate parameters
|
|
@@ -486,24 +524,41 @@ export function useOffchainFractions(
|
|
|
486
524
|
throw new Error(OffchainFractionsError.INVALID_PARAMETERS);
|
|
487
525
|
}
|
|
488
526
|
|
|
489
|
-
const owner =
|
|
527
|
+
const owner = walletClient.account?.address;
|
|
528
|
+
if (!owner) {
|
|
529
|
+
throw new Error("No account found in wallet client");
|
|
530
|
+
}
|
|
490
531
|
|
|
491
|
-
// Run a
|
|
532
|
+
// Run a simulation first to surface any revert reason
|
|
492
533
|
try {
|
|
493
|
-
await
|
|
494
|
-
|
|
534
|
+
await publicClient.simulateContract({
|
|
535
|
+
address: ADDRESSES.OFFCHAIN_FRACTIONS as Address,
|
|
536
|
+
abi: OFFCHAIN_FRACTIONS_ABI,
|
|
537
|
+
functionName: "closeFraction",
|
|
538
|
+
args: [creator as Address, id as `0x${string}`],
|
|
539
|
+
account: walletClient.account!,
|
|
495
540
|
});
|
|
496
|
-
} catch (
|
|
497
|
-
throw new Error(
|
|
541
|
+
} catch (simulationError) {
|
|
542
|
+
throw new Error(parseViemError(simulationError));
|
|
498
543
|
}
|
|
499
544
|
|
|
500
545
|
// Execute the transaction
|
|
501
|
-
const
|
|
502
|
-
|
|
546
|
+
const hash = await walletClient.writeContract({
|
|
547
|
+
address: ADDRESSES.OFFCHAIN_FRACTIONS as Address,
|
|
548
|
+
abi: OFFCHAIN_FRACTIONS_ABI,
|
|
549
|
+
functionName: "closeFraction",
|
|
550
|
+
args: [creator as Address, id as `0x${string}`],
|
|
551
|
+
chain: walletClient.chain,
|
|
552
|
+
account: walletClient.account!,
|
|
553
|
+
});
|
|
554
|
+
|
|
555
|
+
await publicClient.waitForTransactionReceipt({
|
|
556
|
+
hash,
|
|
557
|
+
});
|
|
503
558
|
|
|
504
|
-
return
|
|
559
|
+
return hash;
|
|
505
560
|
} catch (error) {
|
|
506
|
-
throw new Error(
|
|
561
|
+
throw new Error(parseViemError(error));
|
|
507
562
|
} finally {
|
|
508
563
|
setIsProcessing(false);
|
|
509
564
|
}
|
|
@@ -518,14 +573,15 @@ export function useOffchainFractions(
|
|
|
518
573
|
creator: string,
|
|
519
574
|
id: string
|
|
520
575
|
): Promise<FractionData> {
|
|
521
|
-
|
|
576
|
+
assertPublicClient(publicClient);
|
|
522
577
|
|
|
523
578
|
try {
|
|
524
|
-
const
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
579
|
+
const result = (await publicClient.readContract({
|
|
580
|
+
address: ADDRESSES.OFFCHAIN_FRACTIONS as Address,
|
|
581
|
+
abi: OFFCHAIN_FRACTIONS_ABI,
|
|
582
|
+
functionName: "getFraction",
|
|
583
|
+
args: [creator as Address, id as `0x${string}`],
|
|
584
|
+
})) as any;
|
|
529
585
|
|
|
530
586
|
return {
|
|
531
587
|
token: result.token,
|
|
@@ -541,7 +597,7 @@ export function useOffchainFractions(
|
|
|
541
597
|
closer: result.closer,
|
|
542
598
|
};
|
|
543
599
|
} catch (error) {
|
|
544
|
-
throw new Error(
|
|
600
|
+
throw new Error(parseViemError(error));
|
|
545
601
|
}
|
|
546
602
|
}
|
|
547
603
|
|
|
@@ -556,31 +612,28 @@ export function useOffchainFractions(
|
|
|
556
612
|
creator: string,
|
|
557
613
|
id: string
|
|
558
614
|
): Promise<bigint> {
|
|
559
|
-
|
|
615
|
+
assertPublicClient(publicClient);
|
|
560
616
|
|
|
561
617
|
try {
|
|
562
|
-
const contract = getOffchainFractionsContract();
|
|
563
|
-
if (!contract)
|
|
564
|
-
throw new Error(OffchainFractionsError.CONTRACT_NOT_AVAILABLE);
|
|
565
|
-
|
|
566
618
|
// Debug logging
|
|
567
619
|
console.log("getStepsPurchased parameters:", {
|
|
568
620
|
user,
|
|
569
621
|
creator,
|
|
570
622
|
id,
|
|
571
|
-
contractAddress:
|
|
623
|
+
contractAddress: ADDRESSES.OFFCHAIN_FRACTIONS,
|
|
572
624
|
});
|
|
573
625
|
|
|
574
|
-
const result = await
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
626
|
+
const result = (await publicClient.readContract({
|
|
627
|
+
address: ADDRESSES.OFFCHAIN_FRACTIONS as Address,
|
|
628
|
+
abi: OFFCHAIN_FRACTIONS_ABI,
|
|
629
|
+
functionName: "stepsPurchased",
|
|
630
|
+
args: [user as Address, creator as Address, id as `0x${string}`],
|
|
631
|
+
})) as bigint;
|
|
579
632
|
|
|
580
633
|
console.log("getStepsPurchased result:", result.toString());
|
|
581
634
|
return result;
|
|
582
635
|
} catch (error) {
|
|
583
|
-
throw new Error(
|
|
636
|
+
throw new Error(parseViemError(error));
|
|
584
637
|
}
|
|
585
638
|
}
|
|
586
639
|
|
|
@@ -589,17 +642,18 @@ export function useOffchainFractions(
|
|
|
589
642
|
* @returns The wildcard operator address that can perform refunds for any user
|
|
590
643
|
*/
|
|
591
644
|
async function getRefundWildcardOperator(): Promise<string> {
|
|
592
|
-
|
|
645
|
+
assertPublicClient(publicClient);
|
|
593
646
|
|
|
594
647
|
try {
|
|
595
|
-
const
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
648
|
+
const result = (await publicClient.readContract({
|
|
649
|
+
address: ADDRESSES.OFFCHAIN_FRACTIONS as Address,
|
|
650
|
+
abi: OFFCHAIN_FRACTIONS_ABI,
|
|
651
|
+
functionName: "REFUND_WILDCARD_OPERATOR",
|
|
652
|
+
args: [],
|
|
653
|
+
})) as string;
|
|
600
654
|
return result;
|
|
601
655
|
} catch (error) {
|
|
602
|
-
throw new Error(
|
|
656
|
+
throw new Error(parseViemError(error));
|
|
603
657
|
}
|
|
604
658
|
}
|
|
605
659
|
|
|
@@ -614,24 +668,21 @@ export function useOffchainFractions(
|
|
|
614
668
|
creator: string,
|
|
615
669
|
id: string
|
|
616
670
|
): Promise<RefundDetails> {
|
|
617
|
-
|
|
671
|
+
assertPublicClient(publicClient);
|
|
618
672
|
|
|
619
673
|
try {
|
|
620
|
-
const
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
creator,
|
|
627
|
-
id
|
|
628
|
-
);
|
|
674
|
+
const result = (await publicClient.readContract({
|
|
675
|
+
address: ADDRESSES.OFFCHAIN_FRACTIONS as Address,
|
|
676
|
+
abi: OFFCHAIN_FRACTIONS_ABI,
|
|
677
|
+
functionName: "getRefundDetails",
|
|
678
|
+
args: [user as Address, creator as Address, id as `0x${string}`],
|
|
679
|
+
})) as any;
|
|
629
680
|
return {
|
|
630
681
|
refundTo: result.refundTo,
|
|
631
682
|
useCounterfactualAddress: result.useCounterfactualAddress,
|
|
632
683
|
};
|
|
633
684
|
} catch (error) {
|
|
634
|
-
throw new Error(
|
|
685
|
+
throw new Error(parseViemError(error));
|
|
635
686
|
}
|
|
636
687
|
}
|
|
637
688
|
|
|
@@ -648,13 +699,10 @@ export function useOffchainFractions(
|
|
|
648
699
|
refundTo: string,
|
|
649
700
|
useCounterfactualAddress: boolean
|
|
650
701
|
): Promise<string> {
|
|
651
|
-
|
|
702
|
+
assertWalletClient(walletClient);
|
|
703
|
+
assertPublicClient(publicClient);
|
|
652
704
|
|
|
653
705
|
try {
|
|
654
|
-
const contract = getOffchainFractionsContract();
|
|
655
|
-
if (!contract)
|
|
656
|
-
throw new Error(OffchainFractionsError.CONTRACT_NOT_AVAILABLE);
|
|
657
|
-
|
|
658
706
|
setIsProcessing(true);
|
|
659
707
|
|
|
660
708
|
// Validate parameters
|
|
@@ -662,31 +710,51 @@ export function useOffchainFractions(
|
|
|
662
710
|
throw new Error(OffchainFractionsError.INVALID_PARAMETERS);
|
|
663
711
|
}
|
|
664
712
|
|
|
665
|
-
const owner =
|
|
713
|
+
const owner = walletClient.account?.address;
|
|
714
|
+
if (!owner) {
|
|
715
|
+
throw new Error("No account found in wallet client");
|
|
716
|
+
}
|
|
666
717
|
|
|
667
|
-
// Run a
|
|
718
|
+
// Run a simulation first to surface any revert reason
|
|
668
719
|
try {
|
|
669
|
-
await
|
|
670
|
-
.
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
720
|
+
await publicClient.simulateContract({
|
|
721
|
+
address: ADDRESSES.OFFCHAIN_FRACTIONS as Address,
|
|
722
|
+
abi: OFFCHAIN_FRACTIONS_ABI,
|
|
723
|
+
functionName: "setRefundDetails",
|
|
724
|
+
args: [
|
|
725
|
+
creator as Address,
|
|
726
|
+
id as `0x${string}`,
|
|
727
|
+
refundTo as Address,
|
|
728
|
+
useCounterfactualAddress,
|
|
729
|
+
],
|
|
730
|
+
account: walletClient.account!,
|
|
731
|
+
});
|
|
732
|
+
} catch (simulationError) {
|
|
733
|
+
throw new Error(parseViemError(simulationError));
|
|
676
734
|
}
|
|
677
735
|
|
|
678
736
|
// Execute the transaction
|
|
679
|
-
const
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
737
|
+
const hash = await walletClient.writeContract({
|
|
738
|
+
address: ADDRESSES.OFFCHAIN_FRACTIONS as Address,
|
|
739
|
+
abi: OFFCHAIN_FRACTIONS_ABI,
|
|
740
|
+
functionName: "setRefundDetails",
|
|
741
|
+
args: [
|
|
742
|
+
creator as Address,
|
|
743
|
+
id as `0x${string}`,
|
|
744
|
+
refundTo as Address,
|
|
745
|
+
useCounterfactualAddress,
|
|
746
|
+
],
|
|
747
|
+
chain: walletClient.chain,
|
|
748
|
+
account: walletClient.account!,
|
|
749
|
+
});
|
|
750
|
+
|
|
751
|
+
await publicClient.waitForTransactionReceipt({
|
|
752
|
+
hash,
|
|
753
|
+
});
|
|
686
754
|
|
|
687
|
-
return
|
|
755
|
+
return hash;
|
|
688
756
|
} catch (error) {
|
|
689
|
-
throw new Error(
|
|
757
|
+
throw new Error(parseViemError(error));
|
|
690
758
|
} finally {
|
|
691
759
|
setIsProcessing(false);
|
|
692
760
|
}
|
|
@@ -701,13 +769,10 @@ export function useOffchainFractions(
|
|
|
701
769
|
refundOperator: string,
|
|
702
770
|
isApproved: boolean
|
|
703
771
|
): Promise<string> {
|
|
704
|
-
|
|
772
|
+
assertWalletClient(walletClient);
|
|
773
|
+
assertPublicClient(publicClient);
|
|
705
774
|
|
|
706
775
|
try {
|
|
707
|
-
const contract = getOffchainFractionsContract();
|
|
708
|
-
if (!contract)
|
|
709
|
-
throw new Error(OffchainFractionsError.CONTRACT_NOT_AVAILABLE);
|
|
710
|
-
|
|
711
776
|
setIsProcessing(true);
|
|
712
777
|
|
|
713
778
|
// Validate parameters
|
|
@@ -715,29 +780,41 @@ export function useOffchainFractions(
|
|
|
715
780
|
throw new Error(OffchainFractionsError.INVALID_PARAMETERS);
|
|
716
781
|
}
|
|
717
782
|
|
|
718
|
-
const owner =
|
|
783
|
+
const owner = walletClient.account?.address;
|
|
784
|
+
if (!owner) {
|
|
785
|
+
throw new Error("No account found in wallet client");
|
|
786
|
+
}
|
|
719
787
|
|
|
720
|
-
// Run a
|
|
788
|
+
// Run a simulation first to surface any revert reason
|
|
721
789
|
try {
|
|
722
|
-
await
|
|
723
|
-
.
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
790
|
+
await publicClient.simulateContract({
|
|
791
|
+
address: ADDRESSES.OFFCHAIN_FRACTIONS as Address,
|
|
792
|
+
abi: OFFCHAIN_FRACTIONS_ABI,
|
|
793
|
+
functionName: "setRefundOperatorStatus",
|
|
794
|
+
args: [refundOperator as Address, isApproved],
|
|
795
|
+
account: walletClient.account!,
|
|
796
|
+
});
|
|
797
|
+
} catch (simulationError) {
|
|
798
|
+
throw new Error(parseViemError(simulationError));
|
|
729
799
|
}
|
|
730
800
|
|
|
731
801
|
// Execute the transaction
|
|
732
|
-
const
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
802
|
+
const hash = await walletClient.writeContract({
|
|
803
|
+
address: ADDRESSES.OFFCHAIN_FRACTIONS as Address,
|
|
804
|
+
abi: OFFCHAIN_FRACTIONS_ABI,
|
|
805
|
+
functionName: "setRefundOperatorStatus",
|
|
806
|
+
args: [refundOperator as Address, isApproved],
|
|
807
|
+
chain: walletClient.chain,
|
|
808
|
+
account: walletClient.account!,
|
|
809
|
+
});
|
|
810
|
+
|
|
811
|
+
await publicClient.waitForTransactionReceipt({
|
|
812
|
+
hash,
|
|
813
|
+
});
|
|
737
814
|
|
|
738
|
-
return
|
|
815
|
+
return hash;
|
|
739
816
|
} catch (error) {
|
|
740
|
-
throw new Error(
|
|
817
|
+
throw new Error(parseViemError(error));
|
|
741
818
|
} finally {
|
|
742
819
|
setIsProcessing(false);
|
|
743
820
|
}
|
|
@@ -752,20 +829,18 @@ export function useOffchainFractions(
|
|
|
752
829
|
user: string,
|
|
753
830
|
refundOperator: string
|
|
754
831
|
): Promise<boolean> {
|
|
755
|
-
|
|
832
|
+
assertPublicClient(publicClient);
|
|
756
833
|
|
|
757
834
|
try {
|
|
758
|
-
const
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
refundOperator
|
|
765
|
-
);
|
|
835
|
+
const result = (await publicClient.readContract({
|
|
836
|
+
address: ADDRESSES.OFFCHAIN_FRACTIONS as Address,
|
|
837
|
+
abi: OFFCHAIN_FRACTIONS_ABI,
|
|
838
|
+
functionName: "isRefundOperatorApproved",
|
|
839
|
+
args: [user as Address, refundOperator as Address],
|
|
840
|
+
})) as boolean;
|
|
766
841
|
return result;
|
|
767
842
|
} catch (error) {
|
|
768
|
-
throw new Error(
|
|
843
|
+
throw new Error(parseViemError(error));
|
|
769
844
|
}
|
|
770
845
|
}
|
|
771
846
|
|
|
@@ -778,20 +853,18 @@ export function useOffchainFractions(
|
|
|
778
853
|
user: string,
|
|
779
854
|
refundOperator: string
|
|
780
855
|
): Promise<boolean> {
|
|
781
|
-
|
|
856
|
+
assertPublicClient(publicClient);
|
|
782
857
|
|
|
783
858
|
try {
|
|
784
|
-
const
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
refundOperator
|
|
791
|
-
);
|
|
859
|
+
const result = (await publicClient.readContract({
|
|
860
|
+
address: ADDRESSES.OFFCHAIN_FRACTIONS as Address,
|
|
861
|
+
abi: OFFCHAIN_FRACTIONS_ABI,
|
|
862
|
+
functionName: "refundApprovals",
|
|
863
|
+
args: [user as Address, refundOperator as Address],
|
|
864
|
+
})) as any;
|
|
792
865
|
return result.isApproved;
|
|
793
866
|
} catch (error) {
|
|
794
|
-
throw new Error(
|
|
867
|
+
throw new Error(parseViemError(error));
|
|
795
868
|
}
|
|
796
869
|
}
|
|
797
870
|
|
|
@@ -808,7 +881,7 @@ export function useOffchainFractions(
|
|
|
808
881
|
const fraction = await getFraction(creator, id);
|
|
809
882
|
return Date.now() / 1000 > fraction.expiration;
|
|
810
883
|
} catch (error) {
|
|
811
|
-
throw new Error(
|
|
884
|
+
throw new Error(parseViemError(error));
|
|
812
885
|
}
|
|
813
886
|
}
|
|
814
887
|
|
|
@@ -825,7 +898,7 @@ export function useOffchainFractions(
|
|
|
825
898
|
const fraction = await getFraction(creator, id);
|
|
826
899
|
return fraction.soldSteps >= fraction.minSharesToRaise;
|
|
827
900
|
} catch (error) {
|
|
828
|
-
throw new Error(
|
|
901
|
+
throw new Error(parseViemError(error));
|
|
829
902
|
}
|
|
830
903
|
}
|
|
831
904
|
|
|
@@ -842,7 +915,7 @@ export function useOffchainFractions(
|
|
|
842
915
|
const fraction = await getFraction(creator, id);
|
|
843
916
|
return fraction.soldSteps >= fraction.totalSteps;
|
|
844
917
|
} catch (error) {
|
|
845
|
-
throw new Error(
|
|
918
|
+
throw new Error(parseViemError(error));
|
|
846
919
|
}
|
|
847
920
|
}
|
|
848
921
|
|
|
@@ -859,7 +932,7 @@ export function useOffchainFractions(
|
|
|
859
932
|
const fraction = await getFraction(creator, id);
|
|
860
933
|
return fraction.soldSteps * fraction.step;
|
|
861
934
|
} catch (error) {
|
|
862
|
-
throw new Error(
|
|
935
|
+
throw new Error(parseViemError(error));
|
|
863
936
|
}
|
|
864
937
|
}
|
|
865
938
|
|
|
@@ -876,7 +949,7 @@ export function useOffchainFractions(
|
|
|
876
949
|
const fraction = await getFraction(creator, id);
|
|
877
950
|
return fraction.totalSteps - fraction.soldSteps;
|
|
878
951
|
} catch (error) {
|
|
879
|
-
throw new Error(
|
|
952
|
+
throw new Error(parseViemError(error));
|
|
880
953
|
}
|
|
881
954
|
}
|
|
882
955
|
|
|
@@ -889,13 +962,10 @@ export function useOffchainFractions(
|
|
|
889
962
|
params: CreateFractionParams,
|
|
890
963
|
ethPriceInUSD: number | null
|
|
891
964
|
): Promise<string> {
|
|
892
|
-
|
|
965
|
+
assertWalletClient(walletClient);
|
|
966
|
+
assertPublicClient(publicClient);
|
|
893
967
|
|
|
894
968
|
try {
|
|
895
|
-
const contract = getOffchainFractionsContract();
|
|
896
|
-
if (!contract)
|
|
897
|
-
throw new Error(OffchainFractionsError.CONTRACT_NOT_AVAILABLE);
|
|
898
|
-
|
|
899
969
|
const {
|
|
900
970
|
id,
|
|
901
971
|
token,
|
|
@@ -908,26 +978,28 @@ export function useOffchainFractions(
|
|
|
908
978
|
closer,
|
|
909
979
|
} = params;
|
|
910
980
|
|
|
911
|
-
const
|
|
912
|
-
|
|
913
|
-
feeData?.gasPrice ?? feeData?.maxFeePerGas ?? (0n as bigint);
|
|
914
|
-
if (gasPrice === 0n) {
|
|
981
|
+
const gasPrice = await publicClient.getGasPrice();
|
|
982
|
+
if (!gasPrice) {
|
|
915
983
|
throw new Error("Could not fetch gas price to estimate cost.");
|
|
916
984
|
}
|
|
917
985
|
|
|
918
|
-
const estimatedGas
|
|
919
|
-
.
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
986
|
+
const estimatedGas = await publicClient.estimateContractGas({
|
|
987
|
+
address: ADDRESSES.OFFCHAIN_FRACTIONS as Address,
|
|
988
|
+
abi: OFFCHAIN_FRACTIONS_ABI,
|
|
989
|
+
functionName: "createFraction",
|
|
990
|
+
args: [
|
|
991
|
+
id as `0x${string}`,
|
|
992
|
+
token as Address,
|
|
923
993
|
step,
|
|
924
994
|
totalSteps,
|
|
925
995
|
expiration,
|
|
926
|
-
to,
|
|
996
|
+
to as Address,
|
|
927
997
|
useCounterfactualAddress,
|
|
928
998
|
minSharesToRaise,
|
|
929
|
-
closer
|
|
930
|
-
|
|
999
|
+
closer as Address,
|
|
1000
|
+
],
|
|
1001
|
+
account: walletClient.account!,
|
|
1002
|
+
});
|
|
931
1003
|
|
|
932
1004
|
const estimatedCost: bigint = estimatedGas * gasPrice;
|
|
933
1005
|
|
|
@@ -943,7 +1015,7 @@ export function useOffchainFractions(
|
|
|
943
1015
|
);
|
|
944
1016
|
}
|
|
945
1017
|
} catch (error: any) {
|
|
946
|
-
throw new Error(
|
|
1018
|
+
throw new Error(parseViemError(error));
|
|
947
1019
|
}
|
|
948
1020
|
}
|
|
949
1021
|
|
|
@@ -956,13 +1028,10 @@ export function useOffchainFractions(
|
|
|
956
1028
|
params: BuyFractionsParams,
|
|
957
1029
|
ethPriceInUSD: number | null
|
|
958
1030
|
): Promise<string> {
|
|
959
|
-
|
|
1031
|
+
assertWalletClient(walletClient);
|
|
1032
|
+
assertPublicClient(publicClient);
|
|
960
1033
|
|
|
961
1034
|
try {
|
|
962
|
-
const contract = getOffchainFractionsContract();
|
|
963
|
-
if (!contract)
|
|
964
|
-
throw new Error(OffchainFractionsError.CONTRACT_NOT_AVAILABLE);
|
|
965
|
-
|
|
966
1035
|
const {
|
|
967
1036
|
creator,
|
|
968
1037
|
id,
|
|
@@ -973,24 +1042,26 @@ export function useOffchainFractions(
|
|
|
973
1042
|
useCounterfactualAddressForRefund,
|
|
974
1043
|
} = params;
|
|
975
1044
|
|
|
976
|
-
const
|
|
977
|
-
|
|
978
|
-
feeData?.gasPrice ?? feeData?.maxFeePerGas ?? (0n as bigint);
|
|
979
|
-
if (gasPrice === 0n) {
|
|
1045
|
+
const gasPrice = await publicClient.getGasPrice();
|
|
1046
|
+
if (!gasPrice) {
|
|
980
1047
|
throw new Error("Could not fetch gas price to estimate cost.");
|
|
981
1048
|
}
|
|
982
1049
|
|
|
983
|
-
const estimatedGas
|
|
984
|
-
.
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
1050
|
+
const estimatedGas = await publicClient.estimateContractGas({
|
|
1051
|
+
address: ADDRESSES.OFFCHAIN_FRACTIONS as Address,
|
|
1052
|
+
abi: OFFCHAIN_FRACTIONS_ABI,
|
|
1053
|
+
functionName: "buyFractions",
|
|
1054
|
+
args: [
|
|
1055
|
+
creator as Address,
|
|
1056
|
+
id as `0x${string}`,
|
|
988
1057
|
stepsToBuy,
|
|
989
1058
|
minStepsToBuy,
|
|
990
|
-
refundTo,
|
|
991
|
-
creditTo,
|
|
992
|
-
useCounterfactualAddressForRefund
|
|
993
|
-
|
|
1059
|
+
refundTo as Address,
|
|
1060
|
+
creditTo as Address,
|
|
1061
|
+
useCounterfactualAddressForRefund,
|
|
1062
|
+
],
|
|
1063
|
+
account: walletClient.account!,
|
|
1064
|
+
});
|
|
994
1065
|
|
|
995
1066
|
const estimatedCost: bigint = estimatedGas * gasPrice;
|
|
996
1067
|
|
|
@@ -1006,7 +1077,7 @@ export function useOffchainFractions(
|
|
|
1006
1077
|
);
|
|
1007
1078
|
}
|
|
1008
1079
|
} catch (error: any) {
|
|
1009
|
-
throw new Error(
|
|
1080
|
+
throw new Error(parseViemError(error));
|
|
1010
1081
|
}
|
|
1011
1082
|
}
|
|
1012
1083
|
|
|
@@ -1051,7 +1122,7 @@ export function useOffchainFractions(
|
|
|
1051
1122
|
},
|
|
1052
1123
|
addresses: ADDRESSES,
|
|
1053
1124
|
|
|
1054
|
-
//
|
|
1055
|
-
isSignerAvailable: !!
|
|
1125
|
+
// Wallet client availability
|
|
1126
|
+
isSignerAvailable: !!walletClient,
|
|
1056
1127
|
};
|
|
1057
1128
|
}
|